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 instoreDetailsFromObj = sysInstoreInfo.getInstoreDetails(); List addList = new ArrayList(); 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 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 instoreDetails = instoreDetailDao.selectByOrderId(sysInstoreInfo.getId()); if (CollectionUtils.isNotEmpty(instoreDetails)) { sysInstoreDetailDao.deleteByInstoreId(id); return sysInstoreInfoDao.deleteById(id); } else { return 0; } } @Override public List findInPage(SysInstoreInfo sysInstoreInfo, PaginationVO pageVo) { return sysInstoreInfoDao.selectInPage(sysInstoreInfo, pageVo); } @Override public List 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 instoreDetails = instoreDetailDao.selectByOrderId(sysInstoreInfo.getId()); // 需要插入的库存 List 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 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); } }