package com.huimv.admin.controller;


import cn.hutool.core.bean.BeanUtil;
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.ProdStockThreshold;
import com.huimv.admin.service.IProdStockThresholdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 存栏结构阈值 前端控制器
 * </p>
 *
 * @author newspaper
 * @since 2024-01-11
 */
@RestController
@RequestMapping("/prod-stock-threshold")
@CrossOrigin
public class ProdStockThresholdController {

    @Autowired
    private IProdStockThresholdService prodStockThresholdService;

    @PostMapping("/getThreshold")
    public Result getThreshold(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        ProdStockThreshold threshold = prodStockThresholdService.getOne(new QueryWrapper<ProdStockThreshold>().lambda().eq(ProdStockThreshold::getFarmId, farmId));
        if (ObjectUtil.isEmpty(threshold)){
            threshold = new ProdStockThreshold();
            threshold.setFarmId(Integer.valueOf(farmId));
            prodStockThresholdService.save(threshold);
        }
        return new Result(ResultCode.SUCCESS,threshold);
    }

    @PostMapping("/edit")
    public Result getThreshold(HttpServletRequest httpServletRequest, @RequestBody ProdStockThreshold prodStockThreshold) {
        ProdStockThreshold threshold = prodStockThresholdService.getById(prodStockThreshold.getId());
        BeanUtil.copyProperties(prodStockThreshold,threshold);
        prodStockThresholdService.updateById(threshold);
        return Result.SUCCESS();
    }




}
