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

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.business.qcrk.entity.vo.DataCrkywJyxxDefaultVo;
import com.unis.crk.business.qcrk.entity.vo.DataCrkywQyxxDefaultVo;
import com.unis.crk.business.qcrk.entity.vo.Qcrk;
import com.unis.crk.business.qcrk.service.IDataCrkywJyxxDefaultService;
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-10-11
 */
@Api(value = "汽车入库-检验主表",tags = {"汽车入库-检验主表"})
@RestController
@RequestMapping("dataCrkywJyxxDefault")
@CrossOrigin
public class DataCrkywJyxxDefaultController {

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


    @Autowired
    private IDataCrkywJyxxDefaultService dataCrkywJyxxDefaultService;


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

    /**
         * 分页高级查询
         * @return
         */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody DataCrkywJyxxDefaultVo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataCrkywJyxxDefaultService.findPageVo(pageQuery));
        return data;
    }
    /**
     * 查询待处理列表数据
     * @return
     */
    @ApiOperation(value="查询", notes="查询待处理列表数据")
    @PostMapping(value="/getPerData")
    public ResultUtil getPerData(@RequestBody DataCrkywQyxxDefaultVo pageQuery) {
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataCrkywJyxxDefaultService.getPerData(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.dataCrkywJyxxDefaultService.save(qcrk);
        }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 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.dataCrkywJyxxDefaultService.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.dataCrkywJyxxDefaultService.delete(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;
    }


}

