package com.cool.store.vo.region;

import com.cool.store.entity.EnterpriseUserDO;
import com.cool.store.entity.RegionDO;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhangchenbiao
 * @FileName: RegionUserAndSubRegionVO
 * @Description:
 * @date 2023-07-17 19:34
 */
@Data
public class RegionUserAndSubRegionVO {

    @ApiModelProperty("区域列表")
    private List<RegionInfo> regionList;

    @ApiModelProperty("用户列表")
    private List<UserInfo> userList;

    public RegionUserAndSubRegionVO(List<RegionInfo> regionList, List<UserInfo> userList) {
        this.regionList = regionList;
        this.userList = userList;
    }

    public RegionUserAndSubRegionVO() {
        this.regionList = Lists.newArrayList();
        this.userList = Lists.newArrayList();
    }

    @Data
    public static class RegionInfo{

        @ApiModelProperty("区域id")
        private String regionId;

        @ApiModelProperty("区域名称")
        private String name;

    }

    @Data
    public static class UserInfo{

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

        @ApiModelProperty("用户名")
        private String username;

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

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

    }

    public static RegionUserAndSubRegionVO convertVO(List<RegionDO> subRegionList, List<String> userIds, Map<String, EnterpriseUserDO> userMap){
        List<RegionInfo> regionList = Lists.newArrayList();
        List<UserInfo> userList = Lists.newArrayList();
        for (RegionDO regionDO : subRegionList) {
            RegionInfo region = new RegionInfo();
            region.setRegionId(regionDO.getRegionId());
            region.setName(regionDO.getName());
            regionList.add(region);
        }
        for (String userId : userIds) {
            EnterpriseUserDO enterpriseUser = userMap.get(userId);
            if(Objects.isNull(enterpriseUser)){
                continue;
            }
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId(userId);
            userInfo.setUsername(enterpriseUser.getName());
            userInfo.setMobile(enterpriseUser.getMobile());
            userInfo.setAvatar(enterpriseUser.getAvatar());
            userList.add(userInfo);
        }
        return new RegionUserAndSubRegionVO(regionList, userList);
    }
}
