package com.unis.crk.cplbusiness.controller;

import com.unis.crk.aop.SystemLog;
import com.unis.crk.cplbusiness.entity.DataCrkywCplrkDefault;
import com.unis.crk.cplbusiness.service.IDataCrkywCplrkDefaultService;
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.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCplrkDefaultVo;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCplrkDefaultVo;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("dataCrkywCplrkDefault")
public class DataCrkywCplrkDefaultController {

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


    @Autowired
    private IDataCrkywCplrkDefaultService dataCrkywCplrkDefaultService;


    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<DataCrkywCplrkDefault> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplrkDefaultService.getOneData(id));
        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<DataCrkywCplrkDefault>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(unitId)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplrkDefaultService.getCphList(unitId));
        return data;
    }
    /**
     * 根据ddbh查询数据
     * @param ddbh
     * @return
     */
    @ApiOperation(value="查询", notes="根据ddbh查询数据")
    @GetMapping("getAppRkList/{ddbh}")
    public ResultUtil getAppRkList(@PathVariable("ddbh") String ddbh){
        LOGGER.info("入参为:{}",ddbh);
        ResultUtil<List<DataCrkywCplrkDefault>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(ddbh)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplrkDefaultService.getAppRkList(ddbh));
        return data;
    }

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

    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    @SystemLog(module = "成品入库", methods = "新增",uploadName = "cpl.rk")
    public ResultUtil add(@RequestBody DataCrkywCplrkDefault 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.dataCrkywCplrkDefaultService.saveRk(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
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    @SystemLog(module = "成品入库", methods = "修改",uploadName = "cpl.rk")
    public ResultUtil edit(@RequestBody  DataCrkywCplrkDefault 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.dataCrkywCplrkDefaultService.updateRk(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删除数据")
    @GetMapping("delete/{id}")
    @SystemLog(module = "成品入库", methods = "删除",uploadName = "cpl.rk")
    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.dataCrkywCplrkDefaultService.deleteRk(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;
    }

    /**
     * 根据ddid和商品编号查询待退货生成批次
     * @param ddid spbh
     * @return
     */
    @ApiOperation(value="查询", notes="根据ddid和商品编号查询待退货生成批次")
    @GetMapping("getScpcByddbh/{ddid}/{spbh}")
    public ResultUtil getScpcByddbh(@PathVariable("ddid") String ddid,@PathVariable("spbh") String spbh){
        LOGGER.info("入参为:{}",ddid);
        ResultUtil<List<HashMap>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(ddid)){
            data.failed("传入的主键为空");
            return data;
        }
        List<HashMap> scpc=dataCrkywCplrkDefaultService.getScpcByddbh(ddid,spbh);
        return data.success(scpc);
    }


}

