PointInfoServiceImpl.java 25.2 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
package vion.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.io.file.FileNameUtil;
import org.dromara.hutool.core.io.file.FileUtil;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.data.id.IdUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.crypto.SecureUtil;
import org.dromara.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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 me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import org.dromara.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import vion.dto.PointInfoDTO;
import vion.mapper.PointInfoMapper;
import vion.model.*;
import vion.service.*;
import vion.third.DingMod;
import vion.third.WechatMod;
import vion.utils.ResultUtil;
import vion.vo.PointInfoVO;
import vion.vo.UserVO;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author HlQ
 * @date 2024/1/8
 */
@Service
@RequiredArgsConstructor
public class PointInfoServiceImpl extends MPJBaseServiceImpl<PointInfoMapper, PointInfo> implements IPointInfoService {

    private final IFileService fileService;
    private final IRejectInfoService rejectInfoService;
    private final IRPointWxService pointWxService;
    private final IRPointDeviceService pointDeviceService;
    private final WxMpService wxMpService;
    private final DingMod dingMod;
    private final WechatMod wechatMod;
    private final Converter converter;

    @Value("${fileUrl:}")
    private String fileUrl;

    // todo 点位设计推送机器人消息到群组
    private final static String DING_GROUP_TOKEN = "未定";

    @Override
    public Object frontSubmit(PointInfoDTO dto) {
        Object obj = insRec(dto);
        if (obj instanceof Map) {
            Map<String, Long> map = (Map<String, Long>) obj;
            Opt.ofNullable(new String[]{dto.getOpenid(), dto.getNickname()})
                    .filter(ArrayUtil::isAllNotNull)
                    .ifPresent(l -> {
                        String openid = l[0];
                        String nickname = l[1];
                        RPointWx pointWx = new RPointWx();
                        pointWx.setPointId(map.get("id"));
                        pointWx.setOpenid(openid);
                        pointWx.setWxName(nickname);
                        pointWxService.save(pointWx);
                    });
        }
        return obj;
    }

    @Override
    public Object save(PointInfoDTO dto) {
        return insRec(dto);
    }

    private Object insRec(PointInfoDTO dto) {
        PointInfo pointInfo = converter.convert(dto, PointInfo.class);
        pointInfo.setUuid(IdUtil.nanoId());
        if (this.save(pointInfo)) {
            Long id = pointInfo.getId();

            Opt.ofNullable(dto.getFiles())
                    .ifPresent(fileList ->
                            Arrays.stream(fileList).forEach(infile -> {
                                dto.setSourceType(16);
                                saveFile(id, infile, dto);
                            }));

            String content = StrUtil.format("""
                    ### 新的点位设计需要处理
                    #### 项目名称:{}
                    #### 联系人:{}
                    """, dto.getProjectName(), dto.getContact());
            dingMod.robotPush(DING_GROUP_TOKEN, buildMsg("点位设计提醒", content));
            return MapUtil.<String, Long>builder()
                    .put("id", pointInfo.getId())
                    .build();
        }
        return "提交失败";
    }

