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.ProdProductionThreshold;
import com.huimv.admin.entity.ProdStockThreshold;
import com.huimv.admin.service.IProdProductionThresholdService;
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-production-threshold")
@CrossOrigin
public class ProdProductionThresholdController {

    @Autowired
    private IProdProductionThresholdService prodProductionThresholdService;

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

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