package com.ho.filecenter.service.impl;


import com.ho.common.tools.exception.BaseResponseCode;
import com.ho.common.tools.exception.BusinessException;
import com.ho.common.tools.util.BigDecimalUtil;
import com.ho.filecenter.constant.FileConstant;
import com.ho.filecenter.entity.StandingBook;
import com.ho.filecenter.mapper.StandingBookMapper;
import com.ho.filecenter.service.StandingBookService;
import com.ho.filecenter.vo.StandingBookVo;
import com.ho.filecenter.vo.standingbook.StandingBookImport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author yy
 * @description 针对表【standing_book】的数据库操作Service实现
 * @createDate 2023-03-15 13:52:18
 */
@Service
public class StandingBookServiceImpl implements StandingBookService {
    @Autowired
    StandingBookMapper standingBookMapper;

    @Autowired
    BigDecimalUtil bigDecimalUtil;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importCustomer(Integer stationId,List<StandingBookImport> list) {
        //导入
        List<StandingBookImport> standingBookImportList = new ArrayList<>();
        for (StandingBookImport standingBookImport : list) {
            //判断是否为正确的模板表格
            //序号
            if (standingBookImport.getDeviceModel() == null || standingBookImport.getDeviceType() == null
                    || standingBookImport.getModelCode() == null || standingBookImport.getDeviceNum() == null
                    || standingBookImport.getFactory() == null || standingBookImport.getManufacturer() == null
                    || standingBookImport.getWarrantyPeriod() == null || standingBookImport.getNotes() == null) {
                throw new BusinessException(BaseResponseCode.PLEASE_CONFIRM_WHETHER_IMPORTED_TEMPLATE_CORRECT);
            }
            if (!FileConstant.StandingBook.DEVICE_TYPE.equals(standingBookImport.getDeviceType())
                    || !FileConstant.StandingBook.DEVICE_MODEL.equals(standingBookImport.getDeviceModel())
                    || !FileConstant.StandingBook.DEVICE_MODEL.equals(standingBookImport.getDeviceModel())
                    || !FileConstant.StandingBook.MODEL_CODE.equals(standingBookImport.getModelCode())
                    || !FileConstant.StandingBook.DEVICE_NUM.equals(standingBookImport.getDeviceNum())
                    || !FileConstant.StandingBook.FACTORY.equals(standingBookImport.getFactory())
                    || !FileConstant.StandingBook.MANUFACTURER.equals(standingBookImport.getManufacturer())
                    || !FileConstant.StandingBook.WARRANTY_PERIOD.equals(standingBookImport.getWarrantyPeriod())
                    || !FileConstant.StandingBook.NOTES.equals(standingBookImport.getNotes())) {
                throw new BusinessException(BaseResponseCode.PLEASE_CONFIRM_WHETHER_IMPORTED_TEMPLATE_CORRECT);
            }
            break;
        }
        for (StandingBookImport standingBookImport : list) {
            //判断是否为正确的模板表格
            if (!FileConstant.StandingBook.DEVICE_TYPE.equals(standingBookImport.getDeviceType())) {
                standingBookImportList.add(standingBookImport);
            }
        }
        if (standingBookImportList.isEmpty()) {
            throw new BusinessException(BaseResponseCode.EXCEL_DATA_TABLE_EMPTY);
        }

        //判断表格中的数据大小
        for (StandingBookImport standingBookImport : standingBookImportList) {
            Boolean aBoolean = bigDecimalUtil.ifStringLength(standingBookImport);
            if (aBoolean) {
                //请核对excel表中的数据
                throw new BusinessException(BaseResponseCode.SJEKK_EXCEL_DATA);
            }
        }

        //根据电站id删除原本的数据
        standingBookMapper.deleteByStationId(stationId);
        //将新的数据进行入库
        List<StandingBook> standingBookList = new ArrayList<>();
        for (StandingBookImport standingBookImport : standingBookImportList) {
            StandingBook standingBook = new StandingBook();
            standingBook.setCreateTime(new Date());
            standingBook.setStationId(stationId);
            standingBook.setDeviceType(standingBookImport.getDeviceType());
            standingBook.setDeviceModel(standingBookImport.getDeviceModel());
            standingBook.setWarrantyPeriod(standingBookImport.getWarrantyPeriod());
            standingBook.setNotes(standingBookImport.getNotes());
            standingBook.setModelCode(standingBookImport.getModelCode());
            standingBook.setFactory(standingBookImport.getFactory());
            standingBook.setDeviceNum(standingBookImport.getDeviceNum());
            standingBook.setManufacturer(standingBookImport.getManufacturer());
            standingBookList.add(standingBook);
        }
        //批量新增
        standingBookMapper.addBatch(standingBookList);
    }

    @Override
    public List<StandingBook> queryList(StandingBookVo vo) {
        List<StandingBook> standingBookList = standingBookMapper.selectAll(vo.getId());
        return standingBookList;
    }

    @Override
    public void update(StandingBook vo) {
        vo.setUpdateTime(new Date());
        standingBookMapper.updateByPrimaryKeySelective(vo);
    }

    @Override
    public void deleteById(StandingBookVo vo) {
        standingBookMapper.deleteById(vo.getId());
    }

    @Override
    public void deleteByStationId(Integer stationId) {
        standingBookMapper.deleteByStationId(stationId);
    }

    @Override
    public void addBatch(List<StandingBook> standingBookList) {
        standingBookMapper.addBatch(standingBookList);
    }
}
