package com.bonus.sharedstation.app.service.impl;

import com.bonus.sharedstation.app.domain.GreenUserInfo;
import com.bonus.sharedstation.app.mapper.BaseMapper;
import com.bonus.sharedstation.common.utils.AesEncryptUtil;
import com.bonus.sharedstation.common.utils.SystemUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 基础工具类
 *
 * @author 96048
 */
@Component
@Slf4j
public class BaseService {

    @Resource
    private BaseMapper baseMapper;


    public GreenUserInfo getUserInfo(String userId) {
        if (userId == null || userId.trim().isEmpty()) {
            return null;
        }
        GreenUserInfo info = new GreenUserInfo();
        List<GreenUserInfo> userInfolist = baseMapper.getUserInfo(userId);
        if (!userInfolist.isEmpty()) {
            info.setId(userInfolist.get(0).getId());
            info.setUserId(userInfolist.get(0).getUserId());
            info.setLoginName(userInfolist.get(0).getLoginName());
            info.setRealName(userInfolist.get(0).getRealName());
            info.setMobile(userInfolist.get(0).getMobile());
            info.setDepartmentId(userInfolist.get(0).getDepartmentId());
            info.setDepartmentName(userInfolist.get(0).getDepartmentName());
            info.setOwnerLocation(userInfolist.get(0).getOwnerLocation());
            info.setMealNo(userInfolist.get(0).getMealNo());
            info.setIsDeleted(userInfolist.get(0).getIsDeleted());
            info.setIdCard(userInfolist.get(0).getIdCard());
            info.setGender(userInfolist.get(0).getGender());
            info.setTitle(userInfolist.get(0).getTitle());
            info.setLandline(userInfolist.get(0).getLandline());

            if (null != info.getIdCard() && !"".equals(info.getIdCard())) {
                String idCard;
                try {
                    idCard = AesEncryptUtil.desEncrypt(info.getIdCard());
                    if (idCard != null && !idCard.isEmpty()) {
                        int countAge = SystemUtil.countAge(idCard);
                        info.setAge(countAge);
                        info.setIdCard(idCard.trim());
                    } else {
                        info.setIdCard(null);
                    }
                    return info;
                } catch (Exception e) {
                    log.error("身份证解析失败");

                }
            }
            return info;
        }
        return null;
    }


}
