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.QuestionMasterAnswerMapper; import com.farriver.bwf.data.master.model.QuestionMasterAnswer; import com.farriver.bwf.data.master.model.QuestionMasterAnswerExample; import com.farriver.bwf.data.transferobject.queryobject.question.QuestionMasterAnswerQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.question.QuestionMasterAnswerViewModel; 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 jakarta.annotation.Resource; import java.util.*; @Service @CacheConfig(cacheNames = "kbs_QuestionMasterAnswer_cache") public class QuestionMasterAnswerService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(QuestionMasterAnswerService.class); @Resource QuestionMasterAnswerMapper mapper; private String message; public Map GetList(QuestionMasterAnswerQueryObject queryObject) { if (queryObject == null) ApiPageData.error(LangConstants.MSG_ERROR_PARAMETERS); QuestionMasterAnswerExample example = new QuestionMasterAnswerExample(); QuestionMasterAnswerExample.Criteria criteria = example.createCriteria(); if (queryObject.getMasterid() != null && !queryObject.getMasterid().isEmpty()) { criteria.andMasteridEqualTo(queryObject.getMasterid()); } //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andQkeyLike("%" + queryObject.getFilter() + "%"); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getQkey() != null && !queryObject.getQkey().isEmpty()) { criteria.andQkeyLike("%" + queryObject.getQkey() + "%"); } if (queryObject.getStartDateTime() != null && queryObject.getEndDateTime() != null) { criteria.andCreatetimeBetween(queryObject.getStartDateTime(), queryObject.getEndDateTime()); } } //查询总数 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); } 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 QuestionMasterAnswer GetDetail(QuestionMasterAnswerQueryObject 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(QuestionMasterAnswerViewModel model) { QuestionMasterAnswer entity = Check(ActionType.CREATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, mapper.insertSelective(entity)); } else { return ApiData.error(message); } } /* / QuestionMaster has been set @Transactional, no set @Transactional for this function. */ @CacheEvict(allEntries = true) public ApiData BatchCreate(String masterid, List models) { if (masterid == null || masterid.isEmpty()) return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); int count = 0; if (models != null && models.size() > 0) { for (QuestionMasterAnswerViewModel model : models) { QuestionMasterAnswer entity = Check(ActionType.CREATE, model); if (entity != null) { entity.setMasterid(masterid); count += mapper.insertSelective(entity); } } } return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, count); } @CacheEvict(allEntries = true) public ApiData Update(QuestionMasterAnswerViewModel model) { QuestionMasterAnswer 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)); } @CacheEvict(allEntries = true) public ApiData BatchDelete(String masterid) { if (masterid == null || masterid.isEmpty()) return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); QuestionMasterAnswerQueryObject queryObject = new QuestionMasterAnswerQueryObject(); queryObject.setMasterid(masterid); 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 QuestionMasterAnswerViewModel GetViewDetail(QuestionMasterAnswerQueryObject queryObject) { QuestionMasterAnswer entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(QuestionMasterAnswerQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (QuestionMasterAnswer entity : list) { QuestionMasterAnswerViewModel 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 public static QuestionMasterAnswerViewModel BuildViewModel(QuestionMasterAnswer entity) { if (entity == null) return null; QuestionMasterAnswerViewModel model = new QuestionMasterAnswerViewModel(); model.setId(entity.getId()); model.setRemark(entity.getRemark()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setQkey(entity.getQkey()); model.setQvalue(entity.getQvalue()); model.setSequenceno(entity.getSequenceno()); return model; } public QuestionMasterAnswer Check(ActionType actionType, QuestionMasterAnswerViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } QuestionMasterAnswer entity = null; if (ActionType.CREATE == actionType) { entity = new QuestionMasterAnswer(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); } else { QuestionMasterAnswerQueryObject queryObject = new QuestionMasterAnswerQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setRemark(model.getRemark()); entity.setQkey(model.getQkey()); entity.setQvalue(model.getQvalue()); entity.setSequenceno(model.getSequenceno()); return entity; } }