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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
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.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.constance.Dictionary;
import com.matrix.system.constance.SystemConstance;
import com.matrix.system.hive.bean.*;
import com.matrix.system.hive.dao.*;
import com.matrix.system.hive.plugin.util.CollectionUtils;
import com.matrix.core.tools.DateUtil;
import com.matrix.system.hive.plugin.util.MoneyUtil;
import com.matrix.system.hive.service.CodeService;
import com.matrix.system.hive.service.SysCheckInfoService;
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-19 14:52
 */
@Service("sysCheckInfoService")
public class SysCheckInfoServiceImpl implements SysCheckInfoService {
 
    @Autowired
    private SysCheckInfoDao sysCheckInfoDao;
 
    @Autowired
    private SysCheckDetailDao sysCheckDetailDao;
 
    @Autowired
    private SysStoreInfoDao sysStoreInfoDao;
 
    @Autowired
    private SysOutStoreDao sysOutStoreDao;
 
    @Autowired
    private SysOutStoreItemDao sysOutStoreItemDao;
 
    @Autowired
    private CodeService codeService;
 
    @Autowired
    SysInstoreInfoDao instoreInfoDao;
 
    @Autowired
    SysInstoreDetailDao instoreDetailDao;
 
 
 
    public static final String TABLE_NAME = "sys_check_info";
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(SysCheckInfo info) {
        if (isStoreCheckInfoExist(info)) {
            throw new GlobleException("该仓库存在待审核的盘点单,请先处理上条盘点单!");
        }
        info.setCheckStatus(SysCheckInfo.STATUS_WAITE_INPUT);
        info.setCheckNo(codeService.getCheckInfoCode());
        info.setCreateDate(new Date());
        int i = sysCheckInfoDao.insert(info);
 
        // 生成盘点明细
        List<SysCheckDetail> detailList = new ArrayList<>();
        SysStoreInfo queryStoreInfo = new SysStoreInfo();
        queryStoreInfo.setShopId(info.getShopId());
        List<SysStoreInfo> storeInfoList = sysStoreInfoDao.selectCountInPage(queryStoreInfo, null);
        for (SysStoreInfo sysStoreInfo : storeInfoList) {
            SysCheckDetail detail = new SysCheckDetail();
            detail.setCheckId(info.getId());
            detail.setSkuId(sysStoreInfo.getSkuId());
            detailList.add(detail);
        }
        //新增盘点明细
        if (CollectionUtils.isNotEmpty(detailList)) {
            for (SysCheckDetail sysCheckDetail : detailList) {
                sysCheckDetailDao.insert(sysCheckDetail);
            }
        }
        return i;
 
    }
 
    /**
     * @Title: isStoreCheckInfoExist 判断该仓库是否存在待审核的盘点单 @author:jyy @param
     * checkInfo @return boolean 返回类型 @date 2016年9月3日 上午11:01:19 @throws
     */
    private boolean isStoreCheckInfoExist(SysCheckInfo checkInfo) {
        SysCheckInfo info = new SysCheckInfo();
        info.setCheckStatus(Dictionary.CHECK_STATUS_DSH);
        info.setCheckStoreid(checkInfo.getCheckStoreid());
        info.setShopId(checkInfo.getShopId());
        if (sysCheckInfoDao.selectByModel(info).size() > 0) {
            return true;
        }
        return false;
 
    }
 
