package com.bowintek.practice.services.impl; import com.bowintek.practice.filter.exception.BaseException; import com.bowintek.practice.mapper.SrSubjectMapper; import com.bowintek.practice.mapper.SrSubjectfieldMapper; import com.bowintek.practice.mapper.cquery.SubjectCQuery; import com.bowintek.practice.model.*; import com.bowintek.practice.services.service.SubjectService; import com.bowintek.practice.services.service.system.DictionaryService; import com.bowintek.practice.util.Constant; import com.bowintek.practice.util.StringUtils; import com.bowintek.practice.vo.SubjectfieldVo; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; @Service("SubjectService") public class SubjectServiceImpl implements SubjectService { @Autowired private SrSubjectMapper subjectMapper; @Autowired private SrSubjectfieldMapper subjectfieldMapper; @Autowired private SubjectCQuery subjectCQuery; @Autowired private StringUtils stringUtils; @Autowired private DictionaryService dictionaryService; @Override public PageInfo getList(Integer page, Integer rows, String subjectName, String tabName, String tabCode, List idList) { PageHelper.startPage(page, rows); List dataList = subjectCQuery.getList(subjectName, tabName, tabCode, stringUtils.ListToInSql(idList)); PageInfo result = new PageInfo(dataList); return result; } @Override public SrSubject getSubject(String subjectId) { return subjectMapper.selectByPrimaryKey(subjectId); } @Override public int saveSubject(SrSubject model, List fieldList, String userId) { if (model.getIsReferences() == 0) { model.setExecSql(model.getTabCode()); } SrSubject dbData = subjectMapper.selectByPrimaryKey(model.getSubId()); int result = 0; if (dbData == null) { model.setSubId(UUID.randomUUID().toString()); model.setStatus(1); model.setCreatedBy(userId); model.setCreateTime(new Date()); model.setModiTime(new Date()); result = subjectMapper.insert(model); } else { model.setModiTime(new Date()); result = subjectMapper.updateByPrimaryKey(model); } if (fieldList != null && fieldList.size() > 0) { SrSubjectfieldExample example = new SrSubjectfieldExample(); SrSubjectfieldExample.Criteria criteria = example.or(); criteria.andSubIdEqualTo(model.getSubId()); List dbList = subjectfieldMapper.selectByExample(example); List ids = fieldList.stream().map(x -> x.getFieldId()).collect(Collectors.toList()); criteria.andFieldIdNotIn(ids); subjectfieldMapper.deleteByExample(example); fieldList.stream().forEach(x -> { long count = dbList.stream().filter(f -> f.getFieldId() == x.getFieldId()).count(); //不是外键字段编码和显示别名需一致 x.setFieldAlias(x.getIsForeignKey() == 0 ? x.getFieldCode() : x.getFieldAlias()); if (count > 0) { subjectfieldMapper.updateByPrimaryKeySelective(x); } else { x.setSubId(model.getSubId()); x.setFieldId(UUID.randomUUID().toString()); x.setStatus(1); subjectfieldMapper.insertSelective(x); } }); } return result; } @Override public List getFieldList(String subjectId) { SrSubjectfieldExample example = new SrSubjectfieldExample(); SrSubjectfieldExample.Criteria criteria = example.or(); criteria.andSubIdEqualTo(subjectId); List dbList = subjectfieldMapper.selectByExample(example); return dbList; } @Override public int delete(List idList) { try { SrSubjectfieldExample example = new SrSubjectfieldExample(); SrSubjectfieldExample.Criteria criteria = example.or(); criteria.andSubIdIn(idList); subjectfieldMapper.selectByExample(example); SrSubjectExample subExp = new SrSubjectExample(); subExp.or().andSubIdIn(idList); subjectMapper.deleteByExample(subExp); } catch (Exception e) { return 0; } return 1; } @Override public List importData(List dataList, String loginUserID) { if (dataList.size() <= 0) throw new BaseException("", "请添加导入数据!"); List dicQueryTypeList = dictionaryService.getDictionaryItemList("queryType"); List dicSettingTypeList = dictionaryService.getDictionaryItemList("settingType"); dataList.forEach(item -> { String errorInfo = ""; SysDictionaryItem dicQueryType = dicQueryTypeList.stream().filter(e -> e.getName().equals(item.getQueryTypeName())).findFirst().orElse(null); SysDictionaryItem dicSettingType = dicSettingTypeList.stream().filter(e -> e.getName().equals(item.getSettingTypeName())).findFirst().orElse(null); if (stringUtils.IsNullOrEmpty(item.getFieldCode())) errorInfo += "请填写字段编码!"; if (stringUtils.IsNullOrEmpty(item.getFieldName())) errorInfo += "请填写字段名称!"; if (stringUtils.IsNullOrEmpty(item.getSettingTypeName())) errorInfo += "请填写配置类型!"; if (stringUtils.IsNullOrEmpty(item.getDataType())) errorInfo += "请填写数据类型!"; if ("字典值".equals(item.getDataType())) { if (stringUtils.IsNullOrEmpty(item.getDictionaryCode())) errorInfo += "查询类型为字典值请填写取数字典编码!"; } if (item.getDisOrder() == null) errorInfo += "请填写显示排序!"; if (stringUtils.IsNullOrEmpty(item.getIsSearchFieldText())) errorInfo += "请填写是否查询字段!"; else if (!Constant.YES.equals(item.getIsSearchFieldText()) && !Constant.No.equals(item.getIsSearchFieldText())) { errorInfo += "是否查询字段请填是或者否!"; } if (stringUtils.IsNullOrEmpty(item.getIsForeignKeyText())) errorInfo += "请填写是否关联字段!"; else if (!Constant.YES.equals(item.getIsForeignKeyText()) && !Constant.No.equals(item.getIsForeignKeyText())) { errorInfo += "是否关联字段请填是或者否!"; } if (Constant.YES.equals(item.getIsForeignKeyText())) { if (stringUtils.IsNullOrEmpty(item.getFieldAlias())) errorInfo += "关联字段请填写显示别名!"; if (stringUtils.IsNullOrEmpty(item.getReferencesTab())) errorInfo += "关联字段请填写外键表!"; if (stringUtils.IsNullOrEmpty(item.getReferencesTab())) errorInfo += "关联字段请填写外键列!"; if (stringUtils.IsNullOrEmpty(item.getReferencesTab())) errorInfo += "关联字段请填写外键表显示列!"; } if (dicSettingType == null) errorInfo += "配置类型数据有误!"; if (dicQueryType == null) errorInfo += "查询类型不在支持范围!"; if (stringUtils.IsNullOrEmpty(errorInfo)) { item.setQueryTypeId(dicQueryType.getValue()); item.setSettingTypeId(dicSettingType.getValue()); item.setIsSearchField(Constant.YES.equals(item.getIsSearchFieldText()) ? 1 : 0); item.setIsForeignKey(Constant.YES.equals(item.getIsForeignKeyText()) ? 1 : 0); } item.setErrorMessage(errorInfo); }); return dataList; } }