package com.unis.crk.cplbusiness.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.aop.SystemLog;
import com.unis.crk.cplbusiness.entity.DataCrkywCpltzdxxDefault;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCpltzdxxDefaultVo;
import com.unis.crk.cplbusiness.service.IDataCrkywCpltzdxxDefaultService;
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.List;
import java.util.Map;


/**
 * <p>
 * 成品粮出入库订单 前端控制器
 * </p>
 *
 * @author TangSong
 * @since 2022-03-23
 */
@CrossOrigin
@Api(value = "成品粮出入库订单",tags = {"成品粮出入库订单"})
@RestController
@RequestMapping("dataCrkywCpltzdxxDefault")
public class DataCrkywCpltzdxxDefaultController {

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


    @Autowired
    private IDataCrkywCpltzdxxDefaultService dataCrkywCpltzdxxDefaultService;
    /**
     * 通过单位Id及作业类型得到成品粮通知单下拉列表
     * @param unitId 单位id
     * @param zylx 作业类型：成品粮入库：3，成品粮出库：4
     * @return
     */
    @ApiOperation(value="查询", notes="通过单位Id及作业类型得到成品粮通知单下拉列表")
    @GetMapping({"/getCplTzdList/{unitId}/{zylx}","/getCplTzdList/{unitId}"})
    public ResultUtil getCplTzdList(@PathVariable(value = "unitId",required = false) String unitId,@PathVariable(value = "zylx",required = false) String zylx) {
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataCrkywCpltzdxxDefaultService.getCplTzdList(unitId,zylx));
        return data;
    }
    /**
     * 通过主表主键Id到成品粮通知单出入库从表列表
     * @param mainDataId 主表主键id
     */
    @ApiOperation(value="查询", notes="通过主表主键Id到成品粮通知单出入库从表列表")
    @GetMapping({"/getCplCrkTzdFltList/{mainDataId}"})
    public ResultUtil getCplCrkTzdFltList(@PathVariable(value = "mainDataId",required = true) String mainDataId) {
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataCrkywCpltzdxxDefaultService.getCplCrkTzdFltList(mainDataId));
        return data;
    }

    /**
     * 通过主表主键Id到成品粮通知单出入库从表列表
     * @param mainDataId 主表主键id
     */
    @ApiOperation(value="查询", notes="通过主表主键Id到成品粮通知单出入库从表列表")
    @GetMapping({"/getCplCrkTzdFltListAndsp/{mainDataId}"})
    public ResultUtil getCplCrkTzdFltListAndsp(@PathVariable(value = "mainDataId",required = true) String mainDataId) {
        ResultUtil<Map<String,Object>> data = new ResultUtil<>();
        data.success(this.dataCrkywCpltzdxxDefaultService.getCplCrkTzdFltListAndsp(mainDataId));
        return data;
    }
    /**
     * 通过单位Id及作业类型得到成品粮通知单出入库下拉列表
     * @param unitId 单位id
     * @param zylx 作业类型：成品粮正常入库：3，成品粮出库：4，成品粮退货入库：5
     * @return
     */
    @ApiOperation(value="查询", notes="成品粮(正常，退货)入库：3;成品粮出库：4。成品粮退货入库：5。成品粮正常入库：7")
    @GetMapping({"/getCplCrkTzdList/{unitId}/{zylx}","/getCplCrkTzdList/{unitId}"})
    public ResultUtil getCplCrkTzdList(@PathVariable(value = "unitId",required = false) String unitId,@PathVariable(value = "zylx",required = false) String zylx) {
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataCrkywCpltzdxxDefaultService.getCplCrkTzdList(unitId,zylx));
        return data;
    }

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

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

    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    @SystemLog(module = "成品通知单", methods = "新增",uploadName = "cpl.tzd")
    public ResultUtil add(@RequestBody DataCrkywCpltzdxxDefault 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.dataCrkywCpltzdxxDefaultService.saveData(param);
        }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 param
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    @SystemLog(module = "成品通知单", methods = "修改",uploadName = "cpl.tzd")
    public ResultUtil edit(@RequestBody  DataCrkywCpltzdxxDefault 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.dataCrkywCpltzdxxDefaultService.update(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.tzd")
    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  = this.dataCrkywCpltzdxxDefaultService.delete(id);
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }
        return data;
    }
}

