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.QuestionPaperQuestionMasterRelationMapper; import com.farriver.bwf.data.master.model.QuestionPaperQuestionMasterRelation; import com.farriver.bwf.data.master.model.QuestionPaperQuestionMasterRelationExample; import com.farriver.bwf.data.transferobject.queryobject.question.QuestionPaperQuestionMasterRelationQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.question.*; 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.*; @Service @CacheConfig(cacheNames = "bwf_QuestionPaperQuestionMasterRelation_cache") public class QuestionPaperQuestionMasterRelationService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(QuestionPaperQuestionMasterRelationService.class); private String message; @Resource QuestionPaperQuestionMasterRelationMapper mapper; public Map GetList(QuestionPaperQuestionMasterRelationQueryObject queryObject) { if (queryObject == null) return null; QuestionPaperQuestionMasterRelationExample example = new QuestionPaperQuestionMasterRelationExample(); QuestionPaperQuestionMasterRelationExample.Criteria criteria = example.createCriteria(); if (queryObject.getPaperid() != null && !queryObject.getPaperid().isEmpty()) { criteria.andPaperidEqualTo(queryObject.getPaperid()); } //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andQuestioncodeLike("%" + queryObject.getFilter() + "%"); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getQuestionid() != null && !queryObject.getQuestionid().isEmpty()) { criteria.andPaperidEqualTo(queryObject.getQuestionid()); } if (queryObject.getQuestioncode() != null && !queryObject.getQuestioncode().isEmpty()) { criteria.andQuestioncodeLike("%" + queryObject.getQuestioncode() + "%"); } if (queryObject.getQuestionname() != null && !queryObject.getQuestionname().isEmpty()) { criteria.andQuestionnameLike("%" + queryObject.getQuestionname() + "%"); } if (queryObject.getPaperid() != null && !queryObject.getPaperid().isEmpty()) { criteria.andPaperidEqualTo(queryObject.getPaperid()); } if (queryObject.getPapercode() != null && !queryObject.getPapercode().isEmpty()) { criteria.andPapercodeLike("%" + queryObject.getPapercode() + "%"); } if (queryObject.getPapername() != null && !queryObject.getPapername().isEmpty()) { criteria.andPapernameLike("%" + queryObject.getPapername() + "%"); } if (queryObject.getSubjectid() != null && !queryObject.getSubjectid().isEmpty()) { criteria.andSubjectidEqualTo(queryObject.getSubjectid()); } if (queryObject.getSubjectcode() != null && !queryObject.getSubjectcode().isEmpty()) { criteria.andSubjectcodeLike("%" + queryObject.getSubjectcode() + "%"); } if (queryObject.getSubjectname() != null && !queryObject.getSubjectname().isEmpty()) { criteria.andSubjectnameLike("%" + queryObject.getSubjectname() + "%"); } 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 QuestionPaperQuestionMasterRelation GetDetail(QuestionPaperQuestionMasterRelationQueryObject 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(QuestionPaperQuestionMasterRelationViewModel model) { QuestionPaperQuestionMasterRelation 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(QuestionPaperQuestionsViewModel model) { if (model == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } QuestionSubjectViewModel subject = model.getSubject(); if (subject == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } QuestionPaperViewModel paper = model.getPaper(); if (paper == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } List questions = model.getQuestions(); if (questions == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } List oldRelation = new ArrayList<>(); QuestionPaperQuestionMasterRelationQueryObject queryObject = new QuestionPaperQuestionMasterRelationQueryObject(); queryObject.setPaperid(paper.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; questions = questions.stream() .filter(e -> finalOldRelation.stream().noneMatch(r -> r.getQuestionid().equals(e.getId()))) .toList(); } for (QuestionMasterViewModel question : questions) { QuestionPaperQuestionMasterRelationViewModel newRelation = new QuestionPaperQuestionMasterRelationViewModel(); newRelation.setPaperid(paper.getId()); newRelation.setPapercode(paper.getCode()); newRelation.setPapername(paper.getName()); newRelation.setQuestionid(question.getId()); newRelation.setQuestioncode(question.getCode()); newRelation.setQuestionname(question.getName()); newRelation.setQuetioncategory(question.getCategoryname()); newRelation.setSubjectid(subject.getId()); newRelation.setSubjectcode(subject.getCode()); newRelation.setSubjectname(subject.getName()); QuestionPaperQuestionMasterRelation entity = Check(ActionType.CREATE, newRelation); mapper.insertSelective(entity); } return ApiData.ok(LangConstants.MSG_INFO_SUCCESS); } @CacheEvict(allEntries = true) public ApiData Update(QuestionPaperQuestionMasterRelationViewModel model) { QuestionPaperQuestionMasterRelation 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 (QuestionPaperQuestionMasterRelationViewModel 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 (QuestionPaperQuestionMasterRelationViewModel model : models) { Update(model); } } @Cacheable public QuestionPaperQuestionMasterRelationViewModel GetViewDetail(String paperid) { if (paperid == null) { return null; } QuestionPaperQuestionMasterRelationQueryObject queryObject = new QuestionPaperQuestionMasterRelationQueryObject(); queryObject.setPaperid(paperid); QuestionPaperQuestionMasterRelation entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public QuestionPaperQuestionMasterRelationViewModel GetViewDetail(QuestionPaperQuestionMasterRelationQueryObject queryObject) { QuestionPaperQuestionMasterRelation entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(QuestionPaperQuestionMasterRelationQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (QuestionPaperQuestionMasterRelation entity : list) { QuestionPaperQuestionMasterRelationViewModel 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 paperid) { if (paperid == null) { return ApiPageData.error(LangConstants.MSG_ERROR_PARAMETERS); } QuestionPaperQuestionMasterRelationQueryObject queryObject = new QuestionPaperQuestionMasterRelationQueryObject(); queryObject.setPaperid(paperid); Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (QuestionPaperQuestionMasterRelation entity : list) { QuestionPaperQuestionMasterRelationViewModel 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 QuestionPaperQuestionMasterRelationViewModel BuildViewModel(QuestionPaperQuestionMasterRelation entity) { if (entity == null) return null; QuestionPaperQuestionMasterRelationViewModel model = new QuestionPaperQuestionMasterRelationViewModel(); model.setId(entity.getId()); model.setIsdeleted(entity.getIsdeleted()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setStatus(entity.getStatus()); model.setPaperid(entity.getPaperid()); model.setPapercode(entity.getPapercode()); model.setPapername(entity.getPapername()); model.setQuestionid(entity.getQuestionid()); model.setQuestioncode(entity.getQuestioncode()); model.setQuestionname(entity.getQuestionname()); model.setQuetioncategory(entity.getQuetioncategory()); model.setSubjectid(entity.getSubjectid()); model.setSubjectcode(entity.getSubjectcode()); model.setSubjectname(entity.getSubjectname()); return model; } public QuestionPaperQuestionMasterRelation Check(ActionType actionType, QuestionPaperQuestionMasterRelationViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } if (model.getPaperid() == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } QuestionPaperQuestionMasterRelation entity = null; if (ActionType.CREATE == actionType) { entity = new QuestionPaperQuestionMasterRelation(); 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 { QuestionPaperQuestionMasterRelationQueryObject queryObject = new QuestionPaperQuestionMasterRelationQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setStatus(model.getStatus()); entity.setPaperid(model.getPaperid()); entity.setPapercode(model.getPapercode()); entity.setPapername(model.getPapername()); entity.setQuestionid(model.getQuestionid()); entity.setQuestioncode(model.getQuestioncode()); entity.setQuestionname(model.getQuestionname()); entity.setQuetioncategory(model.getQuetioncategory()); entity.setSubjectid(model.getSubjectid()); entity.setSubjectcode(model.getSubjectcode()); entity.setSubjectname(model.getSubjectname()); return entity; } }