package services

import (
	"errors"
	"fmt"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/utils"
	"strconv"
	"strings"
)

// 大套餐的
func InitActivitySpecialTripBillBig() (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
		}
	}()
	// 70w 16次
	list70, err := models.GetActivitySecialTirpBillForInit(1)
	if err != nil {
		return
	}
	items70 := make([]*models.CygxActivitySpecialTripBill, 0)
	for _, v := range list70 {
		itemBill := new(models.CygxActivitySpecialTripBill)
		itemBill.CreateTime = v.ApprovalTime
		itemBill.CompanyId = v.CompanyId
		itemBill.CompanyName = v.CompanyName
		itemBill.Source = 2
		itemBill.DoType = 2
		itemBill.Way = 3
		itemBill.Content = "70w大套餐转正"
		itemBill.BillDetailed = 16
		itemBill.Total = strconv.Itoa(itemBill.BillDetailed) + "次"
		items70 = append(items70, itemBill)
		fmt.Println("CompanyId: ", v.CompanyId)
		fmt.Println("CompanyName: ", v.CompanyName)
	}
	err = models.AddCygxActivitySpecialTripBillMulti(items70)
	if err != nil {
		return
	}

	// 45w 10次
	list45, err := models.GetActivitySecialTirpBillForInit(2)
	if err != nil {
		return
	}
	items45 := make([]*models.CygxActivitySpecialTripBill, 0)
	for _, v := range list45 {
		itemBill := new(models.CygxActivitySpecialTripBill)
		itemBill.CreateTime = v.ApprovalTime
		itemBill.CompanyId = v.CompanyId
		itemBill.CompanyName = v.CompanyName
		itemBill.Source = 2
		itemBill.DoType = 2
		itemBill.Way = 3
		itemBill.Content = "45w大套餐转正"
		itemBill.BillDetailed = 10
		itemBill.Total = strconv.Itoa(itemBill.BillDetailed) + "次"
		items45 = append(items45, itemBill)
		fmt.Println("CompanyId: ", v.CompanyId)
		fmt.Println("CompanyName: ", v.CompanyName)
	}
	err = models.AddCygxActivitySpecialTripBillMulti(items45)
	if err != nil {
		return
	}
	return
}

// 非大套餐的，根据行业权限添加
func InitActivitySpecialTripBillNotBig() (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
		}
	}()
	list, err := models.GetActivitySecialTirpBillForInitNotBig()
	if err != nil {
		return
	}
	items := make([]*models.CygxActivitySpecialTripBill, 0)
	for _, item := range list {
		chartPermissionIdSlice := strings.Split(item.ChartPermissionIds, ",")

		mapChartName := make(map[string]int)
		mapPermissionNameTrip := make(map[string]int)

		chartList, e := models.GetChartPermissionByIds(chartPermissionIdSlice)
		if e != nil {
			err = errors.New("获取品种信息失败, Err:" + e.Error())
			return
		}

		for _, v := range chartList {
			mapChartName[v.PermissionName] = 5 + mapPermissionNameTrip[v.PermissionName]
		}
		itemBill := new(models.CygxActivitySpecialTripBill)
		itemBill.CreateTime = item.ApprovalTime
		itemBill.CompanyId = item.CompanyId
		itemBill.CompanyName = item.CompanyName
		itemBill.Source = 2
		itemBill.DoType = 2
		itemBill.Way = 3
		for k, v := range mapChartName {
			if v > 0 {
				itemBill.BillDetailed += v
				itemBill.Total += k + strconv.Itoa(v) + "次+"
			}
		}
		itemBill.Content = "行业升级套餐转正"
		itemBill.Total = strings.TrimRight(itemBill.Total, "+")
		items = append(items, itemBill)
		fmt.Println("CompanyId: ", item.CompanyId)
		fmt.Println("CompanyName: ", item.CompanyName)
	}
	err = models.AddCygxActivitySpecialTripBillMulti(items)
	if err != nil {
		return
	}
	return
}

