package com.unis.crk.quality.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.quality.entity.QualityBase;
import com.unis.crk.quality.entity.vo.QualityBaseVo;
import com.unis.crk.quality.service.IQualityBaseService;
import com.unis.crk.utils.QualityCheckUtil;
import com.unis.crk.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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


/**
 * <p>
 * 质量标准-根表 前端控制器
 * </p>
 *
 * @author TangSong
 * @since 2022-02-09
 */
@CrossOrigin
@Api(value = "质量标准-根表",tags = {"质量标准-根表"})
@RestController
@RequestMapping("qualityBase")
public class QualityBaseController {

    private final static Logger LOGGER = LoggerFactory.getLogger(QualityBaseController.class);


    @Autowired
    private IQualityBaseService qualityBaseService;


    /**
     * 根据ID,lspzCode查询数据
     * @param id
     * @param lspzCode
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID,lspzCode查询数据")
    @GetMapping({"getOne/{lspzCode}/{id}","getOne/{lspzCode}"})
    public ResultUtil getOne(@PathVariable(value = "lspzCode",required = false)  String lspzCode,@PathVariable(value = "id",required = false)  String id){
        LOGGER.info("入参为:{}",id,lspzCode);
        ResultUtil<QualityBase> data = new ResultUtil<>();
        if (StringUtils.isEmpty(lspzCode)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.qualityBaseService.getOne(id,lspzCode));
        return data;
    }

    /**
     * 根据ID,lspzCode查询成品粮数据
     * @param id
     * @param lspzCode
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID,lspzCode查询成品粮数据")
    @GetMapping({"getCplOne/{lspzCode}/{id}","getCplOne/{lspzCode}"})
    public ResultUtil getCplOne(@PathVariable(value = "lspzCode",required = false)  String lspzCode,@PathVariable(value = "id",required = false)  String id){
        LOGGER.info("入参为:{}",id,lspzCode);
        ResultUtil<QualityBase> data = new ResultUtil<>();
        if (StringUtils.isEmpty(lspzCode)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.qualityBaseService.getCplOne(id,lspzCode));
        return data;
    }
     /**
      * 根据unitId,lspzId,lsdjId查询成品粮標準值
     * @param unitId 单位Id
     * @param lspzId 成品粮商品id
     * @param lsdjId 产品等级id
     * @return
     */
    @ApiOperation(value="查询", notes="根据unitId,lspzId,lsdjId查询成品粮標準值")
    @GetMapping({"getCplBzz/{unitId}/{lspzId}/{lsdjId}"})
    public ResultUtil getCplBzz(@PathVariable(value = "unitId")String unitId,@PathVariable(value = "lspzId")  String lspzId,@PathVariable(value = "lsdjId")  String lsdjId){
        LOGGER.info("入参为:{}",lspzId,lsdjId);
        ResultUtil<JSONArray> data = new ResultUtil<>();
        if (StringUtils.isEmpty(lspzId)){
            data.failed("传入的主键为空");
            return data;
        }
        JSONObject ret = new JSONObject();
        try{
            ret  = this.qualityBaseService.getCplBzz(unitId,lspzId,lsdjId);
        }catch (Exception e){
            ret.set("success", false);
            ret.set("msg", e.getMessage());
        }
        if (ret.getBool("success")){
            data.success(ret.getJSONArray("msg"));
        }else {
            data.failed(ret.getStr("msg"));
        }
        return data;
    }
    /**
     * 根据unitId,lspzId,lsdjId查询原粮标准值
     * @param unitId 单位Id
     * @param lspzId 成品粮商品id
     * @param lsdjId 产品等级id
     * @return
     */
    @ApiOperation(value="查询", notes="根据unitId,lspzId,lsdjId查询成品粮標準值")
    @GetMapping({"getYlBzz/{unitId}/{lspzId}/{lsdjId}"})
    public ResultUtil getYlBzz(@PathVariable(value = "unitId")String unitId,@PathVariable(value = "lspzId")  String lspzId,@PathVariable(value = "lsdjId")  String lsdjId){
        LOGGER.info("入参为:{}",lspzId,lsdjId);
        ResultUtil<JSONArray> data = new ResultUtil<>();
        if (StringUtils.isEmpty(lspzId)){
            data.failed("传入的主键为空");
            return data;
        }
        JSONObject ret = new JSONObject();
        try{
            ret  = this.qualityBaseService.getYlBzz(unitId,lspzId,lsdjId);
        }catch (Exception e){
            ret.set("success", false);
            ret.set("msg", e.getMessage());
        }
        if (ret.getBool("success")){
            data.success(ret.getJSONArray("msg"));
        }else {
            data.failed(ret.getStr("msg"));
        }
        return data;
    }
    /**
         * 分页高级查询
         * @return
         */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody QualityBaseVo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.qualityBaseService.findPageVo(pageQuery));
        return data;
    }
    /**
     * 通过化验值得到增扣信息
     * @param qualityBase
     * @return
     */
    @ApiOperation(value="查询", notes="通过化验值得到增扣信息")
    @PostMapping("/getScaleByFomula")
    public ResultUtil getScaleByFomula(@RequestBody JSONObject qualityBase){
        LOGGER.info("入参为:{}", qualityBase);
        ResultUtil<Map<String,Object>> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(qualityBase)){
            data.failed("所接收对象为null");
            return data;
        }

        Map<String,Object> ret = new HashMap<>();
        try{
            ret  = qualityBaseService.getScaleByFomula(qualityBase);
        }catch (Exception e){
            e.printStackTrace();
//            ret.put("status", "200");
//            ret.put("message", e.getMessage());
            ret.put("amount", 0);
            ret.put("price", 0);
        }
        if ( ret.get("status") != null && ret.get("status").toString() == "3000"){
             data.failed(ret.get("message").toString());
        }else {
            data.success(ret);
        }
        return data;
    }
    /**
     * 通过化验值得到等级信息
     * @param testValue
     * @return
     */
    @ApiOperation(value="查询", notes="通过化验值得到等级信息")
    @PostMapping("/getLevelByTestValue")
    public ResultUtil getLevelByTestValue(@RequestBody JSONObject testValue){
        LOGGER.info("入参为:{}", testValue);
        ResultUtil<Map<String,Object>> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(testValue)){
            data.failed("所接收对象为null");
            return data;
        }
        Map<String,Object> ret = new HashMap<>();
        String str = "";
        try{
             str  = qualityBaseService.getLevelByTestValue(testValue);
        }catch (Exception e){
            ret.put("success", "false");
            ret.put("message", e.getMessage()==null?"查询失败":e.getMessage());
        }
        if ( ret.get("success") != null && ret.get("success").toString() == "false"){
             data.failed((String) ret.get("message"));
        }else {
            ret.put("level", str);
            data.success(ret);
        }
        return data;
    }
    /**
     * 新增一条数据
     * @param qualityBase
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody QualityBase qualityBase){
        LOGGER.info("入参为:{}", qualityBase);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(qualityBase)){
            data.failed("所接收对象为null");
            return data;
        }

        JSONObject ret = new JSONObject();
        try{
            ret  = qualityBaseService.saveBase(qualityBase);
        }catch (Exception e){
            ret.set("success", false);
            ret.set("msg", e.getMessage());
        }
        if (ret.getBool("success")){
            data.success(ret.getStr("msg"));
        }else {
            data.failed(ret.getStr("msg"));
        }
        return data;
    }


    /**
     * 修改一条数据
     * @param qualityBase
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody  QualityBase qualityBase){
        LOGGER.info("入参为:{}", qualityBase);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(qualityBase)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret = new JSONObject();
        try{
            ret  = qualityBaseService.update(qualityBase);
        }catch (Exception e){
            ret.set("success", false);
            ret.set("msg", e.getMessage());
        }
        if (ret.getBool("success")){
            data.success(ret.getStr("msg"));
        }else {
            data.failed(ret.getStr("msg"));
        }
        return data;
    }


    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @ApiOperation(value="删除", notes="根据ID删除数据")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable("id") String id){
        LOGGER.info("入参为:{}", id);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("所接收id为null");
            return data;
        }
        JSONObject ret = new JSONObject();
        try{
            ret  = qualityBaseService.delete(id);
        }catch (Exception e){
            ret.set("success", false);
            ret.set("msg", e.getMessage());
        }
        if (ret.getBool("success")){
            data.success(ret.getStr("msg"));
        }else {
            data.failed(ret.getStr("msg"));
        }
        return data;
    }


}

