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.mapper.ShoppingCartProductMapper; import com.farriver.bwf.data.master.model.ShoppingCartProduct; import com.farriver.bwf.data.master.model.ShoppingCartProductExample; import com.farriver.bwf.data.transferobject.queryobject.shopping.ShoppingCartProductQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.shopping.ShoppingCartProductViewModel; 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 = "bwf_ShoppingCartProduct_cache") public class ShoppingCartProductService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(ShoppingCartProductService.class); private String message; @Resource ShoppingCartProductMapper mapper; public Map GetList(ShoppingCartProductQueryObject queryObject) { if (queryObject == null) return null; ShoppingCartProductExample example = new ShoppingCartProductExample(); ShoppingCartProductExample.Criteria criteria = example.createCriteria(); //查询条件 if (queryObject.getFilter() != null && !queryObject.getFilter().isEmpty()) { criteria.andProductcodeLike("%" + queryObject.getFilter() + "%"); } else { if (queryObject.getId() != null && !queryObject.getId().isEmpty()) { criteria.andIdEqualTo(queryObject.getId()); } if (queryObject.getCartid() != null && !queryObject.getCartid().isEmpty()) { criteria.andCartidEqualTo(queryObject.getCartid()); } if (queryObject.getProductid() != null && !queryObject.getProductid().isEmpty()) { criteria.andProductidEqualTo(queryObject.getProductid()); } if (queryObject.getProductcode() != null && !queryObject.getProductcode().isEmpty()) { criteria.andProductcodeLike("%" + queryObject.getProductcode() + "%"); } if (queryObject.getProductname() != null && !queryObject.getProductname().isEmpty()) { criteria.andProductnameLike("%" + queryObject.getProductname() + "%"); } } //查询总数 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 ShoppingCartProduct GetDetail(ShoppingCartProductQueryObject 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(ShoppingCartProductViewModel model) { ShoppingCartProduct entity = Check(ActionType.CREATE, model); if (entity != null) { return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, mapper.insertSelective(entity)); } else { return ApiData.error(message); } } /* / ShoppingCart has been set @Transactional, no set @Transactional for this function. */ @CacheEvict(allEntries = true) public ApiData BatchCreate(List models) { int count = 0; if (models != null && models.size() > 0) { for (ShoppingCartProductViewModel model : models) { ShoppingCartProduct entity = Check(ActionType.CREATE, model); if (entity != null) { count += mapper.insertSelective(entity); } } } return ApiData.ok(LangConstants.MSG_INFO_SUCCESS, count); } @CacheEvict(allEntries = true) public ApiData Update(ShoppingCartProductViewModel model) { ShoppingCartProduct 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 DeleteByProductID(List productids) { ShoppingCartProductExample example = new ShoppingCartProductExample(); ShoppingCartProductExample.Criteria criteria = example.createCriteria(); criteria.andProductidIn(productids); return ApiData.ok("", mapper.deleteByExample(example)); } /* / ShoppingCart has been set @Transactional, no set @Transactional for this function. */ @CacheEvict(allEntries = true) public ApiData BatchDelete(ShoppingCartProductQueryObject 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 ShoppingCartProductViewModel GetViewDetail(String id) { ShoppingCartProductQueryObject queryObject = new ShoppingCartProductQueryObject(); queryObject.setId(id); ShoppingCartProduct entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(ShoppingCartProductQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (ShoppingCartProduct entity : list) { ShoppingCartProductViewModel 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 ShoppingCartProductViewModel BuildViewModel(ShoppingCartProduct entity) { if (entity == null) return null; ShoppingCartProductViewModel model = new ShoppingCartProductViewModel(); model.setId(entity.getId()); model.setIsdeleted(entity.getIsdeleted()); model.setRemark(entity.getRemark()); model.setCreatetime(entity.getCreatetime()); model.setUpdatetime(entity.getUpdatetime()); model.setProductid(entity.getProductid()); model.setProductcode(entity.getProductcode()); model.setProductname(entity.getProductname()); model.setStatus(entity.getStatus()); model.setColor(entity.getColor()); model.setSize(entity.getSize()); model.setCount(entity.getCount()); model.setImage(entity.getImage()); model.setPrice(entity.getPrice()); model.setSaleprice(entity.getSaleprice()); model.setCartid(entity.getCartid()); model.setCartcode(entity.getCartcode()); return model; } public ShoppingCartProduct Check(ActionType actionType, ShoppingCartProductViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } if (model.getCartid() == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } if (model.getProductid() == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } ShoppingCartProduct entity = null; if (ActionType.CREATE == actionType) { entity = new ShoppingCartProduct(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); entity.setIsdeleted(false); } else { ShoppingCartProductQueryObject queryObject = new ShoppingCartProductQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } entity.setProductid(model.getProductid()); entity.setProductcode(model.getProductcode()); entity.setProductname(model.getProductname()); entity.setStatus(model.getStatus()); entity.setColor(model.getColor()); entity.setSize(model.getSize()); entity.setCount(model.getCount()); entity.setImage(model.getImage()); entity.setPrice(model.getPrice()); entity.setSaleprice(model.getSaleprice()); entity.setCartid(model.getCartid()); entity.setCartcode(model.getCartcode()); return entity; } }