935090232@qq.com
2021-10-30 caa15383382af86c00f7176c1d24280a7da6d9e2
zq-erp/src/main/java/com/matrix/system/hive/service/imp/SysProjServicesServiceImpl.java
@@ -1,7 +1,8 @@
package com.matrix.system.hive.service.imp;
import cn.hutool.core.collection.CollUtil;
import com.matrix.component.rabbitmq.RabiitMqTemplate;
import com.google.common.collect.Lists;
import com.matrix.component.asyncmessage.AsyncMessageManager;
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
@@ -14,8 +15,10 @@
import com.matrix.system.app.vo.ServiceOrderListVo;
import com.matrix.system.common.bean.BusParameterSettings;
import com.matrix.system.common.bean.SysUsers;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.dao.BusParameterSettingsDao;
import com.matrix.system.common.dao.SysUsersDao;
import com.matrix.system.common.service.BusParameterSettingService;
import com.matrix.system.constance.Dictionary;
import com.matrix.system.hive.bean.*;
import com.matrix.system.hive.dao.*;
@@ -24,16 +27,16 @@
import com.matrix.system.score.constant.ScoreSettingConstant;
import com.matrix.system.score.entity.ScoreVipDetail;
import com.matrix.system.score.service.ScoreVipDetailService;
import com.matrix.system.shopXcx.mqTask.MQTaskRouting;
import com.matrix.system.shopXcx.mqTask.AsyncMessageRouting;
import com.matrix.system.wechart.templateMsg.UniformMsgParam;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
@@ -66,7 +69,8 @@
    private SysOutStoreItemDao sysOutStoreItemDao;
    @Autowired
    private SysBedInfoDao sysBedInfoDao;
    @Autowired
    TaiYanAliyunSmsService taiYanAliyunSmsService;
    @Autowired
    private SysStoreInfoDao storeInfoDao;
@@ -110,11 +114,14 @@
    private ShoppingGoodsService shoppingGoodsService;
    @Autowired
    private RabiitMqTemplate rabiitMqTemplate;
    private AsyncMessageManager asyncMessageManager;
    @Autowired
    BusParameterSettingsDao busParameterSettingsDao;
    @Autowired
    BusParameterSettingService busParameterSettingService;
    @Autowired
    ScoreVipDetailService scoreVipDetailService;
