package com.farriver.bwf.service.shopping; 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.entity.ShoppingCartExtend; import com.farriver.bwf.data.master.mapper.ShoppingCartMapper; import com.farriver.bwf.data.master.model.*; import com.farriver.bwf.data.transferobject.queryobject.shopping.ShoppingCartProductQueryObject; import com.farriver.bwf.data.transferobject.queryobject.shopping.ShoppingCartQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.shopping.ShoppingCartProductViewModel; import com.farriver.bwf.data.transferobject.viewmodel.shopping.ShoppingCartViewModel; import com.farriver.bwf.service.ServiceBase; import org.jetbrains.annotations.Nullable; 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_ShoppingCart_cache") public class ShoppingCartService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(ShoppingCartService.class); private String message; @Resource ShoppingCartMapper mapper; @Resource ShoppingCartProductService shoppingCartProductService; public Map GetList(ShoppingCartQueryObject queryObject) { if (queryObject == null) return null; ShoppingCartExample example = new ShoppingCartExample(); ShoppingCartExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andVipnameLike("%" + queryObject.getFilter() + "%"); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getVipuserid() != null && !queryObject.getVipuserid().isEmpty()) { criteria.andVipuseridEqualTo(queryObject.getVipuserid()); } if (queryObject.getVipphone() != null && !queryObject.getVipphone().isEmpty()) { criteria.andVipphoneLike("%" + queryObject.getVipphone() + "%"); } } //查询总数 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 Map GetExtendList(ShoppingCartQueryObject queryObject) { if (queryObject == null) return null; ShoppingCartExample example = new ShoppingCartExample(); ShoppingCartExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andVipnameLike("%" + queryObject.getFilter() + "%"); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getVipuserid() != null && !queryObject.getVipuserid().isEmpty()) { criteria.andVipuseridEqualTo(queryObject.getVipuserid()); } if (queryObject.getVipphone() != null && !queryObject.getVipphone().isEmpty()) { criteria.andVipphoneLike("%" + queryObject.getVipphone() + "%"); } } //查询总数 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.selectShoppingCartExtendByExample(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 ShoppingCart GetDetail(ShoppingCartQueryObject 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; } public ShoppingCartExtend GetExtendDetail(ShoppingCartQueryObject queryObject) { Map map = GetExtendList(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(ShoppingCartViewModel model) { ShoppingCart 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 CreateWithProducts(ShoppingCartViewModel model) { ShoppingCart entity = Check(ActionType.CREATE, model); if (entity != null) { List productViewModels = model.getProducts(); if (productViewModels != null && productViewModels.size() > 0) { for (ShoppingCartProductViewModel productViewModel : productViewModels) { productViewModel.setCartid(entity.getId()); productViewModel.setCartcode(entity.getCode()); } } ApiData productResult = shoppingCartProductService.BatchCreate(model.getProducts()); return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, mapper.insertSelective(entity)); } else { return ApiData.error(message); } } @CacheEvict(allEntries = true) public ApiData Update(ShoppingCartViewModel model) { ShoppingCart 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 UpdateWithProducts(ShoppingCartViewModel model) { ShoppingCart entity = Check(ActionType.UPDATE, model); //清楚旧产品数据 ShoppingCartProductQueryObject queryObject = new ShoppingCartProductQueryObject(); queryObject.setCartid(model.getId()); shoppingCartProductService.BatchDelete(queryObject); if (entity != null) { List productViewModels = model.getProducts(); if (productViewModels != null && productViewModels.size() > 0) { for (ShoppingCartProductViewModel productViewModel : productViewModels) { productViewModel.setCartid(entity.getId()); productViewModel.setCartcode(entity.getCode()); } } ApiData productResult = shoppingCartProductService.BatchCreate(model.getProducts()); return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, mapper.updateByPrimaryKeySelective(entity)); } else { return ApiData.error(message); } } @Transactional public ApiData Save(ShoppingCartViewModel model) { ShoppingCartQueryObject queryObject = new ShoppingCartQueryObject(); queryObject.setVipuserid(model.getVipuserid()); ShoppingCart entity = GetDetail(queryObject); if (entity == null) { return CreateWithProducts(model); } return shoppingCartProductService.BatchCreate(model.getProducts()); } @CacheEvict(allEntries = true) public ApiData Delete(String id) { return ApiData.ok("", mapper.deleteByPrimaryKey(id)); } @Transactional @CacheEvict(allEntries = true) public ApiData BatchDelete(ShoppingCartQueryObject 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 ShoppingCartViewModel GetViewDetail(ShoppingCartQueryObject queryObject) { ShoppingCart entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(ShoppingCartQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (ShoppingCart entity : list) { ShoppingCartViewModel viewModel = BuildViewModel(entity); viewModels.add(viewModel); } ApiPageData bean = ApiPageData.build(); bean.setData(viewModels); bean.setTotal((Long) map.get(MapDataType.TOTAL_COUNT)); return bean; } @Cacheable public ShoppingCartViewModel GetExtendViewDetail(ShoppingCartQueryObject queryObject) { ShoppingCartExtend entity = GetExtendDetail(queryObject); return BuildExtendViewModel(entity); } @Cacheable public ShoppingCartViewModel GetExtendViewDetailById(String id) { ShoppingCartQueryObject queryObject = new ShoppingCartQueryObject(); queryObject.setId(id); ShoppingCartExtend entity = GetExtendDetail(queryObject); return BuildExtendViewModel(entity); } @Cacheable public ApiPageData GetExtendViewPageList(ShoppingCartQueryObject queryObject) { Map map = GetExtendList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (ShoppingCartExtend entity : list) { ShoppingCartViewModel viewModel = BuildExtendViewModel(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 ShoppingCartViewModel BuildViewModel(ShoppingCart entity) { if (entity == null) return null; ShoppingCartViewModel model = new ShoppingCartViewModel(); model.setId(entity.getId()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setCode(entity.getCode()); model.setVipuserid(entity.getVipuserid()); model.setVipname(entity.getVipname()); model.setVipphone(entity.getVipphone()); model.setViptype(entity.getViptype()); model.setVipdiamondtype(entity.getVipdiamondtype()); model.setVippicture(entity.getVippicture()); model.setVipcarddiscountrate(entity.getVipcarddiscountrate()); model.setRemark(entity.getRemark()); return model; } private static ShoppingCartViewModel BuildExtendViewModel(ShoppingCartExtend entity) { if (entity == null) return null; ShoppingCartViewModel model = BuildViewModel(entity); List products = new ArrayList<>(); for (ShoppingCartProduct product : entity.getProducts()) { ShoppingCartProductViewModel productViewModel = ShoppingCartProductService.BuildViewModel(product); products.add(productViewModel); } model.setProducts(products); return model; } public ShoppingCart Check(ActionType actionType, ShoppingCartViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } ShoppingCart entity = null; if (ActionType.CREATE == actionType) { entity = new ShoppingCart(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); entity.setIsdeleted(false); String oldCode = GetLatestCode(); String newCode = generateCode(oldCode); if (newCode == null) return null; entity.setCode(newCode); } else { ShoppingCartQueryObject queryObject = new ShoppingCartQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setVipuserid(model.getVipuserid()); entity.setVipname(model.getVipname()); entity.setVipphone(model.getVipphone()); entity.setViptype(model.getViptype()); entity.setVipdiamondtype(model.getVipdiamondtype()); entity.setVippicture(model.getVippicture()); entity.setVipcarddiscountrate(model.getVipcarddiscountrate()); entity.setRemark(model.getRemark()); return entity; } @Nullable public String generateCode(String oldCode) { String preNo = "C"; int _newCodeNum = 1; try { int _oldCodeNum = 0; if (oldCode != null && !oldCode.equals("") && oldCode.length() > 2) { String _oldNoNumString = oldCode.substring(2); _oldCodeNum = Integer.parseInt(_oldNoNumString); } if (_oldCodeNum != 0) { int a = _oldCodeNum + 1; _newCodeNum = a; } } catch (NumberFormatException e) { e.printStackTrace(); message = LangConstants.MSG_ERROR_SYSTEM; return null; } return String.format("%s%07d", preNo, _newCodeNum); } public String GetLatestCode() { String oldCode = ""; ShoppingCartExample example = new ShoppingCartExample(); example.setOrderByClause("code desc"); example.setRows(1); example.setOffset(0); List list = mapper.selectByExample(example); if (list != null && list.size() > 0) { ShoppingCart entity2 = list.get(0); if (entity2 != null) { oldCode = entity2.getCode(); } } return oldCode; } }