TaskTempServiceImpl.java 6.38 KB
package vion.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import vion.third.DingMod;
import vion.third.WechatMod;
import vion.dto.TaskTempDTO;
import vion.mapper.TaskTempMapper;
import vion.model.FileInfo;
import vion.model.TaskTemp;
import vion.model.User;
import vion.service.IFileService;
import vion.service.ITaskTempService;
import vion.service.IUserService;
import vion.vo.TaskTempVO;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class TaskTempServiceImpl extends ServiceImpl<TaskTempMapper, TaskTemp> implements ITaskTempService {

    private final IFileService fileService;
    private final IUserService userService;
    private final DingMod dingMod;
    private final WechatMod wechatMod;
    private final Converter converter;

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

    @Override
    public Page<TaskTempVO> getTaskTempList(TaskTempDTO data) {
        Page<TaskTemp> taskTempPage = this
                .lambdaQuery(converter.convert(data, new TaskTemp()))
                .between(data.getStartdate() != null && data.getEnddate() != null, TaskTemp::getRepairTime, data.getStartdate(), data.getEnddate())
                .page(Page.of(data.getPageNum(), data.getPageSize()));
        List<TaskTempVO> taskTempVOList = converter.convert(taskTempPage.getRecords(), TaskTempVO.class);
        return Page.<TaskTempVO>of(taskTempPage.getCurrent(), taskTempPage.getSize(), taskTempPage.getTotal()).setRecords(taskTempVOList);
    }

    @Override
    public TaskTempVO getTaskTempById(Long id) {
        TaskTemp taskTemp = this.getById(id);
        List<FileInfo> fileInfos = Opt.ofNullable(taskTemp)
                .map(tmp -> fileService.list(Wrappers.<FileInfo>lambdaUpdate()
                        .eq(FileInfo::getSourceId, tmp.getId())
                        .eq(FileInfo::getStoreId, 0L)))
                .orElse(ListUtil.empty());
        TaskTempVO taskTempVO = converter.convert(taskTemp, TaskTempVO.class);
        taskTempVO.setFileList(fileInfos);
        return taskTempVO;
    }

    @Override
    public Object saveOrUpdTaskTemp(TaskTempDTO data) {
        TaskTemp taskTemp = converter.convert(data, TaskTemp.class);
        this.save(taskTemp);

        Opt.ofNullable(data.getFiles())
                .ifPresent(fileList ->
                        Arrays.stream(fileList).forEach(infile -> {
                            //上传url地址
                            String orgName = infile.getOriginalFilename();
                            String fileName = orgName.substring(0, orgName.lastIndexOf("."));
                            String fileExt = orgName.substring(orgName.lastIndexOf("."));
                            String filename = fileName + "_" + DateUtil.format(new Date(), "yyyyMMdd_HHmmss") + fileExt;
                            String path = fileUrl + FileUtil.FILE_SEPARATOR + 0 + FileUtil.FILE_SEPARATOR + taskTemp.getId() + FileUtil.FILE_SEPARATOR + 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(taskTemp.getId());
                            fileInfo.setSourceType(2);
                            fileInfo.setName(filename);
                            fileInfo.setUrl(path);
                            fileInfo.setType(FileUtil.extName(file));
                            fileInfo.setSha256(SecureUtil.sha256(file).toUpperCase());
                            fileInfo.setUploader(data.getRepairPeople());
                            fileService.save(fileInfo);
                        }));

        // todo 异步发送钉钉消息通知
        List<User> userList = userService.lambdaQuery().eq(User::getPreWorkOrder, 1).list();
        String userids = userList.stream().map(User::getUserid).collect(Collectors.joining(","));
        dingMod.sendMessage(buildMsg(userids, taskTemp));

        if (StrUtil.isNotBlank(data.getOpenid())) {
            return "工单提交成功!";
        } else {
            return wechatMod.genQRCodeUrl(taskTemp.getId());
        }
    }

    JSONObject buildMsg(String userid, TaskTemp taskTemp) {
        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", "客户提交工单,请及时处理哦~_~");
        content.set("markdown", "门店信息: " + taskTemp.getStoreName() +
                "  \n  客户姓名:" + taskTemp.getRepairPeople() +
                "  \n  联系方式:" + taskTemp.getRepairPhone() +
                "  \n  故障描述:" + taskTemp.getFaultDescription() +
                "  \n  发送时间:" + DateUtil.now());
        content.set("btn_orientation", "1");

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(new JSONObject().set("title", "查看详情").set("action_url", "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=dingkrzwks0jpi2di3uo&response_type=code&scope=snsapi_auth&state=STATE&redirect_uri=https%3A%2F%2Fyunwei.vionyun.com%3A8443%2Fyunwei%2Fapi%2Fding%2Fcallback%2Finside%3FtaskTempId%3D" + taskTemp.getId()));
        content.set("btn_json_list", jsonArray);

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