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.QuestionMasterOptionMapper; import com.farriver.bwf.data.master.model.QuestionMasterOption; import com.farriver.bwf.data.master.model.QuestionMasterOptionExample; import com.farriver.bwf.data.transferobject.queryobject.question.QuestionMasterOptionQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.question.QuestionMasterOptionViewModel; 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_QuestionMasterOption_cache") public class QuestionMasterOptionService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(QuestionMasterOptionService.class); @Resource QuestionMasterOptionMapper mapper; private String message; public Map GetList(QuestionMasterOptionQueryObject queryObject) { if (queryObject == null) return null; QuestionMasterOptionExample example = new QuestionMasterOptionExample(); QuestionMasterOptionExample.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 QuestionMasterOption GetDetail(QuestionMasterOptionQueryObject 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(QuestionMasterOptionViewModel model) { QuestionMasterOption 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 (QuestionMasterOptionViewModel model : models) { QuestionMasterOption 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(QuestionMasterOptionViewModel model) { QuestionMasterOption 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); QuestionMasterOptionQueryObject queryObject = new QuestionMasterOptionQueryObject(); 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 QuestionMasterOptionViewModel GetViewDetail(QuestionMasterOptionQueryObject queryObject) { QuestionMasterOption entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(QuestionMasterOptionQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (QuestionMasterOption entity : list) { QuestionMasterOptionViewModel 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 QuestionMasterOptionViewModel BuildViewModel(QuestionMasterOption entity) { if (entity == null) return null; QuestionMasterOptionViewModel model = new QuestionMasterOptionViewModel(); 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 QuestionMasterOption Check(ActionType actionType, QuestionMasterOptionViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } QuestionMasterOption entity = null; if (ActionType.CREATE == actionType) { entity = new QuestionMasterOption(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); } else { QuestionMasterOptionQueryObject queryObject = new QuestionMasterOptionQueryObject(); 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; } }