    /**
     * 修改盘点单
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int modify(SysCheckInfo sysCheckInfo) {
        // 获得id
        Long id = sysCheckInfo.getId();
        SysCheckInfo sCheckInfo = this.sysCheckInfoDao.selectById(id);
        SysUsers users = (SysUsers) WebUtil.getSession().getAttribute(MatrixConstance.LOGIN_KEY);
        if (!users.getSuId().equals(sCheckInfo.getMakingManId())) {
            throw new GlobleException("无权操作盘点单!");
        }
        // 已经审核过的单据不能在修改
        if (!sCheckInfo.getCheckStatus().equals(Dictionary.CHECK_STATUS_DSH)) {
            throw new GlobleException("该盘点单状态为" + sCheckInfo.getCheckStatus() + ",不能修改!");
        }
        return sysCheckInfoDao.update(sysCheckInfo);
    }
 
    /**
     * jyy 删除盘点单
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Long id) {
        SysCheckInfo sCheckInfo = this.sysCheckInfoDao.selectById(id);
        // 验证状态是否为待审核,待审核的才能删除
        SysUsers users = (SysUsers) WebUtil.getSession().getAttribute(MatrixConstance.LOGIN_KEY);
        if (!users.getSuId().equals(sCheckInfo.getMakingManId())) {
            throw new GlobleException("无权操作盘点单!");
        }
        if (!sCheckInfo.getCheckStatus().equals(Dictionary.CHECK_STATUS_DSH)) {
            throw new GlobleException("该盘点单的状态为" + sCheckInfo.getCheckStatus() + ",不能被删除!");
        }
        return sysCheckInfoDao.deleteById(id);
    }
 
    @Override
    public List<SysCheckInfo> findInPage(SysCheckInfo sysCheckInfo, PaginationVO pageVo) {
        // 设置开始开始时间
        Date startDate = DateUtil.getStartDate(sysCheckInfo.getStartTime());
        sysCheckInfo.setStartTime(startDate);
        // 设置结束时间
        Date endDate = DateUtil.getEndDate(sysCheckInfo.getEndTime());
        sysCheckInfo.setEndTime(endDate);
        return sysCheckInfoDao.selectInPage(sysCheckInfo, pageVo);
    }
 
    @Override
    public List<SysCheckInfo> findByModel(SysCheckInfo sysCheckInfo) {
 
        return sysCheckInfoDao.selectByModel(sysCheckInfo);
 
    }
 
    @Override
    public int findTotal(SysCheckInfo sysCheckInfo) {
 
        return sysCheckInfoDao.selectTotalRecord(sysCheckInfo);
 
    }
 
    @Override
    public SysCheckInfo findById(Long id) {
 
        return sysCheckInfoDao.selectById(id);
 
    }
 
    /**
     * @author jiangyouyao
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int check(SysCheckInfo checkInfoVo) {
 
 
        // 验证权限
        SysUsers user = (SysUsers) WebUtil.getSession().getAttribute(MatrixConstance.LOGIN_KEY);
 
 
        // 已审核的不能再审核
        SysCheckInfo getCheckInfo = sysCheckInfoDao.selectById(checkInfoVo.getId());
        //TODO 添加审核记录
 
        if (!getCheckInfo.getCheckStatus().equals(Dictionary.CHECK_STATUS_DSH)) {
            throw new GlobleException("盘点单状态为" + getCheckInfo.getCheckStatus() + ",不能进行审核!");
        }
        if (Dictionary.CHECK_STATUS_YWC.equals(checkInfoVo.getCheckStatus())) {
 
            SysCheckDetail checkDetail = new SysCheckDetail();
            checkDetail.setCheckId(checkInfoVo.getId());
            // 以产品维度盘点遵循先进先出维度
            List<SysCheckDetail> detailList = sysCheckDetailDao.selectByModelSimple(checkDetail);
 
            //盘点入库记录
            List<SysInstoreDetail>  inStoreDetails=new ArrayList<>();
            //盘点出库记录
            List<SysOutStoreItem>  outStoreDetails=new ArrayList<>();
 
 
            for (SysCheckDetail detail : detailList) {
 
                //固定库存快照
                sysCheckDetailDao.update(detail);
 
                if (detail.getActuallySum() != null) {
                    List<SysStoreInfo> storeInfos = sysStoreInfoDao.selectStoInfoSimple(detail.getSkuId(), getCheckInfo.getCheckStoreid());
                    if (CollectionUtils.isNotEmpty(storeInfos)) {
 
                        //计算盈亏数
                        double gainLoss =  detail.getActuallySum() - detail.getBeginBalance();
 
 
                        if (gainLoss > 0) {
 
                            //盘盈 盘多的加入,效期长的
                            SysStoreInfo sysStoreInfo = storeInfos.get(0);
                            sysStoreInfo.setStoreTotal(sysStoreInfo.getStoreTotal()+gainLoss);
                            sysStoreInfoDao.update(sysStoreInfo);
 
                            //新增入库记录
                            SysInstoreDetail instoreDetail=new SysInstoreDetail();
                            instoreDetail.setSkuId(detail.getSkuId());
                            instoreDetail.setAmount(new Double(gainLoss));
                            instoreDetail.setPrice(sysStoreInfo.getGoodsPrice());
                            instoreDetail.setBatch(sysStoreInfo.getBatch());
                            instoreDetail.setPriceTotal(MoneyUtil.mul(Double.parseDouble(instoreDetail.getAmount() + ""),
                                    instoreDetail.getPrice()));
                            inStoreDetails.add(instoreDetail);
 
 
 
                        } else if (gainLoss < 0) {
 
                            //盘亏
                            for(int i=storeInfos.size()-1 ;i>=0; i--){
 
                                SysStoreInfo sysStoreInfo =  storeInfos.get(i);
                                //只有库存大于0的才能被扣减
                                if(sysStoreInfo.getStoreTotal()>0){
                                    double surplus=sysStoreInfo.getStoreTotal()-Math.abs(gainLoss);
 
                                    //新增出库
                                    SysOutStoreItem outStoreItem=new SysOutStoreItem();
                                    outStoreItem.setStoreId(sysStoreInfo.getId());
                                    outStoreItem.setSkuId(detail.getSkuId());
 
 
                                    if(surplus>=0){
                                        outStoreItem.setAmount(new Double(Math.abs(gainLoss)));
                                        sysStoreInfo.setStoreTotal(surplus);
                                        sysStoreInfoDao.update(sysStoreInfo);
 
                                    }else{
                                        outStoreItem.setAmount(new Double(sysStoreInfo.getStoreTotal()));
                                        sysStoreInfo.setStoreTotal(0D);
                                        sysStoreInfoDao.update(sysStoreInfo);
 
                                    }
                                    outStoreDetails.add(outStoreItem);
 
 
                                    //本次扣减完后,剩余数量大于0,结束扣减循环
                                    if(surplus>0){
                                        break;
                                    }else{
                                        gainLoss=surplus;
                                    }
                                }
 
 
                            }
                        }
                    }
                }
 
            }
            // 修改盘点单的审核状态为已完成
            getCheckInfo.setCheckStatus(Dictionary.CHECK_STATUS_YWC);
            //记录出入库记录
 
            if(CollectionUtils.isNotEmpty(inStoreDetails)){
                addInstoreDetails(user,getCheckInfo,inStoreDetails);
            }
            if(CollectionUtils.isNotEmpty(outStoreDetails)){
                addOutstoreDetails(user,getCheckInfo,outStoreDetails);
            }
 
 
        } else if (Dictionary.CHECK_STATUS_SHWTG.equals(checkInfoVo.getCheckStatus())) {
            getCheckInfo.setCheckStatus(Dictionary.CHECK_STATUS_SHWTG);
        } else {
            throw new GlobleException("盘点单审核状态非法");
        }
        getCheckInfo.setAppRemark(checkInfoVo.getAppRemark());
        return sysCheckInfoDao.update(getCheckInfo);
    }
 
    /**'
     * 根据盘点自动生成出库记录
     * @param user
     *
     */
    private void addOutstoreDetails(SysUsers user, SysCheckInfo checkInfoVo, List<SysOutStoreItem> outStoreItems) {
        SysOutStore sysOutStore=new SysOutStore();
        sysOutStore.setCreateBy(SystemConstance.SYSTEM_USER);
        sysOutStore.setUpdateBy(SystemConstance.SYSTEM_USER);
        sysOutStore.setStaffId(user.getSuId());
        sysOutStore.setOutStoreNo(codeService.getOutStoreCode());
        sysOutStore.setCheckStatus(Dictionary.CHECK_STATUS_SHTG);
        sysOutStore.setType("盘亏出库");
        sysOutStore.setTime(new Date());
        sysOutStore.setRemark("根据盘点自动入库,单号:"+checkInfoVo.getCheckNo());
        sysOutStore.setOutStoreNo(codeService.getOutStoreCode());
        sysOutStore.setShopId(user.getShopId());
        sysOutStore.setCompanyId(user.getCompanyId());
        sysOutStoreDao.insert(sysOutStore);
        outStoreItems.forEach(sysOutStoreItem -> sysOutStoreItem.setOutStoreId(sysOutStore.getId()));
        sysOutStoreItemDao.batchInsert(outStoreItems);
 
 
    }
 
