package com.unis.crk.cplbusiness.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.aop.SystemLog;
import com.unis.crk.cplbusiness.entity.DataCrkywCplspDefault;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCplspDefaultVo;
import com.unis.crk.cplbusiness.service.IDataCrkywCplspDefaultService;
import com.unis.crk.utils.ResultUtil;
import io.swagger.annotations.*;
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-03-23
 */
@CrossOrigin
@Api(value = "成品粮商品表",tags = {"成品粮商品表"})
@RestController
@RequestMapping("dataCrkywCplspDefault")
public class DataCrkywCplspDefaultController {

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


    @Autowired
    private IDataCrkywCplspDefaultService dataCrkywCplspDefaultService;

    /**
     * 通过单位Id得到成品粮商品下拉列表
     * @param unitId 单位id
     * @return
     */
    @ApiOperation(value="查询", notes="通过单位Id得到成品粮商品下拉列表")
    @GetMapping(value="/getCplSpList/{unitId}")
    public ResultUtil getCplSpList(@PathVariable("unitId") String unitId) {
        LOGGER.info("入参为:{}",unitId);
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(unitId)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplspDefaultService.getCplSpList(unitId));
        return data;
    }
    /**
     * 通过订单id获得到成品粮商品下拉列表
     * @param ddId 单位id
     * @return
     */
    @ApiOperation(value="查询", notes="通过订单编号得到成品粮商品下拉列表")
    @GetMapping(value="/getCplSpListbydd/{ddId}")
    public ResultUtil getCplSpListbydd(@PathVariable("ddId") String ddId) {
        LOGGER.info("入参为:{}",ddId);
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        if (StringUtils.isEmpty(ddId)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplspDefaultService.getCplSpListbydd(ddId));
        return data;
    }
    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<DataCrkywCplspDefault> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.dataCrkywCplspDefaultService.getById(id));
        return data;
    }

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

    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody DataCrkywCplspDefault param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }
        JSONObject ret  = this.dataCrkywCplspDefaultService.saveSp(param);
        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  DataCrkywCplspDefault param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret  = this.dataCrkywCplspDefaultService.update(param);
        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.dataCrkywCplspDefaultService.delete(id);
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }
        return data;
    }
    /**
     * 根据ID查询数据
     * @param sjjlspmcbh
     * @return
     */
    @ApiOperation(value="检测", notes="检测商品编号是否重复")
    @GetMapping("chekeBh/{sjjlspmcbh}")
    public Boolean chekeBh(@PathVariable("sjjlspmcbh") String sjjlspmcbh){
        LOGGER.info("入参为:{}",sjjlspmcbh);
        ResultUtil<DataCrkywCplspDefault> data = new ResultUtil<>();
        if (StringUtils.isEmpty(sjjlspmcbh)){
            data.failed("传入的主键为空");
            return false;
        }
        return this.dataCrkywCplspDefaultService.chekeBh(sjjlspmcbh);

    }


    @ApiOperation(value="查询", notes="根据商品编号查询数据" )
    @PutMapping("/getSpbySpbhList")
    public ResultUtil save( @RequestParam("spbhs") List<String> spbhs) {
        LOGGER.info("入参为:{}",spbhs);
        ResultUtil<List<DataCrkywCplspDefault>> data = new ResultUtil<>();
    if(spbhs.size()<0){
        data.failed("所接收参数为null");
        return data;
    }
        List<DataCrkywCplspDefault> datas=dataCrkywCplspDefaultService.getBySpbhs(spbhs);
        return data.success(datas);

    }
}

