package net.lab1024.smartadmin.util; import net.lab1024.smartadmin.constant.BpmConst; import net.lab1024.smartadmin.module.system.employee.domain.bo.EmployeeBO; import net.lab1024.smartadmin.module.system.login.domain.RequestTokenBO; import org.apache.commons.beanutils.PropertyUtils; import org.springframework.beans.BeanUtils; import java.util.Collections; import java.util.Date; import java.util.List; import java.util.stream.Collectors; public class SmartBeanUtil { /** * 复制bean的属性 * * @param source 源 要复制的对象 * @param target 目标 复制到此对象 */ public static void copyProperties(Object source, Object target) { BeanUtils.copyProperties(source, target); } /** * 复制对象 * * @param source 源 要复制的对象 * @param target 目标 复制到此对象 * @param * @return */ public static T copy(Object source, Class target) { if(source == null || target == null){ return null; } try { T newInstance = target.newInstance(); BeanUtils.copyProperties(source, newInstance); return newInstance; } catch (Exception e) { throw new RuntimeException(e); } } /** * 复制list * * @param source * @param target * @param * @param * @return */ public static List copyList(List source, Class target) { if (null == source || source.isEmpty()) { return Collections.emptyList(); } return source.stream().map(e -> copy(e, target)).collect(Collectors.toList()); } /** * 复制对象(自动设置操作人员信息) * * @param source 源 要复制的对象 * @param target 目标 复制到此对象 * @param requestToken 登录对象 * @param * @return */ public static T copy(Object source, Class target, RequestTokenBO requestToken) { return copy(source, target, requestToken.getEmployeeBO()); } /** * 复制对象(自动设置操作人员信息) * * @param source 源 要复制的对象 * @param target 目标 复制到此对象 * @param employeeBO 登录用户 * @param * @return */ public static T copy(Object source, Class target, EmployeeBO employeeBO) { if(source == null || target == null){ return null; } try { T newInstance = target.newInstance(); BeanUtils.copyProperties(source, newInstance); try { Object id = PropertyUtils.getProperty(newInstance, "id"); if (id == null) { setInsertInfo(newInstance, employeeBO); }else { setUpdateInfo(newInstance, employeeBO); } //setUpdateInfo(newInstance, employeeBO); } catch (Exception e) { e.printStackTrace(); } return newInstance; } catch (Exception e) { throw new RuntimeException(e); } } /** * 设置默认新增信息 * @return */ public static void setInsertInfo(Object obj, EmployeeBO employeeBO) { if(obj == null){ return; } Date date = SmartDateUtil.getDate();; try { Class type = PropertyUtils.getPropertyType(obj, "createBy"); if (type == Integer.class) { PropertyUtils.setProperty(obj, "createBy", employeeBO.getId().intValue()); } else if (type == Long.class) { PropertyUtils.setProperty(obj, "createBy", employeeBO.getId()); }else{ PropertyUtils.setProperty(obj, "createBy", employeeBO.getId().toString()); } } catch (Exception e) { e.printStackTrace(); } try { PropertyUtils.setProperty(obj, "createTime", date); } catch (Exception e) { e.printStackTrace(); } try { PropertyUtils.setProperty(obj, "department", employeeBO.getDepartmentId().toString()); } catch (Exception e) { e.printStackTrace(); } try { PropertyUtils.setProperty(obj, "company", employeeBO.getCompanyId().toString()); } catch (Exception e) { e.printStackTrace(); } try { PropertyUtils.setProperty(obj, "delFlag", BpmConst.DEL_FLAG_0); } catch (Exception e) { e.printStackTrace(); } setUpdateInfo(obj,employeeBO); } /** * 设置默认更新信息 * @return */ public static void setUpdateInfo(Object obj, EmployeeBO employeeBO) { if(obj == null){ return; } Date date = SmartDateUtil.getDate(); try { Class type = PropertyUtils.getPropertyType(obj, "updateBy"); if (type == Integer.class) { PropertyUtils.setProperty(obj, "updateBy", employeeBO.getId().intValue()); }else if (type == Long.class) { PropertyUtils.setProperty(obj, "updateBy", employeeBO.getId()); } else { PropertyUtils.setProperty(obj, "updateBy", employeeBO.getId().toString()); } } catch (Exception e) { e.printStackTrace(); } try { PropertyUtils.setProperty(obj, "updateTime", date); } catch (Exception e) { e.printStackTrace(); } } }