package com.xzx.gc.system.service; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.convert.Convert; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import com.xzx.gc.common.constant.Constants; import com.xzx.gc.common.exception.RestException; import com.xzx.gc.entity.OtherUserInfo; import com.xzx.gc.entity.SysEnvironmentalInfo; import com.xzx.gc.entity.SysItemPrice; import com.xzx.gc.entity.SysItemUser; import com.xzx.gc.model.system.ItemPriceAddDTO; import com.xzx.gc.system.mapper.SysItemPriceMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import tk.mybatis.mapper.entity.Example; import java.math.BigDecimal; import java.util.List; import java.util.stream.Collectors; @Service @Transactional public class SysItemPriceService { @Autowired private SysItemPriceMapper sysItemPriceMapper; @Autowired private OtherUserService otherUserService; @Autowired private SysItemUserService sysItemUserService; @Autowired private SysEnvironmentalInfoService sysEnvironmentalInfoService; public List findByItemId(Long itemId){ Example example=new Example(SysItemPrice.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("itemId",itemId); criteria.andEqualTo("delFlag",Convert.toShort(Constants.DEL_NOT_FLAG)); example.setOrderByClause("price asc"); return sysItemPriceMapper.selectByExample(example); } public SysItemPrice findById(Long id){ return sysItemPriceMapper.selectByPrimaryKey(id); } public void deleteByItemId(Long itemId){ Example example=new Example(SysItemPrice.class); Example.Criteria criteria = example.createCriteria(); criteria.andEqualTo("itemId",itemId); SysItemPrice sysItemPrice=new SysItemPrice(); sysItemPrice.setDelFlag(Convert.toShort(Constants.DEL_FLAG)); sysItemPriceMapper.updateByExampleSelective(sysItemPrice,example); } public void itemPriceAdd(ItemPriceAddDTO itemPriceAddDTO) { Long itemId = itemPriceAddDTO.getItemId(); SysItemUser byId1 = sysItemUserService.findById(itemId); List list = itemPriceAddDTO.getList(); String userId = itemPriceAddDTO.getUserId(); OtherUserInfo byId = otherUserService.findById(userId); SysEnvironmentalInfo byCityAndItemTypeAndPartner = sysEnvironmentalInfoService.findByItemTypeAndAreaNotDel(byId1.getItemType(), byId.getTownshipId()); for (SysItemPrice sysItemPrice : list) { BigDecimal price = sysItemPrice.getPrice(); if(price==null){ price=BigDecimal.ZERO; } //判断价格设置不能低于或高于平台的价格 if(byCityAndItemTypeAndPartner!=null){ String minPrice = byCityAndItemTypeAndPartner.getMinPrice(); String maxPrice = byCityAndItemTypeAndPartner.getMaxPrice(); if(StrUtil.isNotBlank(minPrice)){ if(price.compareTo(Convert.toBigDecimal(minPrice))<0){ throw new RestException("不能低于分类:"+byCityAndItemTypeAndPartner.getTitle()+"的平台最低价格:"+minPrice); } } if(StrUtil.isNotBlank(maxPrice)){ if(price.compareTo(Convert.toBigDecimal(maxPrice))>0){ throw new RestException("不能高于分类:"+byCityAndItemTypeAndPartner.getTitle()+"的平台最高价格:"+maxPrice); } } } Long id = sysItemPrice.getId(); if(id!=null) { SysItemPrice byId2 = findById(id); byId2.setMinWeight(sysItemPrice.getMinWeight()); byId2.setMaxWeight(sysItemPrice.getMaxWeight()); byId2.setPrice(sysItemPrice.getPrice()); sysItemPriceMapper.updateByPrimaryKey(byId2); }else { sysItemPrice.setItemId(itemId); sysItemPrice.setDelFlag(Convert.toShort(Constants.DEL_NOT_FLAG)); sysItemPrice.setCreateTime(DateUtil.now()); sysItemPriceMapper.insertSelective(sysItemPrice); } } } /** * 根据用户查询设置的所有分类的价格 * @param userId * @return */ public List findPriceByUserId(String userId){ return sysItemPriceMapper.findPriceByUserId(userId); } public List findByItemAndList(List list,String itemType){ if(CollUtil.isNotEmpty(list)){ List collect = list.stream().filter(x -> x.getItemType().equals(itemType)).collect(Collectors.toList()); if(CollUtil.isNotEmpty(collect)){ return collect; } } return null; } }