yujian
2019-11-06 54e6398cabe1b32b1dbc9857c6a99d8f15b549f7
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
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
    @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 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) {
        return banLiShopGoodsSetsMapper.listQuery((page - 1) * pageSize, pageSize, key);
    }
    
    @Override
    public long countQuery(String key) {
        return banLiShopGoodsSetsMapper.countQuery(key);
    }
 
}