InvoiceServiceImpl.java 6.23 KB
package vion.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
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.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vion.dto.InvoiceDTO;
import vion.mapper.InvoiceMapper;
import vion.model.Contract;
import vion.model.Invoice;
import vion.model.RContractUser;
import vion.service.IContractService;
import vion.service.IInvoiceService;
import vion.service.IRContractUserService;
import vion.third.DingMod;
import vion.vo.InvoiceVO;
import vion.vo.RoleVO;
import vion.vo.UserVO;

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

/**
 * @author HlQ
 * @date 2023/12/5
 */
@Service
@RequiredArgsConstructor
public class InvoiceServiceImpl extends MPJBaseServiceImpl<InvoiceMapper, Invoice> implements IInvoiceService {

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

    @Override
    public Page<InvoiceVO> list(InvoiceDTO 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<Invoice> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, Invoice.class))
                .selectAll(Invoice.class)
                .leftJoin(Contract.class, Contract::getContractNo, Invoice::getContractNo)
                .eq(StrUtil.isNotBlank(saleName), Contract::getSaleName, saleName)
                .orderByDesc(Invoice::getCreateTime);
        return this.selectJoinListPage(Page.of(dto.getPageNum(), dto.getPageSize()), InvoiceVO.class, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String save(List<InvoiceDTO> dto) {
        List<Invoice> invoiceList = converter.convert(dto, Invoice.class);
        this.saveBatch(invoiceList);

        List<String> noList = invoiceList.stream().map(Invoice::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());
        // 推送钉钉消息
        invoiceList.forEach(i -> {
            String contractNo = i.getContractNo();
            Contract existContract = no2ContractMap.get(contractNo);
            if (ObjUtil.isNull(existContract)) {
                return;
            }
            Opt.ofBlankAble(no2UseridMap.get(contractNo))
                    .ifPresent(useridStr -> dingMod.workMsg(buildMsg(useridStr, i, existContract)));
        });

        Map<String, List<Invoice>> no2InvoiceMap = invoiceList.stream().collect(Collectors.groupingBy(Invoice::getContractNo));
        List<String> failList = new ArrayList<>();

        no2InvoiceMap.forEach((no, list) -> {
            BigDecimal sum = list.stream().map(Invoice::getInvoiceAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            // 根据合同编号查合同
            Contract existContract = no2ContractMap.get(no);
            if (ObjUtil.isNull(existContract)) {
                failList.add(no);
                return;
            }
            contractService.lambdaUpdate().eq(Contract::getContractNo, no).set(Contract::getInvoiceAmount, NumberUtil.add(sum, existContract.getInvoiceAmount())).update(new Contract());
        });
        if (CollUtil.isNotEmpty(failList)) {
            return StrUtil.format("合同编号为{}的合同不存在", failList.toString());
        }
        return "成功";
    }

    JSONObject buildMsg(String userid, Invoice invoice, Contract contract) {
        JSONObject jsonObj = new JSONObject();
        jsonObj.set("agent_id", 2358374016L);
        jsonObj.set("userid_list", userid);

        JSONObject msg = new JSONObject();
        JSONObject content = new JSONObject();
        content.set("title", "开票提醒");
        String markdown = StrUtil.format("#### 开票提醒" +
                        "  \n  #### 合同编号:**{}**" +
                        "  \n  #### 合同名称:**{}**" +
                        "  \n  #### 发票编号:{}" +
                        "  \n  #### 发票金额:{}" +
                        "  \n  #### 开票时间:{}" +
                        "  \n  #### 备注:{}" +
                        "  \n  #### 发送时间:{}",
                contract.getContractNo(), contract.getName(), invoice.getInvoiceNo(), invoice.getInvoiceAmount(), DateUtil.formatDate(invoice.getInvoicingTime()), invoice.getRemark(), DateUtil.now());
        content.set("text", markdown);

        msg.set("msgtype", "markdown");
        msg.set("markdown", content);
        jsonObj.set("msg", msg);
        return jsonObj;
    }
}