    @Override
    public String upd(Long id, String uuid, PointInfoDTO dto) {
        PointInfo pointInfo = converter.convert(dto, PointInfo.class);
        PointInfo existPoint;
        if (ObjUtil.isNull(id) && StrUtil.isNotBlank(uuid)) {
            existPoint = this.lambdaQuery().eq(PointInfo::getUuid, uuid).one();
            id = existPoint.getId();
        } else {
            existPoint = this.getById(id);
        }

        Wrapper<PointInfo> wrapper = Wrappers.<PointInfo>lambdaUpdate()
                .eq(ObjUtil.isNotEmpty(id), PointInfo::getId, id)
                .eq(StrUtil.isNotBlank(uuid), PointInfo::getUuid, uuid);
        if (this.update(pointInfo, wrapper)) {
            if (ObjUtil.isNotNull(existPoint.getPointNum()) && existPoint.getPointNum() <= 4 && ObjUtil.isNotNull(pointInfo.getIsConstruct())) {
                Integer pointNum = existPoint.getPointNum();
                Integer shippingStatus = pointInfo.getIsConstruct();
                String templateName = StrUtil.format("订购合同-{}-{}.docx", pointNum, shippingStatus);
                String srcPath = fileUrl + FileUtil.FILE_SEPARATOR + "contract" + FileUtil.FILE_SEPARATOR + "template" + FileUtil.FILE_SEPARATOR + templateName;
                String tarPath = fileUrl + FileUtil.FILE_SEPARATOR + "point" + FileUtil.FILE_SEPARATOR + id + FileUtil.FILE_SEPARATOR + "template" + FileUtil.FILE_SEPARATOR + templateName;
                File tarFile = FileUtil.copy(srcPath, tarPath, true);

                FileInfo fileInfo = new FileInfo();
                fileInfo.setStoreId(0L);
                fileInfo.setSourceId(id);
                fileInfo.setSourceType(18);
                fileInfo.setName(templateName);
                fileInfo.setUrl(tarPath);
                fileInfo.setType(FileNameUtil.extName(tarFile));
                fileInfo.setSha256(SecureUtil.sha256(tarFile).toUpperCase());
                fileInfo.setUploader("合同模板,系统生成");
                fileService.save(fileInfo);

                designPush(null, existPoint, "合同范本");
            }

            final Long finalId = id;
            Opt.ofNullable(dto.getContractFile()).ifPresent(infile -> {
                String content = StrUtil.format("""
                        ### 门店合同已上传请及时处理
                        #### 项目名称:{}
                        #### 联系人:{}
                        """, existPoint.getProjectName(), existPoint.getContact());
                dingMod.robotPush(DING_GROUP_TOKEN, buildMsg("点位设计提醒", content));
                dto.setSourceType(19);
                saveFile(finalId, infile, dto);
            });
            Opt.ofNullable(dto.getContractTemplateFile()).ifPresent(infile -> {
                Opt.ofNullable(fileService.lambdaQuery().eq(FileInfo::getSourceId, finalId).eq(FileInfo::getSourceType, 18).list()).ifPresent(fileInfos -> {
                    fileInfos.stream().map(FileInfo::getUrl).forEach(FileUtil::del);
                    fileService.removeBatchByIds(fileInfos.stream().map(FileInfo::getId).collect(Collectors.toList()));
                });
                dto.setSourceType(18);
                saveFile(finalId, infile, dto);
            });
            Opt.ofNullable(dto.getFinalContractFile()).ifPresent(infile -> {
                dto.setSourceType(5);
                saveFile(finalId, infile, dto);
            });

            Opt.ofNullable(dto.getFiles())
                    .ifPresent(fileList -> Arrays.stream(fileList).forEach(infile -> {
                        if (ObjUtil.isNotNull(dto.getSourceType()) && dto.getSourceType().equals(16)) {
                            String content = StrUtil.format("""
                                    ### 门店新图纸已上传请及时处理
                                    #### 项目名称:{}
                                    #### 联系人:{}
                                    """, existPoint.getProjectName(), existPoint.getContact());
                            dingMod.robotPush(DING_GROUP_TOKEN, buildMsg("点位设计提醒", content));
                        }
                        if (ObjUtil.isNotNull(dto.getSourceType()) && dto.getSourceType().equals(20)) {
                            String content = StrUtil.format("""
                                    ### 门店回执单已上传请及时处理
                                    #### 项目名称:{}
                                    #### 联系人:{}
                                    """, existPoint.getProjectName(), existPoint.getContact());
                            dingMod.robotPush(DING_GROUP_TOKEN, buildMsg("点位设计提醒", content));
                        }
                        saveFile(finalId, infile, dto);
                    }));
            if (ObjUtil.isNotNull(dto.getIsConstruct()) && dto.getIsConstruct().equals(1)) {
                String content = StrUtil.format("""
                        ### 门店需要施工请及时处理
                        #### 项目名称:{}
                        #### 联系人:{}
                        """, existPoint.getProjectName(), existPoint.getContact());
                dingMod.robotPush(DING_GROUP_TOKEN, buildMsg("点位设计施工提醒", content));
            }
            if (ObjUtil.isNotNull(dto.getIsInvoice()) && dto.getIsInvoice().equals(1)) {
                String content = StrUtil.format("""
                        ### 门店需要开票请及时处理
                        #### 项目名称:{}
                        #### 联系人:{}
                        """, existPoint.getProjectName(), existPoint.getContact());
                dingMod.robotPush(DING_GROUP_TOKEN, buildMsg("点位设计开票提醒", content));
            }
            return "编辑成功";
        }
        return "编辑失败";
    }

