package com.unis.crk.cplbusiness.controller;

import cn.hutool.core.util.IdUtil;
import com.unis.crk.aop.SystemLog;
import com.unis.crk.basic.entity.Attaches;
import com.unis.crk.basic.service.IAttachesService;
import com.unis.crk.business.qcrk.entity.vo.Qcrk;
import com.unis.crk.cplbusiness.entity.DataCrkywCpljyxxDefault;
import com.unis.crk.cplbusiness.service.IDataCrkywCpljyxxDefaultService;
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.cplbusiness.entity.vo.DataCrkywCpljyxxDefaultVo;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCpljyxxDefaultVo;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.hutool.json.JSONObject;


/**
 * <p>
 * 成品粮抽样质检 前端控制器
 * </p>
 *
 * @author TangSong
 * @since 2022-03-21
 */
@CrossOrigin
@Api(value = "成品粮抽样质检",tags = {"成品粮抽样质检"})
@RestController
@RequestMapping("dataCrkywCpljyxxDefault")
public class DataCrkywCpljyxxDefaultController {

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


    @Autowired
    private IDataCrkywCpljyxxDefaultService dataCrkywCpljyxxDefaultService;

    @Autowired
    private IAttachesService attachesService;
    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOneData/{id}")
    public ResultUtil getOneData(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<DataCrkywCpljyxxDefault> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        DataCrkywCpljyxxDefault oneData = this.dataCrkywCpljyxxDefaultService.getOneData(id);
        //获取附件信息
        oneData.setAttaches(attachesService.getOneAttachesByReferencesID(id));
        data.success(oneData);
        return data;
    }

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

/*    *//**
     * 新增一条数据
     * @param param
     * @return
     *//*
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody DataCrkywCpljyxxDefault param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }

        data.success( this.dataCrkywCpljyxxDefaultService.save(param));
        return  data;
    }*/

    /**
     * 新增质检数据
     * @param dataCrkywCpljyxxDefault
     * @return
     */
    @ApiOperation(value="新增质检数据", notes="新增质检数据")
    @PostMapping("save")
    public ResultUtil save(@RequestBody DataCrkywCpljyxxDefault dataCrkywCpljyxxDefault){
        LOGGER.info("入参为:{}", dataCrkywCpljyxxDefault);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(dataCrkywCpljyxxDefault)){
            data.failed("所接收对象为null");
            return data;
        }
        JSONObject ret = new JSONObject();
        String s = IdUtil.simpleUUID();
        dataCrkywCpljyxxDefault.setDataId(s);
        Attaches attaches = dataCrkywCpljyxxDefault.getAttaches();
        if(!ObjectUtils.isEmpty(attaches)){
            attaches.setReferencesID(s);
            attachesService.save(attaches);
        }
        try{
            ret  = this.dataCrkywCpljyxxDefaultService.saveData(dataCrkywCpljyxxDefault);
        }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 param
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody  DataCrkywCpljyxxDefault param) throws Exception {
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret  = this.dataCrkywCpljyxxDefaultService.updateData(param);
        if(!ObjectUtils.isEmpty(param.getAttaches())){
            attachesService.saveOrUpdate(param.getAttaches());
        }
        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.dataCrkywCpljyxxDefaultService.delete(id);
        attachesService.deletAttachesByReferencesID(id);
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }
        return data;
    }

    /**
     * 成品粮检验信息下拉
     * @param unitId 单位id
     * @param ywlx 作业类型：3，成品粮入库 4，成品粮出库
     * @param ddbh 订单编号
     * @return
     */
    @ApiOperation(value="查询", notes="通过单位Id及作业类型得到质检下拉列表")
    @GetMapping(value="/getJyxxList/{unitId}/{ywlx}/{ddbh}")
    public ResultUtil getJyxxList(@PathVariable("unitId") String unitId,@PathVariable("ywlx") String ywlx,@PathVariable("ddbh") String ddbh) {
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataCrkywCpljyxxDefaultService.getJyxxList(unitId,ywlx,ddbh));
        return data;
    }
    /**
     * 成品粮检验信息下拉
     * @param unitId 单位id
     * @param ywlx 作业类型：3，成品粮入库 4，成品粮出库
     * @return
     */
    @ApiOperation(value="查询", notes="通过单位Id及作业类型得到质检下拉列表")
    @GetMapping(value="/getJyxxList/{unitId}/{ywlx}")
    public ResultUtil getJyxxList(@PathVariable("unitId") String unitId,@PathVariable("ywlx") String ywlx) {
        ResultUtil<List<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dataCrkywCpljyxxDefaultService.getJyxxList(unitId,ywlx,null));
        return data;
    }
}