    /**
     * 根据盘点单生成入库记录
     * @param checkInfoVo
     * @param inStoreDetails
     */
    private void addInstoreDetails(SysUsers users,SysCheckInfo checkInfoVo, List<SysInstoreDetail> inStoreDetails) {
        SysInstoreInfo sysInstoreInfo=new SysInstoreInfo();
        sysInstoreInfo.setShopId(users.getShopId());
        sysInstoreInfo.setMakingmanId(users.getSuId());
        sysInstoreInfo.setMakingmanName(SystemConstance.SYSTEM_USER);
        sysInstoreInfo.setInstoreType("盘盈入库");
        sysInstoreInfo.setCompanyId(users.getCompanyId());
        sysInstoreInfo.setCheckStatus(Dictionary.CHECK_STATUS_SHTG);
        sysInstoreInfo.setCreateBy(SystemConstance.SYSTEM_USER);
        sysInstoreInfo.setUpdateBy(SystemConstance.SYSTEM_USER);
        sysInstoreInfo.setInstoreId(DateUtil.getTimeMark());
        sysInstoreInfo.setInstoreDate(new Date());
        sysInstoreInfo.setRemark("根据盘点自动入库,单号:"+checkInfoVo.getCheckNo());
        sysInstoreInfo.setStoreId(checkInfoVo.getCheckStoreid());
        sysInstoreInfo.setSumall( inStoreDetails.stream().mapToDouble(SysInstoreDetail::getAmount).sum() );
        instoreInfoDao.insert(sysInstoreInfo);
        inStoreDetails.forEach(item->item.setInstoreId(sysInstoreInfo.getId()));
        instoreDetailDao.batchInsert(inStoreDetails);
    }
 
 
    @Override
    public int remove(List<Long> list) {
        return sysCheckInfoDao.deleteByIds(list);
    }
 
}