package com.cool.store.entity;

import com.alibaba.fastjson.JSONObject;
import com.cool.store.constants.CommonConstants;
import com.cool.store.dto.enterprise.EnterpriseUserDTO;
import com.cool.store.enums.DataSourceEnum;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * 
 * @author   zhangchenbiao
 * @date   2023-06-07 02:07
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserRegionMappingDO implements Serializable {
    @ApiModelProperty("主键")
    private Integer id;

    @ApiModelProperty("映射主键（区域或者是门店）")
    private String regionId;

    @ApiModelProperty("用户ID")
    private String userId;

    @ApiModelProperty("类型")
    private Integer type;

    @ApiModelProperty("删除标识")
    private Boolean deleted;

    @ApiModelProperty("创建人")
    private String createId;

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

    @ApiModelProperty("更新人")
    private String updateId;

    @ApiModelProperty("更新时间")
    private Date updateTime;

    public static List<UserRegionMappingDO> convertSyncDO(String regionId, List<EnterpriseUserDTO> departmentUsers){
        if(CollectionUtils.isEmpty(departmentUsers)){
            return Lists.newArrayList();
        }
        List<UserRegionMappingDO> resultList = new ArrayList<>();
        for (EnterpriseUserDTO departmentUser : departmentUsers) {
            UserRegionMappingDO userRegion = new UserRegionMappingDO();
            userRegion.setRegionId(regionId);
            userRegion.setUserId(departmentUser.getUserId());
            userRegion.setType(DataSourceEnum.SYNC.getCode());
            userRegion.setCreateTime(new Date());
            userRegion.setUpdateTime(new Date());
            userRegion.setDeleted(Boolean.FALSE);
            resultList.add(userRegion);
        }
        return resultList;
    }

    public static List<UserRegionMappingDO> convertSyncDO(List<String> regionIds, String userId){
        if(CollectionUtils.isEmpty(regionIds) || StringUtils.isBlank(userId)){
            return Lists.newArrayList();
        }
        List<UserRegionMappingDO> resultList = new ArrayList<>();
        for (String regionId : regionIds) {
            UserRegionMappingDO userRegion = new UserRegionMappingDO();
            userRegion.setRegionId(regionId);
            userRegion.setUserId(userId);
            userRegion.setType(DataSourceEnum.SYNC.getCode());
            userRegion.setCreateTime(new Date());
            userRegion.setUpdateTime(new Date());
            userRegion.setDeleted(Boolean.FALSE);
            resultList.add(userRegion);
        }
        return resultList;
    }


    public static List<UserRegionMappingDO> convertSyncDO(List<EnterpriseUserDO> userList){
        if(CollectionUtils.isEmpty(userList)){
            return Lists.newArrayList();
        }
        List<UserRegionMappingDO> resultList = new ArrayList<>();
        for (EnterpriseUserDO user : userList) {
            List<String> userRegionIds = JSONObject.parseArray(user.getUserRegionIds(), String.class).stream().distinct().collect(Collectors.toList());
            for (String userRegionId : userRegionIds) {
                if(StringUtils.isNotBlank(userRegionId)){
                    continue;
                }
                String[] split = userRegionId.split(CommonConstants.PATH_SPILT);
                if(split.length == 0){
                    continue;
                }
                UserRegionMappingDO userRegion = new UserRegionMappingDO();
                userRegion.setRegionId(split[split.length-1]);
                userRegion.setUserId(user.getUserId());
                userRegion.setType(DataSourceEnum.SYNC.getCode());
                userRegion.setCreateTime(new Date());
                userRegion.setUpdateTime(new Date());
                userRegion.setDeleted(Boolean.FALSE);
                resultList.add(userRegion);
            }
        }
        return resultList;
    }
}