package com.unis.crk.cplbusiness.controller;

import com.unis.crk.aop.SystemLog;
import com.unis.crk.basic.entity.BasicEnum;
import com.unis.crk.basic.entity.FuncInfo;
import com.unis.crk.basic.service.IBasicEnumService;
import com.unis.crk.cplbusiness.entity.DataCrkywCplrkDefault;
import com.unis.crk.cplbusiness.entity.DataKcglCplkcbdDefault;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCplrkDefaultVo;
import com.unis.crk.cplbusiness.service.IDataKcglCplkcbdDefaultService;
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.Iterator;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.cplbusiness.entity.vo.DataKcglCplkcbdDefaultVo;
import com.unis.crk.cplbusiness.entity.vo.DataKcglCplkcbdDefaultVo;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("dataKcglCplkcbdDefault")
public class DataKcglCplkcbdDefaultController {

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


    @Autowired
    private IDataKcglCplkcbdDefaultService dataKcglCplkcbdDefaultService;

    @Autowired
    private IBasicEnumService basicEnumService;


    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<DataKcglCplkcbdDefault> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        DataKcglCplkcbdDefault dataKcglCplkcbdDefault = dataKcglCplkcbdDefaultService.getById(id);
        BasicEnum jldw = basicEnumService.getById(dataKcglCplkcbdDefault.getJldw());
        if (jldw != null ){
            dataKcglCplkcbdDefault.setJldwmc(jldw.getEnumName());
        }
        data.success(this.dataKcglCplkcbdDefaultService.getById(id));
        return data;
    }
    /**
     * 根据仓房货位查询库存实物信息
     * @param ch
     * @param hwh
     * @param unitid
     * @return
     */
    @ApiOperation(value="查询", notes="根据仓房货位查询库存实物信息")
    @GetMapping("getKcsw/{unitid}/{ch}/{hwh}")
    public ResultUtil getKcsw(@PathVariable(value = "ch",required = false)  String ch,@PathVariable(value = "hwh",required = false)  String hwh,@PathVariable(value = "unitid",required = false)  String unitid){
        LOGGER.info("入参为:{}",ch,hwh);
        ResultUtil< List<Map<String, Object>>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(ch)){
            data.failed("仓号未传");
            return data;
        }
        if (StringUtils.isEmpty(hwh)){
            data.failed("货位号未传");
            return data;
        }
        List<Map<String, Object>> kcsw = dataKcglCplkcbdDefaultService.getKcsw(ch, hwh, unitid);
        data.success(kcsw);
        return data;
    }
    /**
         * 分页高级查询
         * @return
         */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody DataKcglCplkcbdDefaultVo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();

        if (ObjectUtils.isEmpty(pageQuery.getBdfs())){
            data.failed("所接收变动方式为null");
            return data;
        }
        data.success(this.dataKcglCplkcbdDefaultService.findPageVo(pageQuery));
        return data;
    }

    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="批量新增", notes="批量新增盘点")
    @PostMapping("addPds")
    @SystemLog(module = "成品变动", methods = "库存",uploadName = "cpl.kcbd")
    public ResultUtil addPds(@RequestBody List<DataKcglCplkcbdDefault> 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.dataKcglCplkcbdDefaultService.saveList(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;
    }
    @ApiOperation(value="批量新增调拨", notes="批量新增调拨")
    @PostMapping("addDbs")
    @SystemLog(module = "成品变动", methods = "库存",uploadName = "cpl.kcbd")
    public ResultUtil addDbs(@RequestBody List<DataKcglCplkcbdDefault> 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.dataKcglCplkcbdDefaultService.saveListDbs(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.kcbd")
    public ResultUtil edit(@RequestBody  DataKcglCplkcbdDefault param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret  = this.dataKcglCplkcbdDefaultService.updateKcbd(param);
        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("deletePd/{id}")
    @SystemLog(module = "成品变动", methods = "库存",uploadName = "cpl.kcbd")
    public ResultUtil deletePd(@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.dataKcglCplkcbdDefaultService.deleteKcPd(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;
    }
    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @ApiOperation(value="删除", notes="根据ID删除调拨数据数据")
    @DeleteMapping("deleteDb/{id}")
    @SystemLog(module = "成品变动", methods = "库存",uploadName = "cpl.kcbd")
    public ResultUtil deleteDb(@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.dataKcglCplkcbdDefaultService.deleteKcDb(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;
    }
    /**
     * 根据unitid查询cph数据
     * @param pageQuery
     * @return
     */
    @ApiOperation(value="根据unitid查询盘点列表数据", notes="根据unitid查询盘点列表数据")
    @PostMapping(value="/getCplPdList")
    public ResultUtil getCplPdList(@RequestBody DataKcglCplkcbdDefaultVo pageQuery){
        LOGGER.info("入参为:{}",pageQuery);
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(pageQuery.getUnitId())){
            data.failed("传入的单位id为空");
            return data;
        }
        data.success(dataKcglCplkcbdDefaultService.getCplPdList(pageQuery));
        return data;
    }
    /**
     * 根据unitid查询cph数据
     * @param pageQuery
     * @return
     */
    @ApiOperation(value="根据盘点单号查询盘点数据", notes="根据盘点单号查询盘点数据")
    @PostMapping(value="/getCplPdListflt")
    public ResultUtil getCplPdListflt(@RequestBody DataKcglCplkcbdDefaultVo pageQuery){
        LOGGER.info("入参为:{}",pageQuery);
        ResultUtil<List<DataKcglCplkcbdDefault>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(pageQuery.getBddh())){
            data.failed("传入的盘点单号为空");
            return data;
        }
        data.success(dataKcglCplkcbdDefaultService.getCplPdListflt(pageQuery));
        return data;
    }
}