package com.unis.crk.cplbusiness.controller;

import com.unis.crk.aop.SystemLog;
import com.unis.crk.cplbusiness.entity.DataCrkywCplckDefault;
import com.unis.crk.cplbusiness.entity.DataCrkywCplrkDefault;
import com.unis.crk.cplbusiness.service.IDataCrkywCplckDefaultService;
import lombok.SneakyThrows;
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.DataCrkywCplckDefaultVo;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCplckDefaultVo;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("dataCrkywCplckDefault")
public class DataCrkywCplckDefaultController {

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


    @Autowired
    private IDataCrkywCplckDefaultService dataCrkywCplckDefaultService;


    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<DataCrkywCplckDefault> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplckDefaultService.getOneData(id));
        return data;
    }
    /**
     * 根据ddbh查询数据
     * @param ddbh
     * @return
     */
    @ApiOperation(value="查询", notes="根据ddbh查询数据")
    @GetMapping("getAppCkList/{ddbh}")
    public ResultUtil getAppCkList(@PathVariable("ddbh") String ddbh){
        LOGGER.info("入参为:{}",ddbh);
        ResultUtil<List<DataCrkywCplckDefault>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(ddbh)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplckDefaultService.getAppCkList(ddbh));
        return data;
    }
    /**
     * 根据unitid查询cph数据
     * @param unitId
     * @return
     */
    @ApiOperation(value="查询", notes="根据unitid查询cph数据")
    @GetMapping("getCphList/{unitId}")
    public ResultUtil getCphList(@PathVariable("unitId") String unitId){
        LOGGER.info("入参为:{}",unitId);
        ResultUtil<List<DataCrkywCplckDefault>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(unitId)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplckDefaultService.getCphList(unitId));
        return data;
    }
    /**
         * 分页高级查询
         * @return
         */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody DataCrkywCplckDefaultVo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataCrkywCplckDefaultService.findPageVo(pageQuery));
        return data;
    }

    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    @SystemLog(module = "成品出库", methods = "新增",uploadName = "cpl.ck")
    public ResultUtil add(@RequestBody DataCrkywCplckDefault param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }
        JSONObject ret = new JSONObject();
        //app
        String cazylx = param.getCazylx();
        if(StringUtils.isEmpty(cazylx)){
            param.setCazylx("4");
        }
        try {
             ret  = this.dataCrkywCplckDefaultService.saveCk(param);
        } catch (Exception e) {
            e.printStackTrace();
            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 param
     * @return
     */
    @SneakyThrows
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    @SystemLog(module = "成品出库", methods = "修改",uploadName = "cpl.ck")
    public ResultUtil edit(@RequestBody  DataCrkywCplckDefault param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret = new JSONObject();
        try {
            ret = this.dataCrkywCplckDefaultService.updateCk(param);
        } catch (Exception e) {
            e.printStackTrace();
            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.ck")
    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.dataCrkywCplckDefaultService.deleteCk(id);
        } catch (Exception e) {
            e.printStackTrace();
            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;
    }


}

