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.entity.GasData;
import com.huimv.admin.entity.ProdAnalysis;
import com.huimv.admin.entity.dto.Gas;
import com.huimv.admin.entity.dto.GasDataDTO;
import com.huimv.admin.mapper.GasDataMapper;
import com.huimv.admin.service.IGasDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-02-21
 */
@RestController
@RequestMapping("/gas-data")
@CrossOrigin
public class GasDataController {

    @Autowired
    private IGasDataService gasDataService;

    @Autowired
    private GasDataMapper gasDataMapper;

    @RequestMapping("/list")
    public Result list(HttpServletRequest httpServletRequest, @RequestBody Map<String,String> paramsMap) {
        return gasDataService.list(httpServletRequest,paramsMap);
    }

    @RequestMapping("/add")
    @Transactional
    public Result add(HttpServletRequest httpServletRequest, @RequestBody GasDataDTO gasDataDTO) {
        String farmId = gasDataDTO.getFarmId();
        List<Gas> list = gasDataDTO.getList();
        gasDataService.remove(new QueryWrapper<GasData>().eq("display_type",1).eq("farm_id",farmId));
        for (Gas gas : list) {
            List<GasData> gasData = new ArrayList<>();
            GasData above = gas.getAbove();
            GasData people = gas.getPeople();
            GasData pigpen = gas.getPigpen();
            GasData under = gas.getUnder();

            above.setCreateTime(gas.getCreateTime());
            above.setDisplayType(1);
            above.setLoctionType(2);
            above.setFarmId(Integer.valueOf(farmId));
            people.setCreateTime(gas.getCreateTime());
            people.setDisplayType(1);
            people.setLoctionType(4);
            people.setFarmId(Integer.valueOf(farmId));
            pigpen.setCreateTime(gas.getCreateTime());
            pigpen.setDisplayType(1);
            pigpen.setLoctionType(1);
            pigpen.setFarmId(Integer.valueOf(farmId));
            under.setCreateTime(gas.getCreateTime());
            under.setDisplayType(1);
            under.setFarmId(Integer.valueOf(farmId));
            under.setLoctionType(3);

            gasData.add(above);
            gasData.add(people);
            gasData.add(pigpen);
            gasData.add(under);
            gasDataService.saveBatch(gasData);
        }
        return Result.SUCCESS();
    }

    @RequestMapping("/listGas")
    public Result listGas(HttpServletRequest httpServletRequest, @RequestBody Map<String,String> paramsMap) {
        return gasDataService.listGas(httpServletRequest,paramsMap);
    }

    @RequestMapping("/listArea")
    public Result listArea(HttpServletRequest httpServletRequest, @RequestBody Map<String,String> paramsMap) {
        return gasDataService.listArea(httpServletRequest,paramsMap);
    }
}
