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

import com.unis.crk.business.qcck.service.IDataCrkywCkcmzDefaultService;
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.qcck.entity.vo.DataCrkywCkcmzDefaultVo;
import com.unis.crk.business.qcck.entity.vo.Qcck;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.hutool.json.JSONObject;


/**
 * <p>
 * 汽车出库-称毛重表 前端控制器
 * </p>
 *
 * @author TangSong
 * @since 2021-10-22
 */
@Api(value = "汽车出库-称毛重表",tags = {"汽车出库-称毛重表"})
@RestController
@RequestMapping("dataCrkywCkcmzDefault")
public class DataCrkywCkcmzDefaultController {

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


    @Autowired
    private IDataCrkywCkcmzDefaultService dataCrkywCkcmzDefaultService;


    /**
     * 根据ID、bizNo查询数据
     * @param id
     * @param bizNo
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID、bizNo查询数据")
    @GetMapping({"getOne/{bizNo}/{id}","getOne/{bizNo}"})
    public ResultUtil getOne(@PathVariable(value = "bizNo",required = false)  String bizNo,@PathVariable(value = "id",required = false)  String id){
        LOGGER.info("入参为:{}",bizNo);
        ResultUtil<Map<String,Object>> data = new ResultUtil<>();
//        ResultUtil<DataCrkywCkcmzDefault> data = new ResultUtil<>();
        if (StringUtils.isEmpty(bizNo)){
            data.failed("传入的bizNo为空");
            return data;
        }
        data.success(this.dataCrkywCkcmzDefaultService.getOne(bizNo,id));
//        data.success(this.dataCrkywCkcmzDefaultService.getById(id));
        return data;
    }

    /**
         * 分页高级查询
         * @return
         */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody DataCrkywCkcmzDefaultVo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataCrkywCkcmzDefaultService.findPageVo(pageQuery));
        return data;
    }
    /**
     * 查询待处理列表数据
     * @return
     */
    @ApiOperation(value="查询", notes="查询待处理列表数据")
    @PostMapping(value="/getPerData")
    public ResultUtil getPerData(@RequestBody DataCrkywCkcmzDefaultVo pageQuery) {
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataCrkywCkcmzDefaultService.getPerData(pageQuery));
        return data;
    }
    /**
     * 新增一条数据
     * @param qcck
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody Qcck qcck){
        LOGGER.info("入参为:{}", qcck);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(qcck)){
            data.failed("所接收对象为null");
            return data;
        }
        JSONObject ret = new JSONObject();
        try{
            ret  = this.dataCrkywCkcmzDefaultService.save(qcck);
        }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 qcck
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody Qcck qcck){
        LOGGER.info("入参为:{}", qcck);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(qcck)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret = new JSONObject();
        try{
            ret  = this.dataCrkywCkcmzDefaultService.update(qcck);
        }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.dataCrkywCkcmzDefaultService.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;
    }


}

