/*
|
* 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;
|
import me.chanjar.weixin.cp.bean.oa.WxCpOaApplyEventRequest;
|
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 me.zhengjie.modules.approval.domain.TemplateControlField;
|
import me.zhengjie.modules.approval.repository.TemplateControlRepository;
|
import me.zhengjie.modules.approval.repository.TemplateRepository;
|
import me.zhengjie.modules.approval.service.dto.TemplateControlQueryCriteria;
|
import me.zhengjie.modules.approval.service.dto.TemplateDto;
|
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.system.domain.Dict;
|
import me.zhengjie.modules.system.domain.DictDetail;
|
import me.zhengjie.modules.system.repository.DictRepository;
|
import me.zhengjie.modules.system.service.DictDetailService;
|
import me.zhengjie.modules.system.service.DictService;
|
import me.zhengjie.utils.*;
|
import lombok.RequiredArgsConstructor;
|
import me.zhengjie.modules.approval.repository.TemplateControlFieldRepository;
|
import me.zhengjie.modules.approval.service.TemplateControlFieldService;
|
import me.zhengjie.modules.approval.service.dto.TemplateControlFieldDto;
|
import me.zhengjie.modules.approval.service.dto.TemplateControlFieldQueryCriteria;
|
import me.zhengjie.modules.approval.service.mapstruct.TemplateControlFieldMapper;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
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;
|
import javax.servlet.http.HttpServletResponse;
|
import java.util.stream.Collectors;
|
|
/**
|
* @website https://eladmin.vip
|
* @description 服务实现
|
* @author baolh
|
* @date 2025-03-17
|
**/
|
@Service
|
@RequiredArgsConstructor
|
public class TemplateControlFieldServiceImpl implements TemplateControlFieldService {
|
|
private final TemplateControlFieldRepository templateControlFieldRepository;
|
private final TemplateRepository templateRepository;
|
private final TemplateControlRepository templateControlRepository;
|
private final TemplateControlFieldMapper templateControlFieldMapper;
|
private final TemplateMapper templateMapper;
|
private final TemplateControlMapper templateControlMapper;
|
private final WxCpService wxCpService;
|
private final DictService dictService;
|
private final DictRepository dictRepository;
|
private final DictDetailService dictDetailService;
|
|
@Override
|
public Map<String,Object> queryAll(TemplateControlFieldQueryCriteria criteria, Pageable pageable){
|
Page<TemplateControlField> page = templateControlFieldRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
|
return PageUtil.toPage(page.map(templateControlFieldMapper::toDto));
|
}
|
|
@Override
|
public List<TemplateControlFieldDto> queryAll(TemplateControlFieldQueryCriteria criteria){
|
return templateControlFieldMapper.toDto(templateControlFieldRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
|
}
|
|
@Override
|
@Transactional
|
public TemplateControlFieldDto findById(Long id) {
|
TemplateControlField templateControlField = templateControlFieldRepository.findById(id).orElseGet(TemplateControlField::new);
|
ValidationUtil.isNull(templateControlField.getId(),"TemplateControlField","id", id);
|
return templateControlFieldMapper.toDto(templateControlField);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public TemplateControlFieldDto create(TemplateControlField resources) {
|
return templateControlFieldMapper.toDto(templateControlFieldRepository.save(resources));
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void update(TemplateControlField resources) {
|
TemplateControlField templateControlField = templateControlFieldRepository.findById(resources.getId()).orElseGet(TemplateControlField::new);
|
ValidationUtil.isNull(templateControlField.getId(),"TemplateControlField","id", resources.getId());
|
templateControlField.copy(resources);
|
templateControlFieldRepository.save(templateControlField);
|
}
|
|
@Override
|
public void deleteAll(Long[] ids) {
|
for (Long id : ids) {
|
templateControlFieldRepository.deleteById(id);
|
}
|
}
|
|
@Override
|
public void download(List<TemplateControlFieldDto> all, HttpServletResponse response) throws IOException {
|
List<Map<String, Object>> list = new ArrayList<>();
|
for (TemplateControlFieldDto templateControlField : all) {
|
Map<String,Object> map = new LinkedHashMap<>();
|
map.put("控件ID", templateControlField.getControlId());
|
map.put("字段表名", templateControlField.getFieldTable());
|
map.put("字段名称", templateControlField.getFieldName());
|
map.put("删除判定", templateControlField.getDelStatus());
|
map.put("创建时间", templateControlField.getCreateTime());
|
map.put("创建人", templateControlField.getCreateBy());
|
map.put("更新时间", templateControlField.getUpdateTime());
|
map.put("更新人", templateControlField.getUpdateBy());
|
list.add(map);
|
}
|
FileUtil.downloadExcel(list, response);
|
}
|
|
@Override
|
public Map<String, Object> queryByTable(TemplateControlFieldQueryCriteria criteria) {
|
Map<String, Object> resultMap = new HashMap<>(2);
|
|
if (StringUtils.isNotBlank(criteria.getFieldTable())) {
|
// 获取审批控件字段绑定信息
|
List<TemplateControlField> templateControlFields = templateControlFieldRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
|
|
if (templateControlFields.size() > 0) {
|
// 已绑定
|
// 获取审批模版信息
|
Template template = templateRepository.findById(templateControlFields.get(0).getTemplateId()).orElseGet(Template::new);
|
if (template.getId() != null) {
|
// 获取审批模板控件信息
|
TemplateControlQueryCriteria controlCriteria = new TemplateControlQueryCriteria();
|
controlCriteria.setTemplateId(template.getId());
|
List<TemplateControl> templateControls = templateControlRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,controlCriteria,criteriaBuilder));
|
TemplateDto templateDto = templateMapper.toDto(template);
|
templateDto.setTemplateControls(templateControlMapper.toDto(templateControls));
|
|
resultMap.put("template", templateDto);
|
}
|
}
|
resultMap.put("templateControlField", templateControlFieldMapper.toDto(templateControlFields));
|
}
|
|
return resultMap;
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void bind(List<TemplateControlField> all) {
|
TemplateControlFieldQueryCriteria criteria = new TemplateControlFieldQueryCriteria();
|
criteria.setFieldTable(all.get(0).getFieldTable());
|
criteria.setDelStatus(ElAdminConstant.Constant.TEMPLATE_DEL_STATUS_NORMAL);
|
List<TemplateControlField> oldRecordList = templateControlFieldRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder));
|
// 删除旧记录(根据表名区分)
|
for (TemplateControlField oldRecord : oldRecordList) {
|
oldRecord.setDelStatus(ElAdminConstant.Constant.TEMPLATE_DEL_STATUS_DEL);
|
}
|
templateControlFieldRepository.saveAll(oldRecordList);
|
|
// 保存新记录
|
for (TemplateControlField newRecord : all) {
|
newRecord.setId(null);
|
newRecord.setDelStatus(ElAdminConstant.Constant.TEMPLATE_DEL_STATUS_NORMAL);
|
}
|
templateControlFieldRepository.saveAll(all);
|
}
|
|
@Override
|
public void updateWecomVacationType() {
|
try {
|
// 获取字典信息
|
Dict dict = dictRepository.findByName(Constant.WECOM_LEAVE_TYPE);
|
|
// 获取企业微信Token
|
wxCpService.getAccessToken(true);
|
// 假勤组件需要获取假期相关配置
|
WxCpCorpConfInfo corpConfInfo = wxCpService.getOaService().getCorpConf();
|
List<DictDetail> newDictDetails = corpConfInfo.getLists().stream().map(m -> {
|
DictDetail dictDetail = new DictDetail();
|
dictDetail.setLabel(m.getName());
|
dictDetail.setValue(m.getId().toString());
|
dictDetail.setDictSort(1);
|
return dictDetail;
|
}).collect(Collectors.toList());
|
|
// 删除旧记录
|
dictService.delete(Collections.singleton(dict.getId()));
|
Dict newDict = new Dict();
|
newDict.setName(Constant.WECOM_LEAVE_TYPE);
|
newDict.setDescription(Constant.WECOM_LEAVE_TYPE_DESCRIPTION);
|
Dict savedDict = dictRepository.save(newDict);
|
|
// 保存新纪录
|
for (DictDetail dictDetail : newDictDetails) {
|
dictDetail.setDict(savedDict);
|
dictDetailService.create(dictDetail);
|
}
|
} catch (WxErrorException wxErrorException) {
|
wxErrorException.printStackTrace();
|
throw new BadRequestException("数据更新异常");
|
}
|
}
|
|
@Override
|
public void updateApprovalSite(String approvalSite) {
|
// 获取字典信息
|
Dict dict = dictRepository.findByName(Constant.APPROVAL_SITE_TYPE);
|
|
if (dict.getDictDetails().size() > 0) {
|
// 更新记录
|
for (DictDetail dictDetail : dict.getDictDetails()) {
|
dictDetail.setValue(approvalSite);
|
dictDetailService.update(dictDetail);
|
}
|
} else {
|
// 新建记录
|
DictDetail dictDetail = new DictDetail();
|
dictDetail.setLabel("站点");
|
dictDetail.setValue(approvalSite);
|
dictDetail.setDictSort(1);
|
dictDetail.setDict(dict);
|
dictDetailService.create(dictDetail);
|
}
|
}
|
|
@Override
|
public <R> Map<String, String> createWecomApproval(R data, String userWxId) {
|
TemplateControlFieldQueryCriteria criteria = new TemplateControlFieldQueryCriteria();
|
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);
|
}
|
|
@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(true);
|
|
// 提交临时素材文件
|
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 {
|
// 校验申请人企微ID
|
if (StringUtils.isBlank(userWxId)) {
|
resultMap.put("error", "申请人企微ID不能为空");
|
return resultMap;
|
}
|
// 验证绑定数据
|
if (bindData.size() <= 0) {
|
resultMap.put("error", "数据模版未绑定");
|
return resultMap;
|
}
|
// 验证Class名称
|
if (!data.getClass().getSimpleName().equals(bindData.get(0).getFieldTable())) {
|
resultMap.put("error", "数据源名称不符:" + bindData.get(0).getFieldTable());
|
return resultMap;
|
}
|
// 验证Class字段
|
List<Field> fields = QueryHelp.getAllFields(data.getClass(), new ArrayList<>());
|
List<String> fieldNames = fields.stream().map(i -> i.getName()).collect(Collectors.toList());
|
List<TemplateControlField> nonExistList = bindData.stream().filter(i -> StringUtils.isNotBlank(i.getFieldName()) && !fieldNames.contains(i.getFieldName())).collect(Collectors.toList());
|
if (nonExistList.size() > 0) {
|
resultMap.put("error", "数据源字段名称不存在:" + nonExistList.stream().map(i -> i.getFieldTable().concat("(").concat(i.getFieldName()).concat(")")).collect(Collectors.joining(",")));
|
return resultMap;
|
}
|
|
// 获取模版信息
|
Template template = templateRepository.findById(bindData.get(0).getTemplateId()).orElseGet(Template::new);
|
if (template.getId().equals(null)) {
|
resultMap.put("error", "模版数据不存在");
|
return resultMap;
|
}
|
// 获取模版控件信息
|
TemplateControlQueryCriteria controlCriteria = new TemplateControlQueryCriteria();
|
controlCriteria.setTemplateId(template.getId());
|
List<TemplateControl> templateControls = templateControlRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,controlCriteria,criteriaBuilder));
|
// 制作控件Map
|
Map<Long, TemplateControl> templateControlsMap = templateControls.stream().collect(Collectors.toMap(TemplateControl::getId, i -> i));
|
// 制作源数据成员Map
|
Map<String, Field> fieldMap = fields.stream().collect(Collectors.toMap(Field::getName, i -> i));
|
|
WxCpOaApplyEventRequest.ApplyData applyData = new WxCpOaApplyEventRequest.ApplyData();
|
|
for (TemplateControlField controlField : bindData) {
|
TemplateControl control = templateControlsMap.get(controlField.getControlId());
|
if (StringUtils.isNotBlank(controlField.getFieldName()) || control.getContentRequire().equals(1)) {
|
// 获取申请结构信息
|
ApplyDataContent applyDataContent = Optional.ofNullable(applyData.getContents()).flatMap(i -> i.stream().filter(f -> f.getControl().equals(control.getControlType()) && f.getId().equals(control.getControlId())).findFirst()).orElse(new ApplyDataContent());
|
applyDataContent.setControl(control.getControlType());
|
applyDataContent.setId(control.getControlId());
|
ContentValue value = Optional.ofNullable(applyDataContent.getValue()).orElse(new ContentValue());
|
// 获取数据值
|
Field field = fieldMap.get(controlField.getFieldName());
|
boolean accessible = field.isAccessible();
|
// 设置对象的访问权限,保证对private的属性的访
|
field.setAccessible(true);
|
Object object = Optional.ofNullable(field.get(data)).orElse(ElAdminConstant.Constant.TEMPLATE_CONTROL_NULL_VALUE);
|
|
if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_TEXT)) {
|
// 文本
|
if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
|
value.setText(String.valueOf(object));
|
} else if (field.getType().equals(Timestamp.class)) {
|
value.setText(String.valueOf(object));
|
}
|
value.setText(String.valueOf(object));
|
} else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_TEXTAREA)) {
|
// 多行文本
|
if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
|
value.setText(String.valueOf(object));
|
} else if (field.getType().equals(Timestamp.class)) {
|
value.setText(String.valueOf(object));
|
}
|
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));
|
} else if (field.getType().equals(Timestamp.class)) {
|
value.setNewNumber(String.valueOf(object));
|
}
|
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.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)) {
|
Timestamp time = (Timestamp) object;
|
date.setTimestamp(String.valueOf(time.getTime() / 1000));
|
}
|
}
|
value.setDate(date);
|
} else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_SELECTOR)) {
|
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")) {
|
if (StringUtils.isNotBlank(control.getContentConfig())) {
|
// 根据config设定填写key值
|
WxCpOaApprovalTemplateResult.TemplateConfig config = JSONObject.parseObject(control.getContentConfig(), WxCpOaApprovalTemplateResult.TemplateConfig.class);
|
Map<String, String> optionMap = config.getSelector().getOptions().stream()
|
.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));
|
|
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(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.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);
|
if (config.getContact().getMode().equals(ElAdminConstant.Constant.TEMPLATE_CONTACT_MODE_DEPARTMENT)) {
|
// 部门控件 TODO:1、与成员控件类型相同,如何区分待验证。(250424添加区分)2、暂时设定只支持一条。
|
ContentValue.Department department = Optional.ofNullable(value.getDepartments()).map(i -> i.get(0)).orElse(new ContentValue.Department());
|
|
if (control.getControlParam().equals("departments[].openapi_id")) {
|
department.setOpenApiId(String.valueOf(object));
|
} else if (control.getControlParam().equals("departments[].name")) {
|
department.setName(String.valueOf(object));
|
}
|
value.setDepartments(Arrays.asList(department));
|
} else {
|
// 成员控件 TODO:1、与部门控件类型相同,如何区分待验证。(250424添加区分)2、暂时设定只支持一条。
|
ContentValue.Member member = Optional.ofNullable(value.getMembers()).map(i -> i.get(0)).orElse(new ContentValue.Member());
|
if (control.getControlParam().equals("members[].userid")) {
|
member.setUserId(String.valueOf(object));
|
} else if (control.getControlParam().equals("members[].name")) {
|
member.setName(String.valueOf(object));
|
}
|
value.setMembers(Arrays.asList(member));
|
}
|
} else {
|
// 默认成员控件
|
ContentValue.Member member = Optional.ofNullable(value.getMembers()).map(i -> i.get(0)).orElse(new ContentValue.Member());
|
if (control.getControlParam().equals("members[].userid")) {
|
member.setUserId(String.valueOf(object));
|
} else if (control.getControlParam().equals("members[].name")) {
|
member.setName(String.valueOf(object));
|
}
|
value.setMembers(Arrays.asList(member));
|
}
|
} else if (control.getControlType().equals(ElAdminConstant.Constant.TEMPLATE_CONTROL_TYPE_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")) {
|
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")) {
|
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);
|
} 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());
|
|
if (control.getControlParam().equals("location.latitude")) {
|
location.setLatitude(new BigDecimal(String.valueOf(object)));
|
} else if (control.getControlParam().equals("location.longitude")) {
|
location.setLongitude(new BigDecimal(String.valueOf(object)));
|
} else if (control.getControlParam().equals("location.title")) {
|
location.setTitle(String.valueOf(object));
|
} else if (control.getControlParam().equals("location.address")) {
|
location.setAddress(String.valueOf(object));
|
} else if (control.getControlParam().equals("location.time")) {
|
if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
|
location.setTime(Long.valueOf(String.valueOf(object)));
|
} else if (field.getType().equals(Timestamp.class)) {
|
Timestamp time = (Timestamp) object;
|
location.setTime(time.getTime() / 1000);
|
} else {
|
location.setTime((Long.valueOf(String.valueOf(object))));
|
}
|
}
|
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());
|
|
if (control.getControlParam().equals("related_approval[].sp_no")) {
|
relatedApproval.setSpNo(String.valueOf(object));
|
}
|
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());
|
|
if (control.getControlParam().equals("date_range.type")) {
|
dateRange.setType(String.valueOf(object));
|
} else if (control.getControlParam().equals("date_range.new_begin")) {
|
if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
|
dateRange.setBegin(Long.parseLong(String.valueOf(object)));
|
} else if (field.getType().equals(Timestamp.class)) {
|
Timestamp time = (Timestamp) object;
|
dateRange.setBegin(Long.valueOf(time.getTime() / 1000));
|
}
|
} 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.setEnd(Long.parseLong(String.valueOf(object)));
|
} else if (field.getType().equals(Timestamp.class)) {
|
Timestamp time = (Timestamp) object;
|
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)) {
|
dateRange.setDuration(Long.parseLong(String.valueOf(object)));
|
} else {
|
dateRange.setDuration(Long.parseLong(String.valueOf(object)));
|
}
|
}
|
|
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(ElAdminConstant.Constant.TEMPLATE_SELECTOR_TYPE_SINGLE);
|
ContentValue.Selector.Option option = new ContentValue.Selector.Option();
|
option.setKey(String.valueOf(object));
|
selector.setOptions(Arrays.asList(option));
|
vacation.setSelector(selector);
|
} else if (control.getControlParam().equals("vacation.attendance.date_range.type")) {
|
ContentValue.Attendance attendance = Optional.ofNullable(vacation.getAttendance()).orElse(new ContentValue.Attendance());
|
ContentValue.Attendance.DataRange dateRange = Optional.ofNullable(attendance.getDateRange()).orElse(new ContentValue.Attendance.DataRange());
|
dateRange.setType(String.valueOf(object));
|
attendance.setDateRange(dateRange);
|
vacation.setAttendance(attendance);
|
} else if (control.getControlParam().equals("vacation.attendance.date_range.new_begin")) {
|
ContentValue.Attendance attendance = Optional.ofNullable(vacation.getAttendance()).orElse(new ContentValue.Attendance());
|
ContentValue.Attendance.DataRange dateRange = Optional.ofNullable(attendance.getDateRange()).orElse(new ContentValue.Attendance.DataRange());
|
if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
|
dateRange.setBegin(Long.parseLong(String.valueOf(object)));
|
} else if (field.getType().equals(Timestamp.class)) {
|
Timestamp time = (Timestamp) object;
|
dateRange.setBegin(Long.valueOf(time.getTime() / 1000));
|
}
|
attendance.setDateRange(dateRange);
|
vacation.setAttendance(attendance);
|
} else if (control.getControlParam().equals("vacation.attendance.date_range.new_end")) {
|
ContentValue.Attendance attendance = Optional.ofNullable(vacation.getAttendance()).orElse(new ContentValue.Attendance());
|
ContentValue.Attendance.DataRange dateRange = Optional.ofNullable(attendance.getDateRange()).orElse(new ContentValue.Attendance.DataRange());
|
if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
|
dateRange.setEnd(Long.parseLong(String.valueOf(object)));
|
} else if (field.getType().equals(Timestamp.class)) {
|
Timestamp time = (Timestamp) object;
|
dateRange.setEnd(Long.valueOf(time.getTime() / 1000));
|
}
|
attendance.setDateRange(dateRange);
|
vacation.setAttendance(attendance);
|
} else if (control.getControlParam().equals("vacation.attendance.date_range.new_duration")) {
|
ContentValue.Attendance attendance = Optional.ofNullable(vacation.getAttendance()).orElse(new ContentValue.Attendance());
|
ContentValue.Attendance.DataRange dateRange = Optional.ofNullable(attendance.getDateRange()).orElse(new ContentValue.Attendance.DataRange());
|
dateRange.setDuration(Long.parseLong(String.valueOf(object)));
|
attendance.setDateRange(dateRange);
|
vacation.setAttendance(attendance);
|
}
|
|
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")) {
|
ContentValue.Attendance.DataRange dateRange = Optional.ofNullable(attendance.getDateRange()).orElse(new ContentValue.Attendance.DataRange());
|
dateRange.setType(String.valueOf(object));
|
attendance.setDateRange(dateRange);
|
} else if (control.getControlParam().equals("attendance.date_range.new_begin")) {
|
ContentValue.Attendance.DataRange dateRange = Optional.ofNullable(attendance.getDateRange()).orElse(new ContentValue.Attendance.DataRange());
|
if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
|
dateRange.setBegin(Long.parseLong(String.valueOf(object)));
|
} else if (field.getType().equals(Timestamp.class)) {
|
Timestamp time = (Timestamp) object;
|
dateRange.setBegin(Long.valueOf(time.getTime() / 1000));
|
}
|
attendance.setDateRange(dateRange);
|
} else if (control.getControlParam().equals("attendance.date_range.new_end")) {
|
ContentValue.Attendance.DataRange dateRange = Optional.ofNullable(attendance.getDateRange()).orElse(new ContentValue.Attendance.DataRange());
|
if (field.getType().equals(String.class) || field.getType().equals(Integer.class) || field.getType().equals(Long.class)) {
|
dateRange.setEnd(Long.parseLong(String.valueOf(object)));
|
} else if (field.getType().equals(Timestamp.class)) {
|
Timestamp time = (Timestamp) object;
|
dateRange.setEnd(Long.valueOf(time.getTime() / 1000));
|
}
|
attendance.setDateRange(dateRange);
|
} else if (control.getControlParam().equals("attendance.date_range.new_duration")) {
|
ContentValue.Attendance.DataRange dateRange = Optional.ofNullable(attendance.getDateRange()).orElse(new ContentValue.Attendance.DataRange());
|
dateRange.setDuration(Long.parseLong(String.valueOf(object)));
|
attendance.setDateRange(dateRange);
|
} else if (control.getControlParam().equals("attendance.type")) {
|
attendance.setType(Integer.parseInt(String.valueOf(object)));
|
}
|
|
value.setAttendance(attendance);
|
}
|
|
field.setAccessible(accessible);
|
// 保存数据
|
applyDataContent.setValue(value);
|
List<ApplyDataContent> applyDataContents = Optional.ofNullable(applyData.getContents()).orElse(new ArrayList<>());
|
if (!applyDataContents.isEmpty() && applyDataContents.stream().anyMatch(m -> m.getControl().equals(control.getControlType()) && m.getId().equals(control.getControlId()))) {
|
applyDataContents = applyDataContents.stream().map(m -> m.getControl().equals(control.getControlType()) && m.getId().equals(control.getControlId()) ? applyDataContent : m).collect(Collectors.toList());
|
} else {
|
applyDataContents.add(applyDataContent);
|
}
|
applyData.setContents(applyDataContents);
|
}
|
}
|
|
// 获取企业微信Token
|
wxCpService.getAccessToken(true);
|
// 初始化参数
|
WxCpOaApplyEventRequest eventRequest = new WxCpOaApplyEventRequest();
|
|
// 申请人userid
|
eventRequest.setCreatorUserId(userWxId);
|
// 模板id
|
eventRequest.setTemplateId(template.getTemplateId());
|
// 审批人模式
|
eventRequest.setUseTemplateApprover(1);
|
// 提单者提单部门id,不填默认为主部门
|
eventRequest.setChooseDepartment(null);
|
// 审批申请数据
|
eventRequest.setApplyData(applyData);
|
// 摘要信息
|
eventRequest.setSummaryList(new ArrayList<>(0));
|
// 发送申请请求
|
String spNo = wxCpService.getOaService().apply(eventRequest);
|
resultMap.put("spNo", spNo);
|
} catch (WxErrorException wxErrorException) {
|
// 通信失败
|
wxErrorException.printStackTrace();
|
resultMap.put("error", "通信失败");
|
} catch (IllegalAccessException illegalAccessException) {
|
// 解析后数据获取失败
|
illegalAccessException.printStackTrace();
|
resultMap.put("error", "解析类成员数据获取失败");
|
} catch (Exception e) {
|
// 处理异常
|
e.printStackTrace();
|
resultMap.put("error", "处理异常");
|
}
|
|
return resultMap;
|
}
|
}
|