package vip.xiaonuo.production.service.impl;

import cn.hutool.core.date.DateUtil;
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.mapper.ProductionEnergyMapper;
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.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);
    }

}
