package cygx

import (
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/utils"
	"strconv"
)

// 分享记录
type UserAdminShareHistoryController struct {
	controllers.BaseAuthController
}

// @Title 分享记录列表
// @Description 分享记录列表接口
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Param   StartDate   query   string  false       "开始时间 ，列如2021-03-06 "
// @Param   EndDate   query   string  false       "结束时间，列如2021-03-06 "
// @Param   AdminId   query   string  false       "销售ID "
// @Param   KeyWord   query   string  false       "搜索关键词"
// @Param   Action   query   string	  false       "用户行为"
// @Success Ret=200 {object} cygx.GetCygxUserAdminShareHistoryListResp
// @router /mfyx/admin/share/list [get]
func (this *UserAdminShareHistoryController) OrderList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	resp := new(cygx.GetCygxUserAdminShareHistoryListResp)
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	startDate := this.GetString("StartDate")
	endDate := this.GetString("EndDate")
	adminId := this.GetString("AdminId")
	keyWord := this.GetString("KeyWord")
	action := this.GetString("Action")

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = utils.StartIndex(currentIndex, pageSize)
	var condition string
	var pars []interface{}
	condition += ` AND  company_id !=  16 ` // 过滤弘则
	if startDate != "" && endDate != "" {   //时间范围
		startDate += " 00:00:00"
		endDate += " 23:59:59"
		condition += ` 	AND create_time  BETWEEN ?   AND  ? `
		pars = append(pars, startDate, endDate)
	}

	if adminId != "" { //所属销售筛选
		condition += ` 	AND share_id IN (` + adminId + `) `
	}

	//用户行为搜索
	//if action == "查看专栏" || action == "查看报告" || action == "查看活动" || action == "注册" {
	if action != "" {
		condition += " 	AND action = ? "
		pars = append(pars, action)
	}

	//如果不是权益管理员和admin，就做可见权限限制
	if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_RAI_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
		condition += ` 	AND share_id IN (` + strconv.Itoa(sysUser.AdminId) + `) `
	}

	if keyWord != "" { //用户姓名，手机号
		keyWord = "%" + keyWord + "%"
		condition += ` 	AND (mobile LIKE ?  OR  real_name LIKE ? ) `
		pars = append(pars, keyWord, keyWord)
	}

	total, err := cygx.GetCygxUserAdminShareHistoryCount(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	condition += "	ORDER BY user_admin_share_history_id  DESC "
	list, err := cygx.GetCygxUserAdminShareHistoryList(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	var mobiles []string
	for _, v := range list {
		mobiles = append(mobiles, v.Mobile)
	}
	listUser, err := models.GetWxUserRaiSllerListByUserMobile(mobiles)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	//获取用户当前最新的公司信息
	mapWxUserSller := make(map[string]*models.WxUserSller)
	for _, v := range listUser {
		mapWxUserSller[v.Mobile] = v
	}

	for _, v := range list {
		switch v.Source {
		case utils.CYGX_OBJ_ARTICLE: //文章详情
			v.HttpUrl = utils.CYGX_MFYX_URL + "/material/info/" + strconv.Itoa(v.SourceId)
		case utils.CYGX_OBJ_ACTIVITY, utils.CYGX_OBJ_ACTIVITYVIDEO, utils.CYGX_OBJ_ACTIVITYVOICE: //活动详情
			v.HttpUrl = utils.CYGX_MFYX_URL + "/activity/detail/" + strconv.Itoa(v.SourceId)
		case utils.CYGX_OBJ_YANXUANSPECIAL: //研选专栏
			v.HttpUrl = utils.CYGX_MFYX_URL + "/column/detail/" + strconv.Itoa(v.SourceId)
		}

		switch v.RegisterPlatform {
		case 5:
			v.RegisterPlatformText = "研选小程序"
		case 6:
			v.RegisterPlatformText = "研选网页版"
		}
		mapItem := mapWxUserSller[v.Mobile]
		if mapItem != nil {
			v.CompanyName = mapItem.CompanyName
			v.CompanyId = mapItem.CompanyId
			v.RealName = mapItem.RealName
		} else {
			v.RealName = "--"
			v.CompanyName = "--"
			v.CompanyId = 1
		}
	}
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp.Paging = page
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