@@ -122,9 +129,9 @@
    @Autowired
    SysVipInfoDao sysVipInfoDao;
    @Value("${evn}")
    private String evn;
    @Autowired
    ShoppingGoodsAssembleDao goodsAssembleDao;
    /**
     * 新增服务单 jyy
@@ -144,10 +151,13 @@
        }
        sysProjServices.setCreateTime(new Date());
        sysProjServices.setServiceNo(codeService.getServiceOrderCode());
        if (sysProjServices.getId() == null) {
            sysProjServices.setCreateTime(new Date());
            sysProjServices.setServiceNo(codeService.getServiceOrderCode());
            int i = sysProjServicesDao.insert(sysProjServices);
        }
        Double hkPrice = 0.0;
        int i = sysProjServicesDao.insert(sysProjServices);
        // 计算项目总时长
        int totalTime = 0;
        //处理订单明细
@@ -195,20 +205,22 @@
                    taocan.setIsOver(Dictionary.FLAG_YES_Y);
                    taocan.setSurplusCount(0);
                    taocan.setStatus(Dictionary.TAOCAN_STATUS_WX);
                } else if (Dictionary.FLAG_YES_Y.equals(taocan.getIsCourse())) {
                    if (StringUtils.isBlank(taocan.getIsInfinite()) || Dictionary.FLAG_NO_N.equals(taocan.getIsInfinite())) {
                        //任选套餐检查套餐整体剩余次数
                        int tcSurplusCount = taocan.getSurplusCount() - sysBeauticianState.getCount();
                        if (tcSurplusCount < 0) {
                            throw new GlobleException(taocan.getProjName() + "已经达到最大使用次数");
                        } else {
                            taocan.setSurplusCount(tcSurplusCount);
                        }
                } else if (
                        Dictionary.FLAG_NO_N.equals(taocan.getIsCourse())
                                || (Dictionary.FLAG_YES_Y.equals(taocan.getIsCourse())
                                && (StringUtils.isBlank(taocan.getIsInfinite()) || Dictionary.FLAG_NO_N.equals(taocan.getIsInfinite())))) {
                        if (tcSurplusCount == 0) {
                            taocan.setIsOver(Dictionary.FLAG_YES_Y);
                            taocan.setStatus(Dictionary.TAOCAN_STATUS_WX);
                        }
                    //任选套餐检查套餐整体剩余次数
                    int tcSurplusCount = taocan.getSurplusCount() - sysBeauticianState.getCount();
                    if (tcSurplusCount < 0) {
                        throw new GlobleException(taocan.getProjName() + "已经达到最大使用次数");
                    } else {
                        taocan.setSurplusCount(tcSurplusCount);
                    }
                    if (tcSurplusCount == 0) {
                        taocan.setIsOver(Dictionary.FLAG_YES_Y);
                        taocan.setStatus(Dictionary.TAOCAN_STATUS_WX);
                    }
                }
@@ -219,6 +231,7 @@
                    Date useInvalidTime = shoppingGoodsService.calInvalidTime(taocanInfo, 2, sysProjUse.getFailTime());
                    taocan.setFailTime(useInvalidTime);
                }
                taocan.setBalance(MoneyUtil.sub(taocan.getBalance(), bcxfje));
                sysProjUseDao.updateSurplusCount(taocan);
            }
@@ -229,12 +242,12 @@
            sysBeauticianState.setProjId(sysProjInfo.getId());
            sysBeauticianState.setServicesId(sysProjServices.getId());
            sysBeauticianState.setShopId(sysProjServices.getShopId());
            sysBeauticianState.setState(Dictionary.BEATUI_STATE_DYY);
            sysBeauticianState.setState(sysProjServices.getState());
            beauticianStateDao.insert(sysBeauticianState);
        }
        // 设置项目总时长
        sysProjServices.setTotalTime(totalTime);
        sysProjServices.setMoney(hkPrice);
        sysProjServices.setMoney(new BigDecimal(hkPrice).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue());
        sysProjServicesDao.update(sysProjServices);
        return sysProjServices;
    }
@@ -252,6 +265,12 @@
        for (SysBeauticianState sysBeauticianState : sysProjServices.getServiceItems()) {
            SysProjUse sysProjUse = projUseService.findById(sysBeauticianState.getPuseId());
            //检查是否已经处于无效状态
            if (Dictionary.TAOCAN_STATUS_WX.equals(sysProjUse.getStatus())) {
                return new VerifyResult(true, sysProjUse.getProjName() + "项目已经失效");
            }
            int kjcs = 1;
            if (sysProjUse.getTaocanId() != null) {
                kjcs = sysBeauticianState.getCount() * sysProjUse.getDeductionNum();
@@ -358,8 +377,8 @@
        /** 验证数据是否正确 */
        SysProjServices checkProjServices = sysProjServicesDao.selectById(projServices.getId());
        // 验证服务单是否存在或者取消
        if (!checkProjServices.getState().equals(Dictionary.SERVICE_STATU_DYY)
                && !checkProjServices.getState().equals(Dictionary.SERVICE_STATU_YYCG)) {
        if (checkProjServices.getState().equals(Dictionary.SERVICE_STATU_YYQX)
                || checkProjServices.getState().equals(Dictionary.SERVICE_STATU_FFJS)) {
            throw new GlobleException("该服务单已被取消或者不存在!");
        }
        // 验证是否选择床位
@@ -386,7 +405,7 @@
        }
        // 判断是否是编辑,还是新增,编辑则需要先释放资源
        if (checkProjServices.getState().equals(Dictionary.SERVICE_STATU_YYCG)) {
        if (!checkProjServices.getState().equals(Dictionary.SERVICE_STATU_DYY)) {
            // 释放床位资源
            bedStateDao.deleteByServiceId(checkProjServices.getId());
        }
@@ -414,8 +433,10 @@
        }
        // 设置成功状态
        projServices.setState(Dictionary.SERVICE_STATU_XPL);
        if (Dictionary.SERVICE_STATU_DYY.equals(projServices.getState())) {
            // 设置成功状态
            projServices.setState(Dictionary.SERVICE_STATU_XPL);
        }
        // 判断当前门店是否有历史美疗师
        LastestWorkBeatuistaff lastWorkStaff = new LastestWorkBeatuistaff();
        lastWorkStaff.setShopId(checkProjServices.getShopId());
