yujian
2020-04-02 0ec22dcf4fd9c4496e6f681e7fab89f56c6e4e8a
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package com.yeshi.fanli.service.impl.shop;
 
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import com.yeshi.fanli.dao.mybatis.shop.BanLiShopGoodsSetsMapper;
import com.yeshi.fanli.entity.shop.BanLiShopGoodsSets;
import com.yeshi.fanli.entity.shop.BanLiShopGoodsSetsPay;
import com.yeshi.fanli.exception.shop.BanLiShopGoodsSetException;
import com.yeshi.fanli.exception.shop.BanLiShopGoodsSetPayException;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetPayService;
import com.yeshi.fanli.service.inter.shop.BanLiShopGoodsSetService;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class BanLiShopGoodsSetServiceImpl implements BanLiShopGoodsSetService {
 
    @Resource
    private BanLiShopGoodsSetPayService banLiShopGoodsSetPayService;
 
    @Resource
    private BanLiShopGoodsSetsMapper banLiShopGoodsSetsMapper;
 
    @Override
    public List<BanLiShopGoodsSets> listByGoodsId(Long goodsId) {
        return banLiShopGoodsSetsMapper.listByGoodsId(goodsId);
    }
 
    @Override
    public int countByGoodsId(Long goodsId) {
        return (int) banLiShopGoodsSetsMapper.countByGoodsId(goodsId);
    }
 
    @Override
    public BanLiShopGoodsSets selectByPrimaryKey(Long id) {
        return banLiShopGoodsSetsMapper.selectByPrimaryKey(id);
    }
 
    @Transactional(rollbackFor=Exception.class)
    @Override
    public void addSet(BanLiShopGoodsSets set) throws BanLiShopGoodsSetException, BanLiShopGoodsSetPayException {
        if (set.getId() == null)// 添加
        {
            if (set.getGoods() == null || set.getGoods().getId() == null) {
                throw new BanLiShopGoodsSetException(1, "请指定商品");
            }
 
            if (StringUtil.isNullOrEmpty(set.getName()))
                throw new BanLiShopGoodsSetException(1, "缺少套餐名字");
 
            if (set.getOriginalPrice() == null || set.getZkPrice() == null) {
                throw new BanLiShopGoodsSetException(1, "价格信息不完整");
            }
 
            if (set.getStock() == null) {
                throw new BanLiShopGoodsSetException(1, "缺少库存信息");
            }
 
            if (set.getSalesCount() == null)
                set.setSalesCount(0L);
 
            if (set.getCreateTime() == null)
                set.setCreateTime(new Date());
 
            banLiShopGoodsSetsMapper.insertSelective(set);
            if (set.getWeight() == null) {// 权重更新
                BanLiShopGoodsSets update = new BanLiShopGoodsSets();
                update.setId(set.getId());
                update.setWeight((int) set.getId().longValue());
                banLiShopGoodsSetsMapper.updateByPrimaryKeySelective(update);
            }
 
            if (set.getPayList() != null && set.getPayList().size() > 0) {
                for (BanLiShopGoodsSetsPay pay : set.getPayList())// 添加支付方式
                {
                    pay.setGoodsSet(set);
                    banLiShopGoodsSetPayService.addSetPay(pay);
                }
            }
 
        } else {// 修改
            if (set.getUpdateTime() == null) {
                set.setUpdateTime(new Date());
            }
 
            banLiShopGoodsSetsMapper.updateByPrimaryKeySelective(set);
 
            // 更新支付方式
            if (set.getPayList() != null && set.getPayList().size() > 0) {
                for (BanLiShopGoodsSetsPay pay : set.getPayList())// 更新支付方式
                    banLiShopGoodsSetPayService.addSetPay(pay);
            }
        }
 
    }
    
    
    @Override
    public void saveObject(BanLiShopGoodsSets record) throws BanLiShopGoodsSetException {
        if (record.getGoods() == null || record.getGoods().getId() == null)
            throw new BanLiShopGoodsSetException(1, "请指定商品");
 
        if (StringUtil.isNullOrEmpty(record.getName()))
            throw new BanLiShopGoodsSetException(1, "缺少套餐名字");
 
        if (record.getOriginalPrice() == null || record.getZkPrice() == null)
            throw new BanLiShopGoodsSetException(1, "价格信息不完整");
 
        if (record.getStock() == null)
            throw new BanLiShopGoodsSetException(1, "缺少库存信息");
 
        record.setUpdateTime(new Date());
        if (record.getId() == null) {
            record.setCreateTime(new Date());
            banLiShopGoodsSetsMapper.insertSelective(record);
            
            if (record.getWeight() == null) {// 权重更新
                BanLiShopGoodsSets update = new BanLiShopGoodsSets();
                update.setId(record.getId());
                update.setWeight((int) record.getId().longValue());
                banLiShopGoodsSetsMapper.updateByPrimaryKeySelective(update);
            }
        } else {
            BanLiShopGoodsSets resultObj = banLiShopGoodsSetsMapper.selectDetailByPrimaryKey(record.getId());
            if (resultObj == null)
                throw new BanLiShopGoodsSetException(1, "修改内容已不存在");
            
            record.setCreateTime(resultObj.getCreateTime());
            banLiShopGoodsSetsMapper.updateByPrimaryKey(record);
        }
    }
    
 
 
    @Override
    public void updateSelectiveByPrimaryKey(BanLiShopGoodsSets set) {
        if (set.getId() == null)
            return;
        if (set.getUpdateTime() == null)
            set.setUpdateTime(new Date());
        banLiShopGoodsSetsMapper.updateByPrimaryKeySelective(set);
    }
 
    @Transactional
    @Override
    public void delete(List<Long> idsList) {
        if (idsList != null)
            for (Long id : idsList)
                deleteByPrimaryKey(id);
    }
 
    @Transactional
    @Override
    public void deleteByPrimaryKey(Long id) {
        banLiShopGoodsSetsMapper.deleteByPrimaryKey(id);
        List<BanLiShopGoodsSetsPay> payList = banLiShopGoodsSetPayService.listByGoodsSetId(id);
        if (payList != null)
            for (BanLiShopGoodsSetsPay pay : payList) {// 删除支付方式
                banLiShopGoodsSetPayService.deleteByPrimaryKey(pay.getId());
            }
    }
 
    @Override
    public List<BanLiShopGoodsSets> listQuery(int page, int pageSize, String key, Long goodsId) {
        return banLiShopGoodsSetsMapper.listQuery((page - 1) * pageSize, pageSize, key, goodsId);
    }
 
    @Override
    public long countQuery(String key, Long goodsId) {
        return banLiShopGoodsSetsMapper.countQuery(key, goodsId);
    }
 
    @Override
    public void addSalesCount(Long id, int count) {
        BanLiShopGoodsSets set = banLiShopGoodsSetsMapper.selectByPrimaryKey(id);
        BanLiShopGoodsSets update = new BanLiShopGoodsSets();
        update.setId(set.getId());
        update.setSalesCount(set.getSalesCount() + count);
        update.setStock(set.getStock() - count);
        banLiShopGoodsSetsMapper.updateByPrimaryKeySelective(update);        
    }
 
}