package com.cool.store.dto.enterprise;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.constants.CommonConstants;
import com.cool.store.entity.EnterpriseUserDO;
import com.cool.store.enums.FSEventTypeEnum;
import com.cool.store.enums.UserStatusEnum;
import com.cool.store.utils.UUIDUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.*;

/**
 * 
 * @author   zhangchenbiao
 * @date   2022-01-18 04:40
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class EnterpriseUserDTO implements Serializable {

    @ApiModelProperty("备注")
    private String remark;

    @ApiModelProperty("用户主键id")
    private Long id;

    @ApiModelProperty("钉钉用户id")
    private String userId;

    @ApiModelProperty("手机号码")
    private String mobile;

    @ApiModelProperty("")
    private String name;

    @ApiModelProperty("是否是主管理员，0：否，1：是")
    private Boolean mainAdmin;

    @ApiModelProperty("是否为企业的管理员, true表示是, false表示不是")
    private Boolean isAdmin;

    @ApiModelProperty("员工的电子邮箱")
    private String email;

    @ApiModelProperty("在当前isv全局范围内唯一标识一个用户的身份,用户无法修改")
    private String unionid;

    @ApiModelProperty("是否号码隐藏, true表示隐藏, false表示不隐藏")
    private Boolean isHide;

    @ApiModelProperty("职务")
    private String jobTitle;

    @ApiModelProperty("员工的企业邮箱")
    private String orgEmail;

    @ApiModelProperty("头像url")
    private String avatar;

    @ApiModelProperty("")
    private String roles;

    @ApiModelProperty("是否是部门的主管, true表示是, false表示不是")
    private Boolean isLeader;

    @ApiModelProperty("用户创建时间")
    private Date createTime;

    @ApiModelProperty("第三方OA系统唯一标识")
    private String thirdOaUniqueFlag;

    @ApiModelProperty("")
    private String orderInDepts;

    @ApiModelProperty("")
    private List<String> isLeaderInDepts;

    @ApiModelProperty("")
    private String monitoredDepartments;

    @ApiModelProperty("工号")
    private String jobnumber;

    private String openUserid;

    @ApiModelProperty("用户所属部门")
    private List<String> departmentLists;


    public static List<EnterpriseUserDO> transUserDtoToDo(List<EnterpriseUserDTO> userList, Map<String, String> regionPathMap, Multimap<String, String> leaderDeptMap, Multimap<String, String> roleUserMap) {
        if(CollectionUtils.isEmpty(userList)){
            return Lists.newArrayList();
        }
        List<EnterpriseUserDO> resultList = new ArrayList<>();
        for (EnterpriseUserDTO user : userList) {
            EnterpriseUserDO enterpriseUserDO = new EnterpriseUserDO();
            enterpriseUserDO.setUserId(user.getUserId());
            enterpriseUserDO.setName(user.getName());
            enterpriseUserDO.setRemark(user.getRemark());
            if(StringUtils.isNotBlank(user.getMobile())){
                enterpriseUserDO.setMobile(user.getMobile().replace("+86", ""));
            }
            enterpriseUserDO.setEmail(user.getEmail());
            enterpriseUserDO.setOrgEmail(user.getOrgEmail());
            enterpriseUserDO.setMainAdmin(user.getMainAdmin());
            enterpriseUserDO.setIsAdmin(user.getIsAdmin());
            enterpriseUserDO.setUnionid(user.getUnionid());
            enterpriseUserDO.setAvatar(user.getAvatar());
            enterpriseUserDO.setJobnumber(user.getJobnumber());
            enterpriseUserDO.setUserStatus(UserStatusEnum.NORMAL.getCode());
            enterpriseUserDO.setIsLeader(Boolean.FALSE);
            List<String> departmentLists = user.getDepartmentLists();
            List<String> regionPaths = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(departmentLists)){
                for (String departmentId : departmentLists) {
                    String regionPath = regionPathMap.get(departmentId);
                    if(StringUtils.isNotBlank(regionPath)){
                        regionPaths.add(regionPath);
                    }
                }
            }
            //挂跟节点
            if(CollectionUtils.isEmpty(regionPaths)){
                regionPaths.add(CommonConstants.ROOT_REGION_PATH);
            }
            Collection<String> deptIds = leaderDeptMap.get(user.getUserId());
            if(CollectionUtils.isNotEmpty(deptIds)){
                enterpriseUserDO.setIsLeader(Boolean.TRUE);
                enterpriseUserDO.setLeaderDeptIds(JSONObject.toJSONString(deptIds));
            }
            enterpriseUserDO.setUserRegionIds(JSONObject.toJSONString(regionPaths));
            enterpriseUserDO.setDeleted(Boolean.FALSE);
            enterpriseUserDO.setCreateTime(new Date());
            if(StringUtils.isNotBlank(user.getJobTitle())){
                roleUserMap.put(user.getJobTitle(), user.getUserId());
            }
            resultList.add(enterpriseUserDO);
        }
        return resultList;
    }


    public static EnterpriseUserDO transUserDtoToDo(EnterpriseUserDTO user, Map<String, String> regionPathMap, Multimap<String, String> leaderDeptMap, FSEventTypeEnum eventType) {
        EnterpriseUserDO enterpriseUserDO = new EnterpriseUserDO();
        enterpriseUserDO.setUserId(user.getUserId());
        enterpriseUserDO.setName(user.getName());
        enterpriseUserDO.setRemark(user.getRemark());
        if(StringUtils.isNotBlank(user.getMobile())){
            enterpriseUserDO.setMobile(user.getMobile().replace("+86", ""));
        }
        enterpriseUserDO.setEmail(user.getEmail());
        enterpriseUserDO.setOrgEmail(user.getOrgEmail());
        enterpriseUserDO.setMainAdmin(user.getMainAdmin());
        enterpriseUserDO.setIsAdmin(user.getIsAdmin());
        enterpriseUserDO.setUnionid(user.getUnionid());
        enterpriseUserDO.setAvatar(user.getAvatar());
        enterpriseUserDO.setJobnumber(user.getJobnumber());
        enterpriseUserDO.setUserStatus(UserStatusEnum.NORMAL.getCode());
        List<String> departmentLists = user.getDepartmentLists();
        List<String> regionPaths = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(departmentLists)){
            for (String departmentId : departmentLists) {
                String regionPath = regionPathMap.get(departmentId);
                if(StringUtils.isNotBlank(regionPath)){
                    regionPaths.add(regionPath);
                }
            }
        }
        if(CollectionUtils.isEmpty(regionPaths)){
            regionPaths.add(CommonConstants.ROOT_REGION_PATH);
        }
        //创建的时候给个默认值
        if(FSEventTypeEnum.USER_CREATED.equals(eventType)){
            enterpriseUserDO.setIsLeader(Boolean.FALSE);
        }
        Collection<String> deptIds = leaderDeptMap.get(user.getUserId());
        if(CollectionUtils.isNotEmpty(deptIds)){
            enterpriseUserDO.setIsLeader(Boolean.TRUE);
            enterpriseUserDO.setLeaderDeptIds(JSONObject.toJSONString(deptIds));
        }
        enterpriseUserDO.setUserRegionIds(JSONObject.toJSONString(regionPaths));
        enterpriseUserDO.setDeleted(Boolean.FALSE);
        enterpriseUserDO.setCreateTime(new Date());
        return enterpriseUserDO;
    }

}