@@ -436,7 +457,49 @@
            lastList.add(lastWork);
        }
        LastestWorkBeatuistaffDao.batchInsert(lastList);
        return sysProjServicesDao.update(projServices);
        int i = sysProjServicesDao.update(projServices);
        //根据配置是否完成自动配料
        if (busParameterSettingService.isSettingOpen(AppConstance.OPEN_SERVICE_ORDER_AUTO_BATCHING, checkProjServices.getCompanyId())){
            autoBatching(projServices);
        }
        return i;
    }
    /**
     * 根据配置是否完成自动配料
     *
     * @param projServices
     */
    private void autoBatching(SysProjServices projServices) {
        //获取最新的服务单信息
        projServices=findById(projServices.getId());
        List<SysBeauticianState> beauticianStateList = beauticianStateDao.selectBySerIds(projServices.getId());
        List<SysOutStoreItem> outStoreItemList = Lists.newLinkedList();
        beauticianStateList.stream().forEach(item -> {
            List<ShoppingGoodsAssemble> shoppingGoodsAssembles = goodsAssembleDao.selectGoodsByShoppingGoodsIdAndType(item.getProjId(), ShoppingGoods.SHOPPING_GOODS_TYPE_JJCP);
            if (CollUtil.isNotEmpty(shoppingGoodsAssembles)) {
                outStoreItemList.addAll(shoppingGoodsAssembles.stream().map(assemble -> {
                    SysOutStoreItem outStoreItem = new SysOutStoreItem();
                    outStoreItem.setSkuId(assemble.getAssembleGoodId());
                    outStoreItem.setAmount(Double.parseDouble(assemble.getTotal()+""));
                    outStoreItem.setRemark("自动配料");
                    return outStoreItem;
                }).collect(Collectors.toList()));
            }
        });
        //组合配料参数
        projServices.setOutStoreItem(outStoreItemList);
        //调用配料出库方法
        modifyPLProjServices(projServices);
    }
@@ -498,7 +561,7 @@
            sysOutStoreItemDao.deleteByOrderId(sysOutStore.getId());
        }
        //删除积分
        scoreVipDetailService.removeByBusinessId(null,checkProjServices.getVipId(), checkProjServices.getId());
        scoreVipDetailService.removeByBusinessId(checkProjServices.getVipId(), checkProjServices.getId());
        //更新服务单状态
        return sysProjServicesDao.update(checkProjServices);
