package controllers

import (
	"hongze/hongze_open_api/logic"
	"hongze/hongze_open_api/models/response/company_user"
	"hongze/hongze_open_api/models/tables"
	"hongze/hongze_open_api/models/tables/wx_user"
	"hongze/hongze_open_api/utils"
	"strconv"
	"time"
)

// CompanyUser
// 客户联系人模块
type CompanyUser struct {
	BaseAuth
}

// GetUserReportList
// @Title 获取用户阅读数据报表
// @Description 获取用户阅读数据报表
// @Param   start_date   query   string  false       "开始日期，格式：2021-11-03"
// @Param   end_date   query   string  false       "结束日期，格式：2021-11-04"
// @Param   mobile   query   string  false       "用户手机号"
// @Param   email   query   string  false       "用户邮箱"
// @Success 200 {object} company_user.UserReportListResp
// @router /user_report_list [get]
func (c *CompanyUser) GetUserReportList() {
	//appid权限校验
	appid := c.GetString("appid", "")
	if utils.RunMode == "release" && appid != "CQWx3EqDLNk7bVHo" {
		c.FailWithMessage("无权限")
		return
	}
	mobile := c.GetString("mobile", "")
	email := c.GetString("email", "")
	startDate := c.GetString("start_date", "")
	endDate := c.GetString("end_date", "")

	//}
	//	return
	//	c.FailWithMessage("end_date必传")
	//if endDate == ""{
	//}
	//	return
	//	c.FailWithMessage("start_date必传")
	//if startDate == ""{

	startTime := ``
	endTime := ``
	if startDate != "" && endDate != "" {
		startDateTime, err := time.Parse(utils.FormatDate, startDate)
		if err != nil {
			c.FailWithMessage("start_date格式异常")
			return
		}
		endDateTime, err := time.Parse(utils.FormatDate, endDate)
		if err != nil {
			c.FailWithMessage("end_date格式异常")
			return
		}
		startTime = startDateTime.Format(utils.FormatDate) + " 00:00:00"
		endTime = endDateTime.Format(utils.FormatDate) + " 23:59:59"
	}

	pageSize := c.PageSize
	startSize := c.StartSize
	currentIndex := c.StartPage

	total, list, err := tables.GetViewReportList(mobile, email, startTime, endTime, startSize, pageSize)
	if err != nil {
		c.FailWithMessage("获取失败")
		return
	}

	for k, v := range list {
		if v.ReportType == "day" {
			list[k].MatchTypeName = "晨报"
		} else if v.ReportType == "week" {
			list[k].MatchTypeName = "周报"
		} else if v.ReportType == "two_week" {
			list[k].MatchTypeName = "双周报"
		} else if v.ReportType == "month" {
			list[k].MatchTypeName = "月报"
		} else if v.ReportType == "rddp" {
			//list[k].MatchTypeName = "日评"
			createdTime := utils.StrTimeToTime(v.CreatedTime)
			monthStr := createdTime.Format("01")
			dayStr := strconv.Itoa(createdTime.Day())
			if len(dayStr) == 1 {
				dayStr = "0" + dayStr
			}
			list[k].ResearchReportName += "(" + monthStr + dayStr + ")"
		}
		if v.StopTime != "--" {
			list[k].StopTime += "s"
		}
		//v.CompanyName = wxUserInfo.CompanyName
		//v.UserName = wxUserInfo.RealName
		//v.Mobile = wxUserInfo.Mobile
	}
	page := utils.GetPaging(currentIndex, pageSize, total)

	resp := company_user.UserReportListResp{
		List:   list,
		Paging: page,
	}
	c.OkDetailed(resp, "获取成功")
}

// GetUserReportListV2
// @Title 获取用户阅读数据报表
// @Description 合同详情接口
// @Param   mobile   query   string  false       "用户手机号"
// @Param   email   query   string  false       "用户邮箱"
// @Success 200 {object} company_user.UserReportListResp

func (c *CompanyUser) GetUserReportListV2() {
	mobile := c.GetString("mobile", "")
	startDate := c.GetString("start_date", "")
	endDate := c.GetString("end_date", "")
	if startDate == "" {
		c.FailWithMessage("start_date必传")
		return
	}
	if endDate == "" {
		c.FailWithMessage("end_date必传")
		return
	}
	startDateTime, err := time.Parse(utils.FormatDate, startDate)
	if err != nil {
		c.FailWithMessage("start_date格式异常")
		return
	}
	endDateTime, err := time.Parse(utils.FormatDate, endDate)
	if err != nil {
		c.FailWithMessage("end_date格式异常")
		return
	}
	var wxUserInfo *wx_user.WxUserItem
	if mobile != "" {
		item, err := wx_user.GetWxUserByMobile(mobile)
		if err != nil {
			c.FailWithMessage("找不到该用户")
			return
		}
		wxUserInfo = item
	}

	email := c.GetString("email", "")
	if wxUserInfo == nil && email != "" {
		item, err := wx_user.GetWxUserByEmail(email)
		if err != nil {
			c.FailWithMessage("找不到该用户")
			return
		}
		wxUserInfo = item
	}
	if wxUserInfo == nil {
		c.FailWithMessage("找不该用户")
	}

	pageSize := c.PageSize
	startSize := c.StartSize
	currentIndex := c.StartPage

	total, list, err := tables.GetViewReportList(wxUserInfo.Mobile, wxUserInfo.Email, startDateTime.Format(utils.FormatDate)+" 00:00:00", endDateTime.Format(utils.FormatDate)+" 23:59:59", startSize, pageSize)
	if err != nil {
		c.FailWithMessage("获取失败")
		return
	}

	for k, v := range list {
		if v.ReportType == "day" {
			list[k].MatchTypeName = "晨报"
		} else if v.ReportType == "week" {
			list[k].MatchTypeName = "周报"
		} else if v.ReportType == "two_week" {
			list[k].MatchTypeName = "双周报"
		} else if v.ReportType == "month" {
			list[k].MatchTypeName = "月报"
		} else if v.ReportType == "rddp" {
			//list[k].MatchTypeName = "日评"
			createdTime := utils.StrTimeToTime(v.CreatedTime)
			monthStr := createdTime.Format("01")
			dayStr := strconv.Itoa(createdTime.Day())
			if len(dayStr) == 1 {
				dayStr = "0" + dayStr
			}
			list[k].ResearchReportName += "(" + monthStr + dayStr + ")"
		}
		if v.StopTime != "--" {
			list[k].StopTime += "s"
		}
		v.CompanyName = wxUserInfo.CompanyName
		v.Mobile = wxUserInfo.Mobile
	}
	page := utils.GetPaging(currentIndex, pageSize, total)

	resp := company_user.UserReportListResp{
		List:   list,
		Paging: page,
	}
	c.OkDetailed(resp, "获取成功")
}

// GetUserReportViewTotalList
// @Title 获取用户阅读数据报表
// @Description 获取用户阅读数据报表
// @Success 200 {object} []logic.UserView
// @router /view_total_list [get]
func (c *CompanyUser) GetUserReportViewTotalList() {
	//appid权限校验
	appid := c.GetString("appid", "")
	if utils.RunMode == "release" && appid != "CQWx3EqDLNk7bVHo" {
		c.FailWithMessage("无权限")
		return
	}
	list, err := logic.GetUserViewTotal()
	if err != nil {
		c.FailWithMessage("获取失败")
		return
	}
	resp := list
	c.OkDetailed(resp, "获取成功")
}
