package com.unis.crk.cplbusiness.controller;

import com.unis.crk.aop.SystemLog;
import com.unis.crk.cplbusiness.entity.DataKcglCplqckcDefault;
import com.unis.crk.cplbusiness.service.IDataKcglCplqckcDefaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.unis.crk.utils.ResultUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.cplbusiness.entity.vo.DataKcglCplqckcDefaultVo;
import com.unis.crk.cplbusiness.entity.vo.DataKcglCplqckcDefaultVo;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.hutool.json.JSONObject;


/**
 * <p>
 * 成品粮期初 前端控制器
 * </p>
 *
 * @author TangSong
 * @since 2022-03-21
 */
@CrossOrigin
@Api(value = "成品粮期初",tags = {"成品粮期初"})
@RestController
@RequestMapping("dataKcglCplqckcDefault")
public class DataKcglCplqckcDefaultController {

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


    @Autowired
    private IDataKcglCplqckcDefaultService dataKcglCplqckcDefaultService;


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

    /**
         * 分页高级查询
         * @return
         */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody DataKcglCplqckcDefaultVo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataKcglCplqckcDefaultService.findPageVo(pageQuery));
        return data;
    }

    /**
     * 新增一条数据
     * @param qckc
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    @SystemLog(module = "成品变动", methods = "新增",uploadName = "cpl.qc")
    public ResultUtil add(@RequestBody DataKcglCplqckcDefault qckc){
        LOGGER.info("入参为:{}", qckc);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(qckc)){
            data.failed("所接收对象为null");
            return data;
        }

        JSONObject ret = new JSONObject();
        try{
            ret  = this.dataKcglCplqckcDefaultService.saveQckc(qckc);
        }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 qckc
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    @SystemLog(module = "成品变动", methods = "修改",uploadName = "cpl.qc")
    public ResultUtil edit(@RequestBody  DataKcglCplqckcDefault qckc){
        LOGGER.info("入参为:{}", qckc);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(qckc)){
        data.failed("所接收对象为null");
        return data;
        }

        JSONObject ret = new JSONObject();
        try{
            ret  = this.dataKcglCplqckcDefaultService.updateQckc(qckc);
        }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}")
    @SystemLog(module = "成品变动", methods = "删除",uploadName = "cpl.qc")
    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  = this.dataKcglCplqckcDefaultService.deleteQckc(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;
    }


}

