package com.project.zcustom.service.service.smart.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.project.common.core.domain.search.PageParamVo;
import com.project.zcustom.domain.smart.PlatGreenSmartEnergy;
import com.project.zcustom.mapper.service.smart.PlatGreenSmartEnergyMapper;
import com.project.zcustom.service.service.smart.IPlatGreenSmartEnergyService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 能耗信息Service业务层处理
 *
 * @author change
 * @date 2024-10-31
 */
@Service
public class PlatGreenSmartEnergyServiceImpl extends ServiceImpl<PlatGreenSmartEnergyMapper, PlatGreenSmartEnergy> implements IPlatGreenSmartEnergyService {

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<PlatGreenSmartEnergy> getLambdaQueryWrapper(PlatGreenSmartEnergy platGreenSmartEnergy) {
        LambdaQueryWrapper<PlatGreenSmartEnergy> lqw = new LambdaQueryWrapper<PlatGreenSmartEnergy>();
        if (StringUtils.isNotBlank(platGreenSmartEnergy.getAppOrg())) {
            lqw.eq(PlatGreenSmartEnergy::getAppOrg, platGreenSmartEnergy.getAppOrg());
        }
        if (StringUtils.isNotBlank(platGreenSmartEnergy.getCanteenName())) {
            lqw.like(PlatGreenSmartEnergy::getCanteenName, platGreenSmartEnergy.getCanteenName());
        }
        return lqw;
    }

