package vip.xiaonuo.production.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.production.entity.ProductionEnergy;
import vip.xiaonuo.production.entity.ProductionEnergyPrice;
import vip.xiaonuo.production.entity.vo.ProductionWriteEnergyVo;
import vip.xiaonuo.production.mapper.ProductionEnergyMapper;
import vip.xiaonuo.production.mapper.ProductionEnergyPriceMapper;
import vip.xiaonuo.production.mapper.ProductionWriteEnergyMapper;
import vip.xiaonuo.production.param.ProductionEnergyParam;
import vip.xiaonuo.production.service.IProductionEnergyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 生成管控能耗分析 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-12
 */
@Service
public class ProductionEnergyServiceImpl extends ServiceImpl<ProductionEnergyMapper, ProductionEnergy> implements IProductionEnergyService {

    @Autowired
    private ProductionEnergyMapper energyMapper;

//    @Override
//    public List<ProductionEnergy> getList(ProductionEnergyParam energyParam) {
//        String orgId = energyParam.getOrgId();
//        Integer timeType = energyParam.getTimeType();
//        String locationName = energyParam.getLocationName();
//        QueryWrapper<ProductionEnergy> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId)
//                .eq(StringUtils.isNotBlank(locationName), "location_name", locationName);
//        queryWrapper.select(" IFNULL(sum(water),'0') water,IFNULL(sum(electricity),'0') electricity,IFNULL(sum(gas),'0') gas,create_time createTime");
//        if (0 == timeType) {
//            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
//        } else {
//            queryWrapper.ge("create_time", DateUtil.beginOfYear(new Date()));
//            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
//        }
//        queryWrapper.orderByAsc("create_time");
//        return energyMapper.selectList(queryWrapper);
//    }

