| 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); | 
|     } | 
|   | 
| } |