package cn.com.ty.lift.business.project.dao.entity;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

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

/**
 * <p>
 * 区域表
 * </p>
 *
 * @author wang-hai-cheng
 * @since 2019-12-09
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class Region implements Serializable {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.ID_WORKER)
    private Long id;

    /**
     * 团队id
     */
    private Long mtCompanyId;

    /**
     * 区域代号
     */
    private String areaCode;

    /**
     * 省级行政代码
     */
    private String provinceCode;

    /**
     * 城市区域代码
     */
    private String cityCode;

    /**
     * 区域名称
     */
    private String areaName;

    /**
     * 用户id
     */
    private Long userId;

    /**
     * 文员id
     */
    private String clerk;

    /**
     * 备注
     */
    private String remark;

    /**
     * 拆分解析文员的id
     */
    public Set<Long> splitClerk(){
        if(StrUtil.isBlank(clerk)){
            return Collections.emptySet();
        }
        try {
            if(clerk.contains(",")){
                return Arrays.stream(clerk.split(",")).map(c -> Long.parseLong(c.trim())).collect(Collectors.toSet());
            }else{
                return new HashSet<>(Arrays.asList(Long.parseLong(clerk.trim())));
            }
        }catch (Exception e){
            return Collections.emptySet();
        }
    }

    /**
     * 返回区域主管和文员的id列表
     */
    public Set<Long> members(){
        Set<Long> members = new HashSet<>();
        if(Objects.nonNull(userId)){
            members.add(userId);
        }
        members.addAll(splitClerk());
        return members;
    }
}
