package com.unis.crk.business.qcrk.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.qcrk.entity.DataCrkywCpzDefault;
import com.unis.crk.business.qcrk.entity.DataCrkywJyxxFlt001;
import com.unis.crk.business.qcrk.entity.DataCrkywRmdjDefault;
import com.unis.crk.business.qcrk.entity.vo.DataCrkywCpzDefaultVo;
import com.unis.crk.business.qcrk.entity.vo.Qcrk;
import com.unis.crk.business.qcrk.mapper.DataCrkywCpzDefaultMapper;
import com.unis.crk.business.qcrk.service.IDataCrkywCpzDefaultService;
import com.unis.crk.business.qcrk.service.IDataCrkywJyxxFlt001Service;
import com.unis.crk.business.qcrk.service.IDataCrkywRmdjDefaultService;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 汽车入库-称皮重 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2021-10-11
 */
@Service
public class DataCrkywCpzDefaultServiceImpl extends ServiceImpl<DataCrkywCpzDefaultMapper, DataCrkywCpzDefault> implements IDataCrkywCpzDefaultService {

    @Resource
    private DataCrkywCpzDefaultMapper dataCrkywCpzDefaultMapper;
    @Autowired
    private IBizflowtrailService bizflowtrailService;
    @Autowired
    private IDataKcglXckcDefaultService dataKcglXckcDefaultService;
    @Autowired
    private IDataKcglFcbgzDefaultService dataKcglFcbgzDefaultService;
    @Autowired
    private IDataCrkywRmdjDefaultService dataCrkywRmdjDefaultService;
    @Autowired
    private IBasicEnumService basicEnumService;
    @Autowired
    private IDataKcglKcswDefaultService dataKcglKcswDefaultService;
    @Autowired
    private IDataCrkywTzdxxDefaultService dataCrkywTzdxxDefaultService;
    @Autowired
    private IDataCrkywJyxxFlt001Service dataCrkywJyxxFlt001Service;
    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;


