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.QuestionSubjectMapper; import com.farriver.bwf.data.master.model.QuestionSubject; import com.farriver.bwf.data.master.model.QuestionSubjectExample; import com.farriver.bwf.data.transferobject.queryobject.question.QuestionSubjectQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.question.QuestionSubjectViewModel; 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_QuestionSubject_cache") public class QuestionSubjectService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(QuestionSubjectService.class); private String message; @Resource QuestionSubjectMapper mapper; public Map GetList(QuestionSubjectQueryObject queryObject) { if (queryObject == null) return null; QuestionSubjectExample example = new QuestionSubjectExample(); QuestionSubjectExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andNameLike("%" + queryObject.getFilter() + "%"); QuestionSubjectExample.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() + "%"); } } //查询总数 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 QuestionSubject GetDetail(QuestionSubjectQueryObject 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(QuestionSubjectViewModel model) { QuestionSubject 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(QuestionSubjectViewModel model) { QuestionSubject 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(QuestionSubjectQueryObject 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 QuestionSubjectViewModel GetViewDetail(String id) { QuestionSubjectQueryObject queryObject = new QuestionSubjectQueryObject(); queryObject.setId(id); QuestionSubject entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(QuestionSubjectQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (QuestionSubject entity : list) { QuestionSubjectViewModel 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 QuestionSubjectViewModel BuildViewModel(QuestionSubject entity) { if (entity == null) return null; QuestionSubjectViewModel model = new QuestionSubjectViewModel(); model.setId(entity.getId()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setName(entity.getName()); model.setCode(entity.getCode()); model.setStatus(entity.getStatus()); return model; } public QuestionSubject Check(ActionType actionType, QuestionSubjectViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } QuestionSubject entity = null; if (ActionType.CREATE == actionType) { entity = new QuestionSubject(); 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 { QuestionSubjectQueryObject queryObject = new QuestionSubjectQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setStatus(model.getStatus()); entity.setName(model.getName()); entity.setRemark(model.getRemark()); return entity; } @Nullable public String generateCode(String oldCode) { String preNo = "S"; 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 = ""; QuestionSubjectExample example = new QuestionSubjectExample(); example.setOrderByClause("code desc"); example.setRows(1); example.setOffset(0); List list = mapper.selectByExample(example); if (list != null && list.size() > 0) { QuestionSubject entity2 = list.get(0); if (entity2 != null) { oldCode = entity2.getCode(); } } return oldCode; } }