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.QuestionPaperMapper; import com.farriver.bwf.data.master.model.QuestionPaper; import com.farriver.bwf.data.master.model.QuestionPaperExample; import com.farriver.bwf.data.transferobject.queryobject.question.QuestionPaperQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.question.QuestionPaperViewModel; import com.farriver.bwf.service.ServiceBase; import org.jetbrains.annotations.Nullable; 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_QuestionPaper_cache") public class QuestionPaperService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(QuestionPaperService.class); private String message; @Resource QuestionPaperMapper mapper; public Map GetList(QuestionPaperQueryObject queryObject) { if (queryObject == null) return null; QuestionPaperExample example = new QuestionPaperExample(); QuestionPaperExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andNameLike("%" + queryObject.getFilter() + "%"); QuestionPaperExample.Criteria criteria2 = example.createCriteria(); criteria2.andCodeLike("%" + queryObject.getFilter() + "%"); example.or(criteria2); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getCode() != null && !queryObject.getCode().isEmpty()) { criteria.andCodeLike("%" + queryObject.getCode() + "%"); } if (queryObject.getName() != null && !queryObject.getName().isEmpty()) { criteria.andNameLike("%" + queryObject.getName() + "%"); } if (queryObject.getSubjectid() != null && !queryObject.getSubjectid().isEmpty()) { criteria.andSubjectidEqualTo(queryObject.getSubjectid()); } if (queryObject.getSubjectname() != null && !queryObject.getSubjectname().isEmpty()) { criteria.andSubjectnameLike("%" + queryObject.getSubjectname() + "%"); } if (queryObject.getCategoryid() != null && !queryObject.getCategoryid().isEmpty()) { criteria.andCategoryidEqualTo(queryObject.getCategoryid()); } if (queryObject.getCategoryname() != null && !queryObject.getCategoryname().isEmpty()) { criteria.andCategorynameLike("%" + queryObject.getCategoryname() + "%"); } if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getSubjectid() != null && !queryObject.getSubjectid().isEmpty()) { criteria.andSubjectidEqualTo(queryObject.getSubjectid()); } } //查询总数 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 QuestionPaper GetDetail(QuestionPaperQueryObject 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(QuestionPaperViewModel model) { QuestionPaper entity = Check(ActionType.CREATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, mapper.insertSelective(entity)); } else { return ApiData.error(message); } } @CacheEvict(allEntries = true) public ApiData Update(QuestionPaperViewModel model) { QuestionPaper 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(QuestionPaperQueryObject queryObject) { Map map = GetList(queryObject); Object object = map.get(MapDataType.DATA_LIST); int count = 0; if (object != null) { List list = (List) object; if (list != null && list.size() == 1) { count += mapper.deleteByPrimaryKey(list.get(0).getId()); } } return ApiData.ok("", count); } @Cacheable public QuestionPaperViewModel GetViewDetail(String id) { QuestionPaperQueryObject queryObject = new QuestionPaperQueryObject(); queryObject.setId(id); QuestionPaper entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(QuestionPaperQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (QuestionPaper entity : list) { QuestionPaperViewModel 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 QuestionPaperViewModel BuildViewModel(QuestionPaper entity) { if (entity == null) return null; QuestionPaperViewModel model = new QuestionPaperViewModel(); model.setId(entity.getId()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setStatus(entity.getStatus()); model.setCode(entity.getCode()); model.setName(entity.getName()); model.setSubjectid(entity.getSubjectid()); model.setSubjectname(entity.getSubjectname()); model.setSubjectcode(entity.getSubjectcode()); model.setCategoryid(entity.getCategoryid()); model.setCategoryname(entity.getCategoryname()); model.setType(entity.getType()); model.setPrice(entity.getPrice()); model.setCoverimage(entity.getCoverimage()); return model; } public QuestionPaper Check(ActionType actionType, QuestionPaperViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } QuestionPaper entity = null; if (ActionType.CREATE == actionType) { entity = new QuestionPaper(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); entity.setIsdeleted(false); String oldCode = GetLatestCode(); String newCode = generateCode(oldCode); if (newCode == null) return null; entity.setCode(newCode); } else { QuestionPaperQueryObject queryObject = new QuestionPaperQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setStatus(model.getStatus()); entity.setName(model.getName()); entity.setSubjectid(model.getSubjectid()); entity.setSubjectname(model.getSubjectname()); entity.setSubjectcode(model.getSubjectcode()); entity.setCategoryid(model.getCategoryid()); entity.setCategoryname(model.getCategoryname()); entity.setRemark(model.getRemark()); entity.setType(model.getType());; entity.setPrice(model.getPrice()); entity.setCoverimage(model.getCoverimage()); return entity; } @Nullable public String generateCode(String oldCode) { String preNo = "P"; int _newCodeNum = 1; try { int _oldCodeNum = 0; if (oldCode != null && !oldCode.equals("") && oldCode.length() > 2) { String _oldNoNumString = oldCode.substring(2); _oldCodeNum = Integer.parseInt(_oldNoNumString); } if (_oldCodeNum != 0) { int a = _oldCodeNum + 1; _newCodeNum = a; } } catch (NumberFormatException e) { e.printStackTrace(); message = LangConstants.MSG_ERROR_SYSTEM; return null; } return String.format("%s%07d", preNo, _newCodeNum); } public String GetLatestCode() { String oldCode = ""; QuestionPaperExample example = new QuestionPaperExample(); example.setOrderByClause("code desc"); example.setRows(1); example.setOffset(0); List list = mapper.selectByExample(example); if (list != null && list.size() > 0) { QuestionPaper entity2 = list.get(0); if (entity2 != null) { oldCode = entity2.getCode(); } } return oldCode; } }