package controllers

import (
	"hongze/hz_crm_api/models"
)

// @Title 调研报名统计列表
// @Description 获取分类
// @Success 200 {object} models.BannerListResp
// @router /research_statistics/list [get]
func (this *BannerController) StatisticsList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	list, err := models.GetYbResearchSignupStatisticsItems()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	resp := new(models.YbResearchSignupStatisticsResp)

	for _, v := range list {
		if v.Enable == 1 {
			resp.OngoingList = append(resp.OngoingList, v)
		} else {
			resp.OverList = append(resp.OverList, v)
		}
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 调研报名统计列表
// @Description 获取分类
// @Param   BannerId   query   int  true       "图片id"
// @Success 200 {object} models.BannerListResp
// @router /research_statistics/item [get]
func (this *BannerController) StatisticsItem() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	bannerId, _ := this.GetInt("BannerId")

	if bannerId <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误"
		return
	}
	var resp models.YbResearchSignupStatisticsItemsResp


	list, err := models.GetYbResearchSignupStatisticsItemsById(bannerId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "GetYbResearchSignupStatisticsItemsById，Err:" + err.Error()
		return
	}
	if len(list) == 0 {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		return
	}
	resp.List = list
	for _, v := range list {
		resp.Total += v.Count
	}

	amountList, err := models.GetYbResearchSignupStatisticsAmount(bannerId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "GetYbResearchSignupStatisticsAmount，Err:" + err.Error()
		return
	}
	for _, v := range amountList {
		if v.Amount > 0 {
			resp.HasPayed.Amount += v.Amount
			resp.HasPayed.Count += 1
		} else {
			resp.NoPay.Count += 1
		}
	}
	resp.HasPayed.Name = "已付款"
	resp.NoPay.Name = "未付款"
	resp.NoPay.Percentage = resp.NoPay.Count * 100 / resp.Total
	resp.HasPayed.Percentage = resp.HasPayed.Count * 100 / resp.Total


	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 调研报名统计列表
// @Description 获取分类
// @Param   Mobile   query   int  true       "分享人手机号"
// @Success 200 {object} models.BannerListResp
// @router /research_statistics/detail [get]
func (this *BannerController) StatisticsDetail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	mobile := this.GetString("Mobile")
	bannerId, _ := this.GetInt("BannerId")

	if mobile == "" {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误"
		return
	}
	list, err := models.GetYbResearchSignupStatisticsItemsByMobile(mobile, bannerId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = list
}

// @Title 调研报名统计列表
// @Description 获取分类
// @Param   Amount   query   float64  true       "付款金额"
// @Param   YbResearchSignupStatisticsId   query   int  true       "报名id"
// @Success 200 {object} models.BannerListResp
// @router /research_statistics/amount [get]
func (this *BannerController) Amount() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()

	ybResearchSignupStatisticsId, _ := this.GetInt("YbResearchSignupStatisticsId")
	amount, _ := this.GetFloat("Amount")

	if ybResearchSignupStatisticsId <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误"
		return
	}

	err := models.UpdateYbResearchSignupStatisticsAmountById(amount, ybResearchSignupStatisticsId)
	if err != nil {
		br.Msg = "更新付款金额失败"
		br.ErrMsg = "UpdateYbResearchSignupStatisticsAmountById，Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "修改成功"
}
