package com.engine.salary.service.impl; import com.cloudstore.eccom.result.WeaResultMsg; import com.engine.core.impl.Service; import com.engine.salary.biz.AttendQuoteFieldBiz; import com.engine.salary.component.SalaryWeaTable; import com.engine.salary.entity.datacollection.bo.AttendQuoteFieldBO; import com.engine.salary.entity.datacollection.dto.AttendQuoteFieldListDTO; import com.engine.salary.entity.datacollection.param.AttendQuoteFieldQueryParam; import com.engine.salary.entity.datacollection.param.AttendQuoteFieldSaveParam; import com.engine.salary.entity.datacollection.po.AttendQuoteFieldPO; import com.engine.salary.enums.datacollection.AttendQuoteFieldSourceTypeEnum; import com.engine.salary.enums.datacollection.AttendQuoteFieldTypeEnum; import com.engine.salary.exception.SalaryRunTimeException; import com.engine.salary.service.AttendQuoteFieldService; import com.engine.salary.util.SalaryI18nUtil; import com.google.common.collect.Lists; import org.apache.commons.collections4.CollectionUtils; import org.springframework.beans.BeanUtils; import java.util.*; import java.util.stream.Collectors; import static com.engine.salary.constant.SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY; /** * 数据采集-考勤引用字段 *

Copyright: Copyright (c) 2022

*

Company: 泛微软件

* * @author qiantao * @version 1.0 **/ public class AttendQuoteFieldServiceImpl extends Service implements AttendQuoteFieldService { AttendQuoteFieldBiz biz = new AttendQuoteFieldBiz(); // @Resource // private AttendQuoteFieldMapper mapper; // @RpcReference // private RemoteAttendInitVacationService remoteAttendInitVacationService; // @Autowired // private LoggerTemplate attendQuoteFieldLoggerTemplate; @Override public Map list(AttendQuoteFieldQueryParam queryParam) { // todo 同步字段 syncAttendFields(); String fields = " t1.id," + " t1.field_name as fieldName," + " t1.source_type as sourceType," + " t1.field_type as fieldType," + " t1.enable_status as enableStatus," + " t1.description"; String fromSql = " FROM" + " hrsa_attend_quote_field t1 "; SalaryWeaTable table = new SalaryWeaTable(user, AttendQuoteFieldListDTO.class); table.setBackfields(fields); table.setSqlform(fromSql); table.setSqlwhere(AttendQuoteFieldQueryParam.genWhereSql(queryParam)); table.setSqlorderby("t1.source_type,t1.id DESC"); table.setSqlprimarykey("t1.id"); table.setSqlisdistinct("false"); WeaResultMsg result = new WeaResultMsg(false); result.putAll(table.makeDataResult()); result.success(); return result.getResultMap(); } @Override public AttendQuoteFieldPO getFrom(Long id) { if (id != null) { AttendQuoteFieldPO po = biz.getById(id); if (po == null) { throw new SalaryRunTimeException(String.format("字段不存在[id:%s]", id)); } return po; } return null; } @Override public String save(AttendQuoteFieldSaveParam saveParam) { //参数校验 AttendQuoteFieldSaveParam.checkParam(saveParam); List attendQuoteFields = biz.listSome(AttendQuoteFieldPO.builder().fieldName(saveParam.getFieldName()).build()); if (CollectionUtils.isNotEmpty(attendQuoteFields)) { throw new SalaryRunTimeException("字段名称不允许重复"); } Date now = new Date(); AttendQuoteFieldPO attendQuoteField = AttendQuoteFieldPO.builder() .fieldName(saveParam.getFieldName()) .sourceType(AttendQuoteFieldSourceTypeEnum.DEFAULT.getValue()) .fieldType(saveParam.getFieldType().getValue()) .enableStatus(saveParam.getEnableStatus() ? 1 : 0) .description(saveParam.getDescription()) .createTime(now) .updateTime(now) .creator((long) user.getUID()) .tenantKey(DEFAULT_TENANT_KEY) .build(); // 保存 biz.save(attendQuoteField); return null; } @Override public String update(AttendQuoteFieldSaveParam saveParam) { // 校验是否可以编辑 AttendQuoteFieldSaveParam.checkParam(saveParam); if (saveParam.getId() == null) { throw new SalaryRunTimeException("id为空"); } AttendQuoteFieldPO attendQuoteField = biz.getById(saveParam.getId()); if (attendQuoteField == null) { throw new SalaryRunTimeException("该字段不存在"); } if (!attendQuoteField.getSourceType().equals(AttendQuoteFieldSourceTypeEnum.DEFAULT.getValue())) { throw new SalaryRunTimeException("来源只能为自定义"); } List attendQuoteFields = biz.listSome(AttendQuoteFieldPO.builder().fieldName(saveParam.getFieldName()).build()); if (CollectionUtils.isNotEmpty(attendQuoteFields)) { boolean fieldNameExist = attendQuoteFields.stream().anyMatch(e -> !Objects.equals(e.getId(), saveParam.getId())); if (fieldNameExist) { throw new SalaryRunTimeException("字段名称不允许重复"); } } AttendQuoteFieldPO newAttendQuoteField = new AttendQuoteFieldPO(); BeanUtils.copyProperties(attendQuoteField, newAttendQuoteField); newAttendQuoteField.setFieldName(saveParam.getFieldName()); newAttendQuoteField.setFieldType(saveParam.getFieldType().getValue()); newAttendQuoteField.setEnableStatus(saveParam.getEnableStatus() ? 1 : 0); newAttendQuoteField.setDescription(saveParam.getDescription()); newAttendQuoteField.setUpdateTime(new Date()); // 更新 biz.update(newAttendQuoteField); return null; } @Override public String delete(Collection ids) { if (CollectionUtils.isEmpty(ids)) { throw new SalaryRunTimeException("参数错误"); } List attendQuoteFields = biz.list(AttendQuoteFieldQueryParam.builder().ids(ids).build()); if (CollectionUtils.isEmpty(attendQuoteFields)) { throw new SalaryRunTimeException("要删除的数据在不存在或已删除"); } List attendQuoteAttendFields = attendQuoteFields.stream().filter(e -> e.getSourceType().equals(String.valueOf(AttendQuoteFieldSourceTypeEnum.ATTEND.getValue()))).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(attendQuoteAttendFields)) { throw new SalaryRunTimeException("考勤模块的字段不能删除"); } // TODO 正在使用的记录不允许删除 biz.deleteByIds(ids); return null; } @Override public String updateEnableStatus(AttendQuoteFieldSaveParam param) { // 校验是否可以编辑 Long id = param.getId(); Boolean enableStatus = param.getEnableStatus(); if (id == null || enableStatus == null) { throw new SalaryRunTimeException("参数错误"); } AttendQuoteFieldPO attendQuoteField = biz.getById(id); if (attendQuoteField == null) { throw new SalaryRunTimeException("该字段不存在"); } if (!attendQuoteField.getSourceType().equals(AttendQuoteFieldSourceTypeEnum.DEFAULT.getValue())) { throw new SalaryRunTimeException("来源只能为自定义"); } AttendQuoteFieldPO newAttendQuoteField = new AttendQuoteFieldPO(); BeanUtils.copyProperties(attendQuoteField, newAttendQuoteField); newAttendQuoteField.setEnableStatus(enableStatus ? 1 : 0); newAttendQuoteField.setUpdateTime(new Date()); // 更新 biz.update(newAttendQuoteField); return null; } @Override public String syncAttendFields() { // 所有字段管理的考勤模块引用字段 List attendQuoteFields = biz.listSome(AttendQuoteFieldPO.builder().sourceType(AttendQuoteFieldSourceTypeEnum.ATTEND.getValue()).build()); // 1.本地字段 List localFieldCodes = attendQuoteFields.stream().map(AttendQuoteFieldPO::getCode).collect(Collectors.toList()); // 2.考勤模块字段 // 请假类型 // List attendVacationTypes; // try { // attendVacationTypes = remoteAttendInitVacationService.queryVacationTypes(Boolean.TRUE, currentTenantKey); // } catch (Exception e) { // log.error("同步考勤字段失败,请检查接口服务remoteAttendInitVacationService.queryVacationTypes:{}", e.getMessage()); // return WeaResult.success(StringUtils.EMPTY); // } // // 请假类型:按小时 // List attendLeaveHourFieldCodes = CollectionUtils.emptyIfNull(attendVacationTypes).stream().map(m -> m.getId() + "_leave_hour").collect(Collectors.toList()); // // 请假类型:按天 // List attendLeaveDayFieldCodes = CollectionUtils.emptyIfNull(attendVacationTypes).stream().map(m -> m.getId() + "_leave_day").collect(Collectors.toList()); // List attendLeaveFieldCodes = Lists.newArrayList(); // attendLeaveFieldCodes.addAll(attendLeaveHourFieldCodes); // attendLeaveFieldCodes.addAll(attendLeaveDayFieldCodes); // 所有考勤模块字段编码 List attendFieldCodes = Lists.newArrayList(); // attendFieldCodes.addAll(attendLeaveFieldCodes); // 考勤模块固定字段 List> attendFixedFieldCodes = AttendQuoteFieldBO.buildAttendFixedFields(); attendFieldCodes.addAll(attendFixedFieldCodes.stream().map(m -> m.get("code")).collect(Collectors.toList())); // 本地已存在则更新【交集】 List updateCodes = localFieldCodes.stream().filter(item -> attendFieldCodes.contains(item)).collect(Collectors.toList()); // 本地比attend多,则删除【差集(local - attend)】 Collection deleteCodes = localFieldCodes.stream().filter(item -> !attendFieldCodes.contains(item)).collect(Collectors.toList()); // attend比本地多,则新增【差集(attend - local)】 List saveCodes = attendFieldCodes.stream().filter(item -> !localFieldCodes.contains(item)).collect(Collectors.toList()); String hourI18n = SalaryI18nUtil.getI18nLabel(100743, "小时"); String dayI18n = SalaryI18nUtil.getI18nLabel(100744, "天"); // 1.更新 == 目前除了请假类型其余都是固定,故只需更新请假类型部分 // List updates = Lists.newArrayList(); // CollectionUtils.emptyIfNull(updateCodes).forEach(e -> { // Optional optionalHour = CollectionUtils.emptyIfNull(attendVacationTypes).stream().filter(m -> (m.getId() + "_leave_hour").equals(e)).findFirst(); // if (optionalHour.isPresent()) { // AttendVacationType hour = optionalHour.get(); // updates.add(AttendQuoteFieldPO.builder() // .code(e) // .fieldName(hour.getName() + "(" + hourI18n + ")") // .updateTime(LocalDateTime.now()) // .build()); // } // Optional optionalDay = CollectionUtils.emptyIfNull(attendVacationTypes).stream().filter(m -> (m.getId() + "_leave_day").equals(e)).findFirst(); // if (optionalDay.isPresent()) { // AttendVacationType day = optionalDay.get(); // updates.add(AttendQuoteFieldPO.builder() // .code(e) // .fieldName(day.getName() + "(" + dayI18n + ")") // .updateTime(LocalDateTime.now()) // .build()); // } // }); // if (CollectionUtils.isNotEmpty(updates)) { // mapper.updateNameByCode(updates); // } // 2.删除 if (CollectionUtils.isNotEmpty(deleteCodes)) { //根据考勤字段编码删除考勤模块字段 biz.deleteAttendByCode(AttendQuoteFieldQueryParam.builder().codes(deleteCodes).sourceType(AttendQuoteFieldSourceTypeEnum.ATTEND.getValue()).build()); } // 3.新增 List saves = new ArrayList<>(); saves.addAll(attendFixedFieldCodes.stream().filter(e -> saveCodes.contains(e.get("code"))).map(m -> buildAttendQuoteField(m.get("code"), m.get("name"), (long)user.getUID()) ).collect(Collectors.toList())); // saveCodes.forEach(e -> { // Optional optionalHour = CollectionUtils.emptyIfNull(attendVacationTypes).stream().filter(m -> (m.getId() + "_leave_hour").equals(e)).findFirst(); // if (optionalHour.isPresent()) { // AttendVacationType hour = optionalHour.get(); // saves.add(buildAttendQuoteField(e, hour.getName() + "(" + hourI18n + ")", currentEmployeeId, currentTenantKey)); // } // Optional optionalDay = CollectionUtils.emptyIfNull(attendVacationTypes).stream().filter(m -> (m.getId() + "_leave_day").equals(e)).findFirst(); // if (optionalDay.isPresent()) { // AttendVacationType day = optionalDay.get(); // saves.add(buildAttendQuoteField(e, day.getName() + "(" + dayI18n + ")", currentEmployeeId, currentTenantKey)); // } // }); if (CollectionUtils.isNotEmpty(saves)) { biz.saveBatch(saves); } return null; } /** * 构建考勤字段 * * @param code * @param name * @param currentEmployeeId * @return */ private AttendQuoteFieldPO buildAttendQuoteField(String code, String name, Long currentEmployeeId) { Date now = new Date(); return AttendQuoteFieldPO.builder() .code(code) .fieldName(name) .sourceType(AttendQuoteFieldSourceTypeEnum.ATTEND.getValue()) .fieldType(AttendQuoteFieldTypeEnum.NUMBER.getValue()) .enableStatus(1) .description("") .createTime(now) .updateTime(now) .creator(currentEmployeeId) .tenantKey(DEFAULT_TENANT_KEY) .build(); } @Override public List listAll() { return biz.list(AttendQuoteFieldQueryParam.builder().build()); } }