@@ -512,12 +575,20 @@
    private void backProjCount(SysProjServices projServices) {
        List<SysBeauticianState> beauticianStateList = beauticianStateDao.selectBySerIds(projServices.getId());
        backProjCountItems(beauticianStateList, 1);
    }
    private void backProjCountItems(List<SysBeauticianState> beauticianStateList, int type) {
        Map<Long, List<SysProjUse>> taocanMap = new HashMap<>();
        // 回退项目余额
        for (SysBeauticianState beauticianState : beauticianStateList) {
            // 该修改为 有效日期判断更新所有
            beauticianState.setState(Dictionary.BEATUI_STATE_YYQX);
            beauticianStateDao.update(beauticianState);
            if (type == 1) {
                // 该修改为 有效日期判断更新所有
                beauticianState.setState(Dictionary.BEATUI_STATE_YYQX);
                beauticianStateDao.update(beauticianState);
            } else {
                beauticianStateDao.deleteById(beauticianState.getId());
            }
            SysProjUse sysProjUse = sysProjUseDao.selectById(beauticianState.getPuseId());
            if (sysProjUse != null && sysProjUse.getId() != null) {
@@ -529,23 +600,28 @@
                if (sysProjUse.getSurplusCount() == 0) {
                    upProjUse.setIsOver(Dictionary.DELETED_N);
                    upProjUse.setStatus(Dictionary.MONEYCARD_STATUS_YX);
                    if (sysProjUse.getTaocanId() != null) {
                        //TODO 更新套餐状态为有效
                        SysProjUse taocanProjUse = sysProjUseDao.selectById(sysProjUse.getTaocanId());
                        taocanProjUse.setIsOver(Dictionary.DELETED_N);
                        taocanProjUse.setStatus(Dictionary.MONEYCARD_STATUS_YX);
                        if (Dictionary.FLAG_YES_Y.equals(taocanProjUse.getIsCourse())) {
                            if (StringUtils.isBlank(taocanProjUse.getIsInfinite()) || Dictionary.FLAG_NO_N.equals(taocanProjUse.getIsInfinite())) {
                                taocanProjUse.setSurplusCount(taocanProjUse.getSurplusCount() + beauticianState.getCount());
                            }
                        }
                        sysProjUseDao.update(taocanProjUse);
                    }
                }
                int surplus = sysProjUse.getSurplusCount() + beauticianState.getCount();
                upProjUse.setSurplusCount(surplus);
                upProjUse.setBalance(MoneyUtil.add(sysProjUse.getBalance(), sysProjUse.getPrice()));
                BigDecimal backBlance = new BigDecimal(sysProjUse.getPrice() * beauticianState.getCount());
                upProjUse.setBalance(MoneyUtil.add(sysProjUse.getBalance(), backBlance.doubleValue()));
                if (sysProjUse.getTaocanId() != null) {
                    SysProjUse taocanProjUse = sysProjUseDao.selectById(sysProjUse.getTaocanId());
                    taocanProjUse.setIsOver(Dictionary.DELETED_N);
                    taocanProjUse.setStatus(Dictionary.MONEYCARD_STATUS_YX);
                    taocanProjUse.setBalance(MoneyUtil.add(taocanProjUse.getBalance(), backBlance.doubleValue()));
                    if (Dictionary.FLAG_YES_Y.equals(taocanProjUse.getIsCourse())) {
                        if (StringUtils.isBlank(taocanProjUse.getIsInfinite()) || Dictionary.FLAG_NO_N.equals(taocanProjUse.getIsInfinite())) {
                            taocanProjUse.setSurplusCount(taocanProjUse.getSurplusCount() + beauticianState.getCount());
                        }
                    }
                    sysProjUseDao.update(taocanProjUse);
                }
                // 取消时,判断该项目之前是否有使用过,若使用过则非第一次使用,则不更新有效日期,若未使用表明此次为第一次使用,则更新有效日期为购买日期或统一失效日期
                List<SysBeauticianState> hasBeautician = beauticianStateDao.selectBeauticianStateByPuseIdAndNoStatus(beauticianState.getPuseId(), Dictionary.BEATUI_STATE_YYQX);
@@ -583,6 +659,16 @@
                }
            }
        }
    }
    @Override
    public int deleteProjServiceItemById(Long id) {
        SysBeauticianState sysBeauticianState = beauticianStateDao.selectById(id);
        List<SysBeauticianState> list = new ArrayList<>();
        list.add(sysBeauticianState);
        backProjCountItems(list, 2);
        return 1;
    }
    // 划扣 jyy
@@ -626,10 +712,9 @@
            //发送微信公众号提醒
            UniformMsgParam uniformMsgParam = new UniformMsgParam(projServices.getCompanyId(), UniformMsgParam.GZH_FWWC);
            uniformMsgParam.put("serviceId", projServices.getId());
            rabiitMqTemplate.sendMsg(MQTaskRouting.SEND_UNIFORM_TEMPLATE_MSG + evn, uniformMsgParam.toJSONString());
            asyncMessageManager.sendMsg(AsyncMessageRouting.SEND_UNIFORM_TEMPLATE_MSG, uniformMsgParam);
            //发送划扣短信提醒
            taiYanAliyunSmsService.sendHkNotice(projServices);
            return result;
        }
