package com.unis.crk.business.qcck.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.crk.aop.SystemLog;
import com.unis.crk.basic.service.IBasicEnumService;
import com.unis.crk.business.kcgl.entity.DataKcglFcbgzDefault;
import com.unis.crk.business.kcgl.entity.DataKcglKcswDefault;
import com.unis.crk.business.kcgl.service.IDataKcglFcbgzDefaultService;
import com.unis.crk.business.kcgl.service.IDataKcglKcswDefaultService;
import com.unis.crk.business.kcgl.service.IDataKcglXckcDefaultService;
import com.unis.crk.business.qcck.entity.DataCrkywCkcmzDefault;
import com.unis.crk.business.qcck.entity.DataCrkywCkrmdjDefault;
import com.unis.crk.business.qcck.entity.vo.DataCrkywCkcmzDefaultVo;
import com.unis.crk.business.qcck.entity.vo.Qcck;
import com.unis.crk.business.qcck.mapper.DataCrkywCkcmzDefaultMapper;
import com.unis.crk.business.qcck.service.IDataCrkywCkcmzDefaultService;
import com.unis.crk.business.qcck.service.IDataCrkywCkrmdjDefaultService;
import com.unis.crk.business.qcrk.service.IDataCrkywTzdxxDefaultService;
import com.unis.crk.common.entity.Bizflowtrail;
import com.unis.crk.common.service.IBizflowtrailService;
import com.unis.crk.common.service.IDeleteDataRecordService;
import com.unis.crk.enums.EditStatus;
import com.unis.crk.enums.StoreWorkType;
import com.unis.crk.utils.ResultUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 汽车出库-称毛重表 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2021-10-22
 */
@Service
public class DataCrkywCkcmzDefaultServiceImpl extends ServiceImpl<DataCrkywCkcmzDefaultMapper, DataCrkywCkcmzDefault> implements IDataCrkywCkcmzDefaultService {


