package service

import (
	"errors"
	"time"

	. "app.yhyue.com/moapp/jybase/date"
	. "bp.jydev.jianyu360.cn/BaseService/resourceCenter/public/entity"
)

/*
 * 获取资源剩余情况
 * @param appid
 * @param function_code 功能代码
 * @param account_id 账户id
 * @param ent_account_id 企业账户id
 * @param ent_id 企业id
 * @return 状态 0：失败 1：成功 -1：不在有效期内 -2：数量不足 -3：没有授权
 * @return 总数
 * @return 剩余数
 * @return 错误信息
 */
func Surplus(appid, function_code string, account_id, ent_account_id, ent_id, ent_user_id int64) (int64, int64, int64, string, string, error) {
	status, use_count, surplus_count, err := func() (int64, int64, int64, error) {
		if appid == "" {
			return 0, 0, 0, errors.New("无效的参数appid")
		} else if function_code == "" {
			return 0, 0, 0, errors.New("无效的参数function_code")
		} else if account_id == 0 && ent_account_id == 0 {
			return 0, 0, 0, errors.New("无效的参数account_id、ent_account_id")
		}
		function := Base_function.FindByCode(appid, function_code)
		if function == nil {
			return 0, 0, 0, errors.New("功能原始表中没有找到该功能")
		}
		myPowers := Base_power.FindMyPowersByFc(appid, function_code, account_id, ent_account_id)
		if myPowers == nil || len(*myPowers) == 0 {
			return -1, 0, 0, errors.New("不在有效期内")
		}
		if function.Power_rule == 1 { //只对周期进行校验
			for _, v := range *myPowers {
				if v.Power_type == 2 && !Base_ent_empower.HasEmpower(appid, function_code, ent_id, ent_user_id) {
					return -3, 0, 0, errors.New("没有对该用户进行授权")
				}
			}
			return 1, 0, 0, nil
		} else if function.Power_rule == 2 { //周期+数量校验
			use_count, surplus_count := int64(0), int64(0)
			for _, v := range *myPowers {
				if v.Power_type == 2 && !Base_ent_empower.HasEmpower(appid, function_code, ent_id, ent_user_id) {
					return -3, 0, 0, errors.New("没有对该用户进行授权")
				}
				use_count += v.Use_count
				surplus_count += v.Surplus_count
			}
			if surplus_count > 0 {
				return 1, use_count, surplus_count, nil
			} else {
				return -2, use_count, surplus_count, nil
			}
		} else if function.Power_rule == 3 { //周期+频率+数量校验
			for _, v := range *myPowers {
				if v.Power_type == 2 && !Base_ent_empower.HasEmpower(appid, function_code, ent_id, ent_user_id) {
					return -3, 0, 0, errors.New("没有对该用户进行授权")
				}
				var use_account_id int64
				if v.Power_type == 1 {
					use_account_id = account_id
				} else if v.Power_type == 2 {
					use_account_id = ent_account_id
				} else {
					continue
				}
				use, err := Base_resource_use.FindLastOne(appid, function_code, use_account_id)
				if err != nil {
					return 0, 0, 0, errors.New("查询资源使用记录失败")
				} else if use == nil {
					return 1, v.Strategy_count, v.Strategy_count, nil
				}
				create_time, err := time.ParseInLocation(Date_Full_Layout, use.Create_time, time.Local)
				if err != nil {
					return 0, 0, 0, errors.New("资源充值/消耗表创建时间错误，" + err.Error())
				}
				now := time.Now()
				if (*myPowers)[0].Limit_strategy == "1d" {
					if create_time.Year() == now.Year() && create_time.Month() == now.Month() && create_time.Day() == now.Day() {
						if use.Surplus_count > 0 {
							return 1, v.Strategy_count, use.Surplus_count, nil
						} else {
							return -2, v.Strategy_count, use.Surplus_count, nil
						}
					} else {
						return 1, v.Strategy_count, v.Strategy_count, nil
					}
				} else if (*myPowers)[0].Limit_strategy == "1m" {
					if create_time.Year() == now.Year() && create_time.Month() == now.Month() {
						if use.Surplus_count > 0 {
							return 1, v.Strategy_count, use.Surplus_count, nil
						} else {
							return -2, v.Strategy_count, use.Surplus_count, nil
						}
					} else {
						return 1, v.Strategy_count, v.Strategy_count, nil
					}
				} else {
					return 0, 0, 0, errors.New("limit_strategy格式错误")
				}
			}
		}
		return 0, 0, 0, nil
	}()
	start_time, end_time := "", ""
	if status != 0 {
		lastPower := Base_power.FindMyLastPowerByFc(appid, function_code, account_id, ent_account_id)
		if lastPower != nil {
			start_time = lastPower.Start_time
			end_time = lastPower.End_time
		}
	}
	return status, use_count, surplus_count, start_time, end_time, err
}
