package com.engine.organization.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.engine.core.impl.Service; import com.engine.organization.entity.DeleteParam; import com.engine.organization.entity.extend.bo.ExtendGroupBO; import com.engine.organization.entity.extend.bo.ExtendInfoBO; import com.engine.organization.entity.extend.param.ExtendInfoFieldParam; import com.engine.organization.entity.extend.param.ExtendTitleSaveParam; import com.engine.organization.entity.extend.po.ExtendGroupPO; import com.engine.organization.entity.extend.po.ExtendInfoPO; import com.engine.organization.entity.extend.po.ExtendTitlePO; import com.engine.organization.entity.fieldset.param.FieldTypeTreeParam; import com.engine.organization.entity.fieldset.vo.TypeTreeVO; import com.engine.organization.enums.DeleteTypeEnum; import com.engine.organization.enums.ModuleTypeEnum; import com.engine.organization.mapper.extend.ExtendGroupMapper; import com.engine.organization.mapper.extend.ExtendInfoMapper; import com.engine.organization.mapper.extend.ExtendTitleMapper; import com.engine.organization.service.FieldDefinedService; import com.engine.organization.util.OrganizationAssert; import com.engine.organization.util.db.MapperProxyFactory; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang.StringUtils; import java.util.*; import java.util.stream.Collectors; /** * @Author weaver_cl * @Description: * @Date 2022/6/13 * @Version V1.0 **/ public class FieldDefinedServiceImpl extends Service implements FieldDefinedService { private ExtendGroupMapper getExtendGroupMapper() { return MapperProxyFactory.getProxy(ExtendGroupMapper.class); } private ExtendTitleMapper getExtendTitleMapper() { return MapperProxyFactory.getProxy(ExtendTitleMapper.class); } private ExtendInfoMapper getExtendInfoMapper() { return MapperProxyFactory.getProxy(ExtendInfoMapper.class); } @Override public List getTree(ModuleTypeEnum moduleTypeEnum) { Integer extendType = moduleTypeEnum.getValue(); List extendGroupPOS = MapperProxyFactory.getProxy(ExtendGroupMapper.class).listByType(extendType); return ExtendGroupBO.buildTypeTree(extendGroupPOS); } @Override public Map getTabInfo(String groupType) { OrganizationAssert.notBlank(groupType, "分组信息有误,请确认"); Map datas = new HashMap<>(); Long groupId = Long.parseLong(groupType); List extendTitleList = getExtendTitleMapper().getTitlesByGroupID(groupId); List> tabs = new ArrayList<>(); int idx = 1; for (ExtendTitlePO extendTitle : extendTitleList) { Map tab = new HashMap<>(); tab.put("tabkey", idx++); tab.put("title", extendTitle.getTitle()); tab.put("isShow", extendTitle.getIsShow()); tab.put("groupid", extendTitle.getId()); tab.put("viewAttr", extendTitle.getGroupId() < 0 ? 1 : 2); tab.put("editable", getExtendInfoMapper().countFieldsByGroupId(extendTitle.getId()) == 0); tabs.add(tab); } datas.put("status", "1"); datas.put("tabs", tabs); return datas; } @Override public int saveFields(String data) { if (StringUtils.isEmpty(data)) { return 0; } JSONObject jsonObject = JSON.parseObject(data); // extend_group主键 Long groupType = jsonObject.getLong("groupType"); ExtendGroupPO extendGroup = getExtendGroupMapper().getGroupById(groupType); // 主表、主表拓展表为extend_title主键;明细表为extend_group主键 Long extendGroupId = jsonObject.getLong("groupId"); JSONArray records = (JSONArray) jsonObject.get("records"); for (int i = 0; i < records.size(); i++) { // 存在ID则更新 ,不存在ID则增加 ExtendInfoFieldParam infoFieldParam = JSONObject.toJavaObject((JSONObject) records.get(i), ExtendInfoFieldParam.class); // TODO ExtendInfoPO extendInfo = ExtendInfoBO.convertFieldParam2ExtendInfo(user, infoFieldParam, extendGroup, extendGroupId, i + 1); if (null != extendInfo.getId()) { // 更新 extend_title数据 getExtendInfoMapper().updateExtendInfo(extendInfo); } else { // 新增extend_title数据 // 新增表结构 } } return 0; } @Override public Map saveGroup(ExtendTitleSaveParam param) { Map datas = new HashMap<>(); Long groupType = param.getGroupType(); Set lsDelGroupId = new HashSet<>(); List extendTitleList = getExtendTitleMapper().getTitlesByGroupID(groupType); extendTitleList.forEach(item -> lsDelGroupId.add(item.getId())); String data = param.getData(); JSONObject jsonObject = JSON.parseObject(data); Set ids = new HashSet<>(); JSONArray records = (JSONArray) jsonObject.get("records"); for (int i = 0; i < records.size(); i++) { ExtendTitlePO extendTitle = new ExtendTitlePO(); JSONObject object = (JSONObject) records.get(i); extendTitle.setId(object.getLong("id")); extendTitle.setGroupId(groupType); extendTitle.setIsShow(object.getString("isShow")); extendTitle.setTitle(object.getString("groupName")); extendTitle.setShowOrder(i + 1); if (null != extendTitle.getId()) { lsDelGroupId.remove(extendTitle.getId()); } if (null == extendTitle.getId()) { extendTitle.setCreator((long) user.getUID()); extendTitle.setCreateTime(new Date()); extendTitle.setDeleteType(0); getExtendTitleMapper().insertIgnoreNull(extendTitle); } else { extendTitle.setUpdateTime(new Date()); getExtendTitleMapper().updateExtendTitle(extendTitle); } ids.add(extendTitle.getId()); } if (CollectionUtils.isNotEmpty(lsDelGroupId)) { getExtendTitleMapper().deleteExtendTitleByIds(lsDelGroupId); } datas.put("groupid", StringUtils.join(ids, ",")); datas.put("status", "1"); return datas; } @Override public Map saveTitle(ExtendTitleSaveParam param) { Map datas = new HashMap<>(); List titlesByGroupID = getExtendTitleMapper().getTitlesByGroupID(param.getGroupType()); ExtendTitlePO extendTitlePO = titlesByGroupID.stream().max(Comparator.comparing(ExtendTitlePO::getShowOrder)).get(); Set ids = titlesByGroupID.stream().map(ExtendTitlePO::getId).collect(Collectors.toSet()); ExtendTitlePO extendTitle = new ExtendTitlePO(); extendTitle.setGroupId(param.getGroupType()); extendTitle.setIsShow(param.getIsShow()); extendTitle.setTitle(param.getGroupName()); extendTitle.setShowOrder(null == extendTitlePO ? 0 : extendTitlePO.getShowOrder() + 1); extendTitle.setCreator((long) user.getUID()); extendTitle.setCreateTime(new Date()); extendTitle.setDeleteType(0); getExtendTitleMapper().insertIgnoreNull(extendTitle); ids.add(extendTitle.getId()); datas.put("groupid", StringUtils.join(ids, ",")); datas.put("status", "1"); return datas; } @Override public void changeTree(ModuleTypeEnum moduleTypeEnum, FieldTypeTreeParam fieldTypeTreeParam) { if (fieldTypeTreeParam.getId() == null) { ExtendGroupPO extendGroupPO = buildExtendGroupType(moduleTypeEnum, fieldTypeTreeParam); MapperProxyFactory.getProxy(ExtendGroupMapper.class).insertIgnoreNull(extendGroupPO); //默认新增title ExtendTitlePO extendTitlePO = buildExtendTitleType(extendGroupPO); MapperProxyFactory.getProxy(ExtendTitleMapper.class).insertIgnoreNull(extendTitlePO); } else { MapperProxyFactory.getProxy(ExtendGroupMapper.class).update(fieldTypeTreeParam.getId(), fieldTypeTreeParam.getName()); } } @Override public int deleteTitle(Long id) { return getExtendTitleMapper().deleteExtendTitleByIds(DeleteParam.builder().ids(id.toString()).build().getIds()); } @Override public Map getFieldDefinedInfo(ExtendTitleSaveParam param) { return null; } private ExtendTitlePO buildExtendTitleType(ExtendGroupPO extendGroupPO) { return ExtendTitlePO.builder() .groupId(extendGroupPO.getId()) .title(extendGroupPO.getGroupName()) .showOrder(1) .isShow("1") .creator((long) user.getUID()) .deleteType(DeleteTypeEnum.NOT_DELETED.getValue()) .createTime(new Date()) .updateTime(new Date()).build(); } private ExtendGroupPO buildExtendGroupType(ModuleTypeEnum moduleTypeEnum, FieldTypeTreeParam fieldTypeTreeParam) { return ExtendGroupPO.builder() .extendType(moduleTypeEnum.getValue()) .groupName(fieldTypeTreeParam.getName()) .creator((long) user.getUID()) .deleteType(DeleteTypeEnum.NOT_DELETED.getValue()) .createTime(new Date()) .updateTime(new Date()) .pid(fieldTypeTreeParam.getParentid()) .isShow(fieldTypeTreeParam.getIsShow() == null ? "1" : fieldTypeTreeParam.getIsShow()).build(); } }