xiaoyong931011
2021-08-25 171a706099da6bd039b31b7a94eb75fb9b6162d2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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<SysItemPrice> 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<SysItemPrice> 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<SysItemPrice> findPriceByUserId(String userId){
        return sysItemPriceMapper.findPriceByUserId(userId);
    }
 
    public List<SysItemPrice> findByItemAndList(List<SysItemPrice> list,String itemType){
        if(CollUtil.isNotEmpty(list)){
            List<SysItemPrice> collect = list.stream().filter(x -> x.getItemType().equals(itemType)).collect(Collectors.toList());
            if(CollUtil.isNotEmpty(collect)){
                return collect;
            }
        }
        return null;
    }
}