@@ -642,7 +727,7 @@
     */
    private void addVipScore(SysProjServices projServices) {
        SysVipInfo vipInfo =sysVipInfoDao.selectById(projServices.getVipId());
        SysVipInfo vipInfo = sysVipInfoDao.selectById(projServices.getVipId());
        List<SysBeauticianState> sysBeauticianStates = beauticianStateDao.selectBySerIds(projServices.getId());
@@ -692,13 +777,13 @@
            }
        }
        int selfScore =principalConsumScore[0]+giveConsumScore[0];
        int parentScore =principalConsumScore[1]+giveConsumScore[1];
        int topParentScore =principalConsumScore[2]+giveConsumScore[2];
        int selfScore = principalConsumScore[0] + giveConsumScore[0];
        int parentScore = principalConsumScore[1] + giveConsumScore[1];
        int topParentScore = principalConsumScore[2] + giveConsumScore[2];
        //添加自己的积分
        if (selfScore > 0) {
            scoreVipDetailService.addScore(null,
            scoreVipDetailService.addScore(
                    vipInfo.getId(),
                    projServices.getCreateStaffId(),
                    projServices.getShopId(),
@@ -713,7 +798,7 @@
            //推荐注册老带新积分奖励
            SysVipInfo referrerVip = sysVipInfoDao.selectById(vipInfo.getRecommendId());
            if (parentScore > 0) {
                scoreVipDetailService.addScore(null,
                scoreVipDetailService.addScore(
                        referrerVip.getId(),
                        projServices.getCreateStaffId(),
                        projServices.getShopId(),
@@ -727,7 +812,7 @@
            if (referrerVip.getRecommendId() != null) {
                SysVipInfo topVipInfo = sysVipInfoDao.selectById(referrerVip.getRecommendId());
                if (topParentScore > 0) {
                    scoreVipDetailService.addScore(null,
                    scoreVipDetailService.addScore(
                            topVipInfo.getId(),
                            projServices.getCreateStaffId(),
                            projServices.getShopId(),
@@ -776,6 +861,7 @@
        if (!projServices.getState().equals(Dictionary.SERVICE_STATU_XPL)) {
            throw new GlobleException("该服务单状态为" + projServices.getState() + ",不可以进行当前操作!");
        }
        projServicesVo.setCompanyId(projServices.getCompanyId());
        if (isNeedOutStore(projServicesVo)) {
            // 生成出库单
@@ -854,6 +940,8 @@
        if (skipServiceOrderStep(Dictionary.SERVICE_OVER_BEGIN_END)) {
            projServices.setStartTime(new Date());
            projServices.setState(Dictionary.SERVICE_STATU_FWWC);
        } else {
            projServices.setState(Dictionary.SERVICE_STATU_PLWC);
        }
        // 释放床位资源
//        SysBedState checkBedState = sysBedStateDao.selectBySerIdAndBedId(projServices.getBedId(), projServices.getId());
@@ -879,10 +967,14 @@
     * @return
     */
    private boolean isNeedOutStore(SysProjServices projServicesVo) {
        if (CollectionUtils.isNotEmpty(projServicesVo.getOutStoreItem())) {
            for (SysOutStoreItem item : projServicesVo.getOutStoreItem()) {
                if (item.getAmount() != null && item.getAmount() > 0) {
                    return true;
            BusParameterSettings manageStockSetting = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WAREHOUSE_MANAGE_STOCK, projServicesVo.getCompanyId());
            if (AppConstance.IS_Y.equals(manageStockSetting.getParamValue())) {
                for (SysOutStoreItem item : projServicesVo.getOutStoreItem()) {
                    if (item.getAmount() != null && item.getAmount() > 0) {
                        return true;
                    }
                }
            }
        }
@@ -992,7 +1084,8 @@
    /**
     * 跳过服务单某步骤
     */
    private boolean skipServiceOrderStep(String step) {
    @Override
    public boolean skipServiceOrderStep(String step) {
        SysUsers users = (SysUsers) WebUtil.getSession().getAttribute(MatrixConstance.LOGIN_KEY);
        ParameterSettings ps = new ParameterSettings();
        ps.setCompanyId(users.getCompanyId());
@@ -1011,4 +1104,27 @@
        return flag;
    }
    @Override
    public int confirmServiceOrder(Long id) {
        SysProjServices services = new SysProjServices();
        services.setId(id);
        services.setState(Dictionary.BEATUI_STATE_DYY);
        int i = modify(services);
        if (i > 0) {
            //发送微信公众号提醒
            services = findById(services.getId());
            UniformMsgParam uniformMsgParam = new UniformMsgParam(services.getCompanyId(), UniformMsgParam.GZH_YYCG);
            uniformMsgParam.put("serviceId", services.getId());
            asyncMessageManager.sendMsg(AsyncMessageRouting.SEND_UNIFORM_TEMPLATE_MSG, uniformMsgParam);
            taiYanAliyunSmsService.sendYycgNotice(services);
        }
        return i;
    }
}