package fms

import (
	"fmt"
	"hongze/hz_crm_api/models/fms"
	"sort"
	"strings"
)

func GetContractServiceNameFormat(registerIds []int) (serviceNameMap map[int]string, serviceFormatMap map[int][]fms.ContractServiceTemplateFormat, err error) {
	serviceNameMap = make(map[int]string)
	serviceMap := make(map[int][]string, 0)
	serviceFormatMap = make(map[int][]fms.ContractServiceTemplateFormat, 0)
	serviceIndustryStr := "医药,消费,科技,智造"
	serviceIdMap := make(map[int]*fms.ContractServiceTemplateFullName, 0)
	serviceIndustryMap := make(map[int]map[string][]*fms.ContractServiceTemplateFullName, 0)
	allServiceList, e := fms.GetContractServiceTemplateFullName()
	if e != nil {
		err = fmt.Errorf("获取所有套餐失败, Err: " + e.Error())
		return
	}
	for _, v := range allServiceList {
		serviceIdMap[v.ServiceTemplateId] = v
	}
	// 获取服务套餐
	servicesList, e := fms.GetContractRegisterServicesByRegisterIds(registerIds)
	if e != nil {
		err = fmt.Errorf("获取套餐字符串失败, Err: " + e.Error())
		return
	}
	for i := range servicesList {
		item := servicesList[i]
		if item.Title == "行业套餐" || item.Title == "权益大套餐" {
			continue
		}
		if fullItem, ok := serviceIdMap[item.ServiceTemplateId]; ok {
			if fullItem.ParentTitle == "行业套餐" {
				continue
			}
			if fullItem.ParentTitle != "" {
				if strings.Contains(serviceIndustryStr, fullItem.ParentTitle) {
					if _, ok1 := serviceIndustryMap[item.ContractRegisterId]; !ok1 {
						serviceIndustryMap[item.ContractRegisterId] = make(map[string][]*fms.ContractServiceTemplateFullName)
					}
					serviceIndustryMap[item.ContractRegisterId][fullItem.ParentTitle] = append(serviceIndustryMap[item.ContractRegisterId][fullItem.ParentTitle], fullItem)
				} else {
					formatTitle := ""
					if fullItem.ParentTitle == "策略" {
						formatTitle = fullItem.Title
					} else {
						formatTitle = fullItem.ParentTitle + "(" + fullItem.Title + ")"
					}
					serviceMap[item.ContractRegisterId] = append(serviceMap[item.ContractRegisterId], formatTitle)
					tmp := fms.ContractServiceTemplateFormat{
						FormatTitle:        formatTitle,
						ServiceTemplateId:  fullItem.ServiceTemplateId,
						ServiceTemplatePid: fullItem.Pid,
						ServiceProductId:   fullItem.ProductId,
					}
					serviceFormatMap[item.ContractRegisterId] = append(serviceFormatMap[item.ContractRegisterId], tmp)
				}
			} else {
				serviceMap[item.ContractRegisterId] = append(serviceMap[item.ContractRegisterId], fullItem.Title)
				tmp := fms.ContractServiceTemplateFormat{
					FormatTitle:        fullItem.Title,
					ServiceTemplateId:  fullItem.ServiceTemplateId,
					ServiceTemplatePid: fullItem.Pid,
					ServiceProductId:   fullItem.ProductId,
				}
				serviceFormatMap[item.ContractRegisterId] = append(serviceFormatMap[item.ContractRegisterId], tmp)
			}
		}
	}
	for k, v := range serviceIndustryMap {
		for sk, sv := range v {
			formatTitle := ""
			templateId := 0
			pid := 0
			if len(sv) >= 2 {
				formatTitle = sk
				templateId = sv[0].Pid
			} else {
				formatTitle = sk + "(" + sv[0].Title + ")"
				templateId = sv[0].ServiceTemplateId
				pid = sv[0].Pid
			}
			serviceMap[k] = append(serviceMap[k], formatTitle)
			tmp := fms.ContractServiceTemplateFormat{
				FormatTitle:        formatTitle,
				ServiceTemplateId:  templateId,
				ServiceTemplatePid: pid,
				ServiceProductId:   sv[0].ProductId,
			}
			serviceFormatMap[k] = append(serviceFormatMap[k], tmp)
		}
	}

	// 给套餐内容排序
	for k, v := range serviceFormatMap {
		sort.Sort(fms.ContractServiceTemplateFormatList(v))
		for _, sv := range v {
			serviceNameMap[k] += sv.FormatTitle + ","
		}
		serviceNameMap[k] = strings.Trim(serviceNameMap[k], ",")
	}
	return
}
