package com.unis.crk.cplbusiness.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.cplbusiness.entity.DataCrkywCplbjdyDefault;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCplbjdyDefaultVo;
import com.unis.crk.cplbusiness.service.IDataCrkywCplbjdyDefaultService;
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 2022-04-28
 */
@CrossOrigin
@Api(value = "标签打印",tags = {"标签打印"})
@RestController
@RequestMapping("dataCrkywCplbjdyDefault")
public class DataCrkywCplbjdyDefaultController {

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


    @Autowired
    private IDataCrkywCplbjdyDefaultService dataCrkywCplbjdyDefaultService;


    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<DataCrkywCplbjdyDefault> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplbjdyDefaultService.getById(id));
        return data;
    }
    /**
     * 根据标签号查询数据
     * @param bjbh
     * @return
     */
    @ApiOperation(value="查询", notes="根据标签号查询数据")
    @GetMapping("getOneBybh/{bjbh}")
    public ResultUtil getOneBybh(@PathVariable("bjbh") String bjbh){
        LOGGER.info("入参为:{}",bjbh);
        ResultUtil<DataCrkywCplbjdyDefault> data = new ResultUtil<>();
        if (StringUtils.isEmpty(bjbh)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplbjdyDefaultService.getOneBybh(bjbh));
        return data;
    }
    /**
     * 根据标签号查询数据
     * @param bjbhs
     * @return
     */
    @ApiOperation(value="查询", notes="根据标签号查询数据")
    @GetMapping("getListBybhs/{bjbhs}")
    public ResultUtil getListBybhs(@PathVariable("bjbhs") String bjbhs){
        LOGGER.info("入参为:{}",bjbhs);
        ResultUtil<List<DataCrkywCplbjdyDefault>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(bjbhs)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplbjdyDefaultService.getListBybhs(bjbhs));
        return data;
    }

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

    /**
     * 新增一条数据
     * @param dataCrkywCplbjdyDefault
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody DataCrkywCplbjdyDefault dataCrkywCplbjdyDefault){
        LOGGER.info("入参为:{}", dataCrkywCplbjdyDefault);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(dataCrkywCplbjdyDefault)){
            data.failed("所接收对象为null");
            return data;
        }
        JSONObject ret  = this.dataCrkywCplbjdyDefaultService.saveBjdy(dataCrkywCplbjdyDefault);
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }

        return data;
    }


    /**
     * 修改一条数据
     * @param dataCrkywCplbjdyDefault
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody  DataCrkywCplbjdyDefault dataCrkywCplbjdyDefault){
        LOGGER.info("入参为:{}", dataCrkywCplbjdyDefault);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(dataCrkywCplbjdyDefault)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret  = this.dataCrkywCplbjdyDefaultService.update(dataCrkywCplbjdyDefault);
        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  = this.dataCrkywCplbjdyDefaultService.delete(id);
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }
        return data;
    }


}