// 到期清零和加入继承表
func ActivitySpecialCompanyTryOutReduce() (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
		}
	}()

	list, err := models.GetActivitySecialTirpBillForReduce()
	if err != nil {
		return
	}
	for _, v := range list {
		userType, tripRemaining, mapChartName, e := GetChartPermissionSpecialSurplusByCompanyForInit(v.CompanyId)
		if e != nil {
			err = e
			fmt.Println("err :", err)
			continue
		}

		itemBill := new(models.CygxActivitySpecialTripBill)
		itemBill.CreateTime = v.CreateTime
		itemBill.Source = 2
		itemBill.DoType = 1
		itemBill.Way = 3
		itemBill.BillDetailed = -tripRemaining
		itemBill.CompanyId = v.CompanyId
		itemBill.CompanyName = v.CompanyName
		itemBill.Content = "到期清零"
		itemBill.Total = "0"
		if tripRemaining == 0 {
			continue
		}
		err = models.AddCygxActivitySpecialTripBill(itemBill)
		if err != nil {
			return
		}
		fmt.Println("CompanyId: ", v.CompanyId)
		fmt.Println("CompanyName: ", v.CompanyName)
		// 删除公司之前的继承表
		err = models.DelCygxActivitySpecialInheritPointsByCompanyId(v.CompanyId)
		if err != nil {
			return
		}
		chartMap := map[string]int{utils.YI_YAO_NAME: utils.YI_YAO_ID, utils.XIAO_FEI_NAME: utils.XIAO_FEI_ID, utils.KE_JI_NAME: utils.KE_JI_ID, utils.ZHI_ZAO_NAME: utils.ZHI_ZAO_ID}
		//添加继承点数表
		inheritItems := make([]*models.CygxActivitySpecialInheritPointsCompany, 0)
		if userType == 2 {
			inheritItem := new(models.CygxActivitySpecialInheritPointsCompany)
			inheritItem.CompanyId = v.CompanyId
			inheritItem.CompanyName = v.CompanyName
			inheritItem.CreateTime = v.CreateTime
			inheritItem.ModifyTime = v.CreateTime
			inheritItem.Points = tripRemaining
			if tripRemaining > 0 {
				inheritItems = append(inheritItems, inheritItem)
			}
		} else {
			for k, num := range mapChartName {
				inheritItem := new(models.CygxActivitySpecialInheritPointsCompany)
				inheritItem.CompanyId = v.CompanyId
				inheritItem.CompanyName = v.CompanyName
				inheritItem.CreateTime = v.CreateTime
				inheritItem.ModifyTime = v.CreateTime
				inheritItem.Points = num
				inheritItem.ChartPermissionName = k
				if _, ok := chartMap[k]; ok {
					inheritItem.ChartPermissionId = chartMap[k]
				}
				if num > 0 {
					inheritItems = append(inheritItems, inheritItem)
				}
			}
		}
		if len(inheritItems) > 0 {
			err = models.AddCygxActivitySpecialInheritPointsCompanyMulti(inheritItems)
			if err != nil {
				return
			}
		}
	}

	return
}

// 补充total
func ActivitySpecialBillTotalInit() (err error) {
	defer func() {
		if err != nil {
			fmt.Println(err)
		}
	}()

	billList, err := models.GetCygxActivitySpecialTripBillForInit()
	if err != nil {
		return
	}
	chartMap := map[int]string{utils.YI_YAO_ID: utils.YI_YAO_NAME, utils.XIAO_FEI_ID: utils.XIAO_FEI_NAME, utils.KE_JI_ID: utils.KE_JI_NAME, utils.ZHI_ZAO_ID: utils.ZHI_ZAO_NAME}

	for _, billItem := range billList {
		userType, tripRemaining, mapChartName, e := GetChartPermissionSpecialSurplusByCompanyForInitTotal(billItem.CompanyId)
		if e != nil {
			err = e
			continue
		}
		fmt.Println("CompanyId: ", billItem.CompanyId)
		fmt.Println("CompanyName: ", billItem.CompanyName)
		list, e := models.GetCygxActivitySpecialTripBillByCompanyIdForInit(billItem.CompanyId)
		if e != nil {
			err = e
			return
		}

		if userType == 2 {
			for i, v := range list {
				if v.ActivityId == 0 {
					continue
				}
				if i == 0 {
					v.Total = strconv.Itoa(tripRemaining) + "次"
				} else {
					v.Total = strconv.Itoa(tripRemaining+v.BillDetailed) + "次"
				}

				fmt.Println("v.Total:",v.Total)
				err = models.UpdateCygxActivitySpecialTripBillByCompanyIdForInit(v.Total, v.Id)
				if err != nil {
					return
				}
			}
		} else {
			for i, v := range list {
				if chart, ok := chartMap[v.ChartPermissionId]; ok {
					v.Total = ``
					for k, num := range mapChartName {
						if k == chart {
							if i > 0 {
								num += v.BillDetailed
							}
						}
						v.Total += k + strconv.Itoa(num) + "次+"
					}
					v.Total = strings.TrimRight(v.Total, "+")
					fmt.Println("v.Total:",v.Total)
					err = models.UpdateCygxActivitySpecialTripBillByCompanyIdForInit(v.Total, v.Id)
					if err != nil {
						return
					}
				}
			}
		}

	}

	return
}