    /**
     * 分页查询能耗信息列表
     *
     * @param platGreenSmartEnergy 能耗信息
     * @return 能耗信息集合
     */
    @Override
    public IPage<PlatGreenSmartEnergy> pagingPlatGreenSmartEnergyList(PlatGreenSmartEnergy platGreenSmartEnergy) {
        IPage<PlatGreenSmartEnergy> page = new Page<>(platGreenSmartEnergy.getPageNum(), platGreenSmartEnergy.getPageSize());
        LambdaQueryWrapper<PlatGreenSmartEnergy> lqw = getLambdaQueryWrapper(platGreenSmartEnergy);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatGreenSmartEnergy::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询能耗信息列表
     *
     * @param lqw
     * @return 能耗信息集合
     */
    @Override
    public IPage<PlatGreenSmartEnergy> pagingPlatGreenSmartEnergyList(PageParamVo pageParamVo, LambdaQueryWrapper<PlatGreenSmartEnergy> lqw) {
        IPage<PlatGreenSmartEnergy> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatGreenSmartEnergy::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询能耗信息列表
     *
     * @param platGreenSmartEnergy 能耗信息
     * @return 能耗信息
     */
    @Override
    public List<PlatGreenSmartEnergy> selectPlatGreenSmartEnergyList(PlatGreenSmartEnergy platGreenSmartEnergy) {
        LambdaQueryWrapper<PlatGreenSmartEnergy> lqw = getLambdaQueryWrapper(platGreenSmartEnergy);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatGreenSmartEnergy::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询能耗信息列表
     *
     * @param platGreenSmartEnergy 能耗信息
     * @return 能耗信息
     */
    @Override
    public List<PlatGreenSmartEnergy> selectPlatGreenSmartEnergyList(PlatGreenSmartEnergy platGreenSmartEnergy, Long limitNum) {
        LambdaQueryWrapper<PlatGreenSmartEnergy> lqw = getLambdaQueryWrapper(platGreenSmartEnergy);
        // 排序 默认时间倒序
        lqw.orderByDesc(PlatGreenSmartEnergy::getCreateTime);
        lqw.last("limit " + limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 能耗信息
     */
    @Override
    public PlatGreenSmartEnergy getPlatGreenSmartEnergyByField(String field, String filedValue) {
        QueryWrapper<PlatGreenSmartEnergy> lqw = new QueryWrapper<PlatGreenSmartEnergy>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 能耗信息
     */
    @Override
    public PlatGreenSmartEnergy getPlatGreenSmartEnergyByField(String field, String filedValue,String fieldString) {
        QueryWrapper<PlatGreenSmartEnergy> lqw = new QueryWrapper<PlatGreenSmartEnergy>();
        if(!(Objects.equals(field,"app_org") && Objects.equals(filedValue,"0000"))){
            lqw.eq(field, filedValue);
        }
        lqw.select(fieldString);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 能耗信息
     */
    @Override
    public List<PlatGreenSmartEnergy> selectPlatGreenSmartEnergyListByField(String field, String filedValue, Long limitNum) {
        QueryWrapper<PlatGreenSmartEnergy> lqw = new QueryWrapper<PlatGreenSmartEnergy>();
        lqw.eq(field, filedValue);
        lqw.last("limit " + limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field      字段名称
     * @param filedValue 字段值
     * @return 能耗信息
     */
    @Override
    public List<PlatGreenSmartEnergy> selectPlatGreenSmartEnergyListByField(String field, String filedValue) {
        QueryWrapper<PlatGreenSmartEnergy> lqw = new QueryWrapper<PlatGreenSmartEnergy>();
        lqw.eq(field, filedValue);
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field      字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue, String tips, Long id) {
        QueryWrapper<PlatGreenSmartEnergy> lqw = new QueryWrapper<PlatGreenSmartEnergy>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if (this.getOne(lqw, false) != null) {
            throw new RuntimeException(tips + "已存在");
        }
    }

    /**
     * 真删接口
     */
    @Override
    public void reallyDeletePlatGreenSmartEnergy(LambdaQueryWrapper<PlatGreenSmartEnergy> lqw) {
        baseMapper.reallyDeleteByWrapper(lqw);
    }

    /**
     * 添加能耗数据
     */
    @Override
    @Transactional
    public void addEnergyData(String appOrg, Map<String, Object> mapField){
        List<String> keyList = Arrays.asList("name", "waterConsumption", "quantityHeat", "gasVolume");
        // 先删除能耗数据
        LambdaQueryWrapper<PlatGreenSmartEnergy> lqw = new LambdaQueryWrapper<PlatGreenSmartEnergy>();
        lqw.eq(PlatGreenSmartEnergy::getAppOrg, appOrg);
        baseMapper.reallyDeleteByWrapper(lqw);
        if(mapField.containsKey("energy")){
            JSONArray energy = JSONArray.parseArray(mapField.get("energy").toString());
            List<PlatGreenSmartEnergy> addList = new ArrayList<>();
            for (Object o : energy){
                Map<String, Object> temp = JSONObject.parseObject(o.toString(), Map.class);
                PlatGreenSmartEnergy smartInfo = new PlatGreenSmartEnergy();
                smartInfo.setAppOrg(appOrg);
                StringUtils.setFieldValue(smartInfo,temp,keyList,0, PlatGreenSmartEnergy::setCanteenName,String.class);
                StringUtils.setFieldValue(smartInfo,temp,keyList,1, PlatGreenSmartEnergy::setWaterConsumptionNum,Long.class);
                StringUtils.setFieldValue(smartInfo,temp,keyList,2, PlatGreenSmartEnergy::setQuantityHeatNum,Long.class);
                StringUtils.setFieldValue(smartInfo,temp,keyList,3, PlatGreenSmartEnergy::setGasVolumeNum,Long.class);
                addList.add(smartInfo);
            }
            this.saveBatch(addList);
        }
    }

    /**
     * 获取能耗信息
     * @param appOrg
     * @return
     */
    @Override
    public Map<String,Object> getPowerData(String appOrg){
        String fieldString = "sum(water_consumption_num) as water_consumption_num,sum(quantity_heat_num) as quantity_heat_num,sum(gas_volume_num) as gas_volume_num";
        PlatGreenSmartEnergy platGreenSmartEnergy = this.getPlatGreenSmartEnergyByField("app_org", appOrg,fieldString);
        Map<String, Object> tempMap = new HashMap<>();
        if(StringUtils.isNull(platGreenSmartEnergy)){
            StringUtils.getMapData("食堂用水量", "waterConsumption", 0, tempMap);
            StringUtils.getMapData("食堂用热量", "quantityHeat", 0, tempMap);
            StringUtils.getMapData("食堂用气量", "gasVolume", 0, tempMap);
        }else{
            StringUtils.getMapData("食堂用水量", "waterConsumptionNum", platGreenSmartEnergy.getWaterConsumptionNum(), tempMap);
            StringUtils.getMapData("食堂用热量", "quantityHeatNum", platGreenSmartEnergy.getQuantityHeatNum(), tempMap);
            StringUtils.getMapData("食堂用气量", "gasVolumeNum", platGreenSmartEnergy.getGasVolumeNum(), tempMap);
        }
        return tempMap;
    }
}
