/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.community.staff.wrapper;

import cn.hutool.core.util.DesensitizedUtil;
import org.springblade.common.Constants;
import org.springblade.community.agency.entity.Agency;
import org.springblade.community.agency.service.IAgencyService;
import org.springblade.community.residential.entity.Residential;
import org.springblade.community.residential.service.IResidentialService;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.community.staff.entity.EnterpriseStaff;
import org.springblade.community.staff.vo.EnterpriseStaffVO;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.utils.AESPlus;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2020-12-11
 */
public class EnterpriseStaffWrapper extends BaseEntityWrapper<EnterpriseStaff, EnterpriseStaffVO>  {

	private IAgencyService agencyService;
	private IResidentialService residentialService;

	public static EnterpriseStaffWrapper build() {
		return new EnterpriseStaffWrapper();
 	}

 	public EnterpriseStaffWrapper(){

	}
	public EnterpriseStaffWrapper(IAgencyService agencyService,IResidentialService residentialService){
		this.agencyService = agencyService;
		this.residentialService = residentialService;
	}
	public static EnterpriseStaffWrapper build(IAgencyService agencyService,IResidentialService residentialService) {
		return new EnterpriseStaffWrapper(agencyService,residentialService);
	}

	@Override
	public EnterpriseStaffVO entityVO(EnterpriseStaff enterpriseStaff) {
		EnterpriseStaffVO enterpriseStaffVO = Objects.requireNonNull(BeanUtil.copy(enterpriseStaff, EnterpriseStaffVO.class));

		//企业所属园区名称
		if(enterpriseStaff.getAgencyId() != null){
			Agency agency = agencyService.getById(enterpriseStaff.getAgencyId());
			if(null != agency){
				enterpriseStaffVO.setAgencyName(agency.getAgencyName());
			}
		}

		//企业所属分区名称
		if(enterpriseStaff.getResidentialId() != null){
			Residential residential = residentialService.getById(enterpriseStaff.getResidentialId());
			if(null != residential){
				enterpriseStaffVO.setResidentialName(residential.getName());
			}
		}
		try {
			enterpriseStaffVO.setRealName(DesensitizedUtil.chineseName(enterpriseStaffVO.getRealName()));
			//員工身份證信息脫敏
			enterpriseStaffVO.setIdcard(DesensitizedUtil.idCardNum(enterpriseStaffVO.getIdcard(), 1, 2));
			//員工手機號碼脫敏
			enterpriseStaffVO.setPhone(DesensitizedUtil.mobilePhone(enterpriseStaffVO.getPhone()));
			//員工郵箱脫敏
			enterpriseStaffVO.setEmail(DesensitizedUtil.email(enterpriseStaffVO.getEmail()));
			/*enterpriseStaffVO.setRealName(DesensitizedUtil.chineseName(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getRealName())));
			//員工身份證信息脫敏
			enterpriseStaffVO.setIdcard(DesensitizedUtil.idCardNum(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getIdcard()), 1, 2));
			//員工手機號碼脫敏
			enterpriseStaffVO.setPhone(DesensitizedUtil.mobilePhone(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getPhone())));
			//員工郵箱脫敏
			enterpriseStaffVO.setEmail(DesensitizedUtil.email(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getEmail())));*/
		} catch (Exception e) {
			e.printStackTrace();
		}
		return enterpriseStaffVO;
	}

	public EnterpriseStaffVO entityVO(EnterpriseStaff enterpriseStaff, Integer isEdit) { //1表示编辑
		EnterpriseStaffVO enterpriseStaffVO = Objects.requireNonNull(BeanUtil.copy(enterpriseStaff, EnterpriseStaffVO.class));

		//企业所属园区名称
		if(enterpriseStaff.getAgencyId() != null){
			Agency agency = agencyService.getById(enterpriseStaff.getAgencyId());
			if(null != agency){
				enterpriseStaffVO.setAgencyName(agency.getAgencyName());
			}
		}

		//企业所属分区名称
		if(enterpriseStaff.getResidentialId() != null){
			Residential residential = residentialService.getById(enterpriseStaff.getResidentialId());
			if(null != residential){
				enterpriseStaffVO.setResidentialName(residential.getName());
			}
		}
		if(isEdit != 1){
			//員工姓名脫敏
			try {
				enterpriseStaffVO.setRealName(DesensitizedUtil.chineseName(enterpriseStaffVO.getRealName()));
				//員工身份證信息脫敏
				enterpriseStaffVO.setIdcard(DesensitizedUtil.idCardNum(enterpriseStaffVO.getIdcard(), 1, 2));
				//員工手機號碼脫敏
				enterpriseStaffVO.setPhone(DesensitizedUtil.mobilePhone(enterpriseStaffVO.getPhone()));
				//員工郵箱脫敏
				enterpriseStaffVO.setEmail(DesensitizedUtil.email(enterpriseStaffVO.getEmail()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		/*if(!isEdit){
			//員工姓名脫敏
			try {
				enterpriseStaffVO.setRealName(DesensitizedUtil.chineseName(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getRealName())));
				//員工身份證信息脫敏
				enterpriseStaffVO.setIdcard(DesensitizedUtil.idCardNum(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getIdcard()), 1, 2));
				//員工手機號碼脫敏
				enterpriseStaffVO.setPhone(DesensitizedUtil.mobilePhone(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getPhone())));
				//員工郵箱脫敏
				enterpriseStaffVO.setEmail(DesensitizedUtil.email(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getEmail())));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			try {
				enterpriseStaffVO.setRealName(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getRealName()));
				//員工身份證信息脫敏
				enterpriseStaffVO.setIdcard(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getIdcard()));
				//員工手機號碼脫敏
				enterpriseStaffVO.setPhone(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getPhone()));
				//員工郵箱脫敏
				enterpriseStaffVO.setEmail(AESPlus.decrypt(Constants.encryptKey, enterpriseStaffVO.getEmail()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}*/
		return enterpriseStaffVO;
	}

}
