package com.energy.manage.service.service.powerwordcontract.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.po.windfield.WindFieldContractPO;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.service.domain.dto.WindFieldContract.WindFieldContractAddDto;
import com.energy.manage.service.domain.dto.powerwordcontract.PowerWordContractDelDto;
import com.energy.manage.service.domain.dto.powerwordcontract.PowerWordContractDto;
import com.energy.manage.service.domain.dto.powerwordcontract.PowerWordContractExcelDto;
import com.energy.manage.service.domain.dto.powerwordcontract.PowerWordContractPageDto;
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.excel.WindFieldExceVo;
import com.energy.manage.service.domain.vo.powerwordcontract.PowerWordContractPageVo;
import com.energy.manage.service.mappers.powerwordcontract.PowerWordContractMapper;
import com.energy.manage.service.service.powerwordcontract.PowerWordContractService;
import com.energy.manage.service.service.windfield.WindFieldContractService;
import com.energy.manage.service.service.windfield.WindFieldService;
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.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PowerWordContractServiceImpl extends BaseServiceImpl<PowerWordContractPO> implements PowerWordContractService {

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private PowerWordContractMapper powerWordContractMapper;

    @Autowired
    private WindFieldService windFieldService;

    @Autowired
    private WindFieldContractService windFieldContractService;



    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultResp powerWordContractImportData(MultipartFile file, PowerWordContractExcelDto powerWordContractExcelDto) throws IOException {
        if (StringUtils.isEmpty(powerWordContractExcelDto.getFieldCode())) {
            return ResultResp.FAIL("未填写机型！");
        }
        List<PowerWordContractExcelVo> vos = EasyExcel.read(file.getInputStream()).head(PowerWordContractExcelVo.class).sheet().doReadSync();
        if (CollectionUtils.isEmpty(vos)) {
            return ResultResp.FAIL("未读取到文件数据，请核对数据准确性！");
        }

        // 过滤空数据
        List<PowerWordContractExcelVo> powerWordContractExcelVos = vos.stream().filter(item -> item.getActivePower()!=null && item.getWindSpeed()!=null).collect(Collectors.toList());


        // 更新风场功率曲线地址
        String path = minioUtils.upload(file);
        log.info(" 合同功率曲线minio上传地址 ====> " + path);

        WindFieldContractAddDto windFieldContractAddDto = new WindFieldContractAddDto();
        windFieldContractAddDto.setCreateBy(powerWordContractExcelDto.getCreateBy());
        windFieldContractAddDto.setContractSource(path);
        windFieldContractAddDto.setFieldCode(powerWordContractExcelDto.getFieldCode());
        windFieldContractAddDto.setMillTypeCode(powerWordContractExcelDto.getMillTypeCode());
        windFieldContractAddDto.setMachineTypeCode(powerWordContractExcelDto.getMachineTypeCode());

        WindFieldContractPO windFieldContractPO = windFieldContractService.addWindFieldContract(windFieldContractAddDto);
        if (windFieldContractPO.getId() == null) {
            return ResultResp.FAIL();
        }
        PowerWordContractPO powerWordContractPO = null;
        List<PowerWordContractPO> list = Lists.newArrayList();

        for (PowerWordContractExcelVo powerWordContractExcelVo : powerWordContractExcelVos) {
            powerWordContractPO = this.createPowerWordContractPO();
            BeanUtil.copyProperties(powerWordContractExcelVo, powerWordContractPO);
            powerWordContractPO.setFieldContractId(windFieldContractPO.getId());
            powerWordContractPO.setFieldCode(powerWordContractExcelDto.getFieldCode());
            powerWordContractPO.setMillTypeCode(powerWordContractExcelDto.getMillTypeCode());
            list.add(powerWordContractPO);
        }
        if (CollectionUtils.isEmpty(list)) {
            return ResultResp.FAIL("标准功率曲线处理异常");

        }
        if (powerWordContractMapper.insertList(list) > 0) {
            return ResultResp.SUCCESS();
        }
        return ResultResp.FAIL();
    }

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


    @Override
    public Page<PowerWordContractPageVo> powerWordContractPage(PowerWordContractPageDto powerWordContractPageDto) {
        PageHelper.startPage(powerWordContractPageDto.getPageNum(), powerWordContractPageDto.getPageSize());
        Example queryExample = new Example(PowerWordContractPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        if (!StringUtils.isEmpty(powerWordContractPageDto.getFieldCode())) {
            criteria.andEqualTo("fieldCode", powerWordContractPageDto.getFieldCode());
        }
        if (!StringUtils.isEmpty(powerWordContractPageDto.getMillTypeCode())) {
            criteria.andEqualTo("millTypeCode", powerWordContractPageDto.getMillTypeCode());
        }
        criteria.andEqualTo("delState", DeleteStatusEnum.NODELETE.getCode());
        List<PowerWordContractPO> list = powerWordContractMapper.selectByExample(queryExample);

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delPowerWordContract(PowerWordContractDelDto powerWordContractDelDto) {
        PowerWordContractPO po = new PowerWordContractPO();
        po.setDelState(DeleteStatusEnum.DELETE.getCode());
        Example queryExample = new Example(PowerWordContractPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("id", powerWordContractDelDto.getId());
        return powerWordContractMapper.updateByExampleSelective(po, queryExample) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delPowerWordContractData(Integer fieldContractId) {
        Example queryExample = new Example(PowerWordContractPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("fieldContractId", fieldContractId);
        return powerWordContractMapper.deleteByExample(queryExample) > 0;
    }

    @Override
    public boolean updatePowerWordContract(PowerWordContractDto powerWordContractDto) {
        PowerWordContractPO po = new PowerWordContractPO();
        BeanUtil.copyProperties(powerWordContractDto, po);
        Example queryExample = new Example(PowerWordContractPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("id", powerWordContractDto.getId());
        return powerWordContractMapper.updateByExampleSelective(po, queryExample) > 0;
    }

    @Override
    public int getPowerWordContractCount(Integer fieldContractId) {
        Example queryExample = new Example(PowerWordContractPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("fieldContractId", fieldContractId);
        return powerWordContractMapper.selectCountByExample(queryExample);
    }
}
