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.SysWarningPhone;
import com.huimv.produce.warning.service.ISysWarningPhoneService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-12-01
 */
@RestController
@RequestMapping("/warningPhone")
public class SysWarningPhoneController {
    @Autowired
    private ISysWarningPhoneService phoneService;

    @PostMapping("/addWarningPhone")
    public Result addThreshold(@RequestBody List<SysWarningPhone> warningPhones){
        Integer id = null;

        if (ObjectUtil.isEmpty(warningPhones)){
            return new Result(10001,"参数为空",true);
        }else {
            id = warningPhones.get(0).getId();
        }
        if (ObjectUtil.isEmpty(id)){
            phoneService.saveBatch(warningPhones);
        }else {
            phoneService.updateBatchById(warningPhones);
        }

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

    @GetMapping("/getWarningPhone")
    public Result getThreshold(@RequestParam(name = "farmId") String farmId){
        List<SysWarningPhone> phones = phoneService.list(new QueryWrapper<SysWarningPhone>().eq("farm_id", farmId));
        Map endMap = new HashMap();
        for (SysWarningPhone phone : phones) {

            Integer level = phone.getLevel();
            if (level== 2){
                endMap.put("two",phone);
            }
            if (level== 3){
                endMap.put("three",phone);
            }
        }
        return new Result(ResultCode.SUCCESS,endMap);
    }
}
