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

import cn.hutool.core.bean.BeanUtil;
import com.beust.jcommander.internal.Lists;
import com.energy.manage.common.base.BaseServiceImpl;
import com.energy.manage.common.enums.DeleteStatusEnum;
import com.energy.manage.common.po.windfield.WindFieldContractPO;
import com.energy.manage.common.po.windfield.WindFieldPO;
import com.energy.manage.common.po.windfield.WindFieldResourcePO;
import com.energy.manage.service.domain.dto.WindFieldContract.WindFieldContractAddDto;
import com.energy.manage.service.domain.dto.WindFieldContract.WindFieldContractDelDto;
import com.energy.manage.service.domain.dto.windenginemill.WindEngineMillCodeDto;
import com.energy.manage.service.domain.vo.WindFieldContract.WindFieldContractPageVo;
import com.energy.manage.service.domain.vo.windenginemill.WindEngineMillPageVo;
import com.energy.manage.service.mappers.windfield.WindFieldContractMapper;
import com.energy.manage.service.mappers.windfield.WindFieldResourceMapper;
import com.energy.manage.service.service.powerwordcontract.PowerWordContractService;
import com.energy.manage.service.service.windenginemill.WindEngineMillService;
import com.energy.manage.service.service.windfield.WindFieldContractService;
import com.energy.manage.service.service.windfield.WindFieldResourceService;
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 tk.mybatis.mapper.entity.Example;

import java.beans.Transient;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class WindFieldContractServiceImpl extends BaseServiceImpl<WindFieldContractPO> implements WindFieldContractService {


    @Autowired
    private WindFieldContractMapper windFieldContractMapper;

    @Autowired
    private PowerWordContractService powerWordContractService;





    @Override
    @Transactional(rollbackFor = Exception.class)
    public WindFieldContractPO addWindFieldContract(WindFieldContractAddDto WindFieldContractAddDto) {

        if (StringUtils.isEmpty(WindFieldContractAddDto.getMillTypeCode())) {
            log.info("机型编号不能为空");
            return null;
        }
        WindFieldContractPO windFieldContractPO = new WindFieldContractPO();
        BeanUtil.copyProperties(WindFieldContractAddDto, windFieldContractPO);

        // 查询是否有旧的机型对应合同曲线数据
        WindFieldContractPO po = this.getWindFieldContractPO(WindFieldContractAddDto.getFieldCode(),WindFieldContractAddDto.getMillTypeCode());
        // 判断机型是否存在
        if (po != null) {

            // 删除旧对象
            WindFieldContractDelDto windFieldContractDelDto = new WindFieldContractDelDto();
            windFieldContractDelDto.setWindFieldContractId(po.getId());
            this.delWindFieldContract(windFieldContractDelDto);

            // 赋值覆盖id
            windFieldContractPO.setCoverId(po.getId());
        }

        windFieldContractPO.setDelState(DeleteStatusEnum.NODELETE.getCode());
        windFieldContractPO.setCreateTime(new Date());
        windFieldContractPO.setCreateBy(WindFieldContractAddDto.getCreateBy());
        windFieldContractMapper.insertUseGeneratedKeys(windFieldContractPO);

        return windFieldContractPO;
    }


    private WindFieldContractPO getWindFieldContractPO(String fieldCode, String millTypecode) {
        Example queryExample = new Example(WindFieldContractPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("fieldCode", fieldCode);
        criteria.andEqualTo("millTypeCode", millTypecode);
        criteria.andEqualTo("delState", DeleteStatusEnum.NODELETE.getCode());
        return windFieldContractMapper.selectOneByExample(queryExample);

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delWindFieldContract(WindFieldContractDelDto windFieldContractDelDto) {
        WindFieldContractPO po = new WindFieldContractPO();
        po.setDelState(DeleteStatusEnum.DELETE.getCode());
        Example queryExample = new Example(WindFieldContractPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("id", windFieldContractDelDto.getWindFieldContractId());

        // 物理删除功率曲线数据
        if(powerWordContractService.getPowerWordContractCount( windFieldContractDelDto.getWindFieldContractId())>0){
            powerWordContractService.delPowerWordContractData(windFieldContractDelDto.getWindFieldContractId());
        }

        // 逻辑删除关系
        return windFieldContractMapper.updateByExampleSelective(po, queryExample) > 0;
    }

    @Override
    public List<WindFieldContractPageVo> getWindFieldContract(String  fieldCode) {
        Example queryExample = new Example(WindFieldContractPO.class);
        Example.Criteria criteria = queryExample.createCriteria();
        criteria.andEqualTo("fieldCode", fieldCode);
        criteria.andEqualTo("delState", DeleteStatusEnum.NODELETE.getCode());
        List<WindFieldContractPO> windFieldContractPOS =  windFieldContractMapper.selectByExample(queryExample);

        WindFieldContractPageVo windFieldContractPageVo = null;
        List<WindFieldContractPageVo> list = Lists.newArrayList();
        for(WindFieldContractPO  po: windFieldContractPOS){
            windFieldContractPageVo = new WindFieldContractPageVo();
            BeanUtil.copyProperties(po,windFieldContractPageVo);
            list.add(windFieldContractPageVo);
        }
        return list;
    }
}