    @Resource
    private DataCrkywCkcmzDefaultMapper dataCrkywCkcmzDefaultMapper;
    @Autowired
    private IBizflowtrailService bizflowtrailService;
    @Autowired
    private IDataKcglXckcDefaultService dataKcglXckcDefaultService;
    @Autowired
    private IDataKcglFcbgzDefaultService dataKcglFcbgzDefaultService;
    @Autowired
    private IDataCrkywCkrmdjDefaultService dataCrkywCkrmdjDefaultService;
    @Autowired
    private IBasicEnumService basicEnumService;
    @Autowired
    private IDataKcglKcswDefaultService dataKcglKcswDefaultService;
    @Autowired
    private IDataCrkywTzdxxDefaultService dataCrkywTzdxxDefaultService;
    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;
    /**
     * 保存汽车出库-称毛重
     *
     * @param qcck 汽车出库-称毛重业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @SystemLog(module = "原粮出库毛重", methods = "新增",uploadName = "yuliang.ck")
    public JSONObject save(Qcck qcck) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success", false);
        DataCrkywCkcmzDefault entity = qcck.getDataCrkywCkcmzDefault();
        if (isExist(entity.getBizNo())){
            jsonObject.set("msg","此单据号已存在");
            return jsonObject;
        }
        entity.setSytelInfoID("4028814763a546b20163ab30b98f0024"); //样式id
        entity.setPeriodID("11"); //期间id
        entity.setDeptID(entity.getUnitID());//部门id
        entity.setRoundID(0); //舍位id
        entity.setShoworder(0);
        entity.setPreorder(2);
        if (StringUtils.isBlank(entity.getBuilder())){
            entity.setBuilder(entity.getUnitID());
            entity.setEditor(entity.getUnitID());
        }
        if (!super.saveOrUpdate(entity)) { //保存汽车出库-称毛重表成功
            throw new Exception("修改汽车出库-称毛重失败");
        }

        String matherId = entity.getDataID();
        String unitId = entity.getUnitID();
        String loginName = entity.getBuilder();//创建人

        //检查库存相关信息
        JSONObject jsonObject1 = isOutWarehouseCapacity(matherId, false);
        Double editZl = jsonObject1.getDouble("editZl", 0.0); //得到此次变动重量，新增就是卸载后扣量净重
        if (jsonObject1.get("success").equals("false")) {
            throw new Exception(jsonObject1.get("msg").toString());
        }
        //保存库存相关数据
        EditStatus editStatus = EditStatus.New;
        Boolean b = dataKcglXckcDefaultService.makeStockDetail(matherId, unitId, loginName, editStatus, StoreWorkType.Out);
        if (!b) {
            jsonObject.set("msg", "保存仓储数据失败");
            throw new RuntimeException("保存仓储数据失败");
        }
        if (!saveBizflowtrailEnt(qcck)) { //保存轨迹表
            jsonObject.set("msg", "保存轨迹表失败");
            return jsonObject;
        }
        //更新通知单
        JSONObject jsonObject2 = dataCrkywTzdxxDefaultService.updateTzdDataQcRk(editZl, matherId,true);
        if (!jsonObject2.getBool("success")) {
            jsonObject.set("msg", jsonObject2.getStr("msg"));
            throw new RuntimeException(jsonObject2.getStr("msg"));
        }
        jsonObject.set("success", true);
        jsonObject.set("msg", "保存成功");
        return jsonObject;
    }

    /**
     * 修改汽车出库-称毛重
     *
     * @param qcrk 汽车出库-称毛重业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @SystemLog(module = "原粮出库毛重", methods = "修改",uploadName = "yuliang.ck")
    public JSONObject update(Qcck qcrk) throws Exception {
        {
            {
                JSONObject jsonObject = new JSONObject();
                jsonObject.set("success", false);
                //查询下一环节是否已处理
                DataCrkywCkcmzDefault entity = qcrk.getDataCrkywCkcmzDefault();
                String bizNo = entity.getBizNo();
                String unitId = entity.getUnitID();
                Integer curBizNodeID = 3; //当前节点
                //条件查询器
                QueryWrapper<Bizflowtrail> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("BillNo", bizNo);
                queryWrapper.eq("UnitID", unitId);
                queryWrapper.eq("CurBizNodeID", curBizNodeID); //当前节点
                queryWrapper.last("limit 1");
                Bizflowtrail bizflowtrail = bizflowtrailService.getOne(queryWrapper);
                if (bizflowtrail == null) {
                    jsonObject.set("msg", "轨迹表数据已被删除，请联系管理员");
                    return jsonObject;
                }
                if (bizflowtrail.getStatus() == 1 || bizflowtrail.getStatus() == 100) { //表示已经执行后面业务，不能删除
                    jsonObject.set("msg", "当前车辆已完成[汽车出库-称毛重]，不能修改");
                    return jsonObject;
                }
                if (!super.saveOrUpdate(entity)) { //修改汽车出库-称毛重表成功
                    throw new Exception("修改汽汽车出库-称毛重失败");
                }
                String matherId = entity.getDataID();
                String loginName = entity.getUnitID();//创建人
                //检查库存相关信息
                JSONObject jsonObject1 = isOutWarehouseCapacity(matherId, true);
                Double editZl = jsonObject1.getDouble("editZl", 0.0); //得到此次变动重量，新增就是卸载后扣量净重
                if (jsonObject1.get("success").equals("false")) {
                    jsonObject.set("msg", jsonObject1.get("msg").toString());
                    throw new Exception(jsonObject1.get("msg").toString());
                }
                //保存库存相关数据
                EditStatus editStatus = EditStatus.Modified;
                Boolean b = dataKcglXckcDefaultService.makeStockDetail(matherId, unitId, loginName, editStatus, StoreWorkType.Out);
                if (!b) {
                    jsonObject.set("msg", "保存仓储数据失败");
                    throw new RuntimeException("保存仓储数据失败");
                }
                //更新通知单
                JSONObject jsonObject2 = dataCrkywTzdxxDefaultService.updateTzdDataQcRk(editZl, matherId,true);
                if (!jsonObject2.getBool("success")) {
                    jsonObject.set("msg", jsonObject2.getStr("msg"));
                    throw new RuntimeException(jsonObject2.getStr("msg"));
                }
                jsonObject.set("success", true);
                jsonObject.set("msg", "修改成功");
                return jsonObject;
            }
        }
    }

    /**
     * 删除汽车出库-称毛重数据
     *
     * @param id 汽车出库-称毛重主键尖
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @SystemLog(module = "原粮出库毛重", methods = "删除",uploadName = "yuliang.ck")
    public JSONObject delete(String id) throws  Exception{
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("success", false);
            //得到对象
            DataCrkywCkcmzDefault entity = super.getById(id);
            if (entity == null) {
                jsonObject.set("msg", "此数据已被删除");
                return jsonObject;
            }
            String bizNo = entity.getBizNo();
            String unitId = entity.getUnitID();
            Integer curBizNodeID = 3; //当前节点
            //条件查询器
            QueryWrapper<Bizflowtrail> deleteWrapper = new QueryWrapper<>();
            deleteWrapper.eq("BillNo", bizNo);
            deleteWrapper.eq("UnitID", unitId);
            deleteWrapper.eq("CurBizNodeID", curBizNodeID); //当前节点
            deleteWrapper.last("limit 1");
            Bizflowtrail bizflowtrail = bizflowtrailService.getOne(deleteWrapper);
            if (bizflowtrail == null) {
                jsonObject.set("msg", "轨迹表数据已被删除，请联系管理员");
                return jsonObject;
            }
            if (bizflowtrail.getStatus() == 1 || bizflowtrail.getStatus() == 100) { //表示已经执行后面业务，不能删除
                jsonObject.set("msg", "当前车辆已完成[称毛重]，不能删除");
                return jsonObject;
            }
            //
            JSONObject jsonObject1 = beforEditOrDelete(id,false);
            Double editZl = jsonObject1.getDouble("editZl", 0.0); //负得扣量后净重
            if (jsonObject1.get("success").equals("false")) {
                jsonObject.set("success", "false");
                jsonObject.set("msg", jsonObject1.get("msg").toString());
                throw new Exception(jsonObject1.get("msg").toString());
            }
            //更新登录通知单
            jsonObject1 = dataCrkywTzdxxDefaultService.updateTzdDataQcRk(editZl,id,true);
            if (!jsonObject1.getBool("success")) {
                jsonObject.set("msg", jsonObject1.getStr("msg"));
                throw new RuntimeException(jsonObject1.getStr("msg"));
            }
            if (!super.removeById(id)) { //删除汽车称皮重成功
                jsonObject.set("msg", "删除汽车称皮重数据失败");
                return jsonObject;
            }
            //更新库存相关数据
            EditStatus editStatus = EditStatus.Deleted;
            Boolean b = dataKcglXckcDefaultService.makeStockDetail(id, unitId, entity.getBuilder(), editStatus, StoreWorkType.Out);
            if (!b) {
                jsonObject.set("msg", "删除仓储数据失败");
                throw new RuntimeException("删除仓储数据失败");
            }
            //条件查询器

            if (!bizflowtrailService.remove(deleteWrapper)) { //删除轨迹表,当前节点数据
                jsonObject.set("msg", "删除轨迹表数据失败");
                throw new RuntimeException("删除轨迹表数据失败");
            }
            //修改上一个节点状态为0
            UpdateWrapper<Bizflowtrail> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("BillNo", bizNo);
            updateWrapper.eq("UnitID", unitId);
            updateWrapper.eq("CurBizNodeID", curBizNodeID - 1);
            updateWrapper.set("status", 0);
            updateWrapper.set("UPDATE_TIME", null);
            if (!bizflowtrailService.update(updateWrapper)) {
                jsonObject.set("msg", "修改上一节点轨迹数据表失败");
                throw new RuntimeException("修改上一节点轨迹数据表失败");
            }
            //保存删除数据记录
            deleteDataRecordService.save(id,"data_crkyw_ckcmz_default",unitId,new Date());
            deleteDataRecordService.save(bizflowtrail.getTrailID(),"bizflowtrail",unitId,new Date());

            jsonObject.set("success", true);
            jsonObject.set("msg", "删除成功");
            return jsonObject;
        }
    }

    private JSONObject beforEditOrDelete(String dataId, Boolean isUpdate) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success", "true");
        jsonObject.set("editZl", 0.0);


//        String sql =" SELECT ch as \"cf\",hwh as \"hw\",pz as \"pz\",hwxz as \"xz\","+FunUtils.fn_ifnull()+"(srsl,0) as \"srsl\","+FunUtils.fn_ifnull()+"(zcsl,0) as \"zcsl\","+FunUtils.fn_FormatDate(FunUtils.DATE_FORMAT_LONG, "rq")+" as \"rq\" FROM data_kcgl_fcbgz_default where DataID = '"+dataId+"'";
//
//        List<Map<String, Object>> list = super.findBySql(sql);
        DataKcglFcbgzDefault dataKcglFcbgzDefault = dataKcglFcbgzDefaultService.getById(dataId);
        if (dataKcglFcbgzDefault != null) { //表示是修改、删除
            Double srsl = dataKcglFcbgzDefault.getSrsl();
            Double zcsl = dataKcglFcbgzDefault.getZcsl();
            Double oldZl = Math.abs(srsl - zcsl); //绝对值
            String pz = dataKcglFcbgzDefault.getPz();
            String xz = dataKcglFcbgzDefault.getHwxz();
            String ch = dataKcglFcbgzDefault.getCh();
            String hw = dataKcglFcbgzDefault.getHwh();
            String date =  DateUtil.format(dataKcglFcbgzDefault.getRq(),"yyyy-MM-dd HH:mm:ss");
//            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(dataKcglFcbgzDefault.getRq());
            Double editZl;
            //得到新重量

            Double zl = super.getById(dataId).getKlhjz();
            editZl = isUpdate ? (Double.valueOf(zl) - oldZl) : (0.0 - Double.valueOf(oldZl)); //修改/删除

            jsonObject.set("editZl", editZl);
//            sql= " SELECT "+FunUtils.fn_ifnull()+"(kcsl,0) as \"kcsl\" FROM data_kcgl_fcbgz_default WHERE  ifnull(ch,'')='%s' AND ifnull(hwh,'')='%s' AND ifnull(pz,'')='%s' AND ifnull(hwxz,'')='%s' and rq >= '%s' and (kcsl+%s) < 0 and DataID != '%s' order by rq ";
//            sql = String.format(sql,
//                    cf, hw, pz, xz,date,editZl.toString(),dataId);
//            List<Map<String, Object>> list1  = this.findBySql(sql);
            QueryWrapper<DataKcglFcbgzDefault> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("unitId", dataKcglFcbgzDefault.getUnitID());
            queryWrapper.eq("ch", ch);
            queryWrapper.eq("hwh", hw);
            queryWrapper.eq("pz", pz);
            queryWrapper.eq("hwxz", xz);
            queryWrapper.ge("rq", date);
            queryWrapper.lt("kcsl+" + editZl, 0); //修改后的库存数量小于0
            queryWrapper.ne("dataId", dataId);
            queryWrapper.orderByDesc("rq");
            if (dataKcglFcbgzDefaultService.list(queryWrapper).size() > 0) {
                jsonObject.set("success", "false");
                jsonObject.set("msg", "不可操作，库存数量变为负数");
                return jsonObject;
            }
        } else { //表示新增
            Double editZl;
            //得到新重量
            Double zl = super.getById(dataId).getKlhjz();
            editZl = zl;

            jsonObject.set("editZl", editZl);
        }
        return jsonObject;
    }

    public JSONObject isOutWarehouseCapacity(String dataId, Boolean isUpdate) {
        JSONObject json = new JSONObject();
        //查询入门相关数据
        DataCrkywCkcmzDefault dataCrkywCkcmzDefault = super.getById(dataId);
        QueryWrapper<DataCrkywCkrmdjDefault> ckrmdjtQueryWrapper = new QueryWrapper<>();
        ckrmdjtQueryWrapper.eq("unitId", dataCrkywCkcmzDefault.getUnitID());
        ckrmdjtQueryWrapper.eq("bizNo", dataCrkywCkcmzDefault.getBizNo());
        ckrmdjtQueryWrapper.last("limit 1");
        DataCrkywCkrmdjDefault dataCrkywCkrmdjDefault = dataCrkywCkrmdjDefaultService.getOne(ckrmdjtQueryWrapper);
        if (dataCrkywCkrmdjDefault == null) {
            json.set("success", "false");
            json.set("msg", "入门登记数据已被删除");
            return json;
        }
        json = beforEditOrDelete(dataId, isUpdate);
        if (StringUtils.equals("false", json.getStr("success"))) {
            return json;
        }
        Double editZl = json.getDouble("editZl");
        String lspz = dataCrkywCkrmdjDefault.getLspz(); //品种
        String lsxz = dataCrkywCkrmdjDefault.getLyxz(); //性质
        String lspzName = basicEnumService.getById(lspz).getEnumName(); //品种名称
        String lsxzName = basicEnumService.getById(lsxz).getEnumName(); //性质名称
        String sjcf = dataCrkywCkcmzDefault.getSjckch(); //仓房id
        String sjhw = dataCrkywCkcmzDefault.getSjhwh();//货位id
        String unitID = dataCrkywCkrmdjDefault.getUnitID();
        // 判断品种和性质是否一致
//        sql.append(" SELECT "+FunUtils.fn_ifnull()+"(pz,'') as \"pz\","+FunUtils.fn_ifnull()+"(hwxz,'') as \"xz\",	b.EnumItemName as \"pzName\",	c.EnumItemName as \"xzName\" FROM 	data_kcgl_kcsw_default a LEFT JOIN enumitem b ON a.pz = b.EnumItemId AND b.EnumId ='1060' LEFT JOIN enumitem c on a.hwxz = c.EnumItemId ");
//        sql.append(" WHERE a.unitid= '"+unitID+"' and ch='"+ sjcf +"' AND "+FunUtils.fn_ifnull()+"(hwh,'')='"+ sjhw +"' ");
//        list = super.findBySql(sql.toString());
        QueryWrapper<DataKcglKcswDefault> dataKcglKcswDefaultQueryWrapper = new QueryWrapper<>();
        dataKcglKcswDefaultQueryWrapper.eq("unitId", unitID);
        dataKcglKcswDefaultQueryWrapper.eq("ch", sjcf);
        dataKcglKcswDefaultQueryWrapper.eq("hwh", sjhw);
        ckrmdjtQueryWrapper.last("limit 1");
        DataKcglKcswDefault dataKcglKcswDefault = dataKcglKcswDefaultService.getOne(dataKcglKcswDefaultQueryWrapper);
        if (  dataKcglKcswDefault != null ) {
            // 判断品种
            if (!StringUtils.equals(lspz, dataKcglKcswDefault.getPz())) {
                json.set("success", "false");
                json.set("msg", "此仓房/油罐，货位[品种:" + basicEnumService.getById(dataKcglKcswDefault.getPz()).getEnumName() + "]已装入其它货物[品种:" + lspzName + "]，不可出库");
                return json;
            }
            //判断性质
            if (!StringUtils.equals(lsxz, dataKcglKcswDefault.getHwxz())) {
                json.set("success", "false");
                json.set("msg", "此仓房/油罐，货位[性质:" + basicEnumService.getById(dataKcglKcswDefault.getHwxz()).getEnumName() + "]已装入其它货物[性质:" + lsxzName + "]，不可出库");
                return json;
            }
            //判断库存容量
            if (dataKcglKcswDefault.getKcsl()-Math.abs(editZl) < 0 ){
                json.set("success", "false");
                json.set("msg", "出库容量大于仓房剩余容量!");
                return json;
            }
        }else{ //表示不存在数据
            json.set("success", "false");
            json.set("msg", "出库容量大于仓房剩余容量!");
            return  json;
        }
        json.set("success", "true");
        return json;
    }

    /**
     * 按照条件查询汽车出库-称毛重信息
     *
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String, Object>> findPageVo(DataCrkywCkcmzDefaultVo pageQuery) {
        //条形查询器
        QueryWrapper<DataCrkywCkcmzDefault> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(pageQuery.getUnitId())){
            queryWrapper.eq("DATA_CRKYW_CKCMZ_DEFAULT.UnitId",pageQuery.getUnitId());
        }
        if (StringUtils.isNotBlank(pageQuery.getBizNo())){
            queryWrapper.like("DATA_CRKYW_CKCMZ_DEFAULT.bizNo", ResultUtil.getQueryWrapperformat(pageQuery.getBizNo()));
        }
        if (StringUtils.isNotBlank(pageQuery.getLspz())){
            queryWrapper.like("DATA_CRKYW_CKRMDJ_DEFAULT.slr",ResultUtil.getQueryWrapperformat(pageQuery.getLspz()));
        }
        if (StringUtils.isNotBlank(pageQuery.getXz())){
            queryWrapper.like("DATA_CRKYW_CKRMDJ_DEFAULT.lyxz",ResultUtil.getQueryWrapperformat(pageQuery.getXz()));
        }
        if (StringUtils.isNotBlank(pageQuery.getSlr())) {
            queryWrapper.like("DATA_CRKYW_CKRMDJ_DEFAULT.slr", ResultUtil.getQueryWrapperformat(pageQuery.getSlr()));
        }
        if (StringUtils.isNotBlank(pageQuery.getCyr())){
            queryWrapper.like("DATA_CRKYW_CKRMDJ_DEFAULT.cyr",ResultUtil.getQueryWrapperformat(pageQuery.getCyr()));
        }
        if (StringUtils.isNotBlank(pageQuery.getCydw())){
            queryWrapper.like("DATA_CRKYW_CKRMDJ_DEFAULT.cydw",ResultUtil.getQueryWrapperformat(pageQuery.getCydw()));
        }
        if (StringUtils.isNotBlank(pageQuery.getCph())){
            queryWrapper.like("DATA_CRKYW_CKRMDJ_DEFAULT.cph",ResultUtil.getQueryWrapperformat(pageQuery.getCph()));
        }
        if (StringUtils.isNotBlank(pageQuery.getCmzsjStart()) && StringUtils.isNotBlank(pageQuery.getCmzsjEnd())){
            queryWrapper.apply(" DATE_FORMAT(ifnull(DATA_CRKYW_CKCMZ_DEFAULT.jmsj,NOW()),'%Y-%m-%d') >=  '"+pageQuery.getCmzsjStart()+"' AND DATE_FORMAT(ifnull(DATA_CRKYW_CKCMZ_DEFAULT.jmsj,NOW()),'%Y-%m-%d') <= '"+pageQuery.getCmzsjEnd()+" 23:59:59'");

        }
        //排序
        queryWrapper.orderByDesc("DATA_CRKYW_CKCMZ_DEFAULT.ModifyDate");

        //分页相关
        Page<DataCrkywCkcmzDefault> p = new Page<>(pageQuery.getPage(), pageQuery.getLimit());

        IPage<Map<String, Object>> page = dataCrkywCkcmzDefaultMapper.selectMyMapsPage(p, queryWrapper);
        return page;
    }

    @Override
    public List<Map<String, Object>> getPerData(DataCrkywCkcmzDefaultVo pageQuery) {
        List<Map<String, Object>> list = dataCrkywCkcmzDefaultMapper.getPerData(pageQuery);
        return  list;
    }

    @Override
    public Map<String, Object> getOne(String bizNo, String id) {
        return dataCrkywCkcmzDefaultMapper.getOne(bizNo,id);
    }

    /**
     * 通过单位号检查是否已经存在
     *
     * @param bizNo
     * @return
     */
    private boolean isExist(String bizNo) {
        QueryWrapper<DataCrkywCkcmzDefault> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BizNo", bizNo);
        queryWrapper.last("limit 1");
        return super.getOne(queryWrapper) != null;
    }
    private Boolean saveBizflowtrailEnt(Qcck qcck) {
        Bizflowtrail bizflowtrail = new Bizflowtrail();
        DataCrkywCkcmzDefault dataCrkywCkcmzDefault = qcck.getDataCrkywCkcmzDefault();
        String flowId = qcck.getBizflowtrail().getFlowID(); //流程id
        flowId = "6FD0418CD28A2B3F3E7E9EC0D7429461";
        String bizNo = dataCrkywCkcmzDefault.getBizNo(); //单据号
        String unitId = dataCrkywCkcmzDefault.getUnitID(); //单位ID
        Integer curBizNodeID = 3; //当前节点
        Integer nextBizNodeID = 4; //下一节点
        Integer bizNodeId = 13; //  report表节点id
        Integer trailOrder = 3;
        String builder = dataCrkywCkcmzDefault.getBuilder(); //创建人
        Integer showOrder = dataCrkywCkcmzDefault.getShoworder(); //序号

        bizflowtrail.setBillNo(bizNo);
        bizflowtrail.setUnitID(unitId);
        bizflowtrail.setTrailOrder(trailOrder);
        bizflowtrail.setCurBizNodeID(curBizNodeID);
        bizflowtrail.setNextBizNodeID(nextBizNodeID);
        bizflowtrail.setBizNodeId(bizNodeId);
        bizflowtrail.setBuilder(builder);
        bizflowtrail.setBuildDate(new Date());
        bizflowtrail.setShowOrder(showOrder);
        bizflowtrail.setFlowID(flowId);
        bizflowtrail.setStatus(0);
        if (!bizflowtrailService.save(bizflowtrail)) {
            return false;
        }
        //更新上一环节业务数据
        //更新查询器
        UpdateWrapper<Bizflowtrail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("BillNo", bizNo);
        updateWrapper.eq("UnitID", unitId);
        updateWrapper.eq("CurBizNodeID", curBizNodeID - 1);
        updateWrapper.eq("NextBizNodeID", nextBizNodeID - 1);
        updateWrapper.eq("BizNodeId", 11);
        updateWrapper.eq("TrailOrder", trailOrder - 1);
        updateWrapper.set("status", 1); //修改状态
        if (!bizflowtrailService.update(updateWrapper)) { //更新轨迹表
            return false;
        }
        return true;
    }
}
