package service

import (
	"boss/internal/datas"
	"boss/internal/models/accounts"
	"boss/internal/models/agent"
	"boss/internal/models/merchant"
	"boss/internal/models/road"
	"boss/internal/models/system"
	"boss/internal/models/user"
	"context"
	"sort"
	"strings"
	"time"

	"github.com/beego/beego/v2/core/logs"
)

type DeleteService struct {
}

func (c *DeleteService) Finish(ctx context.Context) {
	remainderFirstMenuUid := make([]string, 0)
	remainderFirstMenu := make([]string, 0)
	remainderSecondMenuUid := make([]string, 0)
	remainderSecondMenu := make([]string, 0)
	remainderPowerId := make([]string, 0)
	remainderPower := make([]string, 0)
	allRoleInfo := system.GetRole(ctx)
	//如果有删除任何的东西，需要重新赋值权限
	for _, r := range allRoleInfo {
		for _, showFirstUid := range strings.Split(r.ShowFirstUid, "||") {
			if system.FirstMenuUidIsExists(ctx, showFirstUid) {
				remainderFirstMenuUid = append(remainderFirstMenuUid, showFirstUid)
				menuInfo := system.GetMenuInfoByMenuUid(ctx, showFirstUid)
				remainderFirstMenu = append(remainderFirstMenu, menuInfo.FirstMenu)
			}
		}
		for _, showSecondUid := range strings.Split(r.ShowSecondUid, "||") {
			if system.SecondMenuUidIsExists(ctx, showSecondUid) {
				remainderSecondMenuUid = append(remainderSecondMenuUid, showSecondUid)
				secondMenuInfo := system.GetSecondMenuInfoBySecondMenuUid(ctx, showSecondUid)
				remainderSecondMenu = append(remainderSecondMenu, secondMenuInfo.SecondMenu)
			}
		}
		for _, showPowerId := range strings.Split(r.ShowPowerUid, "||") {
			if system.PowerUidExists(ctx, showPowerId) {
				remainderPowerId = append(remainderPowerId, showPowerId)
				powerInfo := system.GetPowerById(ctx, showPowerId)
				remainderPower = append(remainderPower, powerInfo.PowerItem)
			}
		}
		r.ShowFirstUid = strings.Join(remainderFirstMenuUid, "||")
		r.ShowFirstMenu = strings.Join(remainderFirstMenu, "||")
		r.ShowSecondUid = strings.Join(remainderSecondMenuUid, "||")
		r.ShowSecondMenu = strings.Join(remainderSecondMenu, "||")
		r.ShowPowerUid = strings.Join(remainderPowerId, "||")
		r.ShowPower = strings.Join(remainderPower, "||")
		r.UpdateTime = time.Now()
		system.UpdateRoleInfo(ctx, r)
	}
}

