package com.farriver.bwf.service.order; 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.OrderActiveProductMapper; import com.farriver.bwf.data.master.model.OrderActiveProduct; import com.farriver.bwf.data.master.model.OrderActiveProductExample; import com.farriver.bwf.data.transferobject.options.OrderProductStatusOptions; import com.farriver.bwf.data.transferobject.queryobject.order.OrderActiveProductQueryObject; import com.farriver.bwf.data.transferobject.viewmodel.order.OrderActiveProductViewModel; import com.farriver.bwf.data.transferobject.viewmodel.product.ProductMasterViewModel; import com.farriver.bwf.service.ServiceBase; import com.farriver.bwf.service.product.ProductService; 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_OrderActiveProduct_cache") public class OrderActiveProductService extends ServiceBase { private static final Logger logger = LoggerFactory.getLogger(OrderActiveProductService.class); private String message; @Resource OrderActiveProductMapper mapper; @Resource ProductService productService; public Map GetList(OrderActiveProductQueryObject queryObject) { if (queryObject == null) return null; OrderActiveProductExample example = new OrderActiveProductExample(); OrderActiveProductExample.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.getOrderid() != null && !queryObject.getOrderid().isEmpty()) { criteria.andOrderidEqualTo(queryObject.getOrderid()); } if (queryObject.getOrderno() != null && !queryObject.getOrderno().isEmpty()) { criteria.andOrdernoLike("%" + queryObject.getOrderno() + "%"); } 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 OrderActiveProduct GetDetail(OrderActiveProductQueryObject 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(OrderActiveProductViewModel model) { OrderActiveProduct 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 (OrderActiveProductViewModel model : models) { OrderActiveProduct 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(OrderActiveProductViewModel model) { OrderActiveProduct 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)); } /* / ShoppingCart has been set @Transactional, no set @Transactional for this function. */ @CacheEvict(allEntries = true) public ApiData BatchDelete(OrderActiveProductQueryObject 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 OrderActiveProductViewModel GetViewDetail(String id) { OrderActiveProductQueryObject queryObject = new OrderActiveProductQueryObject(); queryObject.setId(id); OrderActiveProduct entity = GetDetail(queryObject); return BuildViewModel(entity); } @Cacheable public ApiPageData GetViewPageList(OrderActiveProductQueryObject queryObject) { Map map = GetList(queryObject); List list = (List) map.get(MapDataType.DATA_LIST); List viewModels = new ArrayList<>(); for (OrderActiveProduct entity : list) { OrderActiveProductViewModel 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 OrderActiveProductViewModel BuildViewModel(OrderActiveProduct entity) { if (entity == null) return null; OrderActiveProductViewModel model = new OrderActiveProductViewModel(); 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.setRejectreason(entity.getRejectreason()); model.setRejectcomment(entity.getRejectcomment()); model.setRejecttime(entity.getRejecttime()); model.setPayer(entity.getPayer()); model.setPayedmoney(entity.getPayedmoney()); model.setDiscount(entity.getDiscount()); model.setPayee(entity.getPayee()); model.setOrderid(entity.getOrderid()); model.setOrderno(entity.getOrderno()); return model; } public OrderActiveProduct Check(ActionType actionType, OrderActiveProductViewModel model) { message = ""; if (model == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } if (model.getOrderid() == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } if (model.getProductid() == null) { message = LangConstants.MSG_ERROR_PARAMETERS; return null; } OrderActiveProduct entity = null; if (ActionType.CREATE == actionType) { entity = new OrderActiveProduct(); entity.setId(UUID.randomUUID().toString()); entity.setCreatetime(new Date()); entity.setIsdeleted(false); } else { OrderActiveProductQueryObject queryObject = new OrderActiveProductQueryObject(); queryObject.setId(model.getId()); entity = GetDetail(queryObject); entity.setUpdatetime(new Date()); } if (model.getProductid() != null && !model.getProductid().isEmpty()) { ApiData apidata = productService.GetProductDetailByMasterId(model.getProductid()); if (apidata != null && apidata.getCode() == 200) { ProductMasterViewModel productMasterViewModel = (ProductMasterViewModel) apidata.getData(); if (productMasterViewModel != null) { entity.setProductcode(productMasterViewModel.getCode()); } } } entity.setProductname(model.getProductid()); entity.setProductname(model.getProductname()); entity.setStatus(OrderProductStatusOptions.Defined.ordinal()); 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.setRejectreason(model.getRejectreason()); entity.setRejectcomment(model.getRejectcomment()); entity.setRejecttime(model.getRejecttime()); entity.setPayer(model.getPayer()); entity.setPayedmoney(model.getPayedmoney()); entity.setDiscount(model.getDiscount()); entity.setPayee(model.getPayee()); entity.setOrderid(model.getOrderid()); entity.setOrderno(model.getOrderno()); return entity; } }