package com.unis.crk.cplbusiness.controller;

import com.unis.crk.cplbusiness.entity.DataKcglCplkcswDefault;
import com.unis.crk.cplbusiness.entity.vo.DataKcglCplxckcDefaultVo;
import com.unis.crk.cplbusiness.service.IDataKcglCplkcswDefaultService;
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.DataKcglCplkcswDefaultVo;
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("dataKcglCplkcswDefault")
public class DataKcglCplkcswDefaultController {

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


    @Autowired
    private IDataKcglCplkcswDefaultService dataKcglCplkcswDefaultService;


    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<DataKcglCplkcswDefault> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataKcglCplkcswDefaultService.getById(id));
        return data;
    }
    /**
     * 根据货位号查询库存对应的库存相关信息
     * @param hwh
     * @return
     */
    @ApiOperation(value="查询", notes="根据货位号查询库存对应的库存相关信息")
    @GetMapping("getKcswInfo/{hwh}")
    public ResultUtil getKcswInfo(@PathVariable("hwh") String hwh){
        LOGGER.info("入参为:{}",hwh);
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(hwh)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataKcglCplkcswDefaultService.getKcswInfoByHwh(hwh));
        return data;
    }
    /**
     * 根据货位号查询库存对应的库存相关信息
     * @param ch
     * @return
     */
    @ApiOperation(value="查询", notes="根据货位号查询库存对应的库存相关信息")
    @GetMapping("getKcswInfoByCh/{ch}")
    public ResultUtil getKcswInfoByCh(@PathVariable("ch") String ch){
        LOGGER.info("入参为:{}",ch);
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(ch)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataKcglCplkcswDefaultService.getKcswInfoByCh(ch));
        return data;
    }
    /**
     * 根据商品编号，规格查询库存对应的仓房相关信息
     * @return
     */
    @ApiOperation(value="查询", notes="根据货位号、商品编号，规格查询库存对应的库存相关信息")
    @GetMapping("getChInfo/{unitid}/{ppbh}/{pzbh}/{spbh}/{gg}")
    public ResultUtil getChInfo(@PathVariable("unitid") String unitid,@PathVariable("ppbh") String ppbh,@PathVariable("pzbh") String pzbh,@PathVariable("spbh") String spbh,@PathVariable("gg") String gg){
        LOGGER.info("入参为:{}",unitid,ppbh,pzbh,spbh,gg);
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(unitid)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataKcglCplkcswDefaultService.getChInfo(unitid,ppbh,pzbh,spbh,gg));
        return data;
    }
    /**
     * 得到仓房信息以及库存总件数
     * @return
     */
    @ApiOperation(value="查询", notes="得到仓房信息以及库存总件数")
    @GetMapping("getChSumInfo/{unitid}")
    public ResultUtil getChSumInfo(@PathVariable("unitid") String unitid){
        LOGGER.info("入参为:{}",unitid);
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(unitid)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataKcglCplkcswDefaultService.getChSumInfo(unitid));
        return data;
    }
    /**
     * 根据货位号、商品编号，规格查询库存对应的库存相关信息
     * @param hwh
     * @return
     */
    @ApiOperation(value="查询", notes="根据货位号、商品编号，规格查询库存对应的库存相关信息")
    @GetMapping("getKcswInfo/{hwh}/{spbh}/{gg}")
    public ResultUtil getKcswInfo(@PathVariable("hwh") String hwh,@PathVariable("spbh") String spbh,@PathVariable("gg") String gg){
        LOGGER.info("入参为:{}",hwh);
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(hwh)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataKcglCplkcswDefaultService.getKcswInfoBySpbh(hwh,spbh,gg));
        return data;
    }

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

    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody DataKcglCplkcswDefault param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }

        data.success( this.dataKcglCplkcswDefaultService.save(param));
        return  data;
    }


    /**
     * 修改一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody  DataKcglCplkcswDefault param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret  = this.dataKcglCplkcswDefaultService.update(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("delete/{id}")
    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.dataKcglCplkcswDefaultService.delete(id);
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }
        return data;
    }
    /**
     * 查询库存预警
     * @return
     */
    @ApiOperation(value="查询库存预警", notes="查询库存预警")
    @PostMapping(value="/findPageKcyj")
    public ResultUtil findPageKcyj(@RequestBody DataKcglCplkcswDefaultVo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        if(StringUtils.isEmpty(pageQuery.getUnitId())){
            data.failed("传入的单位ID为空");
            return data;
        }
        data.success(this.dataKcglCplkcswDefaultService.findPageKcyj(pageQuery));
        return data;
    }

}

