package com.farriver.bwf.service.vip; 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.VIPMasterOrganizationMapper; import com.farriver.bwf.data.master.model.VIPMasterOrganization; import com.farriver.bwf.data.master.model.VIPMasterOrganizationExample; import com.farriver.bwf.data.transferobject.queryobject.vip.VIPMasterOrganizationQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.vip.VIPMasterOrganizationViewModel; 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_VIPMasterOrganization_cache") public class VIPMasterOrganizationService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(VIPMasterOrganizationService.class); private String message; @Resource VIPMasterOrganizationMapper mapper; public Map GetList(VIPMasterOrganizationQueryObject queryObject) { if (queryObject == null) return null; VIPMasterOrganizationExample example = new VIPMasterOrganizationExample(); VIPMasterOrganizationExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andMasternameLike("%" + queryObject.getFilter() + "%"); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getMasterid() != null && !queryObject.getMasterid().isEmpty()) { criteria.andMasteridEqualTo(queryObject.getMasterid()); } if (queryObject.getMastercode() != null && !queryObject.getMastercode().isEmpty()) { criteria.andMastercodeLike("%" + queryObject.getMastercode() + "%"); } if (queryObject.getMastername() != null && !queryObject.getMastername().isEmpty()) { criteria.andMasternameLike("%" + queryObject.getMastername() + "%"); } if (queryObject.getOrganizationid() != null && !queryObject.getOrganizationid().isEmpty()) { criteria.andOrganizationidEqualTo(queryObject.getOrganizationid()); } if (queryObject.getOrganizationcode() != null && !queryObject.getOrganizationcode().isEmpty()) { criteria.andOrganizationcodeLike("%" + queryObject.getOrganizationcode() + "%"); } if (queryObject.getOrganizationname() != null && !queryObject.getOrganizationname().isEmpty()) { criteria.andOrganizationnameLike("%" + queryObject.getOrganizationname() + "%"); } if (queryObject.getDepartmentid() != null && !queryObject.getDepartmentid().isEmpty()) { criteria.andDepartmentidEqualTo(queryObject.getDepartmentid()); } if (queryObject.getDepartmentcode() != null && !queryObject.getDepartmentcode().isEmpty()) { criteria.andDepartmentcodeLike("%" + queryObject.getDepartmentcode() + "%"); } if (queryObject.getDepartmentname() != null && !queryObject.getDepartmentname().isEmpty()) { criteria.andDepartmentnameLike("%" + queryObject.getDepartmentname() + "%"); } if (queryObject.getMajorid() != null && !queryObject.getMajorid().isEmpty()) { criteria.andMajoridEqualTo(queryObject.getMajorid()); } if (queryObject.getMajorcode() != null && !queryObject.getMajorcode().isEmpty()) { criteria.andMajorcodeLike("%" + queryObject.getMajorcode() + "%"); } if (queryObject.getMajorname() != null && !queryObject.getMajorname().isEmpty()) { criteria.andMajornameLike("%" + queryObject.getMajorname() + "%"); } if (queryObject.getGradeid() != null && !queryObject.getGradeid().isEmpty()) { criteria.andGradeidEqualTo(queryObject.getGradeid()); } if (queryObject.getGradecode() != null && !queryObject.getGradecode().isEmpty()) { criteria.andGradecodeLike("%" + queryObject.getGradecode() + "%"); } if (queryObject.getGradename() != null && !queryObject.getGradename().isEmpty()) { criteria.andGradenameLike("%" + queryObject.getGradename() + "%"); } if (queryObject.getClassid() != null && !queryObject.getClassid().isEmpty()) { criteria.andClassidEqualTo(queryObject.getClassid()); } if (queryObject.getClasscode() != null && !queryObject.getClasscode().isEmpty()) { criteria.andClasscodeLike("%" + queryObject.getClasscode() + "%"); } if (queryObject.getClassname() != null && !queryObject.getClassname().isEmpty()) { criteria.andClassnameLike("%" + queryObject.getClassname() + "%"); } } //查询总数 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 VIPMasterOrganization GetDetail(VIPMasterOrganizationQueryObject 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(VIPMasterOrganizationViewModel model) { VIPMasterOrganization 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(VIPMasterOrganizationViewModel model) { VIPMasterOrganization 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(VIPMasterOrganizationQueryObject 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); } @CacheEvict(allEntries = true) public ApiData SetVIPOrganization(VIPMasterOrganizationViewModel model) { if (model == null || model.getMasterid().isEmpty()) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } VIPMasterOrganizationQueryObject queryObject = new VIPMasterOrganizationQueryObject(); queryObject.setMasterid(model.getMasterid()); VIPMasterOrganization vipMasterOrganization = GetDetail(queryObject); if (vipMasterOrganization != null) { model.setId(vipMasterOrganization.getId()); if (vipMasterOrganization.getUpdatecount() == null) { model.setUpdatecount(1); } else { model.setUpdatecount(vipMasterOrganization.getUpdatecount() + 1); } return Update(model); } else { model.setUpdatecount(0); return Create(model); } } @Cacheable public VIPMasterOrganizationViewModel GetViewDetail(String id) { VIPMasterOrganizationQueryObject queryObject = new VIPMasterOrganizationQueryObject(); queryObject.setId(id); VIPMasterOrganization entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public VIPMasterOrganizationViewModel GetViewDetailByMasterId(String masterid) { VIPMasterOrganizationQueryObject queryObject = new VIPMasterOrganizationQueryObject(); queryObject.setMasterid(masterid); VIPMasterOrganization entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public VIPMasterOrganizationViewModel GetViewDetail(VIPMasterOrganizationQueryObject queryObject) { VIPMasterOrganization entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(VIPMasterOrganizationQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (VIPMasterOrganization entity : list) { VIPMasterOrganizationViewModel 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 VIPMasterOrganizationViewModel BuildViewModel(VIPMasterOrganization entity) { if (entity == null) return null; VIPMasterOrganizationViewModel model = new VIPMasterOrganizationViewModel(); model.setId(entity.getId()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setMasterid(entity.getMasterid()); model.setMastercode(entity.getMastercode()); model.setMastername(entity.getMastername()); model.setOrganizationid(entity.getOrganizationid()); model.setOrganizationname(entity.getOrganizationname()); model.setOrganizationcode(entity.getOrganizationcode()); model.setDepartmentid(entity.getDepartmentid()); model.setDepartmentname(entity.getDepartmentname()); model.setDepartmentcode(entity.getDepartmentcode()); model.setMajorid(entity.getMajorid()); model.setMajorname(entity.getMajorname()); model.setMajorcode(entity.getMajorcode()); model.setGradeid(entity.getGradeid()); model.setGradename(entity.getGradename()); model.setGradecode(entity.getGradecode()); model.setClassid(entity.getClassid()); model.setClassname(entity.getClassname()); model.setClasscode(entity.getClasscode()); model.setUpdatecount(entity.getUpdatecount()); return model; } public VIPMasterOrganization Check(ActionType actionType, VIPMasterOrganizationViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } VIPMasterOrganization entity = null; if (ActionType.CREATE == actionType) { entity = new VIPMasterOrganization(); 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 { VIPMasterOrganizationQueryObject queryObject = new VIPMasterOrganizationQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setRemark(model.getRemark()); entity.setMasterid(model.getMasterid()); entity.setMastercode(model.getMastercode()); entity.setMastername(model.getMastername()); entity.setOrganizationid(model.getOrganizationid()); entity.setOrganizationname(model.getOrganizationname()); entity.setOrganizationcode(model.getOrganizationcode()); entity.setDepartmentid(model.getDepartmentid()); entity.setDepartmentname(model.getDepartmentname()); entity.setDepartmentcode(model.getDepartmentcode()); entity.setMajorid(model.getMajorid()); entity.setMajorname(model.getMajorname()); entity.setMajorcode(model.getMajorcode()); entity.setGradeid(model.getGradeid()); entity.setGradename(model.getGradename()); entity.setGradecode(model.getGradecode()); entity.setClassid(model.getClassid()); entity.setClassname(model.getClassname()); entity.setClasscode(model.getClasscode()); entity.setUpdatecount(model.getUpdatecount()); return entity; } }