package com.huimv.receive.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultCode;
import com.huimv.receive.entity.Exist;
import com.huimv.receive.service.IExistService;
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 java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-09-08
 */
@RestController
@RequestMapping("/exist")
@CrossOrigin
public class ExistController {

    @Autowired
    private IExistService existService;

    @RequestMapping("/editExist")
    public Result edit(@RequestBody Map<String, String> paramsMap) {
        String result = paramsMap.get("result");
        String farmId = paramsMap.get("farmId");
        QueryWrapper<Exist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        Exist one = existService.getOne(queryWrapper);
        one.setExist(Integer.parseInt(result));
        existService.updateById(one);
        return new Result(10000, "修改成功！", true);
    }

    @RequestMapping("/listExist")
    public Result listExist(@RequestBody Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<Exist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        Exist one = existService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(one)) {
            Exist exist = new Exist();
            exist.setExist(0);
            exist.setFarmId(farmId);
            existService.save(exist);
            return new Result(ResultCode.SUCCESS, exist);
        }
        return new Result(ResultCode.SUCCESS,one);
    }
}
