Helius
2021-03-11 a72aa4bb793ec765892406c842039a71b33215b1
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package com.matrix.system.hive.service.imp;
 
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.DateUtil;
import com.matrix.core.tools.WebUtil;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.constance.Dictionary;
import com.matrix.system.hive.bean.SysInstoreDetail;
import com.matrix.system.hive.bean.SysInstoreInfo;
import com.matrix.system.hive.bean.SysShopInfo;
import com.matrix.system.hive.bean.SysStoreInfo;
import com.matrix.system.hive.dao.*;
import com.matrix.system.hive.plugin.util.CollectionUtils;
import com.matrix.system.hive.plugin.util.MoneyUtil;
import com.matrix.system.hive.service.SysInstoreInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
 
/**
 * @date 2016-07-15 13:10
 */
@Service("sysInstoreInfoService")
public class SysInstoreInfoServiceImpl implements SysInstoreInfoService {
 
    @Autowired
    private SysInstoreInfoDao sysInstoreInfoDao;
    @Autowired
    private SysInstoreDetailDao sysInstoreDetailDao;
    @Autowired
    private SysStoreInfoDao sysStoreInfoDao;
    @Autowired
    private SysGoodsDao goodsDao;
    @Autowired
    private SysShopInfoDao shopInfoDao;
    @Autowired
    private SysInstoreDetailDao instoreDetailDao;
 
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(SysInstoreInfo sysInstoreInfo) {
        sysInstoreInfo.setCheckStatus(Dictionary.CHECK_STATUS_DSH);
        // 设置入库单编号
        sysInstoreInfo.setInstoreId(DateUtil.getTimeMark());
//        SysShopInfo info = shopInfoDao.selectByShopName(Dictionary.SHOP_NAME_ZONGDIAN);
//        // 系统限制入库只能入库到总店,总店不存在则
//        if (info == null) {
//            throw new GlobleException("总店仓库不存在,请添加!");
//        }
        // 插入
        int i = sysInstoreInfoDao.insert(sysInstoreInfo);
        // 设置总金额,并更新
        sysInstoreInfo.setSumall(savevDetatil(sysInstoreInfo));
        sysInstoreInfoDao.update(sysInstoreInfo);
        return i;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int modify(SysInstoreInfo sysInstoreInfo) {
        Long id = sysInstoreInfo.getId();
        SysInstoreInfo getInstoreInfo = sysInstoreInfoDao.selectById(id);
        // 已经审核过的单据不能在修改
        if (!getInstoreInfo.getCheckStatus().equals(Dictionary.CHECK_STATUS_DSH) && !getInstoreInfo.getCheckStatus().equals(Dictionary.CHECK_STATUS_SHWTG)) {
            throw new GlobleException("已审核,不能再修改");
        }
        sysInstoreInfo.setCheckStatus(Dictionary.CHECK_STATUS_DSH);
        // 设置总金额
        sysInstoreInfo.setSumall(savevDetatil(sysInstoreInfo));
        return sysInstoreInfoDao.update(sysInstoreInfo);
    }
 
    /**
     * 保存入库单明细,返回总价
     *
     * @param sysInstoreInfo
     * @author jiangyouyao
     */
    private double savevDetatil(SysInstoreInfo sysInstoreInfo) {
        // 1.删除入库单的明细
        sysInstoreDetailDao.deleteByInstoreId(sysInstoreInfo.getId());
        // 计算入库总额
        Double total = 0.0;
        // 2.添加新的入库明细
        List<SysInstoreDetail> instoreDetailsFromObj = sysInstoreInfo.getInstoreDetails();
        List<SysInstoreDetail> addList = new ArrayList<SysInstoreDetail>();
        if (!instoreDetailsFromObj.isEmpty()) {
            for (SysInstoreDetail sysInstoreDetail : instoreDetailsFromObj) {
                if (sysInstoreDetail.getSkuId() != null && (!sysInstoreDetail.getSkuId().equals(""))) {
                    // 计算金额
                    total = MoneyUtil.add(MoneyUtil.mul(Double.parseDouble(sysInstoreDetail.getAmount() + ""),
                            sysInstoreDetail.getPrice()), total);
                    sysInstoreDetail.setInstoreId(sysInstoreInfo.getId());
                    // 设置小计
                    sysInstoreDetail.setPriceTotal(MoneyUtil.mul(Double.parseDouble(sysInstoreDetail.getAmount() + ""),
                            sysInstoreDetail.getPrice()));
                    addList.add(sysInstoreDetail);
                }
            }
            sysInstoreDetailDao.batchInsert(addList);
        }
        return total;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int remove(List<Long> list) {
        for (Long id : list) {
            // 取出入库单
            SysInstoreInfo sysInstoreInfo = this.sysInstoreInfoDao.selectById(id);
            // 验证状态是否为待审核,待审核的才能删除
            if (!sysInstoreInfo.getCheckStatus().equals(Dictionary.CHECK_STATUS_DSH)) {
                throw new GlobleException("编号为" + sysInstoreInfo.getInstoreId() + "的入库单已审核,不能被删除");
            }
            // 如果存在明细,把明细也删除
            sysInstoreDetailDao.deleteByInstoreId(sysInstoreInfo.getId());
        }
        return sysInstoreInfoDao.deleteByIds(list);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id) {
 
        SysInstoreInfo sysInstoreInfo = this.sysInstoreInfoDao.selectById(id);
 
        // 验证状态是否为待审核,待审核的才能删除
        if (!sysInstoreInfo.getCheckStatus().equals(Dictionary.CHECK_STATUS_DSH)) {
            throw new GlobleException("编号为" + sysInstoreInfo.getInstoreId() + "的入库单已审核,不能被删除");
        }
        List<SysInstoreDetail> instoreDetails = instoreDetailDao.selectByOrderId(sysInstoreInfo.getId());
 
        if (CollectionUtils.isNotEmpty(instoreDetails)) {
            sysInstoreDetailDao.deleteByInstoreId(id);
            return sysInstoreInfoDao.deleteById(id);
        } else {
            return 0;
        }
    }
 
    @Override
    public List<SysInstoreInfo> findInPage(SysInstoreInfo sysInstoreInfo, PaginationVO pageVo) {
 
        return sysInstoreInfoDao.selectInPage(sysInstoreInfo, pageVo);
 
    }
 
    @Override
    public List<SysInstoreInfo> findByModel(SysInstoreInfo sysInstoreInfo) {
 
        return sysInstoreInfoDao.selectByModel(sysInstoreInfo);
 
    }
 
    @Override
    public int findTotal(SysInstoreInfo sysInstoreInfo) {
 
        return sysInstoreInfoDao.selectTotalRecord(sysInstoreInfo);
 
    }
 
    @Override
    public SysInstoreInfo findById(Long id) {
 
        return sysInstoreInfoDao.selectById(id);
 
    }
 
    /**
     * 入库单审核
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int check(SysInstoreInfo sysInstoreInfo) {
        // 验证权限
        SysUsers user = (SysUsers) WebUtil.getSession().getAttribute(MatrixConstance.LOGIN_KEY);
        sysInstoreInfo.setAppmanId(user.getSuId());
        sysInstoreInfo.setShopId(user.getShopId());
        sysInstoreInfo.setCompanyId(user.getCompanyId());
 
        SysInstoreInfo checkInStore = sysInstoreInfoDao.selectById(sysInstoreInfo.getId());
//        if (!checkInStore.getAppmanId().equals(user.getSuId())) {
//            throw new GlobleException("无权审核该单据!");
//        }
        if (!checkInStore.getCheckStatus().equals(Dictionary.CHECK_STATUS_DSH)) {
            throw new GlobleException("该单据状态为" + checkInStore.getCheckStatus() + ",不可审核!");
        }
        sysInstoreInfo.setStoreId(checkInStore.getStoreId());
        // 修改入库单状态
        sysInstoreInfo.setCheckStatus(Dictionary.CHECK_STATUS_SHTG);
        sysInstoreInfoDao.update(sysInstoreInfo);
        List<SysInstoreDetail> instoreDetails = instoreDetailDao.selectByOrderId(sysInstoreInfo.getId());
 
        // 需要插入的库存
        List<SysStoreInfo> newStore = new ArrayList<>();
        for (SysInstoreDetail instoreDetail : instoreDetails) {
            SysStoreInfo sysStoreInfo = new SysStoreInfo();
            sysStoreInfo.setSkuId(instoreDetail.getSkuId());
            sysStoreInfo.setGoodsPrice(instoreDetail.getPrice());
            // 所属仓库id
            sysStoreInfo.setStoreId(sysInstoreInfo.getStoreId());
            sysStoreInfo.setStoreTotal(new Double(instoreDetail.getAmount() + ""));
            // 设置批次号
            int num = sysStoreInfoDao.countDetail(instoreDetail.getSkuId());
            String batch=DateUtil.dateToString(new Date(), DateUtil.DATE_FORMAT_NO_SPLITE_DD)+"-"+ num;
 
            sysStoreInfo.setBatch(batch);
            sysStoreInfo.setTime(new Date());
            sysStoreInfo.setShopId(user.getShopId());
            sysStoreInfo.setCompanyId(user.getCompanyId());
            newStore.add(sysStoreInfo);
 
            //更新入库记录批次
            instoreDetail.setBatch(batch);
            sysInstoreDetailDao.update(instoreDetail);
 
        }
        if (!newStore.isEmpty()) {
            sysStoreInfoDao.batchInsert(newStore);
        }
        return 1;
    }
 
    /**
     * 入库单弃审
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int unCheck(SysInstoreInfo sysInstoreInfo) {
        // 验证权限
        SysUsers user = (SysUsers) WebUtil.getSession().getAttribute(MatrixConstance.LOGIN_KEY);
        sysInstoreInfo.setAppmanId(user.getSuId());
        sysInstoreInfo.setShopId(user.getShopId());
        sysInstoreInfo.setCompanyId(user.getCompanyId());
 
        SysInstoreInfo checkInStore = sysInstoreInfoDao.selectById(sysInstoreInfo.getId());
//        if (!checkInStore.getAppmanId().equals(user.getSuId())) {
//            throw new GlobleException("无权撤销该单据!");
//        }
        if (!checkInStore.getCheckStatus().equals(Dictionary.CHECK_STATUS_SHTG)) {
            throw new GlobleException("该单据状态为" + checkInStore.getCheckStatus() + ",不可审核!");
        }
 
        // 修改入库单状态
        sysInstoreInfo.setCheckStatus(Dictionary.CHECK_STATUS_DSH);
        if (sysInstoreInfo.getAppRemark() != null) {
            sysInstoreInfo.setAppRemark(sysInstoreInfo.getAppRemark().replace("[弃审]", "") + "[弃审]");
        } else {
            sysInstoreInfo.setAppRemark("[弃审]");
        }
        sysInstoreInfo.setStoreId(checkInStore.getStoreId());
        sysInstoreInfoDao.update(sysInstoreInfo);
 
        // 遍历入库单明细,将产品写入库存
        List<SysInstoreDetail> instoreDetails = instoreDetailDao.selectByOrderId(sysInstoreInfo.getId());
        int i = 0;
        for (SysInstoreDetail instoreDetail : instoreDetails) {
            SysStoreInfo sysStoreInfo = new SysStoreInfo();
            sysStoreInfo.setSkuId(instoreDetail.getSkuId());
            // 所属仓库id
            sysStoreInfo.setStoreId(checkInStore.getStoreId());
            sysStoreInfo.setStoreTotal((double) instoreDetail.getAmount());
            SysStoreInfo storeInfo = sysStoreInfoDao.getStoreByGoodsId(sysStoreInfo);
            if (storeInfo.getStoreTotal() - sysStoreInfo.getStoreTotal() < 0) {
                throw new GlobleException(storeInfo.getGoods().getName() + "库存不足,不能撤销该条记录!");
            }
            storeInfo.setStoreTotal(storeInfo.getStoreTotal() - sysStoreInfo.getStoreTotal());
            i = this.sysStoreInfoDao.update(storeInfo);
        }
        return i;
    }
 
    @Override
    public int update(SysInstoreInfo sysInstoreInfo) {
        return sysInstoreInfoDao.update(sysInstoreInfo);
    }
 
 
}