package com.huimv.produce.warning.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.produce.warning.entity.SysWarningSet;
import com.huimv.produce.warning.service.ISysWarningSetBaseService;
import com.huimv.produce.warning.service.ISysWarningSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-12-01
 */
@RestController
@RequestMapping("/warningSet")
public class SysWarningSetController {
    @Autowired
    private ISysWarningSetService setService;

    @Autowired
    private ISysWarningSetBaseService setBaseService;

    @PostMapping("/addWarningSet")
    public Result addThreshold(@RequestBody List<SysWarningSet> warningSets){

        List<SysWarningSet> sysWarningSets = new ArrayList<>();
        if (ObjectUtil.isNotEmpty(warningSets)){
            Integer farmId = warningSets.get(0).getFarmId();
           sysWarningSets = setService.list(new QueryWrapper<SysWarningSet>().eq("farm_id", farmId));
        }

        if (ObjectUtil.isEmpty(sysWarningSets)){
            setService.saveBatch(warningSets);
        }else {
            setService.updateBatchById(warningSets);
        }

        return new Result(10000,"设置成功",true);
    }

    @GetMapping("/getWarningSet")
    public Result getThreshold(@RequestParam(name = "farmId") String farmId){
        List<SysWarningSet> warningSets = setService.list(new QueryWrapper<SysWarningSet>().eq("farm_id", farmId));
        if (ObjectUtil.isEmpty(warningSets)){
            return new Result(ResultCode.SUCCESS,setBaseService.list());
        }
        return new Result(ResultCode.SUCCESS,warningSets);
    }
}
