package com.huimv.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.DisplayType;
import com.huimv.admin.entity.ProdAnalysis;
import com.huimv.admin.mapper.DisplayTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author newspaper
 * @since 2023-11-20
 */
@CrossOrigin
@RestController
@RequestMapping("/display-type")
public class DisplayTypeController {
    @Autowired
    private DisplayTypeMapper displayTypeMapper;

    @RequestMapping("/get")
    public Result get(HttpServletRequest httpServletRequest, @RequestBody Map<String,String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String eventType = paramsMap.get("eventType");
        QueryWrapper<DisplayType> queryWrapper = new QueryWrapper();
        queryWrapper.eq("farm_id",farmId).eq("event_type",eventType);
        DisplayType displayType = displayTypeMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(displayType)){
            displayType = new DisplayType();
            displayType.setFarmId(Integer.valueOf(farmId));
            displayType.setEventType(Integer.valueOf(eventType));
            displayType.setDisplayType(0);
            displayTypeMapper.insert(displayType);
        }
        return new Result(ResultCode.SUCCESS,displayType);
    }

    @RequestMapping("/getEnergy")
    public Result getEnergy(HttpServletRequest httpServletRequest, @RequestBody Map<String,String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        Map resultMap = new HashMap();
        DisplayType waterDisplayType = displayTypeMapper.selectOne(new QueryWrapper<DisplayType>().eq("farm_id",farmId).eq("event_type",9));
        if (ObjectUtil.isEmpty(waterDisplayType)){
            waterDisplayType = new DisplayType();
            waterDisplayType.setFarmId(Integer.valueOf(farmId));
            waterDisplayType.setEventType(9);
            waterDisplayType.setDisplayType(0);
            resultMap.put("waterDisplayType",0);
            displayTypeMapper.insert(waterDisplayType);
        }else {
            resultMap.put("waterDisplayType",waterDisplayType.getDisplayType());
        }

        DisplayType electricityDisplayType = displayTypeMapper.selectOne(new QueryWrapper<DisplayType>().eq("farm_id",farmId).eq("event_type",10));
        if (ObjectUtil.isEmpty(electricityDisplayType)){
            electricityDisplayType = new DisplayType();
            electricityDisplayType.setFarmId(Integer.valueOf(farmId));
            electricityDisplayType.setEventType(10);
            electricityDisplayType.setDisplayType(0);
            resultMap.put("electricityDisplayType",0);
            displayTypeMapper.insert(electricityDisplayType);
        }else {
            resultMap.put("electricityDisplayType",electricityDisplayType.getDisplayType());
        }

        DisplayType feedDisplayType  = displayTypeMapper.selectOne(new QueryWrapper<DisplayType>().eq("farm_id",farmId).eq("event_type",11));
        if (ObjectUtil.isEmpty(feedDisplayType )){
            feedDisplayType  = new DisplayType();
            feedDisplayType .setFarmId(Integer.valueOf(farmId));
            feedDisplayType .setEventType(11);
            feedDisplayType .setDisplayType(0);
            resultMap.put("feedDisplayType",0);
            displayTypeMapper.insert(feedDisplayType);
        }else {
            resultMap.put("feedDisplayType",feedDisplayType.getDisplayType());
        }
        DisplayType gasDisplayType = displayTypeMapper.selectOne(new QueryWrapper<DisplayType>().eq("farm_id",farmId).eq("event_type",7));
        if (ObjectUtil.isEmpty(gasDisplayType)){
            gasDisplayType = new DisplayType();
            gasDisplayType.setFarmId(Integer.valueOf(farmId));
            gasDisplayType.setEventType(7);
            gasDisplayType.setDisplayType(0);
            resultMap.put("gasDisplayType",0);
            displayTypeMapper.insert(gasDisplayType);
        }else {
            resultMap.put("gasDisplayType",gasDisplayType.getDisplayType());
        }

        DisplayType envDisplayType = displayTypeMapper.selectOne(new QueryWrapper<DisplayType>().eq("farm_id",farmId).eq("event_type",1));
        if (ObjectUtil.isEmpty(envDisplayType)){
            envDisplayType = new DisplayType();
            envDisplayType.setFarmId(Integer.valueOf(farmId));
            envDisplayType.setEventType(1);
            envDisplayType.setDisplayType(0);
            resultMap.put("envDisplayType",0);
            displayTypeMapper.insert(envDisplayType);
        }else {
            resultMap.put("envDisplayType",envDisplayType.getDisplayType());
        }
        return new Result(ResultCode.SUCCESS,resultMap);
    }


    @RequestMapping("/edit")
    public Result edit(HttpServletRequest httpServletRequest, @RequestBody DisplayType displayType) {
        displayTypeMapper.update(displayType,new QueryWrapper<DisplayType>()
                .eq("farm_id",displayType.getFarmId())
                .eq("event_type",displayType.getEventType()));
        return Result.SUCCESS();
    }

}