    private void saveFile(Long id, MultipartFile infile, PointInfoDTO dto) {
        String orgName = infile.getOriginalFilename();
        String mainName = FileNameUtil.mainName(orgName);
        String fileExt = FileNameUtil.extName(orgName);
        String filename = StrUtil.format("{}_{}.{}", mainName, DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS"), fileExt);
        String path = getPath(id, dto, filename);
        File file = FileUtil.touch(path);
        try {
            infile.transferTo(file);
        } catch (IOException e) {
            log.error("保存文件出错", e);
        }

        FileInfo fileInfo = new FileInfo();
        fileInfo.setStoreId(0L);
        fileInfo.setSourceId(id);
        fileInfo.setContractId(dto.getContractId());
        fileInfo.setSourceType(dto.getSourceType());
        fileInfo.setName(filename);
        fileInfo.setUrl(path);
        fileInfo.setType(FileNameUtil.extName(file));
        fileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
        if (StrUtil.isNotBlank(dto.getContact())) {
            fileInfo.setUploader(dto.getContact());
        } else {
            UserVO user = (UserVO) StpUtil.getTokenSession().get("curLoginUser");
            fileInfo.setUploader(user.getUsername());
        }
        fileInfo.setUploader(dto.getContact());
        fileService.save(fileInfo);
    }

    private String getPath(Long id, PointInfoDTO dto, String filename) {
        String path = fileUrl + FileUtil.FILE_SEPARATOR + "point" + FileUtil.FILE_SEPARATOR + id + FileUtil.FILE_SEPARATOR + filename;
        if (dto.getSourceType().equals(5)) {
            // 合同文件保存到合同相关目录下
            // fixme 目前该合同的文件信息的 sourceId 是根据点位设计图来存的。
            // fixme 假如合同管理那边查询的话,应该用合同id + sourceType 来查询吗? 目前 dto.getContractId() 获取不到值
            path = fileUrl + FileUtil.FILE_SEPARATOR + "contract" + FileUtil.FILE_SEPARATOR + dto.getContractId() + FileUtil.FILE_SEPARATOR + filename;
        } else if (dto.getSourceType().equals(18)) {
            path = fileUrl + FileUtil.FILE_SEPARATOR + "point" + FileUtil.FILE_SEPARATOR + id + FileUtil.FILE_SEPARATOR + "template" + FileUtil.FILE_SEPARATOR + filename;
        }
        return path;
    }

    @Override
    public Page<PointInfoVO> list(PointInfoDTO dto) {
        MPJLambdaWrapper<PointInfo> wrapper = new MPJLambdaWrapper<>(converter.convert(dto, PointInfo.class))
                .selectAll(PointInfo.class)
                .selectAs(Account::getName, PointInfoVO::getAccountName)
                .leftJoin(Account.class, Account::getId, PointInfo::getAccountId)
                .orderByDesc(PointInfo::getCreateTime);
        Page<PointInfoVO> pointInfoVO = this.selectJoinListPage(Page.of(dto.getPageNum(), dto.getPageSize()), PointInfoVO.class, wrapper);

        Opt.ofEmptyAble(pointInfoVO.getRecords())
                .ifPresent(vos -> {
                    List<Long> ids = vos.stream().map(PointInfoVO::getId).collect(Collectors.toList());
                    List<FileInfo> fileInfoList = fileService.lambdaQuery()
                            .in(FileInfo::getSourceId, ids)
                            .in(FileInfo::getSourceType, ListUtil.of(5, 16, 17, 18, 19, 20)).list();
                    Map<Long, Long> id2DrawingCntMap = fileInfoList.stream().filter(v -> v.getSourceType().equals(16)).collect(Collectors.groupingBy(FileInfo::getSourceId, Collectors.counting()));
                    Map<Long, Long> id2DesignCntMap = fileInfoList.stream().filter(v -> v.getSourceType().equals(17)).collect(Collectors.groupingBy(FileInfo::getSourceId, Collectors.counting()));
                    Map<Long, Long> id2ContractCntMap = fileInfoList.stream().filter(v -> ListUtil.of(5, 18, 19).contains(v.getSourceType())).collect(Collectors.groupingBy(FileInfo::getSourceId, Collectors.counting()));
                    Map<Long, Long> id2ReceiptCntMap = fileInfoList.stream().filter(v -> v.getSourceType().equals(20)).collect(Collectors.groupingBy(FileInfo::getSourceId, Collectors.counting()));

                    vos.forEach(vo -> {
                        vo.setDrawingCnt(id2DrawingCntMap.getOrDefault(vo.getId(), 0L));
                        vo.setDesignCnt(id2DesignCntMap.getOrDefault(vo.getId(), 0L));
                        vo.setContractCnt(id2ContractCntMap.getOrDefault(vo.getId(), 0L));
                        vo.setReceiptCnt(id2ReceiptCntMap.getOrDefault(vo.getId(), 0L));
                    });
                });
        return pointInfoVO;
    }

    @Override
    public PointInfoVO getPointDetail(Long id, String uuid) {
        MPJLambdaWrapper<PointInfo> wrapper = new MPJLambdaWrapper<PointInfo>()
                .selectAll(PointInfo.class)
                .selectCollection(RPointWx.class, PointInfoVO::getWxNameList, map -> map.result(RPointWx::getWxName))
                .selectCollection(RPointDevice.class, PointInfoVO::getDeviceNoList, map -> map.result(RPointDevice::getDeviceNo))
                .selectAs(Account::getName, PointInfoVO::getAccountName)
                .selectAs(Contract::getTotalAmount, PointInfoVO::getContractAmount)
                .leftJoin(Account.class, Account::getId, PointInfo::getAccountId)
                .leftJoin(Contract.class, Contract::getContractNo, PointInfo::getContractNo)
                .leftJoin(RPointWx.class, RPointWx::getPointId, PointInfo::getId)
                .leftJoin(RPointDevice.class, RPointDevice::getPointId, PointInfo::getId)
                .eq(ObjUtil.isNotEmpty(id), PointInfo::getId, id)
                .eq(StrUtil.isNotBlank(uuid), PointInfo::getUuid, uuid)
                .orderByDesc(PointInfo::getCreateTime);
        PointInfoVO pointInfoVO = this.selectJoinOne(PointInfoVO.class, wrapper);

        List<FileInfo> fileInfoList = fileService.lambdaQuery().eq(FileInfo::getSourceId, pointInfoVO.getId()).in(FileInfo::getSourceType, ListUtil.of(5, 16, 17, 18, 19, 20)).list();
        Map<Integer, List<FileInfo>> type2FileMap = fileInfoList.stream().collect(Collectors.groupingBy(FileInfo::getSourceType));
        pointInfoVO.setDrawingFiles(type2FileMap.get(16));
        pointInfoVO.setDesignFiles(type2FileMap.get(17));
        pointInfoVO.setFinalContractFiles(type2FileMap.get(5));
        pointInfoVO.setContractTemplateFiles(type2FileMap.get(18));
        pointInfoVO.setContractFiles(type2FileMap.get(19));
        pointInfoVO.setReceiptFiles(type2FileMap.get(20));
        return pointInfoVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delById(Long id) {
        if (this.removeById(id)) {
            // todo 文件删除
            fileService.lambdaUpdate().eq(FileInfo::getSourceId, id).in(FileInfo::getSourceType, ListUtil.of(5, 16, 17, 18, 19, 20)).remove();
            pointWxService.lambdaUpdate().eq(RPointWx::getPointId, id).remove();
            rejectInfoService.lambdaUpdate().eq(RejectInfo::getPointId, id).remove();
            pointDeviceService.lambdaUpdate().eq(RPointDevice::getPointId, id).remove();
            return "删除成功";
        }
        return "删除失败";
    }

    @Override
    public String reject(RejectInfo dto, String userStr) {
        if (dto.getType() == 1) {
            this.lambdaUpdate().set(PointInfo::getStatus, 2).eq(PointInfo::getId, dto.getPointId()).update(new PointInfo());
        } else if (dto.getType() == 2 && StrUtil.equals(userStr, "client")) {
            if (this.lambdaUpdate().set(PointInfo::getStatus, 4).eq(PointInfo::getId, dto.getPointId()).update(new PointInfo())) {
                PointInfo info = this.getById(dto.getPointId());
                String content = StrUtil.format("""
                        ### 门店驳回点位实际图,请及时处理
                        #### 项目名称:{}
                        #### 联系人:{}
                        #### 驳回意见:{}
                        """, info.getProjectName(), info.getContact(), dto.getContent());
                dingMod.robotPush(DING_GROUP_TOKEN, buildMsg("点位设计图驳回提醒", content));
            }
        } else if (dto.getType() == 3) {
            this.lambdaUpdate().set(PointInfo::getStatus, 5).eq(PointInfo::getId, dto.getPointId()).update(new PointInfo());
        }
        return rejectInfoService.save(dto) ? "提交成功" : "提交失败";
    }

    @Override
    public Page<RejectInfo> rejectInfoList(Long pointId, RejectInfo dto) {
        return rejectInfoService.lambdaQuery(dto).orderByDesc(RejectInfo::getCreateTime).page(Page.of(dto.getPageNum(), dto.getPageSize()));
    }

    @Override
    public Page<RejectInfo> rejectInfoList(String uuid, RejectInfo dto) {
        MPJLambdaWrapper<RejectInfo> wrapper = new MPJLambdaWrapper<>(dto)
                .selectAll(RejectInfo.class)
                .leftJoin(PointInfo.class, PointInfo::getId, RejectInfo::getPointId)
                .orderByDesc(RejectInfo::getCreateTime);
        return rejectInfoService.page(Page.of(dto.getPageNum(), dto.getPageSize()), wrapper);
    }

    @Override
    public Object designPush(Long pointId, PointInfo pointInfo, String pushType) {
        if (ObjUtil.isNull(pointInfo)) {
            pointInfo = this.getById(pointId);
        }

        List<RPointWx> pointWxList = pointWxService.lambdaQuery().eq(RPointWx::getPointId, pointInfo.getId()).list();
        if (CollUtil.isEmpty(pointWxList)) {
            return ResultUtil.error(500, "该点位" + pointInfo.getId() + "没有绑定微信号,无法推送消息。");
        }

        String content = "";
        String url = null;
        if (StrUtil.equals(pushType, "图纸")) {
            content = "图纸资料不符合要求,请点击查看详情。";
            url = "https://yunwei.vionyun.com:8443/wap/re-point";
        } else if (StrUtil.equals(pushType, "点位设计")) {
            content = "点位设计已完成,请查看详情确认点位信息。";
            url = "https://yunwei.vionyun.com:8443/wap/verify-design";
        } else if (StrUtil.equals(pushType, "合同范本")) {
            content = "您的合同范本已生成,请盖章后再次上传。";
            url = "https://yunwei.vionyun.com:8443/wap/verify-contract";
        } else if (StrUtil.equals(pushType, "客户合同")) {
            content = "合同有误。请点击详情确认信息。";
            url = "https://yunwei.vionyun.com:8443/wap/re-contract";
        } else if (StrUtil.equals(pushType, "终版合同")) {
            content = "合同内容已经审核通过,向您发送合同终版。";
            url = "https://yunwei.vionyun.com:8443/wap/final-contract";
        } else if (StrUtil.equals(pushType, "发票")) {
            content = "发票已寄出,请注意查收。";
            url = "https://yunwei.vionyun.com:8443/wap/invoice-express";
        } else if (StrUtil.equals(pushType, "发货")) {
            content = "您的设备已发货,请点击详情获取快递信息。";
            url = "https://yunwei.vionyun.com:8443/wap/express-info";
        } else if (StrUtil.equals(pushType, "邮件截图")) {
            content = "设备上线证明文件已上传,请点击详情下载。";
            url = "https://yunwei.vionyun.com:8443/wap/setup-device";
        }
        Set<String> resSet = new HashSet<>();
        for (RPointWx pointWx : pointWxList) {
            String sendRes = wechatPush(pointInfo, pointWx.getOpenid(), content, url);
            resSet.add(pointWx.getOpenid() + ":" + sendRes);
        }
        if (!resSet.contains("失败")) {
            return "消息推送成功。";
        } else {
            String openidStr = resSet.stream().filter(s -> s.contains("失败")).map(errInfo -> {
                String[] split = errInfo.split(":");
                return split[0];
            }).collect(Collectors.joining(","));
            return ResultUtil.error(513, openidStr + "消息推送失败,请手动发送消息或联系管理员。");
        }
    }

    @Override
    public String installSubmit(String uuid, List<String> deviceList) {
        PointInfo pointInfo = this.lambdaQuery().eq(PointInfo::getUuid, uuid).one();
        List<RPointDevice> pointDeviceList = deviceList.stream().map(no -> {
            RPointDevice pointDevice = new RPointDevice();
            pointDevice.setPointId(pointInfo.getId());
            pointDevice.setDeviceNo(no);
            return pointDevice;
        }).collect(Collectors.toList());
        return pointDeviceService.saveBatch(pointDeviceList) ? "提交成功" : "提交失败";
    }

    @Override
    public Object bindOpenid(String uuid, String code) {
        try {
            WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
            WxOAuth2UserInfo userInfo = wxMpService.getOAuth2Service().getUserInfo(accessToken, null);
            String openid = userInfo.getOpenid();
            WxMpUser wxMpUser = wxMpService.getUserService().userInfo(openid);
            if (!wxMpUser.getSubscribe()) {
                return new ModelAndView("weChatNeedAttention");
            }
            String nickname = userInfo.getNickname();

            PointInfo pointInfo = this.lambdaQuery().eq(PointInfo::getUuid, uuid).one();
            RPointWx pointWx = new RPointWx();
            pointWx.setPointId(pointInfo.getId());
            pointWx.setOpenid(openid);
            pointWx.setWxName(nickname);
            pointWxService.save(pointWx);
            return new ModelAndView("weChatBindSuccess");
        } catch (WxErrorException e) {
            log.error("调用微信接口异常!", e);
        }
        return new ModelAndView("weChatError");
    }

    private String wechatPush(PointInfo pointInfo, String openid, String content, String url) {
        Map<Integer, String> statusMap = Map.of(
                1, "图纸已上传",
                2, "图纸已驳回",
                3, "设计待确认",
                4, "驳回设计",
                5, "合同签订中",
                6, "合同待确认",
                7, "合同完成",
                8, "金蝶订单生成",
                9, "已发货",
                10, "已签收");

        List<WxMpTemplateData> wxMpTemplateDataList = ListUtil.of(
                new WxMpTemplateData("thing4", pointInfo.getProjectName()),
                new WxMpTemplateData("phrase11", statusMap.get(pointInfo.getStatus())),
                new WxMpTemplateData("time3", DateUtil.formatDateTime(new Date())),
                new WxMpTemplateData("thing18", content));
        return wechatMod.sendMsg("IzPvAwx4zgXZmm4bwMc8Pka6piBK2sUf4zf9xA4hkNk", openid, wxMpTemplateDataList, StrUtil.format("{}?uuid={}", url, pointInfo.getUuid()));
    }

    String buildMsg(String title, String content) {
        var jsonObj = JSONUtil.ofObj().set("msgtype", "markdown");

        JSONObject markdown = JSONUtil.ofObj()
                .set("title", title)
                .set("text", content);
        jsonObj.set("markdown", markdown);
        return jsonObj.toString();
    }
}