package com.huimv.admin.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.DataUill;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.BasePigpen;
import com.huimv.admin.entity.EnergyData;
import com.huimv.admin.entity.EnergyGas;
import com.huimv.admin.entity.vo.EnergyDataVo;
import com.huimv.admin.entity.vo.EnergyGasDataVo;
import com.huimv.admin.mapper.BasePigpenMapper;
import com.huimv.admin.mapper.EnergyDataMapper;
import com.huimv.admin.mapper.EnergyGasMapper;
import com.huimv.admin.service.IEnergyGasService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-14
 */
@Service
public class EnergyGasServiceImpl extends ServiceImpl<EnergyGasMapper, EnergyGas> implements IEnergyGasService {

    @Autowired
    private EnergyGasMapper energyGasMapper;
    @Autowired
    private BasePigpenMapper basePigpenMapper;
    @Autowired
    private EnergyDataMapper dataMapper;

    @Override
    public Result listPigpen(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String displayType = paramsMap.get("displayType");
        String id = paramsMap.get("id");

        JSONArray jsonArray = new JSONArray();
        QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).eq("parent_id", id);
        List<BasePigpen> basePigpens = basePigpenMapper.selectList(queryWrapper);//得到所有的楼层
        for (BasePigpen basePigpen : basePigpens) {
            Float count = 0.00f;
            QueryWrapper<BasePigpen> basePigpenQueryWrapper = new QueryWrapper<>();
            basePigpenQueryWrapper.like("other2", basePigpen.getId()).eq("farm_id", farmId).eq("f_type", 3);
            List<BasePigpen> basePigpens1 = basePigpenMapper.selectList(basePigpenQueryWrapper);//得到所有的单元
            List<Integer> idList = new ArrayList<>();
            basePigpens1.forEach(item->{
                idList.add(item.getId());
            });
            if (ObjectUtil.isEmpty(idList)){
                return  new Result(ResultCode.SUCCESS, jsonArray);
            }
            JSONObject jsonObject = new JSONObject();
            QueryWrapper<EnergyData> queryWrapper1 = new QueryWrapper<>();
            if (ObjectUtil.isNotEmpty(displayType)){
                queryWrapper1.eq("display_type",displayType);
            }
            queryWrapper1.in("unit_id", idList).eq("farm_id", farmId);
            DateTime dateTime = DateUtil.beginOfDay(new Date());
            queryWrapper1.ge("create_date", dateTime);
            queryWrapper1.select(" IFNULL(SUM(gas_value),'0') gasValue");
            EnergyData energyGas = dataMapper.selectOne(queryWrapper1);

            if (ObjectUtil.isNotEmpty(energyGas)) {
                count = count + Float.parseFloat(energyGas.getGasValue());
            }

//            DecimalFormat df = new DecimalFormat("#.00");
            jsonObject.put("value",count );
            jsonObject.put("buildName", basePigpen.getBuildName());
            jsonObject.put("id", basePigpen.getId());
            jsonArray.add(jsonObject);
        }

        return new Result(ResultCode.SUCCESS, jsonArray);
    }

    @Override
    public Result listData(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String displayType = paramsMap.get("displayType");
        String type = paramsMap.get("type");
        String id = paramsMap.get("id");//楼层id
        String startTime = paramsMap.get("startTime");
        String endTime = paramsMap.get("endTime");

        List<EnergyGasDataVo> energyGasList = null;
        //默认显示本年
        if (type == null || type == "") {
            type = "3";
        }
        QueryWrapper<BasePigpen> pigpenQueryWrapper = new QueryWrapper<>();
        pigpenQueryWrapper.eq("farm_id", farmId).like("other2", id);
        List<BasePigpen> basePigpens = basePigpenMapper.selectList(pigpenQueryWrapper);//单元的集合
        List<Integer> unitIds = new ArrayList<>();
        basePigpens.forEach(item -> {
            unitIds.add(item.getId());
        });

        QueryWrapper<EnergyData> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(displayType)){
            queryWrapper.eq("display_type",displayType);
        }
        queryWrapper.eq("farm_id", farmId).in("unit_id", unitIds);
        //自定义查询
        if ("4".equals(type)) {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 23:59:59";
            queryWrapper.between("create_date", startTime, endTime);
            energyGasList = dataMapper.listDay(queryWrapper);
        }
        //全年
        else if ("3".equals(type)) {
            DateTime dateTime = DateUtil.beginOfYear(new Date());
            queryWrapper.ge("create_date", dateTime);
            List<EnergyGasDataVo> energyGasDataVos = dataMapper.listYear(queryWrapper);
            energyGasList = energyGasDataVos;
        }
        //本月
        else if ("2".equals(type)) {
            Date timesMonthmorning = DataUill.getTimesMonthmorning();
            queryWrapper.ge("create_date", timesMonthmorning);
            energyGasList = dataMapper.listDay(queryWrapper);
        }
        //本周
        else if ("1".equals(type)) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
            queryWrapper.ge("create_date", calendar.getTime());
            List<EnergyGasDataVo> energyGasDataVos = dataMapper.listDay(queryWrapper);
            energyGasList = energyGasDataVos;
        }

        return new Result(ResultCode.SUCCESS, energyGasList);
    }
}
