BAOLH
2026-11-10 4d730af0447c686133130d0116a0fa3dd82c2dca
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/approval/service/impl/TemplateControlFieldServiceImpl.java
@@ -1,21 +1,22 @@
/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.approval.service.impl;
import com.alibaba.fastjson.JSONObject;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.oa.WxCpCorpConfInfo;
@@ -23,6 +24,8 @@
import me.chanjar.weixin.cp.bean.oa.WxCpOaApprovalTemplateResult;
import me.chanjar.weixin.cp.bean.oa.applydata.ApplyDataContent;
import me.chanjar.weixin.cp.bean.oa.applydata.ContentValue;
import me.chanjar.weixin.cp.bean.oa.wedrive.WxCpFileUpload;
import me.chanjar.weixin.cp.bean.oa.wedrive.WxCpFileUploadRequest;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.approval.domain.Template;
import me.zhengjie.modules.approval.domain.TemplateControl;
@@ -34,6 +37,7 @@
import me.zhengjie.modules.approval.service.mapstruct.TemplateControlMapper;
import me.zhengjie.modules.approval.service.mapstruct.TemplateMapper;
import me.zhengjie.modules.constant.Constant;
import me.zhengjie.modules.quartz.service.impl.WeComServiceImpl;
import me.zhengjie.modules.system.domain.Dict;
import me.zhengjie.modules.system.domain.DictDetail;
import me.zhengjie.modules.system.repository.DictRepository;
@@ -51,8 +55,11 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.io.File;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
@@ -60,11 +67,11 @@
import java.util.stream.Collectors;
/**
* @website https://eladmin.vip
* @description 服务实现
* @author baolh
* @date 2025-03-17
**/
 * @website https://eladmin.vip
 * @description 服务实现
 * @author baolh
 * @date 2025-03-17
 **/
@Service
@RequiredArgsConstructor
public class TemplateControlFieldServiceImpl implements TemplateControlFieldService {
@@ -79,6 +86,7 @@
    private final DictService dictService;
    private final DictRepository dictRepository;
    private final DictDetailService dictDetailService;
    private final WeComServiceImpl weComService;
    @Override
    public Map<String,Object> queryAll(TemplateControlFieldQueryCriteria criteria, Pageable pageable){
@@ -221,6 +229,9 @@
            }
        } catch (WxErrorException wxErrorException) {
            wxErrorException.printStackTrace();
            weComService.sendNotice(Arrays.asList("YueYuHuanXiangQinYuFenDou"), "企微通信异常", String.format("<div class=\"normal\">异常方法:%s</div>" +
                            "<div class=\"highlight\">请前往API服务查看LOG。</div>",
                    "updateWecomVacationType"), Constant.WECOM_MESSAGE_TYPE_TEXTCARD);
            throw new BadRequestException("数据更新异常");
        }
    }