func (c *DeleteService) DeleteMenu(ctx context.Context, menuUid, userID string) *datas.BaseDataJSON {

	dataJSON := new(datas.BaseDataJSON)
	menuInfo := system.GetMenuInfoByMenuUid(ctx, menuUid)
	if menuInfo.MenuUid == "" {
		dataJSON.Msg = "不存在该菜单"
		dataJSON.Code = -1
	} else {
		logs.Info(userID + "，执行了删除一级菜单操作")
		system.DeleteMenuInfo(ctx, menuUid)
		//删除该一级目下下的所有二级目录
		system.DeleteSecondMenuByFirstMenuUid(ctx, menuUid)
		SortFirstMenuOrder(ctx)
		dataJSON.Code = 200
	}

	return dataJSON
}
func (c *DeleteService) DeleteSecondMenu(ctx context.Context, secondMenuUid string) *datas.BaseDataJSON {

	secondMenuInfo := system.GetSecondMenuInfoBySecondMenuUid(ctx, secondMenuUid)
	dataJSON := new(datas.BaseDataJSON)
	if secondMenuUid == "" || secondMenuInfo.SecondMenuUid == "" {
		dataJSON.Code = -1
		dataJSON.Msg = "该二级菜单不存在"
	} else {
		if system.DeleteSecondMenuBySecondMenuUid(ctx, secondMenuUid) {
			dataJSON.Code = 200
			ml := system.GetSecondMenuLenByFirstMenuUid(ctx, secondMenuInfo.FirstMenuUid)
			//删除该二级页面下的所有权限项
			system.DeletePowerBySecondUid(ctx, secondMenuUid)
			if ml == 0 {
				//如果该二级类目已经被全部删除，那么对应的一级类目也应当删除
				system.DeleteMenuInfo(ctx, secondMenuInfo.FirstMenuUid)
				SortFirstMenuOrder(ctx)
			} else {
				secondMenuInfoList := system.GetSecondMenuListByFirstMenuUid(ctx, secondMenuInfo.FirstMenuUid)
				sort.Sort(system.SecondMenuSlice(secondMenuInfoList))
				for i := 0; i < len(secondMenuInfoList); i++ {
					m := secondMenuInfoList[i]
					system.UpdateSecondMenuOrderBySecondUid(ctx, m.SecondMenuUid, i+1)
				}
			}
		} else {
			dataJSON.Code = -1
			dataJSON.Msg = "删除失败"
		}
	}
	return dataJSON
}

func (c *DeleteService) DeletePowerItem(ctx context.Context, powerID string) *datas.BaseDataJSON {
	system.DeletePowerItemByPowerID(ctx, powerID)
	dataJSON := new(datas.BaseDataJSON)
	dataJSON.Code = 200
	return dataJSON
}

func (c *DeleteService) DeleteRole(ctx context.Context, roleUid string) *datas.BaseDataJSON {
	dataJSON := new(datas.BaseDataJSON)

	if system.DeleteRoleByRoleUid(ctx, roleUid) {
		dataJSON.Code = 200
	} else {
		dataJSON.Code = -1
	}
	return dataJSON
}

func (c *DeleteService) DeleteOperator(ctx context.Context, userId string) *datas.BaseDataJSON {

	dataJSON := new(datas.BaseDataJSON)

	if user.DeleteUserByUserId(ctx, userId) {
		dataJSON.Code = 200
	} else {
		dataJSON.Code = -1
	}
	return dataJSON
}

func (c *DeleteService) DeleteBankCardRecord(ctx context.Context, uid string) *datas.BankCardDataJSON {

	dataJSON := new(datas.BankCardDataJSON)
	dataJSON.Code = -1

	if system.DeleteBankCardByUid(ctx, uid) {
		dataJSON.Code = 200
	}
	return dataJSON
}

func (c *DeleteService) DeleteRoad(ctx context.Context, roadUid string) *datas.BaseDataJSON {

	dataJSON := new(datas.BaseDataJSON)
	dataJSON.Code = -1

	if road.DeleteRoadByRoadUid(ctx, roadUid) {
		dataJSON.Code = 200
	}
	params := make(map[string]string)
	roadPoolInfoList := road.GetAllRollPool(ctx, params)
	//将轮询池中的对应的通道删除
	for _, roadPoolInfo := range roadPoolInfoList {
		var uids []string
		roadInfoList := strings.Split(roadPoolInfo.RoadUidPool, "||")
		for _, uid := range roadInfoList {
			if uid != roadUid {
				uids = append(uids, uid)
			}
		}
		roadPoolInfo.RoadUidPool = strings.Join(uids, "||")
		roadPoolInfo.UpdateTime = time.Now()
		road.UpdateRoadPool(ctx, roadPoolInfo)
	}
	return dataJSON
}

func (c *DeleteService) DeleteRoadPool(ctx context.Context, roadPoolCode string) *datas.BaseDataJSON {
	dataJSON := new(datas.BaseDataJSON)
	dataJSON.Code = -1

	if road.DeleteRoadPoolByCode(ctx, roadPoolCode) {
		dataJSON.Code = 200
	} else {
		dataJSON.Msg = "删除通道池失败"
	}
	return dataJSON
}

