| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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; |
| | |
| | | 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 { |
| | |
| | | // 日期/日期+时间 |
| | | 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)) { |
| | |
| | | } |
| | | 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")) { |
| | |
| | | .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"); |
| | | 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)); |
| | |
| | | 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)); |
| | | } else if (control.getControlParam().equals("files[].file_name")) { |
| | | file.setFileName(String.valueOf(object)); |
| | | } else if (control.getControlParam().equals("files[].file_url")) { |
| | | file.setFileUrl(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)); |
| | | } |
| | | value.setFiles(Arrays.asList(file)); |
| | | } 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")) { |
| | | 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")) { |
| | | 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(fileList); |
| | | } else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_TABLE)) { |
| | | // 明细 TODO:因结构复杂,暂时设定不提供支持 |
| | | value.setChildren(null); |
| | |
| | | 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)); |