@@ -253,10 +264,69 @@
        criteria.setFieldTable(data.getClass().getSimpleName());
        criteria.setDelStatus(ElAdminConstant.Constant.TEMPLATE_DEL_STATUS_NORMAL);
        List<TemplateControlField> templateControlFields = templateControlFieldRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
        return this.sendApplyevent(data, templateControlFields, userWxId);
        return this.sendApplyEvent(data, templateControlFields, userWxId);
    }
    private <R> Map<String, String> sendApplyevent(R data, List<TemplateControlField> bindData, String userWxId) {
    @Override
    public List<String> uploadWecomDriveFile(List<String> filePaths, String spaceId, String fatherId) {
        try {
            return filePaths.stream().map(path -> {
                WxCpFileUploadRequest uploadRequest = new WxCpFileUploadRequest();
                uploadRequest.setSpaceId(spaceId);
                uploadRequest.setFatherId(fatherId);
                uploadRequest.setFileName(Arrays.stream(path.split("/")).reduce((first, second) -> Optional.ofNullable(second).orElse(first)).orElse(""));
                try {
                    // 读取文件字节
                    byte[] fileContent = Files.readAllBytes(Paths.get(path));
                    // 转换为Base64字符串
                    String base64String = Base64.getEncoder().encodeToString(fileContent);
                    uploadRequest.setFileBase64Content(base64String);
                } catch (IOException e) {
                    throw new BadRequestException("文件转换异常");
                }
                try {
                    WxCpFileUpload upload = wxCpService.getOaWeDriveService().fileUpload(uploadRequest);
                    return upload.getFileId();
                } catch (WxErrorException e) {
                    e.printStackTrace();
                    throw new BadRequestException("文件上传异常");
                }
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BadRequestException("文件上传准备异常");
        }
    }
    @Override
    public List<String> uploadWecomMediaFile(List<String> filePaths) {
        try {
            // 获取企业微信Token
            wxCpService.getAccessToken();
            // 提交临时素材文件
            return filePaths.stream().map(path -> {
                File file = new File(path);
                try {
                    WxMediaUploadResult uploadResult = wxCpService.getMediaService().upload("file", file);
                    return uploadResult.getMediaId();
                } catch (WxErrorException e) {
                    e.printStackTrace();
                    throw new BadRequestException("文件上传异常");
                }
            }).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
            weComService.sendNotice(Arrays.asList("YueYuHuanXiangQinYuFenDou"), "企微通信异常", String.format("<div class=\"normal\">异常方法:%s</div>" +
                            "<div class=\"highlight\">请前往API服务查看LOG。</div>",
                    "uploadWecomMediaFile"), Constant.WECOM_MESSAGE_TYPE_TEXTCARD);
            throw new BadRequestException("文件上传准备异常");
        }
    }
    private <R> Map<String, String> sendApplyEvent(R data, List<TemplateControlField> bindData, String userWxId) {
        Map<String, String> resultMap = new HashMap<>(2);
        try {
@@ -314,7 +384,7 @@
                    boolean accessible = field.isAccessible();
                    // 设置对象的访问权限,保证对private的属性的访
                    field.setAccessible(true);
                    Object object = Optional.ofNullable(field.get(data)).orElse("未填写");
                    Object object = Optional.ofNullable(field.get(data)).orElse(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE);
                    if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_TEXT)) {
                        // 文本
@@ -333,6 +403,11 @@
                        }
                        value.setText(String.valueOf(object));
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_NUMBER)) {
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        // 数字
                        if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
                            value.setNewNumber(String.valueOf(object));
@@ -341,15 +416,35 @@
                        }
                        value.setNewNumber(String.valueOf(object));
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_MONEY)) {
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        // 金额
                        value.setNewMoney(String.valueOf(object));
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_DATE)) {
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        // 日期/日期+时间
                        ContentValue.Date date = Optional.ofNullable(value.getDate()).orElse(new ContentValue.Date());
                        if (control.getControlParam().equals("date.type")) {
                            date.setType(String.valueOf(object));
                        } else if (control.getControlParam().equals("date.s_timestamp")) {
                        if (control.getControlParam().equals("date.s_timestamp")) {
                            // 时间展示类型
                            if (StringUtils.isNotBlank(control.getContentConfig())) {
                                WxCpOaApprovalTemplateResult.TemplateConfig config = JSONObject.parseObject(control.getContentConfig(), WxCpOaApprovalTemplateResult.TemplateConfig.class);
                                if (StringUtils.isNotBlank(config.getDate().getType())) {
                                    date.setType(config.getDate().getType());
                                } else {
                                    date.setType(ElAdminConstant.Constant.TEMPLATE_DATE_TYPE_DATE);
                                }
                            } else {
                                date.setType(ElAdminConstant.Constant.TEMPLATE_DATE_TYPE_DATE);
                            }
                            // 时间戳
                            if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
                                date.setTimestamp(String.valueOf(object));
                            } else if (field.getType().equals(Timestamp.class)) {
@@ -359,7 +454,12 @@
                        }
                        value.setDate(date);
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_SELECTOR)) {
                        // 单选/多选 TODO:暂时设定只支持单选
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        // 单选/多选 TODO:暂时设定只支持单选,250421添加多选
                        ContentValue.Selector selector = Optional.ofNullable(value.getSelector()).orElse(new ContentValue.Selector());
                        if (control.getControlParam().equals("selector.options[].key")) {
@@ -370,12 +470,32 @@
                                        .flatMap(opt -> opt.getValue().stream().map(val -> new AbstractMap.SimpleEntry<>(val.getText(), opt.getKey())))
                                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (existing, replacement) -> existing));
                                selector.setType("single");
                                ContentValue.Selector.Option option = new ContentValue.Selector.Option();
                                option.setKey(optionMap.containsKey(String.valueOf(object)) ? optionMap.get(String.valueOf(object)) : String.valueOf(object));
                                selector.setOptions(Arrays.asList(option));
                                if (config.getSelector().getType().equals(ElAdminConstant.Constant.TEMPLATE_SELECTOR_TYPE_MULTI)) {
                                    // 多选
                                    List<String> optionValList;
                                    if (object instanceof Collection<?>) {
                                        optionValList = ((Collection<?>) object).stream().map(i -> String.valueOf(i)).collect(Collectors.toList());
                                    } else {
                                        optionValList = Arrays.stream(String.valueOf(object).split(",")).collect(Collectors.toList());
                                    }
                                    // 填充多选数据
                                    selector.setType(ElAdminConstant.Constant.TEMPLATE_SELECTOR_TYPE_MULTI);
                                    selector.setOptions(optionValList.stream().map(s -> {
                                        ContentValue.Selector.Option option = new ContentValue.Selector.Option();
                                        option.setKey(optionMap.containsKey(s) ? optionMap.get(s) : s);
                                        return option;
                                    }).collect(Collectors.toList()));
                                } else {
                                    // 单选
                                    selector.setType(ElAdminConstant.Constant.TEMPLATE_SELECTOR_TYPE_SINGLE);
                                    ContentValue.Selector.Option option = new ContentValue.Selector.Option();
                                    option.setKey(optionMap.containsKey(String.valueOf(object)) ? optionMap.get(String.valueOf(object)) : String.valueOf(object));
                                    selector.setOptions(Arrays.asList(option));
                                }
                            } else {
                                selector.setType("single");
                                // 默认单选
                                selector.setType(ElAdminConstant.Constant.TEMPLATE_SELECTOR_TYPE_SINGLE);
                                ContentValue.Selector.Option option = new ContentValue.Selector.Option();
                                option.setKey(String.valueOf(object));
                                selector.setOptions(Arrays.asList(option));
@@ -383,6 +503,11 @@
                        }
                        value.setSelector(selector);
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_CONTACT)) {
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        if (StringUtils.isNotBlank(control.getContentConfig())) {
                            // 根据config设定判断控件属性
                            WxCpOaApprovalTemplateResult.TemplateConfig config = JSONObject.parseObject(control.getContentConfig(), WxCpOaApprovalTemplateResult.TemplateConfig.class);
@@ -417,21 +542,64 @@
                            value.setMembers(Arrays.asList(member));
                        }
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_FILE)) {
                        // 附件 TODO:暂时设定只支持一条。
                        ContentValue.File file = Optional.ofNullable(value.getFiles()).map(i -> i.get(0)).orElse(new ContentValue.File());
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        // 附件 TODO:暂时设定只支持一条。250421添加多条
                        List<ContentValue.File> fileList = Optional.ofNullable(value.getFiles()).orElse(new ArrayList<>());
                        if (control.getControlParam().equals("files[].file_id")) {
                            file.setFileId(String.valueOf(object));
                            List<String> fileIdList = Arrays.stream(String.valueOf(object).split("\\|")).collect(Collectors.toList());
                            if (fileList.size() > 0) {
                                for (int i = 0; i < fileList.size(); i++) {
                                    fileList.get(i).setFileId(Optional.ofNullable(fileIdList.get(i)).orElse(null));
                                }
                            } else {
                                fileList = fileIdList.stream().map(s -> {
                                    ContentValue.File file = new ContentValue.File();
                                    file.setFileId(s);
                                    return file;
                                }).collect(Collectors.toList());
                            }
                        } else if (control.getControlParam().equals("files[].file_name")) {
                            file.setFileName(String.valueOf(object));
                            List<String> fileNameList = Arrays.stream(String.valueOf(object).split("|")).collect(Collectors.toList());
                            if (fileList.size() > 0) {
                                for (int i = 0; i < fileList.size(); i++) {
                                    fileList.get(i).setFileName(Optional.ofNullable(fileNameList.get(i)).orElse(null));
                                }
                            } else {
                                fileList = fileNameList.stream().map(s -> {
                                    ContentValue.File file = new ContentValue.File();
                                    file.setFileName(s);
                                    return file;
                                }).collect(Collectors.toList());
                            }
                        } else if (control.getControlParam().equals("files[].file_url")) {
                            file.setFileUrl(String.valueOf(object));
                            List<String> fileUrlList = Arrays.stream(String.valueOf(object).split("|")).collect(Collectors.toList());
                            if (fileList.size() > 0) {
                                for (int i = 0; i < fileList.size(); i++) {
                                    fileList.get(i).setFileUrl(Optional.ofNullable(fileUrlList.get(i)).orElse(null));
                                }
                            } else {
                                fileList = fileUrlList.stream().map(s -> {
                                    ContentValue.File file = new ContentValue.File();
                                    file.setFileUrl(s);
                                    return file;
                                }).collect(Collectors.toList());
                            }
                        }
                        value.setFiles(Arrays.asList(file));
                        value.setFiles(fileList);
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_TABLE)) {
                        // 明细 TODO:因结构复杂,暂时设定不提供支持
                        value.setChildren(null);
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_LOCATION)) {
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        // 位置
                        ContentValue.Location location = Optional.ofNullable(value.getLocation()).orElse(new ContentValue.Location());
@@ -455,6 +623,11 @@
                        }
                        value.setLocation(location);
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_RELATEDAPPROVAL)) {
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        // 关联审批单 TODO:按照文档暂时仅支持审批单绑定,但RelatedApproval貌似不止仅支持该功能
                        ContentValue.RelatedApproval relatedApproval = Optional.ofNullable(value.getRelatedApproval()).map(i -> i.get(0)).orElse(new ContentValue.RelatedApproval());
@@ -463,11 +636,21 @@
                        }
                        value.setRelatedApproval(Arrays.asList(relatedApproval));
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_FORMULA)) {
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        // 公式
                        ContentValue.Formula formula = new ContentValue.Formula();
                        formula.setValue(String.valueOf(object));
                        value.setFormula(formula);
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_DATERANGE)) {
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        // 时长
                        ContentValue.Attendance.DataRange dateRange = Optional.ofNullable(value.getDateRange()).orElse(new ContentValue.Attendance.DataRange());
@@ -482,10 +665,10 @@
                            }
                        } else if (control.getControlParam().equals("date_range.new_end")) {
                            if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
                                dateRange.setBegin(Long.parseLong(String.valueOf(object)));
                                dateRange.setEnd(Long.parseLong(String.valueOf(object)));
                            } else if (field.getType().equals(Timestamp.class)) {
                                Timestamp time = (Timestamp) object;
                                dateRange.setBegin(Long.valueOf(time.getTime() / 1000));
                                dateRange.setEnd(Long.valueOf(time.getTime() / 1000));
                            }
                        } else if (control.getControlParam().equals("date_range.new_duration")) {
                            if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
@@ -497,13 +680,18 @@
                        value.setDateRange(dateRange);
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_VACATION)) {
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        // 假勤组件-请假组件
                        ContentValue.Vacation vacation = Optional.ofNullable(value.getVacation()).orElse(new ContentValue.Vacation());
                        if (control.getControlParam().equals("vacation.selector.options[].key")) {
                            ContentValue.Selector selector = Optional.ofNullable(vacation.getSelector()).orElse(new ContentValue.Selector());
                            // 假勤组件固定单选
                            selector.setType("single");
                            selector.setType(ElAdminConstant.Constant.TEMPLATE_SELECTOR_TYPE_SINGLE);
                            ContentValue.Selector.Option option = new ContentValue.Selector.Option();
                            option.setKey(String.valueOf(object));
                            selector.setOptions(Arrays.asList(option));
@@ -546,6 +734,11 @@
                        value.setVacation(vacation);
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_ATTENDANCE)) {
                        if (control.getContentRequire().equals(0) && String.valueOf(object).equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE)) {
                            // 非必传且未填写则跳过
                            continue;
                        }
                        ContentValue.Attendance attendance = Optional.ofNullable(value.getAttendance()).orElse(new ContentValue.Attendance());
                        if (control.getControlParam().equals("attendance.date_range.type")) {
@@ -617,6 +810,9 @@
        } catch (WxErrorException wxErrorException) {
            // 通信失败
            wxErrorException.printStackTrace();
            weComService.sendNotice(Arrays.asList("YueYuHuanXiangQinYuFenDou"), "企微通信异常", String.format("<div class=\"normal\">异常方法:%s</div>" +
                            "<div class=\"highlight\">请前往API服务查看LOG。</div>",
                    "sendApplyEvent"), Constant.WECOM_MESSAGE_TYPE_TEXTCARD);
            resultMap.put("error", "通信失败");
        } catch (IllegalAccessException illegalAccessException) {
            // 解析后数据获取失败