package controllers

/***************************************************
 ** @Desc : This file for 首页
 ** @Time : 19.11.30 11:49
 ** @Author : Joker
 ** @File : index
 ** @Last Modified by : Joker
 ** @Last Modified time: 19.11.30 11:49
 ** @Software: GoLand
****************************************************/
import (
	"fmt"
	"merchant/models/account"
	"merchant/models/merchant"
	"merchant/models/order"
	"merchant/models/road"
	"merchant/sys/enum"
	"strconv"
)

type Index struct {
	KeepSession
}

// ShowUI 首页
func (c *Index) ShowUI() {
	us := c.GetSession(enum.UserSession)
	u := us.(merchant.MerchantInfo)
	c.Data["userName"] = u.MerchantName

	c.TplName = "index.html"
}

// LoadUserAccountInfo 加载用户账户金额信息
func (c *Index) LoadUserAccountInfo() {
	us := c.GetSession(enum.UserSession)
	u := us.(merchant.MerchantInfo)

	ac := account.GetAccountByUid(u.MerchantUid)

	info := make(map[string]interface{})
	// 账户余额
	info["balanceAmt"] = pubMethod.FormatFloat64ToString(ac.Balance)

	// 可用余额
	info["settAmount"] = pubMethod.FormatFloat64ToString(ac.WaitAmount)

	// 冻结金额
	info["freezeAmt"] = pubMethod.FormatFloat64ToString(ac.FreezeAmount)

	// 押款金额
	info["amountFrozen"] = pubMethod.FormatFloat64ToString(ac.LoanAmount)

	c.Data["json"] = info
	c.ServeJSON()
	c.StopRun()
}

// LoadCountOrder 加载总订单信息
func (c *Index) LoadCountOrder() {
	us := c.GetSession(enum.UserSession)
	u := us.(merchant.MerchantInfo)

	md := merchant.GetMerchantDeployByUid(u.MerchantUid)

	type orderInPayWay struct {
		PayWayName    string // 支付方式名
		OrderCount    int    // 订单数
		SucOrderCount int    // 成功订单数
		SucRate       string // 成功率
	}

	ways := make([]orderInPayWay, len(md))

	for k, v := range md {
		in := make(map[string]string)
		in["merchant_uid"] = u.MerchantUid

		ways[k].PayWayName = road.GetRoadInfoByRoadUid(v.SingleRoadUid).ProductName

		in["road_uid"] = v.SingleRoadUid
		ways[k].OrderCount = order.GetOrderLenByMap(in)

		in["status"] = enum.SUCCESS
		ways[k].SucOrderCount = order.GetOrderLenByMap(in)

		if ways[k].OrderCount == 0 {
			ways[k].SucRate = "0"
			continue
		}
		ways[k].SucRate = fmt.Sprintf("%0.4f", float64(ways[k].SucOrderCount)/float64(ways[k].OrderCount))
	}

	c.Data["json"] = ways
	c.ServeJSON()
	c.StopRun()
}

// LoadOrderCount 加载总订单数
func (c *Index) LoadOrderCount() {
	us := c.GetSession(enum.UserSession)
	u := us.(merchant.MerchantInfo)

	out := make(map[string]interface{})

	in := make(map[string]string)
	in["merchant_uid"] = u.MerchantUid
	out["orders"] = order.GetOrderLenByMap(in)

	in["status"] = enum.SUCCESS
	out["suc_orders"] = order.GetOrderLenByMap(in)

	{
		supplierAll := 0.0
		platformAll := 0.0
		agentAll := 0.0
		allAmount := 0.0
		TadaySuccessNum := 0
		datainfo := order.GetOrderProfitByMap(in, -1, 0)

		for _, v := range datainfo {
			if v.Status != "success" {
				continue
			}
			allAmount += v.FactAmount
			supplierAll += v.SupplierProfit
			platformAll += v.PlatformProfit
			agentAll += v.AgentProfit
			TadaySuccessNum += 1
		}

		out["TadaySupplierProfit"], _ = strconv.ParseFloat(fmt.Sprintf("%.3f", supplierAll), 3)
		out["TadayPlatformProfit"], _ = strconv.ParseFloat(fmt.Sprintf("%.3f", platformAll), 3)
		out["TadayAgentProfit"], _ = strconv.ParseFloat(fmt.Sprintf("%.3f", agentAll), 3)
		out["TadayAllAmount"], _ = strconv.ParseFloat(fmt.Sprintf("%.3f", allAmount), 3)

		out["TadaySuccessNum"] = TadaySuccessNum
		out["TadayAllNum"] = len(datainfo)

	}

	if out["orders"].(int) == 0 {
		out["suc_rate"] = 0
	} else {
		out["suc_rate"] = fmt.Sprintf("%0.4f", float64(out["suc_orders"].(int))/float64(out["orders"].(int)))
	}

	c.Data["json"] = out
	c.ServeJSON()
	c.StopRun()
}

// LoadUserPayWayUI 加载用户支付配置
func (c *Index) LoadUserPayWayUI() {
	us := c.GetSession(enum.UserSession)
	u := us.(merchant.MerchantInfo)

	c.Data["userName"] = u.MerchantName
	c.TplName = "pay_way.html"
}

func (c *Index) LoadUserPayWay() {
	us := c.GetSession(enum.UserSession)
	u := us.(merchant.MerchantInfo)

	md := merchant.GetMerchantDeployByUid(u.MerchantUid)

	type payConfig struct {
		No   string  // 通道编号
		Name string  // 产品名
		Rate float64 // 通道费率
	}

	ways := make([]payConfig, len(md))

	for k, v := range md {
		road_ := road.GetRoadInfoByRoadUid(v.SingleRoadUid)
		ways[k].No = road_.RoadUid
		ways[k].Name = road_.ProductName
		//ways[k].Rate = road.BasicFee + v.SingleRoadPlatformRate + v.SingleRoadAgentRate
	}

	c.Data["json"] = ways
	c.ServeJSON()
	c.StopRun()
}
