package com.cool.store.vo;

import com.cool.store.entity.HyOpenAreaInfoDO;
import com.cool.store.entity.LineInfoDO;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @author zhangchenbiao
 * @FileName: LineUsernameAndMobileVO
 * @Description:
 * @date 2024-04-07 14:39
 */
@Data
public class LinePointBaseInfoVO {

    @ApiModelProperty("线索id")
    private Long lineId;

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

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

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

    @ApiModelProperty("区域路径")
    private String areaPath;

    @ApiModelProperty("用户标签")
    private List<String> userPortraitList;

    @ApiModelProperty("招商经理")
    private String investmentManagerUsername;

    @ApiModelProperty("已推门店")
    private Integer recommendShopNum;

    @ApiModelProperty("已选门店")
    private Integer selectedShopNum;

    @ApiModelProperty("意向开店数量")
    private Integer wantShopNum;

    public LinePointBaseInfoVO(Long lineId, String username, String mobile) {
        this.lineId = lineId;
        this.username = username;
        this.mobile = mobile;
    }

    public static List<LinePointBaseInfoVO> convertList(List<LineInfoDO> lineList, Map<String, String> userNameMap, Map<Long, String> userPortraitMap, Map<Long, HyOpenAreaInfoDO> cityMap, Map<Long, Integer> recommendShopNumMap, Map<Long, Integer> selectedShopNumMap){
        List<LinePointBaseInfoVO> resultList = new ArrayList<>();
        for (LineInfoDO lineInfo : lineList) {
            LinePointBaseInfoVO linePointBaseInfo = new LinePointBaseInfoVO(lineInfo.getId(), lineInfo.getUsername(), lineInfo.getMobile());
            linePointBaseInfo.setWantShopNum(lineInfo.getWantShopNum());
            String username = userNameMap.get(lineInfo.getInvestmentManager());
            linePointBaseInfo.setInvestmentManagerUsername(username);
            HyOpenAreaInfoDO areaInfo = cityMap.get(lineInfo.getWantShopAreaId());
            if(Objects.nonNull(areaInfo)){
                linePointBaseInfo.setAreaName(areaInfo.getAreaName());
                linePointBaseInfo.setAreaPath(areaInfo.getAreaPath().replace("/", "").trim());
            }
            linePointBaseInfo.setRecommendShopNum(recommendShopNumMap.get(lineInfo.getId()));
            linePointBaseInfo.setSelectedShopNum(selectedShopNumMap.get(lineInfo.getId()));
            if(StringUtils.isNotBlank(lineInfo.getUserPortrait())){
                List<String> userPortraitList = new ArrayList<>();
                String[] parts = lineInfo.getUserPortrait().split(",");
                for (String part : parts) {
                    String trimmedPart = part.trim();
                    if (StringUtils.isNotBlank(trimmedPart)) {
                        String s = userPortraitMap.get(Long.valueOf(part));
                        if(StringUtils.isNotBlank(s)){
                            userPortraitList.add(s);
                        }
                    }
                }
                linePointBaseInfo.setUserPortraitList(userPortraitList);
            }
            resultList.add(linePointBaseInfo);
        }
        return resultList;
    }


    public static LinePointBaseInfoVO convert(LineInfoDO lineInfo, String username, Map<Long, String> userPortraitMap, HyOpenAreaInfoDO cityArea, Map<Long, Integer> recommendShopNumMap, Map<Long, Integer> selectedShopNumMap){
        LinePointBaseInfoVO linePointBaseInfo = new LinePointBaseInfoVO(lineInfo.getId(), lineInfo.getUsername(), lineInfo.getMobile());
        linePointBaseInfo.setWantShopNum(lineInfo.getWantShopNum());
        linePointBaseInfo.setInvestmentManagerUsername(username);
        if(Objects.nonNull(cityArea)){
            linePointBaseInfo.setAreaName(cityArea.getAreaName());
            linePointBaseInfo.setAreaPath(cityArea.getAreaPath().replace("/", "").trim());
        }
        linePointBaseInfo.setRecommendShopNum(recommendShopNumMap.get(lineInfo.getId()));
        linePointBaseInfo.setSelectedShopNum(selectedShopNumMap.get(lineInfo.getId()));
        if(StringUtils.isNotBlank(lineInfo.getUserPortrait())){
            List<String> userPortraitList = new ArrayList<>();
            String[] parts = lineInfo.getUserPortrait().split(",");
            for (String part : parts) {
                String trimmedPart = part.trim();
                if (StringUtils.isNotBlank(trimmedPart)) {
                    String s = userPortraitMap.get(Long.valueOf(part));
                    if(StringUtils.isNotBlank(s)){
                        userPortraitList.add(s);
                    }
                }
            }
            linePointBaseInfo.setUserPortraitList(userPortraitList);
        }
        return linePointBaseInfo;
    }

}
