package com.energy.manage.service.service.windenginemill.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.energy.manage.common.base.BaseServiceImpl;
import com.energy.manage.common.base.Page;
import com.energy.manage.common.constant.Constants;
import com.energy.manage.common.constant.redis.ManagerRedisKeyConstant;
import com.energy.manage.common.enums.DeleteStatusEnum;
import com.energy.manage.common.enums.IdPrefixEnum;
import com.energy.manage.common.po.windenginemill.WindEngineMillPO;
import com.energy.manage.common.util.IdGeneratorUtil;
import com.energy.manage.service.domain.dto.windenginemill.WindEngineMillCodeDto;
import com.energy.manage.service.domain.dto.windenginemill.WindEngineMillCreateDto;
import com.energy.manage.service.domain.dto.windenginemill.WindEngineMillDto;
import com.energy.manage.service.domain.dto.windenginemill.WindEngineMillPageDto;
import com.energy.manage.service.domain.vo.excel.WindEngineMillExcelVo;
import com.energy.manage.service.domain.vo.windenginemill.WindEngineMillPageVo;
import com.energy.manage.service.domain.vo.windenginemill.WindEngineMillVo;
import com.energy.manage.service.mappers.windenginemill.WindEngineMillMapper;
import com.energy.manage.service.service.cache.CacheService;
import com.energy.manage.service.service.windenginemill.WindEngineMillService;
import com.energy.manage.service.util.MinioUtils;
import com.energy.manage.service.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

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


@Slf4j
@Service
public class WindEngineMillServiceImpl extends BaseServiceImpl<WindEngineMillPO> implements WindEngineMillService {


    @Autowired
    private CacheService cacheService;


    @Autowired
    private WindEngineMillMapper windEngineMillMapper;


