package com.unis.crk.business.kcgl.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.aop.SystemLog;
import com.unis.crk.business.kcgl.entity.DataKcglKcbdDefault;
import com.unis.crk.business.kcgl.entity.vo.DataKcglKcbdDefaultVo;
import com.unis.crk.business.kcgl.service.IDataKcglKcbdDefaultService;
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.Map;


/**
 * <p>
 * 库存管理-库存变动 前端控制器
 * </p>
 *
 * @author TangSong
 * @since 2022-02-07
 */
@CrossOrigin
@Api(value = "库存管理-库存变动",tags = {"库存管理-库存变动"})
@RestController
@RequestMapping("dataKcglKcbdDefault")
public class DataKcglKcbdDefaultController {

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


    @Autowired
    private IDataKcglKcbdDefaultService dataKcglKcbdDefaultService;


    /**
     * 根据仓房、货位id查询数据
     * @param cfid
     * @param hwid
     * @return
     */
    @ApiOperation(value="查询", notes="根据仓房、货位id查询数据")
    @GetMapping("getKcInfo/{cfid}/{hwid}/{unitId}")
    public ResultUtil getKcInfo(@PathVariable("cfid") String cfid,@PathVariable("hwid") String hwid,@PathVariable("unitId") String unitId){
        LOGGER.info("入参为:{}",cfid,hwid,unitId);
        ResultUtil<Map<String,Object>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(cfid) || StringUtils.isEmpty(hwid)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataKcglKcbdDefaultService.getKcInfo(cfid,hwid,unitId));
        return data;
    }
    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
//        ResultUtil<DataKcglKcbdDefault> data = new ResultUtil<>();
        ResultUtil<Map<String,Object>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataKcglKcbdDefaultService.getOneKcbd(id));
//        data.success(this.dataKcglKcbdDefaultService.getById(id));
        return data;
    }

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

    /**
     * 新增一条数据
     * @param kcbd
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
@SystemLog(module = "原粮库存变动", methods = "新增",uploadName = "yuliang.kcbd")
    public ResultUtil add(@RequestBody DataKcglKcbdDefault kcbd){
        LOGGER.info("入参为:{}", kcbd);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(kcbd)){
            data.failed("所接收对象为null");
            return data;
        }
        JSONObject ret = new JSONObject();
        try{
            ret  = this.dataKcglKcbdDefaultService.saveKcbd(kcbd);
        }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 kcbd
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    @SystemLog(module = "原粮库存变动", methods = "库存",uploadName = "yuliang.kcbd")
    public ResultUtil edit(@RequestBody DataKcglKcbdDefault kcbd){
        LOGGER.info("入参为:{}", kcbd);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(kcbd)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret = new JSONObject();
        try{
            ret  = this.dataKcglKcbdDefaultService.update(kcbd);
        }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;
    }


    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @ApiOperation(value="删除", notes="根据ID删除数据")
    @DeleteMapping("delete/{id}")
    @SystemLog(module = "原粮库存变动", methods = "库存",uploadName = "yuliang.kcbd")
    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.dataKcglKcbdDefaultService.delete(id);
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }
        return data;
    }


}

