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.baomidou.mybatisplus.core.toolkit.StringUtils;
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.GasData;
import com.huimv.admin.entity.dto.Gas;
import com.huimv.admin.entity.vo.GasDataVo;
import com.huimv.admin.mapper.GasDataMapper;
import com.huimv.admin.service.IGasDataService;
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.util.*;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-02-21
 */
@Service
public class GasDataServiceImpl extends ServiceImpl<GasDataMapper, GasData> implements IGasDataService {

    @Autowired
    private GasDataMapper dataMapper;

    @Override
    public Result list(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String date = paramsMap.get("time");
        String displayType = paramsMap.get("displayType");
        if (StringUtils.isBlank(displayType)){
            displayType = "0";
        }
        QueryWrapper<GasData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("display_type",Integer.parseInt(displayType));
        queryWrapper.eq("farm_id", farmId);
        if (farmId.equals("22")) {
            if (date == null || date == "") {
                queryWrapper.orderByAsc("create_time");
            } else {
                String startTime = date + " 00:00:00";
                String endTime = date + " 23:59:59";
                queryWrapper.between("create_time", startTime, endTime).orderByAsc("create_time");
            }
            List<GasData> gasData = dataMapper.selectList(queryWrapper);
            GasDataVo gasDataVo = new GasDataVo();
            List<GasData> gasDataList = new LinkedList<>();
            List<GasData> gasDataList1 = new LinkedList<>();
            List<GasData> gasDataList2 = new LinkedList<>();
            List<GasData> gasDataList3 = new LinkedList<>();
            for (GasData gasDatum : gasData) {
                if (gasDatum.getLoctionType() == 1) {
                    gasDataList.add(gasDatum);
                } else if (gasDatum.getLoctionType() == 2) {
                    gasDataList1.add(gasDatum);
                } else if (gasDatum.getLoctionType() == 3) {
                    gasDataList2.add(gasDatum);
                } else if (gasDatum.getLoctionType() == 4) {
                    gasDataList3.add(gasDatum);
                }
            }
            gasDataVo.setPigpen(gasDataList);
            gasDataVo.setAbove(gasDataList1);
            gasDataVo.setUnder(gasDataList2);
            gasDataVo.setPeople(gasDataList3);
            return new Result(ResultCode.SUCCESS,gasDataVo);
        }else {
            if (date == null || date == "") {
                DateTime dateTime = DateUtil.beginOfDay(new Date());
                date = dateTime.toString();
                queryWrapper.ge("create_time", date).orderByAsc("create_time");
            } else {
                String startTime = date + " 00:00:00";
                String endTime = date + " 23:59:59";
                queryWrapper.between("create_time", startTime, endTime).orderByAsc("create_time");
            }
            List<GasData> gasData = dataMapper.selectList(queryWrapper);
            GasDataVo gasDataVo = new GasDataVo();
            List<GasData> gasDataList = new LinkedList<>();
            List<GasData> gasDataList1 = new LinkedList<>();
            List<GasData> gasDataList2 = new LinkedList<>();
            List<GasData> gasDataList3 = new LinkedList<>();
            for (GasData gasDatum : gasData) {
                if (gasDatum.getLoctionType() == 1) {
                    gasDataList.add(gasDatum);
                } else if (gasDatum.getLoctionType() == 2) {
                    gasDataList1.add(gasDatum);
                } else if (gasDatum.getLoctionType() == 3) {
                    gasDataList2.add(gasDatum);
                } else if (gasDatum.getLoctionType() == 4) {
                    gasDataList3.add(gasDatum);
                }
            }
            gasDataVo.setPigpen(gasDataList);
            gasDataVo.setAbove(gasDataList1);
            gasDataVo.setUnder(gasDataList2);
            gasDataVo.setPeople(gasDataList3);
            return new Result(ResultCode.SUCCESS,gasDataVo);
        }

    }

    @Override
    public Result listArea(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String displayType = paramsMap.get("displayType");
        JSONObject jsonObject = new JSONObject();

        if (StringUtils.isBlank(displayType)){
            displayType = "0";
        }
        QueryWrapper<GasData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("display_type",Integer.parseInt(displayType));
        queryWrapper.eq("farm_id", farmId).eq("loction_type", 1).orderByDesc("create_time")
                .last(" limit 1");
        GasData gasData = dataMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(gasData)){
            jsonObject.put("pigpen", new GasData());
        }else {
            jsonObject.put("pigpen", gasData);
        }


        QueryWrapper<GasData> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("display_type",Integer.parseInt(displayType));
        queryWrapper2.eq("farm_id", farmId).eq("loction_type", 2).orderByDesc("create_time")
                .last(" limit 1");
        GasData gasData2 = dataMapper.selectOne(queryWrapper2);
        if (ObjectUtil.isEmpty(gasData2)){
            jsonObject.put("above", new GasData());
        }else {
            jsonObject.put("above", gasData2);
        }


        QueryWrapper<GasData> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("display_type",Integer.parseInt(displayType));
        queryWrapper3.eq("farm_id", farmId).eq("loction_type", 3).orderByDesc("create_time")
                .last(" limit 1");
        GasData gasData3 = dataMapper.selectOne(queryWrapper3);
        if (ObjectUtil.isEmpty(gasData3)){
            jsonObject.put("under", new GasData());
        }else {
            jsonObject.put("under", gasData3);
        }


        QueryWrapper<GasData> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.eq("display_type",Integer.parseInt(displayType));
        queryWrapper4.eq("farm_id", farmId).eq("loction_type", 4).orderByDesc("create_time")
                .last(" limit 1");
        GasData gasData4 = dataMapper.selectOne(queryWrapper4);
        if (ObjectUtil.isEmpty(gasData4)){
            jsonObject.put("people", new GasData());
        }else {
            jsonObject.put("people", gasData4);
        }


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

    @Override
    public Result listGas(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        List<Date> dateList = dataMapper.selectTime(farmId);
        List<Gas> gasList = new ArrayList<>();
        for (Date date : dateList) {
            Gas gas = new Gas();
            gas.setCreateTime(date);
            gas.setAbove(dataMapper.selectOne(new QueryWrapper<GasData>().eq("loction_type",2).eq("farm_id",farmId).eq("create_time",date).eq("display_type",1)));
            gas.setPeople(dataMapper.selectOne(new QueryWrapper<GasData>().eq("loction_type",4).eq("farm_id",farmId).eq("create_time",date).eq("display_type",1)));
            gas.setPigpen(dataMapper.selectOne(new QueryWrapper<GasData>().eq("loction_type",1).eq("farm_id",farmId).eq("create_time",date).eq("display_type",1)));
            gas.setUnder(dataMapper.selectOne(new QueryWrapper<GasData>().eq("loction_type",3).eq("farm_id",farmId).eq("create_time",date).eq("display_type",1)));
            gasList.add(gas);
        }
        return new Result(ResultCode.SUCCESS,gasList);
    }
}
