package com.highdatas.mdm.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.generator.config.querys.AbstractDbQuery; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.baomidou.mybatisplus.service.impl.ServiceImpl; import com.highdatas.mdm.entity.Flows; import com.highdatas.mdm.entity.Maintain; import com.highdatas.mdm.entity.MaintainDetail; import com.highdatas.mdm.mapper.MaintainMapper; import com.highdatas.mdm.mapper.TableInfoMapper; import com.highdatas.mdm.pojo.*; import com.highdatas.mdm.service.*; import com.highdatas.mdm.util.Constant; import com.highdatas.mdm.util.DbUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.MessageFormat; import java.util.*; import java.util.stream.Collectors; /** *

* 服务实现类 *

* * @author kimi * @since 2019-12-16 */ @Service public class MaintainServiceImpl extends ServiceImpl implements IMaintainService { @Autowired IMaintainDetailService maintainDetailService; @Autowired TableInfoMapper tableInfoMapper; @Autowired MasterDataService masterDataService; @Autowired ISysFieldService fieldService; @Autowired MaintainMapper maintainMapper; @Autowired IFlowsService flowsService; @Override public HashMap compare(String maintainId, String maintainId2) { Maintain baseMaintain = selectById(maintainId); Maintain compareMaintain = selectById(maintainId2); HashMap operateIntegerMap = new HashMap(); if (DbUtils.compareVersion(baseMaintain, compareMaintain) > 0) { addCompareInfo(baseMaintain, compareMaintain, operateIntegerMap); operateIntegerMap.put(Operate.common, MessageFormat.format("{0} 对比 {1}", baseMaintain.getVersion(), compareMaintain.getVersion())); }else { addCompareInfo(compareMaintain, baseMaintain, operateIntegerMap); operateIntegerMap.put(Operate.common, MessageFormat.format("{0} 对比 {1}", compareMaintain.getVersion(), baseMaintain.getVersion())); } return operateIntegerMap; } private void addCompareInfo(Maintain baseMaintain, Maintain compareMaintain, HashMap operateIntegerMap) { Integer fromNo = compareMaintain.getOrderNo(); Integer toNo = baseMaintain.getOrderNo(); for (int i = fromNo + 1; i <= toNo; i++) { Maintain maintain = selectOne(new EntityWrapper().eq("table_name", baseMaintain.getTableName()).eq("order_no", i)); List maintainDetailList = maintainDetailService.selectList(new EntityWrapper().eq("parent_id", maintain.getId())); for (MaintainDetail maintainDetail : maintainDetailList) { Operate operate = maintainDetail.getOperate(); Integer integer = (Integer) operateIntegerMap.get(operate); if (integer == null) { integer = 1; } else { integer++; } operateIntegerMap.put(operate, integer); } } } @Override public void dealFlow(String maintainId, ActivitiStatus status) { Maintain maintain = selectById(maintainId); String tableName = maintain.getTableName(); List maintainDetailList = maintainDetailService.selectList(new EntityWrapper().eq("parent_id", maintainId)); if (status.equals(ActivitiStatus.close)) { String collect = maintainDetailList.stream().map(detail -> DbUtils.quotedStr(detail.getPreMergeId())).collect(Collectors.joining(Constant.COMMA)); //delete temp if (!StringUtils.isEmpty(collect)) { masterDataService.delete(tableName + Constant.RECORD, "id in (" +collect+ ")"); } //delete detail for (MaintainDetail maintainDetail : maintainDetailList) { maintainDetail.deleteById(); } //delete maintain maintain.deleteById(); return; } String fields = masterDataService.getTempFields(tableName); String tempFields = masterDataService.getFields(tableName + Constant.RECORD); String tableTempName = tableName + Constant.RECORD; // insert 2 std tableInfoMapper.tempCreate2std(tableName, fields, tableTempName, DbUtils.quotedStr(maintainId)); // update List> updeateDatas = tableInfoMapper.tempByOperate(DbUtils.quotedStr(Operate.update.name()), tempFields, tableTempName, DbUtils.quotedStr(maintainId)); for (Map updeateData : updeateDatas) { String stdId = updeateData.get(Constant.STD_ID).toString(); String id = updeateData.get(Constant.ID).toString(); Result result = masterDataService.selectById(tableName, stdId); Object tempData = result.getData(); String datas = JSON.toJSONString(tempData); JSONObject object = JSON.parseObject(datas); Object grid = object.get("grid"); object = JSON.parseObject(JSON.toJSONString(grid)); object.put(Constant.STD_ID, stdId); object.put(Constant.ID, id); datas = JSON.toJSONString(object); masterDataService.updateById(tableTempName,datas, id,true); updeateData.put(Constant.STD_ID, null); updeateData.put(Constant.ID, stdId); masterDataService.updateById(tableName,JSON.toJSONString(updeateData), stdId,true); } //delete List> delDatas = tableInfoMapper.tempByOperate(DbUtils.quotedStr(Operate.delete.name()), tempFields, tableTempName, DbUtils.quotedStr(maintainId)); for (Map delData : delDatas) { Object id = delData.get(Constant.STD_ID); masterDataService.deleteById(tableName, id.toString()); } // update deal tableInfoMapper.tempDeal(tableTempName, DbUtils.quotedStr(maintainId)); } @Override public boolean checkNowVersion(String maintainId) { Maintain maintain = selectById(maintainId); Maintain maxVersion = getNowVersion(maintain.getTableName()); Integer compared = DbUtils.compareVersion(maxVersion, maintain); if (compared != null && compared == 0) { return true; } else { return false; } } @Override public boolean checkFirstVersion(String id) { Maintain maintain = selectById(id); Maintain order_no = selectOne(new EntityWrapper().eq("table_name", maintain.getTableName()).orderBy("order_no")); if (maintain.getId().equalsIgnoreCase(order_no.getId())) { return true; } return false; } @Override public Result getUnSubmitData(String tableName, Integer pageNo, String pageSize, String whereSegment) { Wrapper versionWrapper = new EntityWrapper().eq("table_name", tableName).isNull("flow_id").orderBy("order_no desc"); Maintain maintain = selectOne(versionWrapper); if (maintain == null) { return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND); } String maintainId = maintain.getId(); String tempName = tableName + Constant.RECORD; Long aLong = maintainMapper.countUnSubmitData(tempName, whereSegment, DbUtils.quotedStr(maintainId)); Page page = new Page(aLong); page.setPageNo(pageNo); if (!StringUtils.isEmpty(pageSize)) { page.setPageSize(Integer.valueOf(pageSize)); } String tempFields = masterDataService.getTempFields(tableName); List> maps = maintainMapper.selectUnSubmitData(tempName, tempFields, whereSegment, DbUtils.quotedStr(maintainId), page.getLimitSQL()); JSONObject object = new JSONObject(); object.fluentPut("records", maps); object.fluentPut("maintainId", maintainId); object.fluentPut("total", page.getRecordCount()); object.fluentPut("size", page.getPageSize()); object.fluentPut("pages", page.getPageCount()); object.fluentPut("current", page.getPageNo()); return Result.success(object); } @Override public Maintain getNextMaintain(String tableName, String userId) { Maintain maintain = this.getMaxVersion(tableName); Maintain unFlowMaxVersion = this.getUnFlowMaxVersion(tableName); if (unFlowMaxVersion == null) { unFlowMaxVersion = new Maintain(); if (maintain != null) { unFlowMaxVersion.setVersion(DbUtils.versionAddSub(maintain.getVersion())); int orderNo = maintain.getOrderNo(); orderNo++; unFlowMaxVersion.setOrderNo(orderNo); } else { unFlowMaxVersion.setVersion(Constant.VERSION_Default); unFlowMaxVersion.setOrderNo(0); } unFlowMaxVersion.setId(DbUtils.getUUID()); unFlowMaxVersion.setChargeId(userId); unFlowMaxVersion.setCreateTime(new Date()); unFlowMaxVersion.setTableName(tableName); } Maintain nowMaintain = unFlowMaxVersion; if (DbUtils.compareVersion(maintain, unFlowMaxVersion) != 0) { nowMaintain = new Maintain(); String maintanId = DbUtils.getUUID(); nowMaintain.setId(maintanId); nowMaintain.setCreateTime(new Date()); int orderNo = maintain.getOrderNo(); orderNo++; nowMaintain.setVersion(DbUtils.versionAddSub(maintain.getVersion())); nowMaintain.setOrderNo(orderNo); nowMaintain.setTableName(tableName); } nowMaintain.setChargeId(userId); String maintainId = nowMaintain.getId(); if(StringUtils.isEmpty(maintainId)) { nowMaintain.setId(DbUtils.getUUID()); } return nowMaintain; } @Override public boolean checkdMaxVersion(String maintainId) { Maintain maintain = selectById(maintainId); Maintain maxVersion = getMaxVersion(maintain.getTableName()); Integer compared = DbUtils.compareVersion(maxVersion, maintain); if (compared == 0) { return true; } else { return false; } } @Override public Maintain getMaxVersion(String tableName) { Wrapper versionWrapper = new EntityWrapper().eq("table_name", tableName).orderBy("order_no desc"); Maintain maxVersion = selectOne(versionWrapper); return maxVersion; } @Override public Maintain getUnFlowMaxVersion(String tableName) { Wrapper versionWrapper = new EntityWrapper().isNull("flow_id").eq("table_name", tableName).orderBy("order_no desc"); Maintain maxVersion = selectOne(versionWrapper); return maxVersion; } @Override public Maintain getInvalidMaxVersion(String tableName) { Maintain result = null; Wrapper versionWrapper = new EntityWrapper().eq("table_name", tableName).orderBy("order_no desc"); List maintainList = selectList(versionWrapper); for (Maintain maintain : maintainList) { String flowId = maintain.getFlowId(); if (StringUtils.isEmpty(flowId)) { result = maintain; } else { Flows flows = flowsService.selectById(flowId); if (flows == null) { continue; } if (!flows.getStatus().equals(ActivitiStatus.open) && !flows.getStatus().equals(ActivitiStatus.close)) { result = maintain; } } } return result; } @Override public Result getInvalidVerionData(String tableName, String where, Integer pageNo, Integer pageSize) { Maintain invalidVersion = getInvalidMaxVersion(tableName); if (invalidVersion == null) { return Result.success(null); } String tableTempName = tableName + Constant.RECORD; String tempFields = masterDataService.getTempFields(tableName); Long count = maintainMapper.countInvalidVersionData(tableTempName, where, invalidVersion.getOrderNo()); Page page = new Page(count); page.setPageNo(pageNo); if (pageSize != null) { page.setPageSize(pageSize); } List> maps = maintainMapper.selectInvalidVersionData(tableTempName, tempFields, where, invalidVersion.getOrderNo(), page.getLimitSQL()); JSONObject resultObj = new JSONObject(); JSONObject object = new JSONObject(); object.fluentPut("record", maps); object.fluentPut("total", page.getRecordCount()); object.fluentPut("size", page.getPageSize()); object.fluentPut("pages", page.getPageCount()); object.fluentPut("current", page.getPageNo()); resultObj.fluentPut("grid", object); return Result.success(resultObj); } @Override public Maintain getNowVersion(String tableName) { Wrapper versionWrapper = new EntityWrapper().eq("table_name", tableName).isNotNull("flow_id").orderBy("order_no desc"); List maintains = selectList(versionWrapper); for (Maintain maintain : maintains) { Flows flows = flowsService.selectById(maintain.getFlowId()); if (flows == null){ continue; } ActivitiStatus status = flows.getStatus(); if (status.equals(ActivitiStatus.open)) { return maintain; } } return null; //return selectOne(new EntityWrapper().eq("table_name", tableName).orderBy("version desc")); } @Override public Maintain getRecentHistoryVersion(String tableName) { Maintain nowMaintain = null; Wrapper versionWrapper = new EntityWrapper().eq("table_name", tableName).isNotNull("flow_id").orderBy("order_no desc"); List maintains = selectList(versionWrapper); for (Maintain maintain : maintains) { Flows flows = flowsService.selectById(maintain.getFlowId()); if (flows == null){ continue; } ActivitiStatus status = flows.getStatus(); if (status.equals(ActivitiStatus.open) && nowMaintain == null) { nowMaintain = maintain; continue; } if (!status.equals(ActivitiStatus.open) && nowMaintain != null){ nowMaintain = null; } } if (nowMaintain == null) { nowMaintain = selectOne(new EntityWrapper().eq("table_name", tableName).orderBy("order_no desc")); } int orderNo = nowMaintain.getOrderNo(); Maintain nowVersion = getNowVersion(tableName); if (nowVersion != null && nowVersion.getOrderNo() == orderNo) { orderNo--; } Maintain maintain = selectOne(new EntityWrapper().eq("table_name", nowMaintain.getTableName()).eq("order_no", orderNo)); return maintain; } @Override public Maintain getMaintainFromVersion(String tableName, String verison) { Wrapper versionWrapper = new EntityWrapper().eq("version", verison).eq("table_name", tableName); Maintain maxVersion = selectOne(versionWrapper); return maxVersion; } @Override public List> selectVersionOperator(String tableTempName, String maintaInId, String where) { return maintainMapper.selectVersionOperator(tableTempName, where, maintaInId); } @Override public List> selectVersionOperator(String tableTempName, String maintaInId) { return maintainMapper.selectVersionOperator(tableTempName, Constant.WHERE_DEFAULT, maintaInId); } @Override public List> selectVersionOperatorByType(String tableTempName, String maintaInId, Operate operate, String where) { return maintainMapper.selectVersionOperatorByType(tableTempName, where, maintaInId, operate.name()); } @Override public List> selectVersionOperatorByType(String tableTempName, String maintaInId, Operate operate) { return maintainMapper.selectVersionOperatorByType(tableTempName, Constant.WHERE_DEFAULT, maintaInId, operate.name()); } @Override public long countVersionOperator(String tableTempName, String maintaInId, String whereSegment) { return maintainMapper.countVersionOperator(tableTempName, maintaInId, whereSegment); } @Override public long countVersionOperator(String tableTempName, String maintaInId) { return maintainMapper.countVersionOperator(tableTempName, maintaInId, Constant.WHERE_DEFAULT); } @Override public long countVersionOperatorByType(String tableTempName, String filterSegment, String maintaInId, Operate operate) { return maintainMapper.countVersionOperatorByType(tableTempName,filterSegment, maintaInId,operate.name()); } @Override public List> selectVersionOperatorByPage(String tableTempName, String maintaInId, String limit, String where) { return maintainMapper.selectVersionOperatorByPage(tableTempName, maintaInId, limit, where); } @Override public List> selectVersionOperatorByPage(String tableTempName, String maintaInId, String limit) { return maintainMapper.selectVersionOperatorByPage(tableTempName, Constant.WHERE_DEFAULT, maintaInId, limit); } @Override public List> selectVersionOperatorByTypeByPage(String tableTempName, String maintaInId, Operate operate, String limit) { return maintainMapper.selectVersionOperatorByTypeByPage(tableTempName, Constant.WHERE_DEFAULT, maintaInId, operate.name(), limit); } @Override public List> selectVersionOperatorByTypeByPage(String tableTempName, String maintaInId, Operate operate, String limit, String where) { return maintainMapper.selectVersionOperatorByTypeByPage(tableTempName, where, maintaInId, operate.name(), limit); } @Override public List getCompareVersionMaintains(Maintain unFlowMaxVersion, Maintain maintainFromVersion) { Integer compare = DbUtils.compareVersion(unFlowMaxVersion, maintainFromVersion); ArrayList ids = new ArrayList<>(); if (compare > 0) { getVerionMatintains(unFlowMaxVersion,maintainFromVersion, ids); }else if(compare == 0) { ids.add(unFlowMaxVersion.getId()); }else { getVerionMatintains(maintainFromVersion,unFlowMaxVersion, ids); } return ids; } @Override public Result tempDataByVersionByFlow(String flowId, String whereSegment, Integer pageNo, Integer pageSize) { Flows flows = flowsService.selectById(flowId); if (flows == null) { return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND); } String businessId = flows.getBusinessId(); Maintain maintain = selectById(businessId); if (maintain == null) { return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND); } String fields = masterDataService.getTempFields(maintain.getTableName()); String tableTempName = maintain.getTableName() + Constant.RECORD; long total = maintainMapper.countTempDataByVersionByFlow(tableTempName, flowId, whereSegment); Page page = new Page(total); if (pageNo != null) { page.setPageNo(pageNo); if (pageSize != null) { page.setPageSize(pageSize); } } else { page.setPageNo(1); } List> maps = maintainMapper.tempDataByVersionByFlow(tableTempName, flowId, fields, whereSegment, page.getLimitSQL()); JSONObject object = new JSONObject(); object.fluentPut("total", page.getRecordCount()); object.fluentPut("size", page.getPageSize()); object.fluentPut("pages", page.getPageCount()); object.fluentPut("current", page.getPageNo()); object.fluentPut("record", maps); return Result.success(object); } private void getVerionMatintains(Maintain firstVersion, Maintain secondVersion, List ids) { Integer fromNo = firstVersion.getOrderNo(); Integer toNo = secondVersion.getOrderNo(); for (int i = fromNo + 1; i <= toNo; i++) { Maintain maintain = selectOne(new EntityWrapper().eq("table_name", firstVersion.getTableName()).eq("order_no", i)); ids.add(maintain.getId()); } } }