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

import com.unis.crk.aop.SystemLog;
import com.unis.crk.business.qcrk.entity.DataCrkywTzdxxDefault;
import com.unis.crk.business.qcrk.service.IDataCrkywTzdxxDefaultService;
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.business.qcrk.entity.vo.DataCrkywTzdxxDefaultVo;
import com.unis.crk.business.qcrk.entity.vo.Qcrk;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.hutool.json.JSONObject;


/**
 * <p>
 * 汽车出入库-通知单主表 前端控制器
 * </p>
 *
 * @author TangSong
 * @since 2021-10-13
 */
@Api(value = "汽车出入库-通知单主表",tags = {"汽车出入库-通知单主表"})
@RestController
@RequestMapping("dataCrkywTzdxxDefault")
public class DataCrkywTzdxxDefaultController {

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


    @Autowired
    private IDataCrkywTzdxxDefaultService dataCrkywTzdxxDefaultService;


    /**
     * 通过单位Id及作业类型得到通知单下拉列表
     * @param unitId 单位id
     * @param zylx 作业类型：粮油入库：1，粮油出库：2，专项入库：3，专项出库：4
     * @return
     */
    @ApiOperation(value="查询", notes="通过单位Id及作业类型得到通知单下拉列表")
    @GetMapping(value="/getTzdList/{unitId}/{zylx}")
    public ResultUtil getTzdList(@PathVariable("unitId") String unitId,@PathVariable("zylx") Integer zylx) {
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataCrkywTzdxxDefaultService.getTzdList(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<DataCrkywTzdxxDefault> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywTzdxxDefaultService.getOne(id));
        return data;
    }

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

    /**
     * 新增一条数据
     * @param qcrk
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody Qcrk qcrk){
        LOGGER.info("入参为:{}", qcrk);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(qcrk)){
            data.failed("所接收对象为null");
            return data;
        }
        JSONObject ret = new JSONObject();
        try{
            ret  = this.dataCrkywTzdxxDefaultService.save(qcrk);
        }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 qcrk
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody  Qcrk qcrk){
        LOGGER.info("入参为:{}", qcrk);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(qcrk)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret = new JSONObject();
        try{
            ret  = this.dataCrkywTzdxxDefaultService.update(qcrk);
        }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}")
    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.dataCrkywTzdxxDefaultService.delete(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;
    }


}

