package com.unis.crk.basic.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.basic.entity.BasicStorehouse;
import com.unis.crk.basic.entity.vo.BasicStorehouseVo;
import com.unis.crk.basic.service.IBasicStorehouseService;
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 2021-11-25
 */
@Api(value = "仓房信息",tags = {"仓房信息"})
@RestController
@RequestMapping("basicStorehouse")
@CrossOrigin
public class BasicStorehouseController {

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


    @Autowired
    private IBasicStorehouseService basicStorehouseService;


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

    /**
         * 分页高级查询
         * @return
         */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody BasicStorehouseVo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.basicStorehouseService.findPageVo(pageQuery));
        return data;
    }
    /**
     * 通过单位Id得到相应的仓房及油罐信息
     * @return
     */
    @ApiOperation(value="查询", notes="通过单位Id得到相应的仓房及油罐信息")
    @GetMapping(value="/getWarehouseList/{unitId}")
    public ResultUtil getWarehouseList(@PathVariable("unitId") String unitId) {
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.basicStorehouseService.getStorehouseList(unitId));
        return data;
    }
    /**
     * 通过单位Id和储存类型得到相应的仓房及油罐信息
     * @return
     */
    @ApiOperation(value="根据储存类型查询仓房", notes="原粮:952851、成品粮:952852、副产物:952853、物资:952854、原油:952855、原粮和油:9528")
    @GetMapping(value="/getStorehouseListByCclx/{unitId}/{cclx}")
    public ResultUtil getStorehouseListByCclx(@PathVariable("unitId") String unitId,@PathVariable("cclx") String cclx) {
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.basicStorehouseService.getStorehouseListByCclx(unitId,cclx));
        return data;
    }
    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody BasicStorehouse 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.basicStorehouseService.saveChhw(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")
    public ResultUtil edit(@RequestBody  BasicStorehouse 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.basicStorehouseService.updateChhw(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;
    }


    /**
     * 根据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 = new JSONObject();
        try{
            ret  = this.basicStorehouseService.deleteChhw(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;
    }


}