    @Autowired
    private ProductionEnergyPriceMapper priceMapper;
    @Autowired
    private ProductionWriteEnergyMapper writeEnergyMapper;
    @Override
    public   List<ProductionWriteEnergyVo> getList(ProductionEnergyParam energyParam) {
        String orgId = energyParam.getOrgId();
        Integer timeType = energyParam.getTimeType();
        if (ObjectUtil.isEmpty(timeType) ){
            timeType = 0;
        }
        Integer number = energyParam.getNumber();
        QueryWrapper<ProductionEnergyPrice> priceQueryWrapper = new QueryWrapper<>();
        priceQueryWrapper.eq(StringUtils.isNotBlank(orgId),"org_id",orgId);
        List<ProductionEnergyPrice> energyPrice = priceMapper.selectList(priceQueryWrapper);

        ProductionEnergyPrice productionEnergyPrice = null;
        if (ObjectUtil.isEmpty(energyPrice)){
             productionEnergyPrice  =new ProductionEnergyPrice();
                productionEnergyPrice.setElectricity("1");

                productionEnergyPrice.setZhengQi("1");

                productionEnergyPrice.setDanQi("1");

                productionEnergyPrice.setYaSuo("1");

                productionEnergyPrice.setRoWater("1");

                productionEnergyPrice.setZlWater("1");

                productionEnergyPrice.setXhWater("1");

                productionEnergyPrice.setQdWater("1");

                productionEnergyPrice.setSwdWater("1");

                productionEnergyPrice.setEswdWater("1");

                productionEnergyPrice.setReWater("1");

                productionEnergyPrice.setDiwenWater("1");

                productionEnergyPrice.setFuSwdYierchun("1");

                productionEnergyPrice.setFuEswdYierchun("1");

        }
        if (ObjectUtil.isNotEmpty(energyPrice)) {
             productionEnergyPrice = energyPrice.get(0);
            if ("".equals(productionEnergyPrice.getElectricity())) {
                productionEnergyPrice.setElectricity("1");
            }
            if ("".equals(productionEnergyPrice.getZhengQi())) {
                productionEnergyPrice.setZhengQi("1");
            }
            if ("".equals(productionEnergyPrice.getDanQi())) {
                productionEnergyPrice.setDanQi("1");
            }
            if ("".equals(productionEnergyPrice.getYaSuo())) {
                productionEnergyPrice.setYaSuo("1");
            }
            if ("".equals(productionEnergyPrice.getRoWater())) {
                productionEnergyPrice.setRoWater("1");
            }
            if ("".equals(productionEnergyPrice.getZlWater())) {
                productionEnergyPrice.setZlWater("1");
            }
            if ("".equals(productionEnergyPrice.getXhWater())) {
                productionEnergyPrice.setXhWater("1");
            }
            if ("".equals(productionEnergyPrice.getQdWater())) {
                productionEnergyPrice.setQdWater("1");
            }
            if ("".equals(productionEnergyPrice.getSwdWater())) {
                productionEnergyPrice.setSwdWater("1");
            }
            if ("".equals(productionEnergyPrice.getEswdWater())) {
                productionEnergyPrice.setEswdWater("1");
            }
            if ("".equals(productionEnergyPrice.getReWater())) {
                productionEnergyPrice.setReWater("1");
            }
            if ("".equals(productionEnergyPrice.getDiwenWater())) {
                productionEnergyPrice.setDiwenWater("1");
            }
            if ("".equals(productionEnergyPrice.getFuSwdYierchun())) {
                productionEnergyPrice.setFuSwdYierchun("1");
            }
            if ("".equals(productionEnergyPrice.getFuEswdYierchun())) {
                productionEnergyPrice.setFuEswdYierchun("1");
            }
        }
        QueryWrapper<ProductionEnergy> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(orgId),"org_id",orgId);
        wrapper.groupBy("ymonth");
        List<ProductionWriteEnergyVo> list ;
        if (timeType == 0){
            Calendar calendar = Calendar.getInstance();
            if (null == number) {
                calendar.add(Calendar.YEAR, -3);
            } else {
                calendar.add(Calendar.YEAR, -number);
            }
            wrapper.between("create_time", calendar.getTime(), new Date());
          list =  writeEnergyMapper.selectYear(wrapper,
                    productionEnergyPrice.getElectricity() ,
                    productionEnergyPrice.getZhengQi(),
                    productionEnergyPrice.getDanQi(),
                    productionEnergyPrice.getYaSuo(),
                    productionEnergyPrice.getRoWater(),
                    productionEnergyPrice.getZlWater(),
                    productionEnergyPrice.getXhWater(),
                    productionEnergyPrice.getQdWater(),
                    productionEnergyPrice.getSwdWater(),
                    productionEnergyPrice.getEswdWater(),
                    productionEnergyPrice.getReWater(),
                    productionEnergyPrice.getDiwenWater(),
                    productionEnergyPrice.getFuSwdYierchun(),
                    productionEnergyPrice.getFuEswdYierchun());
        }else {
            Calendar calendar = Calendar.getInstance();
            if (null == number) {
                calendar.add(Calendar.MONTH, -13);
            } else {
                calendar.add(Calendar.MONTH, -number);
            }
            wrapper.between("create_time", calendar.getTime(), new Date());
            list =  writeEnergyMapper.selectMonth(wrapper,
                    productionEnergyPrice.getElectricity() ,
                    productionEnergyPrice.getZhengQi(),
                    productionEnergyPrice.getDanQi(),
                    productionEnergyPrice.getYaSuo(),
                    productionEnergyPrice.getRoWater(),
                    productionEnergyPrice.getZlWater(),
                    productionEnergyPrice.getXhWater(),
                    productionEnergyPrice.getQdWater(),
                    productionEnergyPrice.getSwdWater(),
                    productionEnergyPrice.getEswdWater(),
                    productionEnergyPrice.getReWater(),
                    productionEnergyPrice.getDiwenWater(),
                    productionEnergyPrice.getFuSwdYierchun(),
                    productionEnergyPrice.getFuEswdYierchun());
        }
        return list;


    }

}
