package cygx

import (
	"errors"
	"fmt"
	"hongze/hz_crm_api/models/company"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/services/alarm_msg"
	"hongze/hz_crm_api/utils"
	"strconv"
	"strings"
	"time"
)

//func init() {
//	UpdateCygxCompanyUserType(708)
//}

// UpdateCygxCompanyUserType 处理权益客户身份类型(判断是否为大套餐客户)
func UpdateCygxCompanyUserType(companyContractId int) (err error) {
	defer func() {
		if err != nil {
			//fmt.Println(err)
			go alarm_msg.SendAlarmMsg("处理权益客户身份类型, 判断是否为大套餐客户失败, Err："+err.Error()+"companyContractId:"+strconv.Itoa(companyContractId), 3)
		}
	}()
	permissionList, err := company.GetCompanyContractPermissionListByContractId(companyContractId)
	if err != nil {
		err = errors.New("GetCompanyContractPermissionListByContractId, Err:" + err.Error())
		return
	}
	var companyId int
	mappermissionName := make(map[string]int)
	for _, v := range permissionList {
		if v.IsUpgrade == 0 {
			mappermissionName[v.ChartPermissionName]++
		}
		companyId = v.CompanyId
	}
	fmt.Println(mappermissionName)
	//如果这些条件都满足那么就是大套餐客户
	if mappermissionName[utils.YI_YAO_NAME] == 2 && mappermissionName[utils.XIAO_FEI_NAME] == 2 && mappermissionName[utils.KE_JI_NAME] == 2 && mappermissionName[utils.ZHI_ZAO_NAME] == 2 && mappermissionName[utils.CE_LUE_NAME] == 1 && mappermissionName[utils.LU_YAN_FU_WU] == 1 {
		total, e := cygx.GetCygxCompanyUserTypeByCompanyId(companyId)
		if e != nil {
			err = errors.New("GetCygxCompanyUserTypeByCompanyId, Err:" + e.Error())
			return
		}

		// 获取合同信息-套餐信息
		companyContract, e := company.GetCompanyContractById(companyContractId)
		if e != nil {
			err = errors.New("GetCompanyContractById, Err: " + e.Error())
			return
		}

		if total == 0 {
			item := new(cygx.CygxCompanyUserType)
			item.CompanyId = companyId
			item.CustomerTypeId = 2 // 大套餐客户类型定义为 2
			item.CreateTime = time.Now()
			item.ModifyTime = time.Now()
			item.PackageType = companyContract.RaiPackageType
			if e = cygx.AddCygxCompanyUserType(item); e != nil {
				err = errors.New("AddCygxCompanyUserType, Err:" + e.Error())
				return
			}
		} else {
			if e = cygx.UpdateCygxCompanyUserTypeAndPackageType(companyId, 2, companyContract.RaiPackageType); e != nil {
				err = errors.New("UpdateCygxCompanyUserTypeAndPackageType, Err:" + e.Error())
				return
			}
		}
	} else {
		if e := cygx.UpdateCygxCompanyUserType(companyId, 0); e != nil {
			err = errors.New("UpdateCygxCompanyUserType, Err:" + e.Error())
			return
		}
	}
	return
}

func GetActivityCcustomerTypeList() (mapItem map[int]string, err error) {
	list, e := cygx.GetActivityCcustomerTypeList("")
	if e != nil {
		err = e
		return
	}
	mapUserType := make(map[int]string)
	for _, v := range list {
		mapUserType[v.CustomerTypeId] = v.PermissionValue
	}
	mapUserType[0] = "0"
	mapItem = mapUserType
	return
}

func GetCustomerTypeIds(customerTypeIds string) (customerTypeIdMap map[int]int, err error) {
	if customerTypeIds == "" {
		return
	}
	customerTypeIdslist := strings.Split(customerTypeIds, ",")
	var permissionValueStr string
	for _, v := range customerTypeIdslist {
		permissionValueStr += "'" + v + "',"
	}
	permissionValueStr = strings.TrimRight(permissionValueStr, ",")
	condition := ` AND  permission_value IN  (` + permissionValueStr + `)`
	listValue, err := cygx.GetActivityCcustomerTypeList(condition)
	if err != nil {
		return
	}
	customerTypeIdMap = make(map[int]int, 0)
	for _, v := range listValue {
		customerTypeIdMap[v.CustomerTypeId] = v.CustomerTypeId
	}
	return
}