    /**
     * 手动创建机型信息
     *
     * @param windEngineMillCreateDto
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createEngineMill(WindEngineMillCreateDto windEngineMillCreateDto) {

        List<String> stringList = Lists.newArrayList();
        String value = StrUtil.format("{},{}", windEngineMillCreateDto.getMachineTypeCode(), windEngineMillCreateDto.getBrand());
        stringList.add(value);

        Integer count = windEngineMillMapper.selectWindEngineMillCount(stringList);
        if (count > 0) {
            log.warn("=============== 机型编号+厂商编号已存在！！! =============== ");
            return false;
        }

        WindEngineMillPO windEngineMillPO = new WindEngineMillPO();
        BeanUtil.copyProperties(windEngineMillCreateDto, windEngineMillPO);
        String number = IdPrefixEnum.WIND_EILL_NUMBER.getCode().concat(IdGeneratorUtil.zeroFillUtil(cacheService.incr(ManagerRedisKeyConstant.build(ManagerRedisKeyConstant.IDGENERATOR_CONSTANTS_KEY, IdPrefixEnum.WIND_EILL_NUMBER.getCode()))));
        windEngineMillPO.setMillTypeCode(number);
        windEngineMillPO.setCreateTime(new Date());
        windEngineMillPO.setUpdateTime(new Date());
        windEngineMillPO.setState(Constants.GLOBAL_IN_USE);
        windEngineMillPO.setDelState(DeleteStatusEnum.NODELETE.getCode());
        // 生成厂商编号
        String mfNumber = IdPrefixEnum.WIND_EILL_MF_NUMBER.getCode().concat(IdGeneratorUtil.zeroFillUtil(cacheService.incr(ManagerRedisKeyConstant.build(ManagerRedisKeyConstant.IDGENERATOR_CONSTANTS_KEY, IdPrefixEnum.WIND_EILL_MF_NUMBER.getCode()))));
        windEngineMillPO.setManufacturerCode(mfNumber);
        windEngineMillPO.setCombination(StrUtil.format("{},{}", windEngineMillCreateDto.getMachineTypeCode(), windEngineMillCreateDto.getBrand()));

        return windEngineMillMapper.insertUseGeneratedKeys(windEngineMillPO) > 0;
    }


    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean windEngineMillImportData(MultipartFile file) {
        List<WindEngineMillExcelVo> windFieldExceVos = EasyExcel.read(file.getInputStream()).head(WindEngineMillExcelVo.class).sheet().doReadSync();
        if (CollectionUtils.isEmpty(windFieldExceVos)) {
            return false;
        }
        List<String> stringList = Lists.newArrayList();
        for (WindEngineMillExcelVo windEngineMillExcelVo : windFieldExceVos) {
            String value = StrUtil.format("{},{}", windEngineMillExcelVo.getMachineTypeCode(),windEngineMillExcelVo.getBrand());
            stringList.add(value);
        }
        Integer count = windEngineMillMapper.selectWindEngineMillCount(stringList);
        if (count > 0) {
            log.warn("复合主键不能重复！！!");
            return false;
        }
        WindEngineMillPO windEngineMillPO = null;
        List<com.energy.manage.common.po.windenginemill.WindEngineMillPO> windEngineMillPOS = Lists.newArrayList();
        for (WindEngineMillExcelVo windEngineMillExcelVo : windFieldExceVos) {
            windEngineMillPO = new com.energy.manage.common.po.windenginemill.WindEngineMillPO();
            BeanUtil.copyProperties(windEngineMillExcelVo, windEngineMillPO);
            windEngineMillPO.setMillTypeCode(UUIDUtil.getShortUUID());
            windEngineMillPO.setCombination(StrUtil.format("{},{}", windEngineMillExcelVo.getMachineTypeCode(), windEngineMillExcelVo.getBrand()));
            windEngineMillPO.setCreateTime(new Date());
            windEngineMillPO.setUpdateTime(new Date());
            windEngineMillPO.setState(DeleteStatusEnum.NODELETE.getCode());
            windEngineMillPO.setDelState(Constants.GLOBAL_IN_USE);
            windEngineMillPOS.add(windEngineMillPO);
        }
        return windEngineMillMapper.insertList(windEngineMillPOS) > 0;
    }

    @Override
    public Page<WindEngineMillPageVo> windEngineMillPage(WindEngineMillPageDto windEngineMillPageDto) {
        PageHelper.startPage(windEngineMillPageDto.getPageNum(), windEngineMillPageDto.getPageSize());
        Example queryExample = new Example(WindEngineMillPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        if (!StringUtils.isEmpty(windEngineMillPageDto.getMachineTypeCode())) {
            criteria.andLike("machineTypeCode", "%" + windEngineMillPageDto.getMachineTypeCode() + "%");
        }

        if (!StringUtils.isEmpty(windEngineMillPageDto.getBeginTime())&&!StringUtils.isEmpty(windEngineMillPageDto.getEndTime())) {
            criteria.andGreaterThanOrEqualTo("createTime", windEngineMillPageDto.getBeginTime());
            criteria.andLessThanOrEqualTo("createTime", windEngineMillPageDto.getEndTime());
        }

        criteria.andEqualTo("delState", DeleteStatusEnum.NODELETE.getCode());
        List<WindEngineMillPO> list = windEngineMillMapper.selectByExample(queryExample);

        WindEngineMillPageVo windEngineMillPageVo = null;
        List<WindEngineMillPageVo> windEngineMillPageVos = Lists.newArrayList();
        for (WindEngineMillPO po : list) {
            windEngineMillPageVo = new WindEngineMillPageVo();
            BeanUtil.copyProperties(po, windEngineMillPageVo);
            windEngineMillPageVos.add(windEngineMillPageVo);
        }
        Long total = org.apache.commons.collections.CollectionUtils.isEmpty(list) ? 0L : new PageInfo<>(list).getTotal();
        return Page.build(windEngineMillPageDto.getPageNum(), windEngineMillPageDto.getPageSize(), total, windEngineMillPageVos);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delWindEngineMill(WindEngineMillCodeDto windEngineMillCodeDto) {
        WindEngineMillPO po = new WindEngineMillPO();
        po.setDelState(DeleteStatusEnum.DELETE.getCode());
        Example queryExample = new Example(WindEngineMillPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("millTypeCode", windEngineMillCodeDto.getMillTypeCode());
        return windEngineMillMapper.updateByExampleSelective(po, queryExample) > 0;
    }


    @Override
    public WindEngineMillPageVo getWindEngineMillPageVo(WindEngineMillCodeDto windEngineMillCodeDto) {
        WindEngineMillPO po = new WindEngineMillPO();
        po.setMillTypeCode(windEngineMillCodeDto.getMillTypeCode());
        WindEngineMillPO windEngineMillPO = windEngineMillMapper.selectOne(po);
        if (windEngineMillPO == null) {
            return null;
        }
        WindEngineMillPageVo windEngineMillPageVo = new WindEngineMillPageVo();
        BeanUtil.copyProperties(windEngineMillPO, windEngineMillPageVo);
        return windEngineMillPageVo;
    }

    @Override
    public List<WindEngineMillVo> getWindEngineMillList() {

        Example queryExample = new Example(WindEngineMillPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("delState", DeleteStatusEnum.NODELETE.getCode());
        criteria.andEqualTo("state",Constants.GLOBAL_IN_USE);
        List<WindEngineMillPO> list = windEngineMillMapper.selectByExample(queryExample);
        WindEngineMillVo windEngineMillVo = null;
        List<WindEngineMillVo> windEngineMillVos = Lists.newArrayList();
        for(WindEngineMillPO windEngineMillPO : list){
            windEngineMillVo = new WindEngineMillVo();
            BeanUtil.copyProperties(windEngineMillPO,windEngineMillVo);
            windEngineMillVos.add(windEngineMillVo);
        }
        return windEngineMillVos;
    }

    @Override
    public List<WindEngineMillVo> getWindEngineMillListByInMachineTypeCode(List<String> machineTypeCodes) {
        Example queryMCExample = new Example(WindEngineMillPO.class);
        Example.Criteria criteriaMC = queryMCExample.createCriteria();
        criteriaMC.andIn("machineTypeCode", machineTypeCodes);
        criteriaMC.andEqualTo("delState", DeleteStatusEnum.NODELETE.getCode());
        List<WindEngineMillPO> windEngineMillPOS = windEngineMillMapper.selectByExample(queryMCExample);
        WindEngineMillVo windEngineMillVo = null;
        List<WindEngineMillVo> windEngineMillVos = Lists.newArrayList();
        for(WindEngineMillPO windEngineMillPO : windEngineMillPOS){
            windEngineMillVo = new WindEngineMillVo();
            BeanUtil.copyProperties(windEngineMillPO,windEngineMillVo);
            windEngineMillVos.add(windEngineMillVo);
        }
        return windEngineMillVos;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateWindEngineMill(WindEngineMillDto dto) {
        WindEngineMillPO po = new WindEngineMillPO();
        BeanUtil.copyProperties(dto, po);
        Example queryExample = new Example(WindEngineMillPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("millTypeCode", dto.getMillTypeCode());
        return windEngineMillMapper.updateByExampleSelective(po, queryExample) > 0;
    }


    @Override
    public boolean verificationData(String machineTypeCode, String manufacturerCode) {

        Example queryExample = new Example(WindEngineMillPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("machineTypeCode", machineTypeCode);
        criteria.andEqualTo("manufacturerCode", manufacturerCode);
        int num = windEngineMillMapper.selectCountByExample(queryExample);
        return num > 0 ? false : true;

    }


}
