package com.farriver.bwf.service.question; import com.farriver.bwf.common.model.ApiData; import com.farriver.bwf.common.model.ApiPageData; import com.farriver.bwf.common.options.ActionType; import com.farriver.bwf.common.options.MapDataType; import com.farriver.bwf.common.statics.LangConstants; import com.farriver.bwf.data.master.mapper.QuestionSubjectGradeRelationMapper; import com.farriver.bwf.data.master.model.QuestionSubjectGradeRelation; import com.farriver.bwf.data.master.model.QuestionSubjectGradeRelationExample; import com.farriver.bwf.data.transferobject.queryobject.question.QuestionSubjectGradeRelationQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.organization.OrganizationGradeViewModel; import com.farriver.bwf.data.transferobject.viewmodel.question.QuestionGradeSubjectsViewModel; import com.farriver.bwf.data.transferobject.viewmodel.question.QuestionSubjectGradeRelationViewModel; import com.farriver.bwf.data.transferobject.viewmodel.question.QuestionSubjectViewModel; import com.farriver.bwf.service.ServiceBase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cache.annotation.CacheConfig; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import jakarta.annotation.Resource; import java.util.*; import java.util.stream.Collectors; @Service @CacheConfig(cacheNames = "bwf_QuestionSubjectGradeRelation_cache") public class QuestionSubjectGradeRelationService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(QuestionSubjectGradeRelationService.class); private String message; @Resource QuestionSubjectGradeRelationMapper mapper; public Map GetList(QuestionSubjectGradeRelationQueryObject queryObject) { if (queryObject == null) return null; QuestionSubjectGradeRelationExample example = new QuestionSubjectGradeRelationExample(); QuestionSubjectGradeRelationExample.Criteria criteria = example.createCriteria(); if (queryObject.getSubjectid() != null && !queryObject.getSubjectid().isEmpty()) { criteria.andSubjectidEqualTo(queryObject.getSubjectid()); } //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andSubjectcodeLike("%" + queryObject.getFilter() + "%"); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getSubjectcode() != null && !queryObject.getSubjectcode().isEmpty()) { criteria.andSubjectcodeLike("%" + queryObject.getSubjectcode() + "%"); } if (queryObject.getSubjectname() != null && !queryObject.getSubjectname().isEmpty()) { criteria.andSubjectnameLike("%" + queryObject.getSubjectname() + "%"); } if (queryObject.getGradeid() != null && !queryObject.getGradeid().isEmpty()) { criteria.andGradeidEqualTo(queryObject.getGradeid()); } if (queryObject.getGradecode() != null && !queryObject.getGradecode().isEmpty()) { criteria.andGradecodeLike("%" + queryObject.getGradecode() + "%"); } if (queryObject.getGradename() != null && !queryObject.getGradename().isEmpty()) { criteria.andGradenameLike("%" + queryObject.getGradename() + "%"); } if (queryObject.getStatus() != null && queryObject.getStatus() >= 0) { criteria.andStatusEqualTo(queryObject.getStatus()); } if (queryObject.getStatuslist() != null && queryObject.getStatuslist().size() > 0) { criteria.andStatusIn(queryObject.getStatuslist()); } } //查询总数 long total = mapper.countByExample(example); //分页 int pageIndex = queryObject.getPageIndex(); int pageSize = queryObject.getPageSize(); if (pageIndex > 0 && pageSize > 0) { example.setRows(pageSize); example.setOffset((pageIndex - 1) * pageSize); } example.setOrderByClause("createtime desc"); List list = mapper.selectByExample(example); Map map = new HashMap<>(); map.put(MapDataType.DATA_LIST, list); map.put(MapDataType.TOTAL_COUNT, total); map.put(MapDataType.PAGE_INDEX, pageIndex); map.put(MapDataType.PAGE_SIZE, pageSize); return map; } public QuestionSubjectGradeRelation GetDetail(QuestionSubjectGradeRelationQueryObject queryObject) { Map map = GetList(queryObject); Object object = map.get(MapDataType.DATA_LIST); if (object != null) { List list = (List) object; if (list != null && list.size() == 1) return list.get(0); } return null; } @CacheEvict(allEntries = true) public ApiData Create(QuestionSubjectGradeRelationViewModel model) { QuestionSubjectGradeRelation entity = Check(ActionType.CREATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, mapper.insertSelective(entity)); } else { return ApiData.error(message); } } @Transactional @CacheEvict(allEntries = true) public ApiData BatchCreate(QuestionGradeSubjectsViewModel model) { if (model == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } OrganizationGradeViewModel grade = model.getGrade(); if (grade == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } List subjects = model.getSubjects(); if (subjects == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } List oldRelation = new ArrayList<>(); QuestionSubjectGradeRelationQueryObject queryObject = new QuestionSubjectGradeRelationQueryObject(); queryObject.setGradeid(grade.getId()); Map map = GetList(queryObject); Object object = map.get(MapDataType.DATA_LIST); if (object != null) { oldRelation = (List) object; } if (oldRelation != null && oldRelation.size() > 0) { List finalOldRelation = oldRelation; subjects = subjects.stream() .filter(e -> !finalOldRelation.stream().anyMatch(r -> r.getSubjectid().equals(e.getId()))) .collect(Collectors.toList()); } for (QuestionSubjectViewModel subject : subjects) { QuestionSubjectGradeRelationViewModel newRelation = new QuestionSubjectGradeRelationViewModel(); newRelation.setGradeid(grade.getId()); newRelation.setGradecode(grade.getCode()); newRelation.setGradename(grade.getName()); newRelation.setSubjectid(subject.getId()); newRelation.setSubjectcode(subject.getCode()); newRelation.setSubjectname(subject.getName()); QuestionSubjectGradeRelation entity = Check(ActionType.CREATE, newRelation); mapper.insertSelective(entity); } return ApiData.ok(LangConstants.MSG_INFO_SUCCESS); } @CacheEvict(allEntries = true) public ApiData Update(QuestionSubjectGradeRelationViewModel model) { QuestionSubjectGradeRelation entity = Check(ActionType.UPDATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, mapper.updateByPrimaryKeySelective(entity)); } else { return ApiData.error(message); } } @CacheEvict(allEntries = true) public ApiData Delete(String id) { return ApiData.ok("", mapper.deleteByPrimaryKey(id)); } @Transactional @CacheEvict(allEntries = true) public ApiData BatchDelete(List models) { if (models == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } for (QuestionSubjectGradeRelationViewModel model : models) { mapper.deleteByPrimaryKey(model.getId()); } return ApiData.ok(LangConstants.MSG_INFO_SUCCESS); } @Transactional @CacheEvict(allEntries = true) public void BatchUpdate(List models) { if (models == null) { return; } for (QuestionSubjectGradeRelationViewModel model : models) { Update(model); } } @Cacheable public QuestionSubjectGradeRelationViewModel GetViewDetail(String subjectid) { if (subjectid == null) { return null; } QuestionSubjectGradeRelationQueryObject queryObject = new QuestionSubjectGradeRelationQueryObject(); queryObject.setSubjectid(subjectid); QuestionSubjectGradeRelation entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public QuestionSubjectGradeRelationViewModel GetViewDetail(QuestionSubjectGradeRelationQueryObject queryObject) { QuestionSubjectGradeRelation entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(QuestionSubjectGradeRelationQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (QuestionSubjectGradeRelation entity : list) { QuestionSubjectGradeRelationViewModel viewModel = BuildViewModel(entity); viewModels.add(viewModel); } ApiPageData bean = ApiPageData.build(); bean.setData(viewModels); bean.setTotal((Long) map.get(MapDataType.TOTAL_COUNT)); return bean; } @Cacheable public ApiPageData GetViewPageListByPrimaryKey(String subjectid) { if (subjectid == null) { return ApiPageData.error(LangConstants.MSG_ERROR_PARAMETERS); } QuestionSubjectGradeRelationQueryObject queryObject = new QuestionSubjectGradeRelationQueryObject(); queryObject.setSubjectid(subjectid); Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (QuestionSubjectGradeRelation entity : list) { QuestionSubjectGradeRelationViewModel viewModel = BuildViewModel(entity); viewModels.add(viewModel); } ApiPageData bean = ApiPageData.build(); bean.setData(viewModels); bean.setTotal((Long) map.get(MapDataType.TOTAL_COUNT)); return bean; } //private methods private static QuestionSubjectGradeRelationViewModel BuildViewModel(QuestionSubjectGradeRelation entity) { if (entity == null) return null; QuestionSubjectGradeRelationViewModel model = new QuestionSubjectGradeRelationViewModel(); model.setId(entity.getId()); model.setIsdeleted(entity.getIsdeleted()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setStatus(entity.getStatus()); model.setSubjectid(entity.getSubjectid()); model.setSubjectcode(entity.getSubjectcode()); model.setSubjectname(entity.getSubjectname()); model.setGradeid(entity.getGradeid()); model.setGradecode(entity.getGradecode()); model.setGradename(entity.getGradename()); return model; } public QuestionSubjectGradeRelation Check(ActionType actionType, QuestionSubjectGradeRelationViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } if (model.getSubjectid() == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } QuestionSubjectGradeRelation entity = null; if (ActionType.CREATE == actionType) { entity = new QuestionSubjectGradeRelation(); if (model.getId() != null && !model.getId().isEmpty()) { entity.setId(model.getId()); } else { entity.setId(UUID.randomUUID().toString()); } entity.setCreatetime(new Date()); entity.setIsdeleted(false); } else { QuestionSubjectGradeRelationQueryObject queryObject = new QuestionSubjectGradeRelationQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setStatus(model.getStatus()); entity.setSubjectid(model.getSubjectid()); entity.setSubjectcode(model.getSubjectcode()); entity.setSubjectname(model.getSubjectname()); entity.setGradeid(model.getGradeid()); entity.setGradecode(model.getGradecode()); entity.setGradename(model.getGradename()); return entity; } }