|
@@ -0,0 +1,304 @@
|
|
|
+package com.gree.mall.manager.logic.material.manage;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.gree.mall.manager.bean.admin.AdminUserCom;
|
|
|
+import com.gree.mall.manager.bean.material.manage.WebsitMPurchaseBean;
|
|
|
+import com.gree.mall.manager.bean.material.manage.WebsitMPurchaseItemBean;
|
|
|
+import com.gree.mall.manager.bean.material.manage.WebsitPurchaseApplyVO;
|
|
|
+import com.gree.mall.manager.bean.material.vender.WebsitVenderGoodsRelaVO;
|
|
|
+import com.gree.mall.manager.commonmapper.MaterialMapper;
|
|
|
+import com.gree.mall.manager.enums.IsYesNoEnum;
|
|
|
+import com.gree.mall.manager.enums.material.MaterialFlagEnum;
|
|
|
+import com.gree.mall.manager.enums.material.StateEnum;
|
|
|
+import com.gree.mall.manager.enums.material.WebsitGoodsTypeEnum;
|
|
|
+import com.gree.mall.manager.exception.RemoteServiceException;
|
|
|
+import com.gree.mall.manager.logic.common.CommonLogic;
|
|
|
+import com.gree.mall.manager.logic.material.vender.VenderGoodsLogic;
|
|
|
+import com.gree.mall.manager.plus.entity.WebsitPurchase;
|
|
|
+import com.gree.mall.manager.plus.entity.WebsitPurchaseItem;
|
|
|
+import com.gree.mall.manager.plus.entity.WebsitVender;
|
|
|
+import com.gree.mall.manager.plus.service.WebsitPurchaseItemService;
|
|
|
+import com.gree.mall.manager.plus.service.WebsitPurchaseService;
|
|
|
+import com.gree.mall.manager.plus.service.WebsitVenderService;
|
|
|
+import com.gree.mall.manager.zfire.bean.QueryParamBean;
|
|
|
+import com.gree.mall.manager.zfire.bean.ZfireParamBean;
|
|
|
+import com.gree.mall.manager.zfire.util.FieldUtils;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.*;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class WebsitMPurchaseLogic {
|
|
|
+
|
|
|
+ private final CommonLogic commonLogic;
|
|
|
+ private final MaterialMapper materialMapper;
|
|
|
+ private final WebsitPurchaseService websitPurchaseService;
|
|
|
+ private final WebsitPurchaseItemService websitPurchaseItemService;
|
|
|
+ private final VenderGoodsLogic venderGoodsLogic;
|
|
|
+ private final WebsitVenderService websitVenderService;
|
|
|
+
|
|
|
+ public IPage<WebsitPurchaseApplyVO> page(ZfireParamBean zfireParamBean) {
|
|
|
+ AdminUserCom adminUser = commonLogic.getAdminUser();
|
|
|
+ FieldUtils.materialParam(zfireParamBean, WebsitPurchaseApplyVO.class, adminUser);
|
|
|
+
|
|
|
+ return materialMapper.websitPurchaseApplyPage(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
|
|
|
+ }
|
|
|
+
|
|
|
+ public WebsitMPurchaseBean detail(String purchaseId) {
|
|
|
+ AdminUserCom adminUser = commonLogic.getAdminUser();
|
|
|
+ WebsitMPurchaseBean bean = new WebsitMPurchaseBean();
|
|
|
+ WebsitPurchase purchase = websitPurchaseService.getById(purchaseId);
|
|
|
+ BeanUtils.copyProperties(purchase, bean);
|
|
|
+ bean.setImageUrl(purchase.getImageUrl());
|
|
|
+ String companyId = Objects.isNull(adminUser.getAdminCompanyWechat()) ? null : adminUser.getAdminCompanyWechat().getCompanyWechatId();
|
|
|
+ List<WebsitMPurchaseItemBean> list = websitPurchaseItemService.lambdaQuery()
|
|
|
+ .eq(StringUtils.isNotBlank(companyId), WebsitPurchaseItem::getCompanyWechatId, companyId)
|
|
|
+ .eq(WebsitPurchaseItem::getPurchaseId, purchaseId)
|
|
|
+ .list()
|
|
|
+ .stream()
|
|
|
+ .map(v -> {
|
|
|
+ WebsitMPurchaseItemBean item = new WebsitMPurchaseItemBean();
|
|
|
+ BeanUtils.copyProperties(v, item);
|
|
|
+ return item;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ bean.setItems(list);
|
|
|
+
|
|
|
+ return bean;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public void add(WebsitMPurchaseBean bean) {
|
|
|
+ AdminUserCom adminUser = commonLogic.getAdminUser();
|
|
|
+
|
|
|
+ if (adminUser.getType() == 2) {
|
|
|
+ throw new RemoteServiceException("平台账号禁用操作");
|
|
|
+ }
|
|
|
+
|
|
|
+ bean.setCompanyWechatId(adminUser.getAdminCompanyWechat().getCompanyWechatId());
|
|
|
+ bean.setCompanyWechatName(adminUser.getAdminCompanyWechat().getCompanyName());
|
|
|
+
|
|
|
+ bean.setFlag(MaterialFlagEnum.SAVE.getKey());
|
|
|
+
|
|
|
+ // 检查参数值
|
|
|
+ this.validApplyParams(bean);
|
|
|
+
|
|
|
+ WebsitPurchase purchase = new WebsitPurchase();
|
|
|
+ BeanUtils.copyProperties(bean, purchase);
|
|
|
+
|
|
|
+ purchase.insert();
|
|
|
+
|
|
|
+ List<WebsitPurchaseItem> itemList = new ArrayList<>();
|
|
|
+ for (WebsitMPurchaseItemBean beanItem : bean.getItems()) {
|
|
|
+ WebsitPurchaseItem item = new WebsitPurchaseItem();
|
|
|
+ BeanUtils.copyProperties(beanItem, item);
|
|
|
+ item.setPurchaseId(purchase.getPurchaseId())
|
|
|
+ .setCompanyWechatId(bean.getCompanyWechatId())
|
|
|
+ .setWebsitId(bean.getWebsitId())
|
|
|
+ .setWebsitName(bean.getWebsitName())
|
|
|
+ .setGoodsType(WebsitGoodsTypeEnum.M.getKey());
|
|
|
+ itemList.add(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ websitPurchaseItemService.saveBatch(itemList);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void edit(WebsitMPurchaseBean bean) {
|
|
|
+ AdminUserCom adminUser = commonLogic.getAdminUser();
|
|
|
+
|
|
|
+ bean.setFlag(MaterialFlagEnum.SAVE.getKey());
|
|
|
+
|
|
|
+ final WebsitPurchase oldPurchase = websitPurchaseService.getById(bean.getPurchaseId());
|
|
|
+
|
|
|
+ if (Objects.isNull(oldPurchase)) {
|
|
|
+ throw new RemoteServiceException("单据不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查参数值
|
|
|
+ this.validApplyParams(bean);
|
|
|
+
|
|
|
+ WebsitPurchase purchase = new WebsitPurchase();
|
|
|
+ BeanUtils.copyProperties(bean, purchase);
|
|
|
+ purchase.setCreateBy(oldPurchase.getCreateBy())
|
|
|
+ .setCreateTime(oldPurchase.getCreateTime());
|
|
|
+
|
|
|
+ purchase.updateById();
|
|
|
+
|
|
|
+ List<WebsitPurchaseItem> itemList = new ArrayList<>();
|
|
|
+ websitPurchaseItemService.lambdaUpdate()
|
|
|
+ .eq(WebsitPurchaseItem::getPurchaseId, bean.getPurchaseId())
|
|
|
+ .remove();
|
|
|
+ for (WebsitMPurchaseItemBean beanItem : bean.getItems()) {
|
|
|
+ WebsitPurchaseItem item = new WebsitPurchaseItem();
|
|
|
+ BeanUtils.copyProperties(beanItem, item);
|
|
|
+ item.setPurchaseId(purchase.getPurchaseId())
|
|
|
+ .setCompanyWechatId(bean.getCompanyWechatId())
|
|
|
+ .setWebsitId(bean.getWebsitId())
|
|
|
+ .setWebsitName(bean.getWebsitName())
|
|
|
+ .setGoodsType(WebsitGoodsTypeEnum.M.getKey());
|
|
|
+ itemList.add(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ websitPurchaseItemService.saveBatch(itemList);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validApplyParams(WebsitMPurchaseBean bean) {
|
|
|
+ if (StringUtils.isBlank(bean.getWebsitId()) || StringUtils.isBlank(bean.getWebsitName())) {
|
|
|
+ throw new RemoteServiceException("请选择网点");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(bean.getVenderId()) || StringUtils.isBlank(bean.getVenderName())) {
|
|
|
+ throw new RemoteServiceException("请选择供应商");
|
|
|
+ }
|
|
|
+ if (CollectionUtil.isEmpty(bean.getItems())) {
|
|
|
+ throw new RemoteServiceException("请先添加明细");
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < bean.getItems().size(); i++) {
|
|
|
+ WebsitMPurchaseItemBean itemBean = bean.getItems().get(i);
|
|
|
+ String preStr = "第" + (i+1) + "行, ";
|
|
|
+ if (StringUtils.isBlank(itemBean.getGoodsId()) || StringUtils.isBlank(itemBean.getGoodsName())) {
|
|
|
+ throw new RemoteServiceException(preStr + "辅材编号不能为空");
|
|
|
+ }
|
|
|
+ if (Objects.isNull(itemBean.getApplyCost())) {
|
|
|
+ throw new RemoteServiceException(preStr + "预估进价不能为空");
|
|
|
+ }
|
|
|
+ if (Objects.isNull(itemBean.getApplyQty()) || itemBean.getApplyQty().compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ throw new RemoteServiceException(preStr + "申请数量不能为空或少于等于0");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查明细重复辅材编号
|
|
|
+ Map<String, List<WebsitMPurchaseItemBean>> goodsGroup = bean.getItems().stream().collect(Collectors.groupingBy(WebsitMPurchaseItemBean::getGoodsId));
|
|
|
+ for (Map.Entry<String, List<WebsitMPurchaseItemBean>> entry : goodsGroup.entrySet()) {
|
|
|
+ if (entry.getValue().size() > 1) {
|
|
|
+ throw new RemoteServiceException("明细有重复的辅材编号 " + entry.getKey());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取供应商商品记录
|
|
|
+ List<WebsitVenderGoodsRelaVO> relaList = this.queryVenderGoods(bean);
|
|
|
+ final Map<String, WebsitVenderGoodsRelaVO> relaMap = relaList.stream()
|
|
|
+ .collect(Collectors.toMap(WebsitVenderGoodsRelaVO::getGoodsId, Function.identity()));
|
|
|
+
|
|
|
+ // 校验供应商商品
|
|
|
+ this.validVendorGoods(bean, relaMap);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validVendorGoods(WebsitMPurchaseBean bean, Map<String, WebsitVenderGoodsRelaVO> relaMap) {
|
|
|
+ for (int i = 0; i < bean.getItems().size(); i++) {
|
|
|
+ WebsitMPurchaseItemBean itemBean = bean.getItems().get(i);
|
|
|
+ String preStr = "第" + (i+1) + "行, ";
|
|
|
+ final WebsitVenderGoodsRelaVO relaGoods = relaMap.get(itemBean.getGoodsId());
|
|
|
+ if (Objects.isNull(relaGoods)) {
|
|
|
+ throw new RemoteServiceException(preStr + "供应商辅材不存在或无效");
|
|
|
+ }
|
|
|
+ if (StrUtil.equals(relaGoods.getStatus().getKey(), StateEnum.OFF.getKey())) {
|
|
|
+ throw new RemoteServiceException(preStr + "供应商辅材资料无效");
|
|
|
+ }
|
|
|
+ if (bean.getFlag().equals(MaterialFlagEnum.SAVE.getKey())) {
|
|
|
+ // 检查进价范围值
|
|
|
+ if (Objects.nonNull(relaGoods.getCostRangeMini())
|
|
|
+ && itemBean.getApplyCost().compareTo(relaGoods.getCostRangeMini()) < 0) {
|
|
|
+ throw new RemoteServiceException(preStr + "不能低于辅材资料最小进价范围值");
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(relaGoods.getCostRangeMax())
|
|
|
+ && itemBean.getApplyCost().compareTo(relaGoods.getCostRangeMax()) > 0) {
|
|
|
+ throw new RemoteServiceException(preStr + "不能高于辅材资料最大进价范围值");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 申请进价*申请数量=申请进价金额
|
|
|
+ itemBean.setApplyCostValue(itemBean.getApplyCost().multiply(itemBean.getApplyQty()).setScale(2, BigDecimal.ROUND_HALF_UP));
|
|
|
+ itemBean.setCheckQty(itemBean.getApplyCost());
|
|
|
+ itemBean.setGoodsCode(relaGoods.getGoodsCode());
|
|
|
+ itemBean.setGoodsSpecification(relaGoods.getGoodsSpecification());
|
|
|
+ itemBean.setGoodsStockUnit(relaGoods.getGoodsStockUnit());
|
|
|
+ itemBean.setParentCategoryId(relaGoods.getParentCategoryId());
|
|
|
+ itemBean.setParentCategoryName(relaGoods.getParentCategoryName());
|
|
|
+ itemBean.setGoodsCategoryId(relaGoods.getGoodsCategoryId());
|
|
|
+ itemBean.setGoodsCategoryName(relaGoods.getCategoryName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<WebsitVenderGoodsRelaVO> queryVenderGoods(WebsitMPurchaseBean bean) {
|
|
|
+ ZfireParamBean zfireParamBean = new ZfireParamBean();
|
|
|
+ zfireParamBean.setPageNum(1);
|
|
|
+ zfireParamBean.setPageSize(-1);
|
|
|
+ zfireParamBean.setParams(new ArrayList<>());
|
|
|
+ QueryParamBean paramBean = new QueryParamBean();
|
|
|
+ paramBean.setParam("a.vender_id").setCompare("=").setValue(bean.getVenderId());
|
|
|
+ QueryParamBean paramBean2 = new QueryParamBean();
|
|
|
+ paramBean2.setParam("a.rela_status").setCompare("=").setValue(StateEnum.ON.getKey());
|
|
|
+ zfireParamBean.getParams().add(paramBean);
|
|
|
+ zfireParamBean.getParams().add(paramBean2);
|
|
|
+
|
|
|
+ final IPage<WebsitVenderGoodsRelaVO> page = venderGoodsLogic.ralaPage(zfireParamBean);
|
|
|
+ if (CollectionUtil.isEmpty(page.getRecords())) {
|
|
|
+ throw new RemoteServiceException(bean.getVenderName() + "供应商没有可供应的辅材记录");
|
|
|
+ }
|
|
|
+ return page.getRecords();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public void batchDel(List<String> purchaseIds) {
|
|
|
+ if (CollectionUtil.isEmpty(purchaseIds)) {
|
|
|
+ throw new RemoteServiceException("请选择记录");
|
|
|
+ }
|
|
|
+
|
|
|
+ websitPurchaseService.lambdaUpdate()
|
|
|
+ .in(WebsitPurchase::getPurchaseId, purchaseIds)
|
|
|
+ .remove();
|
|
|
+
|
|
|
+ websitPurchaseItemService.lambdaUpdate()
|
|
|
+ .in(WebsitPurchaseItem::getPurchaseId, purchaseIds)
|
|
|
+ .remove();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public void revoke(String purchaseId) {
|
|
|
+ if (!websitPurchaseService.lambdaUpdate()
|
|
|
+ .eq(WebsitPurchase::getPurchaseId, purchaseId)
|
|
|
+ .eq(WebsitPurchase::getFlag, MaterialFlagEnum.SUBMIT.getKey())
|
|
|
+ .eq(WebsitPurchase::getIsRecheck, IsYesNoEnum.NO.getKey())
|
|
|
+ .set(WebsitPurchase::getFlag, MaterialFlagEnum.SAVE.getKey())
|
|
|
+ .update()) {
|
|
|
+ throw new RemoteServiceException("撤消失败,单据状态已发生变化,非“提交待核实”");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public void submit(String purchaseId) {
|
|
|
+ AdminUserCom adminUser = commonLogic.getAdminUser();
|
|
|
+ WebsitPurchase purchase = websitPurchaseService.getById(purchaseId);
|
|
|
+
|
|
|
+ if (Objects.isNull(purchase) || !StrUtil.equals(purchase.getFlag(), MaterialFlagEnum.SAVE.getKey())) {
|
|
|
+ throw new RemoteServiceException("单据不存在或状态已发生变化");
|
|
|
+ }
|
|
|
+
|
|
|
+ final WebsitVender vender = websitVenderService.getById(purchase.getVenderId());
|
|
|
+
|
|
|
+ // 供应商为自动审核时
|
|
|
+ if (StrUtil.equals(vender.getIsAuto(), IsYesNoEnum.YES.getKey())) {
|
|
|
+ purchase.setIsRecheck(IsYesNoEnum.YES.getKey())
|
|
|
+ .setCheckBy(adminUser.getNickName())
|
|
|
+ .setCheckTime(DateUtil.date());
|
|
|
+ }
|
|
|
+
|
|
|
+ purchase.setFlag(MaterialFlagEnum.SUBMIT.getKey())
|
|
|
+ .setSubmitBy(adminUser.getNickName())
|
|
|
+ .setSubmitTime(DateUtil.date())
|
|
|
+ .updateById();
|
|
|
+ }
|
|
|
+}
|