package ink.wgink.login.base.service;

import com.alibaba.fastjson.JSONObject;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.interfaces.group.IGroupUserBaseService;
import ink.wgink.interfaces.position.IPositionUserBaseService;
import ink.wgink.interfaces.role.IRoleUserBaseService;
import ink.wgink.pojo.pos.DepartmentPO;
import ink.wgink.pojo.pos.GroupPO;
import ink.wgink.pojo.pos.PositionPO;
import ink.wgink.pojo.pos.RolePO;
import ink.wgink.properties.app.AppTokenProperties;
import ink.wgink.service.department.service.IDepartmentUserService;
import ink.wgink.service.user.pojo.pos.UserPO;
import ink.wgink.util.AesUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: BaseAppSignService
 * @Description: APP登录基础业务
 * @Author: wanggeng
 * @Date: 2021/4/7 5:40 下午
 * @Version: 1.0
 */
@Service
public class BaseAppSignService extends DefaultBaseService {

    @Autowired
    private IDepartmentUserService departmentUserService;
    @Autowired(required = false)
    private IRoleUserBaseService roleUserBaseService;
    @Autowired(required = false)
    private IPositionUserBaseService positionUserBaseService;
    @Autowired(required = false)
    private IGroupUserBaseService groupUserBaseService;
    @Autowired
    private AppTokenProperties appTokenProperties;

    /**
     * 获取token
     *
     * @param userPO
     * @return
     * @throws Exception
     */
    protected String getToken(UserPO userPO) throws UnsupportedEncodingException {
        return getToken(userPO, appTokenProperties.getHasDepartment(), appTokenProperties.getHasRole(), appTokenProperties.getHasPosition(), appTokenProperties.getHasGroup());
    }

    /**
     * 获取token
     *
     * @param userPO        用户信息
     * @param hasDepartment 包含部门
     * @param hasRole       包含角色
     * @param hasPosition   包含职位
     * @param hasGroup      包含组
     * @return
     * @throws UnsupportedEncodingException
     */
    protected String getToken(UserPO userPO, boolean hasDepartment, boolean hasRole, boolean hasPosition, boolean hasGroup) throws UnsupportedEncodingException {
        Map<String, Object> userInfo = new HashMap<>(8);
        // 这个参数是token认证标识
        userInfo.put(ISystemConstant.APP_TOKEN_SIGN, ISystemConstant.APP_TOKEN_VERIFY + System.currentTimeMillis());
        userInfo.put("id", userPO.getUserId());
        userInfo.put("name", userPO.getUserName());
        userInfo.put("avatar", userPO.getUserAvatar());
        userInfo.put("username", userPO.getUserUsername());
        userInfo.put("phone", userPO.getUserPhone());
        userInfo.put("email", userPO.getUserEmail());

        if (hasDepartment) {
            LOG.debug("人员部门列表");
            List<DepartmentPO> departmentPOs = departmentUserService.listDepartmentPOByUserId(userPO.getUserId());
            List<Map<String, Object>> departments = new ArrayList<>();
            for (DepartmentPO departmentPO : departmentPOs) {
                Map<String, Object> departmentMap = new HashMap<>(4);
                departmentMap.put("departmentId", departmentPO.getDepartmentId());
                departmentMap.put("departmentName", departmentPO.getDepartmentName());
                departments.add(departmentMap);
            }
            userInfo.put("departments", departments);
        }
        if (hasRole && roleUserBaseService != null) {
            LOG.debug("人员角色列表");
            List<RolePO> rolePOs = roleUserBaseService.listRolePOByUserId(userPO.getUserId());
            List<Map<String, Object>> roles = new ArrayList<>();
            for (RolePO rolePO : rolePOs) {
                Map<String, Object> roleMap = new HashMap<>(4);
                roleMap.put("roleId", rolePO.getRoleId());
                roleMap.put("roleName", rolePO.getRoleName());
                roles.add(roleMap);
            }
            userInfo.put("roles", roles);
        }
        if (hasPosition && positionUserBaseService != null) {
            LOG.debug("人员职位列表");
            List<PositionPO> positionPOs = positionUserBaseService.listPositionPOByUserId(userPO.getUserId());
            List<Map<String, Object>> positions = new ArrayList<>();
            for (PositionPO positionPO : positionPOs) {
                Map<String, Object> positionMap = new HashMap<>(2);
                positionMap.put("positionId", positionPO.getPositionId());
                positionMap.put("positionName", positionPO.getPositionName());
                positions.add(positionMap);
            }
            userInfo.put("positions", positions);
        }
        if (hasGroup && groupUserBaseService != null) {
            LOG.debug("人员组列表");
            List<GroupPO> groupPOs = groupUserBaseService.listGroupPOByUserId(userPO.getUserId());
            List<Map<String, Object>> groups = new ArrayList<>();
            for (GroupPO groupPO : groupPOs) {
                Map<String, Object> groupMap = new HashMap<>(2);
                groupMap.put("groupId", groupPO.getGroupId());
                groupMap.put("groupName", groupPO.getGroupName());
                groups.add(groupMap);
            }
            userInfo.put("groups", groups);
        }
        LOG.debug("userInfo: " + userInfo);
        return Base64.encodeBase64String(AesUtil.aesCommonEncoder(ISystemConstant.APP_TOKEN_AES_KEY, JSONObject.toJSONString(userInfo)).getBytes("UTF-8"));
    }

}
