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.VIPCouponHistoryMapper; import com.farriver.bwf.data.master.model.VIPCouponHistory; import com.farriver.bwf.data.master.model.VIPCouponHistoryExample; import com.farriver.bwf.data.transferobject.queryobject.vip.VIPCouponHistoryQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.vip.VIPCouponHistoryViewModel; 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_VIPCouponHistory_cache") public class VIPCouponHistoryService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(VIPCouponHistoryService.class); private String message; @Resource VIPCouponHistoryMapper mapper; public Map GetList(VIPCouponHistoryQueryObject queryObject) { if (queryObject == null) return null; VIPCouponHistoryExample example = new VIPCouponHistoryExample(); VIPCouponHistoryExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andCodeLike("%" + 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.getCode() != null && !queryObject.getCode().isEmpty()) { criteria.andCodeLike("%" + queryObject.getCode() + "%"); } 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); } 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 VIPCouponHistory GetDetail(VIPCouponHistoryQueryObject 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(VIPCouponHistoryViewModel model) { VIPCouponHistory 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(VIPCouponHistoryViewModel model) { VIPCouponHistory 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(VIPCouponHistoryQueryObject 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 VIPCouponHistoryViewModel GetViewDetail(String id) { VIPCouponHistoryQueryObject queryObject = new VIPCouponHistoryQueryObject(); queryObject.setId(id); VIPCouponHistory entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public VIPCouponHistoryViewModel GetViewDetail(VIPCouponHistoryQueryObject queryObject) { VIPCouponHistory entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(VIPCouponHistoryQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (VIPCouponHistory entity : list) { VIPCouponHistoryViewModel 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 VIPCouponHistoryViewModel BuildViewModel(VIPCouponHistory entity) { if (entity == null) return null; VIPCouponHistoryViewModel model = new VIPCouponHistoryViewModel(); model.setId(entity.getId()); model.setCode(entity.getCode()); model.setType(entity.getType()); model.setActivityname(entity.getActivityname()); model.setDistributioncondition(entity.getDistributioncondition()); model.setAmount(entity.getAmount()); model.setDiscount(entity.getDiscount()); model.setDiscountrate(entity.getDiscountrate()); model.setDiscountrate(entity.getDiscountrate()); model.setValiddatefrom(entity.getValiddatefrom()); model.setValiddateto(entity.getValiddateto()); model.setIssuingtime(entity.getIssuingtime()); model.setUsagetime(entity.getUsagetime()); model.setOrderno(entity.getOrderno()); model.setMasterid(entity.getMasterid()); model.setMastercode(entity.getMastercode()); model.setMastername(entity.getMastername()); model.setStatus(entity.getStatus()); model.setIsdeleted(entity.getIsdeleted()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setRemark(entity.getRemark()); return model; } public VIPCouponHistory Check(ActionType actionType, VIPCouponHistoryViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } VIPCouponHistory entity = null; if (ActionType.CREATE == actionType) { VIPCouponHistoryQueryObject queryObject = new VIPCouponHistoryQueryObject(); queryObject.setCode(model.getCode()); entity = GetDetail(queryObject); if (entity != null) { message = LangConstants.MSG_ERROR_CELLPHONE_EXISTED; return null; } entity = new VIPCouponHistory(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); entity.setIsdeleted(false); } else { VIPCouponHistoryQueryObject queryObject = new VIPCouponHistoryQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setCode(model.getCode()); entity.setType(model.getType()); entity.setActivityname(model.getActivityname()); entity.setDistributioncondition(model.getDistributioncondition()); entity.setAmount(model.getAmount()); entity.setDiscount(model.getDiscount()); entity.setDiscountrate(model.getDiscountrate()); entity.setDiscountrate(model.getDiscountrate()); entity.setValiddatefrom(model.getValiddatefrom()); entity.setValiddateto(model.getValiddateto()); entity.setIssuingtime(model.getIssuingtime()); entity.setUsagetime(model.getUsagetime()); entity.setOrderno(model.getOrderno()); entity.setMasterid(model.getMasterid()); entity.setMastercode(model.getMastercode()); entity.setMastername(model.getMastername()); entity.setStatus(model.getStatus()); entity.setRemark(model.getRemark()); entity.setIsdeleted(model.getIsdeleted()); return entity; } }