    /**
     * 保存汽车入库-称皮重
     *
     * @param qcrk 汽车入库-称皮重业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @SystemLog(module = "原粮入库新增皮重", methods = "新增",uploadName = "yuliang.rk")
    public JSONObject save(Qcrk qcrk) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success", false);
        DataCrkywCpzDefault dataCrkywCpzDefault = qcrk.getDataCrkywCpzDefault();
        if (isExist(dataCrkywCpzDefault.getBizNo())){
            jsonObject.set("msg","此单据号已存在");
            return jsonObject;
        }
        dataCrkywCpzDefault.setSytelInfoID("4028814763a546b20163a9f469950006"); //样式id
        dataCrkywCpzDefault.setPeriodID("11"); //期间id
        dataCrkywCpzDefault.setDeptID(dataCrkywCpzDefault.getUnitID());//部门id
        dataCrkywCpzDefault.setRoundID(0); //舍位id
        dataCrkywCpzDefault.setShoworder(0);
        dataCrkywCpzDefault.setPreorder(4);
        if (StringUtils.isBlank(dataCrkywCpzDefault.getBuilder())){
            dataCrkywCpzDefault.setBuilder(dataCrkywCpzDefault.getUnitID());
            dataCrkywCpzDefault.setEditor(dataCrkywCpzDefault.getUnitID());
        }
        if (!super.saveOrUpdate(dataCrkywCpzDefault)) { //保存汽车入库-称皮重表成功
            throw new Exception("修改汽车入库-称皮重失败");
        }

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

        //检查库存相关信息
        JSONObject jsonObject1 = isInWarehouseCapacity(matherId, false);
        Double editZl = dataCrkywCpzDefault.getXzklhjz(); //得到此次变动重量，新增就是卸载后扣量净重
        if (jsonObject1.get("success").equals("false")) {
            jsonObject.set("msg", jsonObject1.get("msg").toString());
            throw new Exception(jsonObject1.get("msg").toString());
        }
        //保存库存相关数据
        EditStatus editStatus = EditStatus.New;
        Boolean b = dataKcglXckcDefaultService.makeStockDetail(matherId, unitId, loginName, editStatus, StoreWorkType.In);
        if (!b) {
            jsonObject.set("msg", "保存仓储数据失败");
            throw new RuntimeException("保存仓储数据失败");
        }
        if (!saveBizflowtrailEnt(qcrk)) { //保存轨迹表
            jsonObject.set("msg", "保存轨迹表失败");
            return jsonObject;
        }
        //更新通知单
        JSONObject jsonObject2 = dataCrkywTzdxxDefaultService.updateTzdDataQcRk(editZl, matherId);
        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.rk")
    public JSONObject update(Qcrk qcrk) throws Exception {
                JSONObject jsonObject = new JSONObject();
                jsonObject.set("success", false);
                //查询下一环节是否已处理
                DataCrkywCpzDefault entity = qcrk.getDataCrkywCpzDefault();
                String bizNo = entity.getBizNo();
                String unitId = entity.getUnitID();
                Integer curBizNodeID = 5; //当前节点
                //条件查询器
                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.updateById(entity)) { //修改汽车入库-称皮重表成功
                    throw new Exception("修改汽车入库-称皮重失败");
                }
                String matherId = entity.getDataID();
                String loginName = entity.getBuilder();//创建人
                //检查库存相关信息
                JSONObject jsonObject1 = isInWarehouseCapacity(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.In);
                if (!b) {
                    jsonObject.set("msg", "保存仓储数据失败");
                    throw new RuntimeException("保存仓储数据失败");
                }
                //更新通知单
                JSONObject jsonObject2 = dataCrkywTzdxxDefaultService.updateTzdDataQcRk(editZl, matherId);
                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.rk")
    public JSONObject delete(String id) throws  Exception{
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("success", false);
            //得到对象
            DataCrkywCpzDefault entity = super.getById(id);
            if (entity == null) {
                jsonObject.set("msg", "此数据已被删除");
                return jsonObject;
            }
            String bizNo = entity.getBizNo();
            String unitId = entity.getUnitID();
            Integer curBizNodeID = 5; //当前节点
            //条件查询器
            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);
            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.In);
            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_cpz_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).getXzklhjz();
            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).getXzklhjz();
            editZl = zl;

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

    public JSONObject isInWarehouseCapacity(String dataId, Boolean isUpdate) {
        JSONObject json = new JSONObject();
//        sql.append(" select DISTINCT dcrd.lspz as \"lspz\",dcrd.lsxz  as \"lsxz\",dcrd.UnitID  as \"UnitID\",dccd.sjchid   as \"sjcf\",dccd.sjhwid   as \"sjhw\",b.EnumItemName as \"pzName\",c.EnumItemName as \"xzName\" from data_crkyw_rmdj_default dcrd");
//        sql.append(" INNER JOIN data_crkyw_cpz_default dccd");
//        sql.append(" ON dcrd.BizNo=dccd.BizNo LEFT JOIN enumitem b ON dcrd.lspz = b.EnumItemId AND b.EnumId ='1060' LEFT JOIN enumitem c on dcrd.lsxz = c.EnumItemId WHERE dccd.DataID='" + dataId + "'");
//        List<Map<String,Object>> list = super.findBySql(sql.toString());
        //查询入门相关数据
        DataCrkywCpzDefault dataCrkywCpzDefault = super.getById(dataId);
        QueryWrapper<DataCrkywRmdjDefault> dataCrkywRmdjDefaultQueryWrapper = new QueryWrapper<>();
        dataCrkywRmdjDefaultQueryWrapper.eq("unitId", dataCrkywCpzDefault.getUnitID());
        dataCrkywRmdjDefaultQueryWrapper.eq("bizNo", dataCrkywCpzDefault.getBizNo());
        dataCrkywRmdjDefaultQueryWrapper.last("limit 1");
        DataCrkywRmdjDefault dataCrkywRmdjDefault = dataCrkywRmdjDefaultService.getOne(dataCrkywRmdjDefaultQueryWrapper);
        if (dataCrkywRmdjDefault == null) {
            json.set("success", "false");
            json.set("msg", "入门登记数据已被删除");
            return json;
        }
        json = beforEditOrDelete(dataId, isUpdate);
        if (StringUtils.equals("false", json.getStr("success"))) {
            return json;
        }
        String lspz = dataCrkywRmdjDefault.getLspz(); //品种
        String lsxz = dataCrkywRmdjDefault.getLsxz(); //性质
        String lspzName = basicEnumService.getById(lspz).getEnumName(); //品种名称
        String lsxzName = basicEnumService.getById(lsxz).getEnumName(); //性质名称
        String sjcf = dataCrkywCpzDefault.getSjchID(); //仓房id
        String sjhw = dataCrkywCpzDefault.getSjhwID();//货位id
        String unitID = dataCrkywRmdjDefault.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);
        dataCrkywRmdjDefaultQueryWrapper.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;
            }
        }
        json.set("success", "true");
        return json;
    }

    /**
     * 按照条件查询汽车入库-称皮重信息
     *
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String, Object>> findPageVo(DataCrkywCpzDefaultVo pageQuery) {
            //条形查询器
            QueryWrapper<DataCrkywCpzDefault> queryWrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(pageQuery.getUnitId())){
                queryWrapper.eq("DATA_CRKYW_CPZ_DEFAULT.UnitId",pageQuery.getUnitId());
            }
            if (StringUtils.isNotBlank(pageQuery.getBizNo())){
                queryWrapper.like("DATA_CRKYW_CPZ_DEFAULT.bizNo", ResultUtil.getQueryWrapperformat(pageQuery.getBizNo()));
            }
            if (StringUtils.isNotBlank(pageQuery.getSlr())){
                queryWrapper.like("DATA_CRKYW_RMDJ_DEFAULT.slr",ResultUtil.getQueryWrapperformat(pageQuery.getSlr()));
            }
            if (StringUtils.isNotBlank(pageQuery.getCyr())){
                queryWrapper.like("DATA_CRKYW_RMDJ_DEFAULT.cyr",ResultUtil.getQueryWrapperformat(pageQuery.getCyr()));
            }
            if (StringUtils.isNotBlank(pageQuery.getCydw())){
                queryWrapper.like("DATA_CRKYW_RMDJ_DEFAULT.cydw",ResultUtil.getQueryWrapperformat(pageQuery.getCydw()));
            }
            if (StringUtils.isNotBlank(pageQuery.getCph())){
                queryWrapper.like("DATA_CRKYW_RMDJ_DEFAULT.cph",ResultUtil.getQueryWrapperformat(pageQuery.getCph()));
            }
            if (StringUtils.isNotBlank(pageQuery.getCpzsjStart()) && StringUtils.isNotBlank(pageQuery.getCpzsjEnd())){
                queryWrapper.apply(" DATE_FORMAT(ifnull(DATA_CRKYW_CPZ_DEFAULT.cpzsj,NOW()),'%Y-%m-%d') >=  '"+pageQuery.getCpzsjStart()+"' AND DATE_FORMAT(ifnull(DATA_CRKYW_CPZ_DEFAULT.cpzsj,NOW()),'%Y-%m-%d') <= '"+pageQuery.getCpzsjEnd()+" 23:59:59'");

            }
            //排序
            queryWrapper.orderByDesc("DATA_CRKYW_CPZ_DEFAULT.BuildDate");

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

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

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

    @Override
    public Map<String, Object> getOne(String bizNo, String id) {
        Map<String, Object> map = dataCrkywCpzDefaultMapper.getOne(bizNo,id);
        /*if (StringUtils.isNotBlank(bizNo)){
            List<Map<String,Object>> fltList = dataCrkywJyxxFlt001Service.getLitByBizNoOrMainDatId(bizNo,null);
            if (fltList.isEmpty()){
                fltList = new ArrayList<>();
            }
            map.put("fltList",fltList);
        }*/
        return map;
    }

    /**
     * 通过单位号检查是否已经存在
     *
     * @param bizNo
     * @return
     */
    private boolean isExist(String bizNo) {
        QueryWrapper<DataCrkywCpzDefault> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BizNo", bizNo);
        queryWrapper.last("limit 1");
        queryWrapper.last("limit 1");
        return super.getOne(queryWrapper) != null;
    }
    private Boolean saveBizflowtrailEnt(Qcrk qcrk) {
        Bizflowtrail bizflowtrail = new Bizflowtrail();
        DataCrkywCpzDefault dataCrkywCpzDefault = qcrk.getDataCrkywCpzDefault();
        String flowId = qcrk.getBizflowtrail().getFlowID(); //流程id
        String bizNo = dataCrkywCpzDefault.getBizNo(); //单据号
        String unitId = dataCrkywCpzDefault.getUnitID(); //单位ID
        Integer curBizNodeID = 5; //当前节点
        Integer nextBizNodeID = 6; //下一节点
        Integer bizNodeId = 6; //  report表节点id
        Integer trailOrder = 5;
        String builder = dataCrkywCpzDefault.getBuilder(); //创建人
        Integer showOrder = dataCrkywCpzDefault.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", bizNodeId - 1);
        updateWrapper.eq("TrailOrder", trailOrder - 1);
        updateWrapper.set("status", 1); //修改状态
        if (!bizflowtrailService.update(updateWrapper)) { //更新轨迹表
            return false;
        }
        return true;
    }
}
