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.mapper.datacollection.AttendQuoteFieldMapper;
import com.engine.salary.service.AttendQuoteFieldService;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.db.MapperProxyFactory;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.util.page.PageUtil;
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();
private AttendQuoteFieldMapper getAttendQuoteFieldMapper() {
return MapperProxyFactory.getProxy(AttendQuoteFieldMapper.class);
}
// @RpcReference
// private RemoteAttendInitVacationService remoteAttendInitVacationService;
// @Autowired
// private LoggerTemplate attendQuoteFieldLoggerTemplate;
@Override
public List getAllAttendQuoteFields() {
return getAttendQuoteFieldMapper().listAll();
}
@Override
public PageInfo listPage(AttendQuoteFieldQueryParam queryParam) {
PageUtil.start(queryParam.getCurrent(), queryParam.getPageSize());
List list = getAttendQuoteFieldMapper().list(queryParam);
return new PageInfo<>(list);
}
@Override
public AttendQuoteFieldPO getById(Long id) {
return getAttendQuoteFieldMapper().getById(id);
}
@Override
public Map list(AttendQuoteFieldQueryParam queryParam) {
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