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


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.energy.manage.common.base.BaseServiceImpl;
import com.energy.manage.common.base.Page;
import com.energy.manage.common.enums.DeleteStatusEnum;
import com.energy.manage.common.po.powerwordcontract.PowerWordContractPO;
import com.energy.manage.common.po.powerwordcriterion.PowerWordCriterionPO;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.service.domain.dto.powerwordcriterion.PowerWordCriterionDelDto;
import com.energy.manage.service.domain.dto.powerwordcriterion.PowerWordCriterionDto;
import com.energy.manage.service.domain.dto.powerwordcriterion.PowerWordCriterionExcelDto;
import com.energy.manage.service.domain.dto.powerwordcriterion.PowerWordCriterionPageDto;
import com.energy.manage.service.domain.dto.windenginemill.WindEngineMillDto;
import com.energy.manage.service.domain.dto.windfield.WindFieldUpdateDto;
import com.energy.manage.service.domain.vo.excel.PowerWordContractExcelVo;
import com.energy.manage.service.domain.vo.excel.PowerWordCriterionExcelVo;
import com.energy.manage.service.domain.vo.powerwordcontract.PowerWordContractPageVo;
import com.energy.manage.service.domain.vo.powerwordcriterion.PowerWordCriterionPageVo;
import com.energy.manage.service.mappers.powerwordcriterion.PowerWordCriterionMapper;
import com.energy.manage.service.service.powerwordcriterion.PowerWordCriterionService;
import com.energy.manage.service.service.windenginemill.WindEngineMillService;
import com.energy.manage.service.util.MinioUtils;
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.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class PowerWordCriterionServiceImpl extends BaseServiceImpl<PowerWordCriterionPO> implements PowerWordCriterionService {


    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private WindEngineMillService windEngineMillService;

    @Autowired
    private PowerWordCriterionMapper powerWordCriterionMapper;


    @SneakyThrows
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultResp powerWordCriterionImportData(MultipartFile file, PowerWordCriterionExcelDto powerWordCriterionExcelDto) {

        if (StringUtils.isEmpty(powerWordCriterionExcelDto.getMillTypeCode())) {
            return ResultResp.FAIL("未选择机型！");
        }
        List<PowerWordCriterionExcelVo> powerWordCriterionExcelVos = EasyExcel.read(file.getInputStream()).head(PowerWordCriterionExcelVo.class).sheet().doReadSync();
        if (CollectionUtils.isEmpty(powerWordCriterionExcelVos)) {
            return ResultResp.FAIL("未读取到文件数据，请核对数据准确性！");
        }

        // 删除机型
        PowerWordCriterionPO po = new PowerWordCriterionPO();
        po.setDelState(DeleteStatusEnum.DELETE.getCode());
        Example queryExample = new Example(PowerWordCriterionPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("millTypeCode", powerWordCriterionExcelDto.getMillTypeCode());
        powerWordCriterionMapper.updateByExampleSelective(po, queryExample);

        PowerWordCriterionPO powerWordCriterionPO = null;
        List<PowerWordCriterionPO> list = Lists.newArrayList();
        for (PowerWordCriterionExcelVo powerWordCriterionExcelVo : powerWordCriterionExcelVos) {
            powerWordCriterionPO = this.createPowerWordCriterionPO();
            BeanUtil.copyProperties(powerWordCriterionExcelVo, powerWordCriterionPO);
            powerWordCriterionPO.setMillTypeCode(powerWordCriterionExcelDto.getMillTypeCode());
            list.add(powerWordCriterionPO);
        }
        if (CollectionUtils.isEmpty(list)) {
            return ResultResp.FAIL("标准功率曲线处理异常");

        }
        if (powerWordCriterionMapper.insertList(list) > 0) {
            // 更新风场功率曲线地址
            String path = minioUtils.upload(file);
            log.info(" 标准功率曲线minio上传地址 ====> " + path);

            if(!StringUtils.isEmpty(path)){
                WindEngineMillDto dto = new WindEngineMillDto();
                dto.setMillTypeCode(powerWordCriterionExcelDto.getMillTypeCode());
                dto.setPowerCriterionUrl(path);
                windEngineMillService.updateWindEngineMill(dto);
            }
            return ResultResp.SUCCESS();
        }
        return ResultResp.FAIL();
    }


    /**
     * 创建对象
     *
     * @param
     * @return
     */
    private PowerWordCriterionPO createPowerWordCriterionPO() {
        PowerWordCriterionPO po = new PowerWordCriterionPO();
        po.setCreateTime(new Date());
        po.setUpdateTime(new Date());
        po.setDelState(DeleteStatusEnum.NODELETE.getCode());
        return po;
    }

    @Override
    public Page<PowerWordCriterionPageVo> powerWordCriterionPage(PowerWordCriterionPageDto powerWordCriterionPageDto) {
        PageHelper.startPage(powerWordCriterionPageDto.getPageNum(), powerWordCriterionPageDto.getPageSize());
        Example queryExample = new Example(PowerWordContractPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        if (!StringUtils.isEmpty(powerWordCriterionPageDto.getMillTypeCode())) {
            criteria.andEqualTo("millTypeCode", powerWordCriterionPageDto.getMillTypeCode());
        }
        criteria.andEqualTo("delState", DeleteStatusEnum.NODELETE.getCode());
        List<PowerWordCriterionPO> list = powerWordCriterionMapper.selectByExample(queryExample);

        PowerWordCriterionPageVo vo = null;
        List<PowerWordCriterionPageVo> powerWordContractPageVos = Lists.newArrayList();
        for (PowerWordCriterionPO po : list) {
            vo = new PowerWordCriterionPageVo();
            BeanUtil.copyProperties(po, vo);
        }
        Long total = org.apache.commons.collections.CollectionUtils.isEmpty(list) ? 0L : new PageInfo<>(list).getTotal();
        return Page.build(powerWordCriterionPageDto.getPageNum(), powerWordCriterionPageDto.getPageSize(), total, powerWordContractPageVos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delpowerWordCriterion(PowerWordCriterionDelDto powerWordCriterionDelDto) {
        PowerWordCriterionPO po = new PowerWordCriterionPO();
        po.setDelState(DeleteStatusEnum.DELETE.getCode());
        Example queryExample = new Example(PowerWordCriterionPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("id", powerWordCriterionDelDto.getId());
        return powerWordCriterionMapper.updateByExampleSelective(po, queryExample) > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updatePowerWordCriterion(PowerWordCriterionDto powerWordCriterionDto) {
        PowerWordCriterionPO po = new PowerWordCriterionPO();
        BeanUtil.copyProperties(powerWordCriterionDto, po);
        Example queryExample = new Example(PowerWordCriterionPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("id", powerWordCriterionDto.getId());
        return powerWordCriterionMapper.updateByExampleSelective(po, queryExample) > 0;
    }


}
