PaymentServiceImpl.java 9 KB
package vion.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.date.TimeUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vion.dto.PaymentDTO;
import vion.mapper.PaymentMapper;
import vion.model.Contract;
import vion.model.Payment;
import vion.model.RContractUser;
import vion.service.IContractPaymentService;
import vion.service.IContractService;
import vion.service.IPaymentService;
import vion.service.IRContractUserService;
import vion.third.DingMod;
import vion.utils.JsonUtil;
import vion.vo.PaymentVO;
import vion.vo.RoleVO;
import vion.vo.UserVO;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author vion
 * @date 2023/12/5
 */
@Service
@RequiredArgsConstructor
public class PaymentServiceImpl extends MPJBaseServiceImpl<PaymentMapper, Payment> implements IPaymentService {

    private final IContractService contractService;
    private final IContractPaymentService contractPaymentService;
    private final IRContractUserService contractUserService;
    private final DingMod dingMod;
    private final Converter converter;

    @Override
    public Page<PaymentVO> list(PaymentDTO dto) {
        UserVO userVO = (UserVO) StpUtil.getTokenSession().get("curLoginUser");
        String saleName = Opt.ofEmptyAble(userVO.getRoleVOList())
                .map(l -> l.stream().map(RoleVO::getCode).collect(Collectors.toList()))
                .map(roleCodeList -> {
                    if (!roleCodeList.contains("admin") && !roleCodeList.contains("shangwu") && !roleCodeList.contains("caiwu")) {
                        return userVO.getUsername();
                    } else {
                        return "";
                    }
                }).orElse("未知");

        MPJLambdaWrapper<Payment> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, Payment.class))
                .selectAll(Payment.class)
                .leftJoin(Contract.class, Contract::getContractNo, Payment::getContractNo)
                .eq(StrUtil.isNotBlank(saleName), Contract::getSaleName, saleName)
                .between(ArrayUtil.isAllNotNull(dto.getCollectionTimeStart(), dto.getCollectionTimeEnd()), Payment::getCollectionTime, dto.getCollectionTimeStart(), dto.getCollectionTimeEnd());
        Page<PaymentVO> page = Page.of(dto.getPageNum(), dto.getPageSize());
        Opt.ofNullable(dto.getOrderItem())
                .ifPresent(page::addOrder);
        return this.selectJoinListPage(page, PaymentVO.class, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String save(List<PaymentDTO> dto) {
        List<Payment> paymentList = converter.convert(dto, Payment.class);
        paymentList.forEach(p -> {
            Payment payment = this.lambdaQuery().eq(Payment::getSerialNo, p.getSerialNo()).one();
            if (ObjUtil.isNull(payment)) {
                this.save(p);
            } else {
                p.setId(payment.getId());
                this.updateById(p);
            }
        });
        List<String> noList = paymentList.stream().map(Payment::getContractNo).collect(Collectors.toList());

        Map<String, Contract> no2ContractMap = Opt.ofEmptyAble(contractService.lambdaQuery()
                        .in(Contract::getContractNo, noList).list())
                .map(l -> l.stream().collect(Collectors.toMap(Contract::getContractNo, Function.identity())))
                .orElse(MapUtil.empty());
        Map<String, String> no2UseridMap = Opt.ofEmptyAble(contractUserService.lambdaQuery()
                        .in(RContractUser::getContractNo, noList).list())
                .map(l -> l.stream().collect(Collectors.groupingBy(RContractUser::getContractNo, Collectors.mapping(RContractUser::getUserId, Collectors.joining(",")))))
                .orElse(MapUtil.empty());
        // 推送钉钉消息
        paymentList.forEach(p -> {
            String contractNo = p.getContractNo();
            Contract existContract = no2ContractMap.get(contractNo);
            if (ObjUtil.isNull(existContract)) {
                return;
            }
            Opt.ofBlankAble(no2UseridMap.get(contractNo))
                    .ifPresent(useridStr -> dingMod.workMsg(buildMsg(useridStr, p, existContract)));
        });

        Map<String, List<Payment>> no2PaymentMap = this.lambdaQuery()
                .in(Payment::getContractNo, noList)
                .list()
                .stream().collect(Collectors.groupingBy(Payment::getContractNo));
        List<String> failList = new ArrayList<>();
        no2PaymentMap.forEach((no, list) -> {
            BigDecimal sumAmount = list.stream().map(Payment::getPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

            Contract existContract = no2ContractMap.get(no);
            if (ObjUtil.isNull(existContract)) {
                failList.add(no);
                return;
            }
            // 最新的应收款去计算
            Contract updDto = new Contract();
            updDto.setId(existContract.getId());
            updDto.setPaidAmount(sumAmount);
            contractPaymentService.calMoney(existContract, updDto);
            contractService.updateById(updDto);

        });
        if (CollUtil.isNotEmpty(failList)) {
            return StrUtil.format("合同编号为{}的合同不存在", failList.toString());
        }
        return "成功";
    }

    @Override
    public String update(Long id, PaymentDTO dto) {
        Payment payment = converter.convert(dto, Payment.class);
        if (this.updateById(payment)) {
            List<Payment> paymentList = this.lambdaQuery()
                    .in(Payment::getContractNo, dto.getContractNo())
                    .list();

            BigDecimal sumAmount = paymentList.stream().map(Payment::getPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

            Contract existContract = contractService.lambdaQuery().eq(Contract::getContractNo, dto.getContractNo()).one();
            if (ObjUtil.isNull(existContract)) {
                return "该合同编号对应的合同不存在,只更新已收款信息,合同相关金额无法更新";
            }
            // 最新的应收款去计算
            Contract updDto = new Contract();
            updDto.setId(existContract.getId());
            updDto.setPaidAmount(sumAmount);
            contractPaymentService.calMoney(existContract, updDto);
            contractService.updateById(updDto);
            return "更新成功";
        }
        return "更新失败";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delById(Long id, String contractNo) {
        Contract existContract = contractService.lambdaQuery().eq(Contract::getContractNo, contractNo).one();
        Assert.notNull(existContract, "该合同编号对应的合同不存在");
        this.removeById(id);

        // 最新的应收款去计算
        BigDecimal sumAmount = this.lambdaQuery().eq(Payment::getContractNo, contractNo).list()
                .stream().map(Payment::getPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        Contract updDto = new Contract();
        updDto.setId(existContract.getId());
        updDto.setPaidAmount(sumAmount);
        contractPaymentService.calMoney(existContract, updDto);
        contractService.updateById(updDto);
        return "删除成功";
    }

    String buildMsg(String userid, Payment payment, Contract contract) {
        var jsonObj = JsonUtil.createObj()
                .put("agent_id", 2358374016L)
                .put("userid_list", userid);

        var content = JsonUtil.createObj().put("title", "收款提醒");
        String template = """
                #### 收款提醒
                #### 合同编号:**{}**
                #### 合同名称:**{}**
                #### 收款金额:{}
                #### 收款时间:{}
                #### 备注:{}
                #### 发送时间:{}
                """;
        String markdown = StrUtil.format(template,
                contract.getContractNo(), contract.getName(), payment.getPaymentAmount(), payment.getCollectionTime(), payment.getRemark(), TimeUtil.formatNormal(LocalDateTime.now()));
        content.put("text", markdown);

        var msg = JsonUtil.createObj().put("msgtype", "markdown").set("markdown", content);
        jsonObj.set("msg", msg);
        return jsonObj.toString();
    }
}