/*
 *      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.houseuser.wrapper;


import cn.hutool.core.util.DesensitizedUtil;
import org.springblade.common.Constants;
import org.springblade.community.houseuser.entity.Houseuser;
import org.springblade.community.houseuser.vo.HouseuserVO;
import org.springblade.core.mp.support.BaseEntityWrapper;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.utils.AESPlus;

import java.util.Objects;

/**
 * 包装类,返回视图层所需的字段
 *
 * @author BladeX
 * @since 2020-11-06
 */
public class HouseuserWrapper extends BaseEntityWrapper<Houseuser, HouseuserVO> {

	private static final String SYMBOL = "*";

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

	@Override
	public HouseuserVO entityVO(Houseuser houseuser) {
		HouseuserVO houseuserVO = Objects.requireNonNull(BeanUtil.copy(houseuser, HouseuserVO.class));
		try{
			if(houseuser.getEncrypt() == 1){
				houseuserVO.setName(DesensitizedUtil.chineseName(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getName())));
				houseuserVO.setIdCard(DesensitizedUtil.idCardNum(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getIdCard()), 1, 2));
				houseuserVO.setTel(DesensitizedUtil.mobilePhone(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getTel())));
				houseuserVO.setImageUri(DesensitizedUtil.address(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getImageUri()), 20));
				houseuserVO.setAgencyName(DesensitizedUtil.chineseName(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getAgencyName())));
				houseuserVO.setResidentialName(DesensitizedUtil.address(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getResidentialName()), 2));
				houseuserVO.setBuildingName(DesensitizedUtil.address(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getBuildingName()), 2));
				houseuserVO.setUnitName(DesensitizedUtil.address(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getUnitName()), 2));
				houseuserVO.setRoomName(DesensitizedUtil.address(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getRoomName()), 2));
			}else{
				houseuserVO.setName(DesensitizedUtil.chineseName(houseuserVO.getName()));
				houseuserVO.setIdCard(DesensitizedUtil.idCardNum(houseuserVO.getIdCard(), 1, 2));
				houseuserVO.setTel(DesensitizedUtil.mobilePhone(houseuserVO.getTel()));
				houseuserVO.setImageUri(DesensitizedUtil.address(houseuserVO.getImageUri(), 20));
				houseuserVO.setAgencyName(DesensitizedUtil.chineseName(houseuserVO.getAgencyName()));
				houseuserVO.setResidentialName(DesensitizedUtil.address(houseuserVO.getResidentialName(), 2));
				houseuserVO.setBuildingName(DesensitizedUtil.address(houseuserVO.getBuildingName(), 2));
				houseuserVO.setUnitName(DesensitizedUtil.address(houseuserVO.getUnitName(), 2));
				houseuserVO.setRoomName(DesensitizedUtil.address(houseuserVO.getRoomName(), 2));
			}
		}catch (Exception e){

		}

		//名称脱敏
//		houseuserVO.setName(StringUtils.rightPad(StringUtils.left(houseuser.getName(),1),StringUtils.length(houseuser.getName()),SYMBOL));
		//手机号码脱敏
//		houseuserVO.setTel(StringUtils.left(houseuser.getTel(), 3).concat(StringUtils.removeStart(StringUtils.leftPad(
//			StringUtils.right(houseuser.getTel(), 4), StringUtils.length(houseuser.getTel()), SYMBOL), SYMBOL+SYMBOL+SYMBOL)));
		//身份证好
//		houseuserVO.setIdCard(StringUtils.left(houseuser.getIdCard(), 6).concat(StringUtils.removeStart(StringUtils.leftPad(
//			StringUtils.right(houseuser.getIdCard(), 4), StringUtils.length(houseuser.getIdCard()), SYMBOL),
//			SYMBOL+SYMBOL+SYMBOL+SYMBOL+SYMBOL+SYMBOL)));
		return houseuserVO;
	}

	public HouseuserVO editEntityVO(Houseuser houseuser) {
		HouseuserVO houseuserVO = Objects.requireNonNull(BeanUtil.copy(houseuser, HouseuserVO.class));
		try{
			houseuserVO.setName(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getName()));
			houseuserVO.setIdCard(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getIdCard()));
			houseuserVO.setTel(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getTel()));
			houseuserVO.setImageUri(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getImageUri()));
			houseuserVO.setAgencyName(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getAgencyName()));
			houseuserVO.setResidentialName(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getResidentialName()));
			houseuserVO.setBuildingName(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getBuildingName()));
			houseuserVO.setUnitName(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getUnitName()));
			houseuserVO.setRoomName(AESPlus.decrypt(Constants.encryptKey, houseuserVO.getRoomName()));
		}catch (Exception e){

		}
		return houseuserVO;
	}

}
