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.VIPMasterWalletMapper; import com.farriver.bwf.data.master.model.VIPMasterWallet; import com.farriver.bwf.data.master.model.VIPMasterWalletExample; import com.farriver.bwf.data.transferobject.queryobject.vip.VIPMasterWalletQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.vip.VIPMasterViewModel; import com.farriver.bwf.data.transferobject.viewmodel.vip.VIPMasterWalletChargeViewModel; import com.farriver.bwf.data.transferobject.viewmodel.vip.VIPMasterWalletRechargeViewModel; import com.farriver.bwf.data.transferobject.viewmodel.vip.VIPMasterWalletViewModel; 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.math.BigDecimal; import java.util.*; @Service @CacheConfig(cacheNames = "bwf_VIPMasterWallet_cache") public class VIPMasterWalletService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(VIPMasterWalletService.class); private String message; @Resource VIPMasterService vipMasterService; @Resource VIPMasterWalletMapper mapper; public Map GetList(VIPMasterWalletQueryObject queryObject) { if (queryObject == null) return null; VIPMasterWalletExample example = new VIPMasterWalletExample(); VIPMasterWalletExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andMastercodeLike("%" + queryObject.getFilter() + "%"); VIPMasterWalletExample.Criteria criteria2 = example.createCriteria(); criteria2.andMasternameLike("%" + queryObject.getFilter() + "%"); example.or(criteria2); } 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.getStatus() != null && queryObject.getStatus() >= 0) { criteria.andStatusEqualTo(queryObject.getStatus()); } } //查询总数 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 VIPMasterWallet GetDetail(VIPMasterWalletQueryObject 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(VIPMasterWalletViewModel model) { VIPMasterWallet 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(VIPMasterWalletViewModel model) { VIPMasterWallet 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)); } public ApiData Charge(VIPMasterWalletChargeViewModel model) { if (model == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } if (model.getMasterid() == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } VIPMasterWalletQueryObject queryObject = new VIPMasterWalletQueryObject(); queryObject.setMasterid(model.getMasterid()); VIPMasterWalletViewModel vipMasterWalletViewModel = GetViewDetail(queryObject); if (vipMasterWalletViewModel == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } BigDecimal balance = vipMasterWalletViewModel.getBalance().subtract(model.getAmount()); vipMasterWalletViewModel.setBalance(balance); return Update(vipMasterWalletViewModel); } public ApiData Recharge(VIPMasterWalletRechargeViewModel model) { VIPMasterViewModel vipMasterViewModel = vipMasterService.GetViewDetail(model.getMasterid()); if (vipMasterViewModel == null) { return ApiData.error(LangConstants.MSG_ERROR_PARAMETERS); } VIPMasterWalletQueryObject queryObject = new VIPMasterWalletQueryObject(); queryObject.setMasterid(model.getMasterid()); VIPMasterWalletViewModel walletViewModel = GetViewDetail(queryObject); if (walletViewModel == null) { walletViewModel = new VIPMasterWalletViewModel(); walletViewModel.setMasterid(model.getMasterid()); walletViewModel.setMastercode(vipMasterViewModel.getCode()); walletViewModel.setMastername(vipMasterViewModel.getName()); walletViewModel.setBalance(model.getAmount()); return Create(walletViewModel); } else { BigDecimal balance = walletViewModel.getBalance().add(model.getAmount()); walletViewModel.setBalance(balance); return Update(walletViewModel); } } @Transactional @CacheEvict(allEntries = true) public ApiData BatchDelete(VIPMasterWalletQueryObject 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 VIPMasterWalletViewModel GetViewDetail(String masterid) { VIPMasterWalletQueryObject queryObject = new VIPMasterWalletQueryObject(); queryObject.setMasterid(masterid); VIPMasterWallet entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public VIPMasterWalletViewModel GetViewDetail(VIPMasterWalletQueryObject queryObject) { VIPMasterWallet entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(VIPMasterWalletQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (VIPMasterWallet entity : list) { VIPMasterWalletViewModel 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 VIPMasterWalletViewModel BuildViewModel(VIPMasterWallet entity) { if (entity == null) return null; VIPMasterWalletViewModel model = new VIPMasterWalletViewModel(); model.setId(entity.getId()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setStatus(entity.getStatus()); model.setBalance(entity.getBalance()); model.setMasterid(entity.getMasterid()); model.setMastercode(entity.getMastercode()); model.setMastername(entity.getMastername()); return model; } public VIPMasterWallet Check(ActionType actionType, VIPMasterWalletViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } if (model.getMasterid() == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } VIPMasterWallet entity = null; if (ActionType.CREATE == actionType) { entity = new VIPMasterWallet(); 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 { VIPMasterWalletQueryObject queryObject = new VIPMasterWalletQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setRemark(model.getRemark()); entity.setStatus(model.getStatus()); entity.setBalance(model.getBalance()); entity.setMasterid(model.getMasterid()); entity.setMastercode(model.getMastercode()); entity.setMastername(model.getMastername()); return entity; } }