func (c *DeleteService) DeleteMerchant(ctx context.Context, merchantUid string) *datas.KeyDataJSON {
	keyDataJSON := new(datas.KeyDataJSON)
	if merchantUid == "" {
		keyDataJSON.Code = -1
		return keyDataJSON
	}

	if merchant.DeleteMerchantByUid(ctx, merchantUid) {
		keyDataJSON.Code = 200
	} else {
		keyDataJSON.Code = -1
	}
	return keyDataJSON
}

func (c *DeleteService) DeleteAccount(ctx context.Context, accountUid string) *datas.BaseDataJSON {
	dataJSON := new(datas.BaseDataJSON)
	if merchant.IsExistByMerchantUid(ctx, accountUid) || agent.IsExistByAgentUid(ctx, accountUid) {
		dataJSON.Code = -1
		dataJSON.Msg = "用户还存在，不能删除"
	} else {
		if accounts.DeleteAccountByUid(ctx, accountUid) {
			dataJSON.Code = 200
			dataJSON.Msg = "删除账户成功"
		} else {
			dataJSON.Code = -1
			dataJSON.Msg = "删除账户失败"
		}
	}
	return dataJSON
}

func (c *DeleteService) DeleteAgent(ctx context.Context, agentUid string) *datas.KeyDataJSON {
	keyDataJSON := new(datas.KeyDataJSON)
	//判断是否有商户还绑定了该代理
	if merchant.IsExistMerchantByAgentUid(ctx, agentUid) {
		keyDataJSON.Code = -1
		keyDataJSON.Msg = "已有商户绑定改代理，不能删除"
	} else {
		if agent.DeleteAgentByAgentUid(ctx, agentUid) {
			keyDataJSON.Code = 200
		} else {
			keyDataJSON.Code = -1
			keyDataJSON.Msg = "删除失败"
		}
	}
	return keyDataJSON
}
func (c *DeleteService) DeleteAgentRelation(ctx context.Context, merchantUid string) *datas.KeyDataJSON {

	merchantInfo := merchant.GetMerchantByUid(ctx, merchantUid)

	keyDataJSON := new(datas.KeyDataJSON)
	keyDataJSON.Code = 200

	if merchantInfo.MerchantUid == "" {
		keyDataJSON.Code = -1
		keyDataJSON.Msg = "不存在这样的商户"
	} else {
		merchantInfo.UpdateTime = time.Now()
		merchantInfo.BelongAgentUid = ""
		merchantInfo.BelongAgentName = ""

		if !merchant.UpdateMerchant(ctx, merchantInfo) {
			keyDataJSON.Code = -1
			keyDataJSON.Msg = "更新商户失败"
		}
	}
	return keyDataJSON
}

/*
* 对一级菜单重新进行排序
 */
func SortFirstMenuOrder(ctx context.Context) {
	menuInfoList := system.GetMenuAll(ctx)
	sort.Sort(system.MenuInfoSlice(menuInfoList))

	for i := 0; i < len(menuInfoList); i++ {
		m := menuInfoList[i]
		m.UpdateTime = time.Now()
		m.MenuOrder = i + 1
		system.UpdateMenuInfo(ctx, m)
		//对应的二级菜单也应该重新分配顺序号
		SortSecondMenuOrder(ctx, m)
	}
}

/*
* 对二级菜单分配顺序号
 */
func SortSecondMenuOrder(ctx context.Context, firstMenuInfo system.MenuInfo) {
	secondMenuInfoList := system.GetSecondMenuListByFirstMenuUid(ctx, firstMenuInfo.MenuUid)
	for _, sm := range secondMenuInfoList {
		sm.FirstMenuOrder = firstMenuInfo.MenuOrder
		sm.UpdateTime = time.Now()
		system.UpdateSecondMenu(ctx, sm)
		//删除下下一级的所有权限项
		system.DeletePowerBySecondUid(ctx, sm.SecondMenuUid)
	}
}
