DeliverLogServiceImpl.java 8.85 KB
package vion.service.impl;

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.date.DateUtil;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.JSONUtil;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import vion.constant.RedisKeyEnum;
import vion.dto.DeliverLogDTO;
import vion.mapper.DeliverLogMapper;
import vion.model.*;
import vion.service.IDeliverLogService;
import vion.service.IRStoreConfederateService;
import vion.service.IStoreService;
import vion.third.DingMod;
import vion.vo.DeliverLogVO;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class DeliverLogServiceImpl extends MPJBaseServiceImpl<DeliverLogMapper, DeliverLog> implements IDeliverLogService {

    private final IStoreService storeService;
    private final IRStoreConfederateService storeConfederateService;
    private final RedissonClient redissonClient;
    private final Converter converter;
    private final DingMod dingMod;

    @Override
    public Map<String, TreeMap<String, List<DeliverLogVO>>> list(DeliverLogDTO dto) {
        var wrapper = new MPJLambdaWrapper<>(converter.convert(dto, DeliverLog.class))
                .selectAll(DeliverLog.class)
                .selectAs(Store::getName, DeliverLogVO::getStoreName)
                .selectAs(RStoreConfederate::getName, DeliverLogVO::getConfederateName)
                .selectAs(User::getUsername, DeliverLogVO::getUsername)
                .selectCollection(RContractUser.class, DeliverLogVO::getContractUserList,
                        map -> map.result(RContractUser::getUsername).result(RContractUser::getEnterDate))
                .leftJoin(Store.class, Store::getId, DeliverLog::getStoreId)
                .leftJoin(RStoreConfederate.class, RStoreConfederate::getId, DeliverLog::getConfederateId)
                .leftJoin(User.class, User::getId, DeliverLog::getUserId)
                .leftJoin(RContractUser.class, RContractUser::getContractNo, DeliverLog::getContractNo)
                .in(ArrayUtil.isNotEmpty(dto.getUserIds()), DeliverLog::getUserId, dto.getUserIds())
                .between(DeliverLog::getLogDate, dto.getLogDateStart(), dto.getLogDateEnd());
        var deliverLogList = this.selectJoinList(DeliverLogVO.class, wrapper);
        deliverLogList.forEach(log -> log.getContractUserList().stream()
                .max(Comparator.comparing(RContractUser::getEnterDate))
                .ifPresent(rContractUser -> log.setContractSaleName(rContractUser.getUsername()))
        );
        return deliverLogList.stream().collect(Collectors.groupingBy(DeliverLogVO::getUsername,
                Collectors.groupingBy(v -> DateUtil.formatDate(v.getLogDate()), () -> new TreeMap<String,
                        List<DeliverLogVO>>(Comparator.reverseOrder()), Collectors.toList())));
    }

    @Override
    public DeliverLogVO getById(Long id) {
        var wrapper = new MPJLambdaWrapper<DeliverLog>()
                .selectAll(DeliverLog.class)
                .selectAs(Store::getName, DeliverLogVO::getStoreName)
                .selectAs(RStoreConfederate::getName, DeliverLogVO::getConfederateName)
                .selectAs(User::getUsername, DeliverLogVO::getUsername)
                .selectAs(Contract::getSaleName, DeliverLogVO::getContractSaleName)
                .leftJoin(Store.class, Store::getId, DeliverLog::getStoreId)
                .leftJoin(RStoreConfederate.class, RStoreConfederate::getId, DeliverLog::getConfederateId)
                .leftJoin(User.class, User::getId, DeliverLog::getUserId)
                .leftJoin(Contract.class, Contract::getContractNo, DeliverLog::getContractNo)
                .eq(DeliverLog::getId, id)
                .orderByDesc(DeliverLog::getUpdateTime);
        return this.selectJoinOne(DeliverLogVO.class, wrapper);
    }

    @Override
    public String save(List<DeliverLogDTO> deliverLogDTOList) {
        var deliverLogList = converter.convert(deliverLogDTOList, DeliverLog.class);
        var storeIdList = deliverLogList.stream().map(DeliverLog::getStoreId).toList();
        var confederateIdList = deliverLogList.stream().map(DeliverLog::getConfederateId).toList();

        var stores = storeService.listByIds(storeIdList);
        var id2StoreMap = stores.stream().collect(Collectors.toMap(Store::getId, Function.identity()));

        var confederateList = storeConfederateService.listByIds(confederateIdList);
        var storeId2NameMap = confederateList.stream().collect(Collectors.toMap(RStoreConfederate::getId, RStoreConfederate::getName));

        deliverLogList.forEach(deliverLog -> {
            var storeId = deliverLog.getStoreId();
            var store = id2StoreMap.get(storeId);
            var confederateName = Opt.ofNullable(deliverLog.getConfederateId()).map(storeId2NameMap::get).orElse("暂无");

            Opt.ofNullable(redissonClient.getBucket(RedisKeyEnum.DING_PREFIX.getVal() + RedisKeyEnum.USER_ID.getVal() + store.getSalesperson()).get())
                    .map(u -> (User) u)
                    .map(User::getUserid)
                    .ifPresent(userid -> dingMod.workMsg(buildMsg(userid, store.getName(), confederateName, deliverLog)));
        });
        return this.saveBatch(deliverLogList) ? "新增成功" : "新增失败";
    }

    @Override
    public String update(DeliverLogDTO dto) {
        var deliverLog = converter.convert(dto, DeliverLog.class);
        var store = storeService.getById(dto.getStoreId());
        var confederate = storeConfederateService.getById(dto.getConfederateId());
        Opt.ofNullable(redissonClient.getBucket(RedisKeyEnum.DING_PREFIX.getVal() + RedisKeyEnum.USER_ID.getVal() + store.getSalesperson()).get())
                .map(u -> (User) u)
                .map(User::getUserid)
                .ifPresent(userid -> dingMod.workMsg(buildMsg(userid, store.getName(),
                        Opt.ofNullable(confederate).map(RStoreConfederate::getName).orElse("暂无"),
                        deliverLog)));
        return this.updateById(deliverLog) ? "修改成功" : "修改失败";
    }

    @Override
    public Boolean logNotify(String[] userIdStr, String dateStr) {
        var pushRes = dingMod.workMsg(buildMsg1(userIdStr, dateStr));
        return StrUtil.containsIgnoreCase(pushRes, "ok");
    }

    JSONObject buildMsg(String userid, String storeName, String confederateName, DeliverLog deliverLog) {
        var jsonObj = JSONUtil.ofObj()
                .set("agent_id", 2358374016L)
                .set("userid_list", userid);

        var msg = new JSONObject();
        var content = new JSONObject();
        content.set("title", "项目交付日志提醒")
                .set("text", StrUtil.format("""
                                ### 项目交付日志提醒
                                #### 项目:{}
                                #### 合同编号:{}
                                #### 日志提交人:{}
                                #### 干系人:{}
                                #### 工时:{}
                                #### 工作内容:{}
                                #### 发送时间:{}""", storeName,
                        Opt.ofBlankAble(deliverLog.getContractNo()).orElse("暂无"),
                        Opt.ofNullable(redissonClient.getBucket(RedisKeyEnum.DING_PREFIX.getVal() + RedisKeyEnum.USER_ID.getVal() + deliverLog.getUserId()).get()).map(u -> (User) u).map(User::getUsername).orElse("不存在该提交人"),
                        confederateName,
                        deliverLog.getLogHour(),
                        deliverLog.getContent(),
                        DateUtil.now()));

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

    JSONObject buildMsg1(String[] userIdStr, String dateStr) {
        var jsonObj = JSONUtil.ofObj()
                .set("agent_id", 2358374016L)
                .set("userid_list", String.join(",", userIdStr));

        var msg = new JSONObject();
        var content = new JSONObject();
        content.set("title", "项目交付日志提醒")
                .set("text", StrUtil.format("""
                        ### 项目交付日志提醒
                        #### 您的日志还未提交,请尽快提交!
                        #### 未提交日志日期:{}
                        #### 发送时间:{}""", dateStr, DateUtil.now()));

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