BAOLH
2025-04-29 4d54f4ad704f36c12fc9890414a3aab86956ccd9
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;
@@ -51,8 +54,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 +66,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 {
@@ -253,10 +259,62 @@
        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 {
            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();
            throw new BadRequestException("文件上传准备异常");
        }
    }
    private <R> Map<String, String> sendApplyEvent(R data, List<TemplateControlField> bindData, String userWxId) {
        Map<String, String> resultMap = new HashMap<>(2);
        try {
@@ -347,9 +405,19 @@
                        // 日期/日期+时间
                        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 +427,7 @@
                        }
                        value.setDate(date);
                    } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_SELECTOR)) {
                        // 单选/多选 TODO:暂时设定只支持单选
                        // 单选/多选 TODO:暂时设定只支持单选,250421添加多选
                        ContentValue.Selector selector = Optional.ofNullable(value.getSelector()).orElse(new ContentValue.Selector());
                        if (control.getControlParam().equals("selector.options[].key")) {
@@ -370,12 +438,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));
@@ -417,17 +505,50 @@
                            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());
                        // 附件 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);
@@ -503,7 +624,7 @@
                        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));