package cygx

import (
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/utils"
	"strings"
)

// 活动类型
type ActivityTypeCoAntroller struct {
	controllers.BaseAuthController
}

// @Title 活动类型列表
// @Description活动类型列表接口
// @Param   IsResearch   query   bool  true       "是否为研选"
// @Param   IsGetAll   query   bool  true       "是否获取所有"
// @Success 200 {object} cygx.ActivityTypeListResp
// @router /activityType/list [get]
func (this *ActivityTypeCoAntroller) List() {
	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
	}
	isResearch, _ := this.GetBool("IsResearch", false)
	isGetAll, _ := this.GetBool("IsGetAll", false)
	var condition string
	resp := new(cygx.ActivityTypeListResp)
	if !isGetAll {
		if isResearch {
			condition = " AND source_type IN (0,2) "
		} else {
			condition = " AND source_type IN (0,1) "
		}
	}

	list, err := cygx.GetActivityTypeSearchList(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	//如果身份是管理员，权益管理员，权益研究员就展示 加号 图标
	if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_RESEARCHR || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
		resp.IsShowAddIcon = true
	} else {
		//后台一些权益销售，需要权益研究员的部分权限,根据配置表里的手机号判断
		co, _ := cygx.GetCygxConfigDetailByCode("rai_sales_role_change")
		if co != nil {
			if strings.Contains(co.ConfigValue, sysUser.Mobile) {
				resp.IsShowAddIcon = true
			}
		}
	}
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 活动类型详情
// @Description 活动类型详情接口
// @Param   ActivityTypeId   query   int  true       "活动类型ID"
// @Success 200 {object} cygx.ActivityType
// @router /activityType/detail [get]
func (this *ActivityTypeCoAntroller) Detail() {
	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
	}
	activityTypeId, _ := this.GetInt("ActivityTypeId")
	if activityTypeId <= 0 {
		br.Msg = "活动类型不存在"
		br.ErrMsg = "活动类型不存在，活动ID错误"
		return
	}
	detail, errDetail := cygx.GetActivityTypeDetailById(activityTypeId)
	if errDetail != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取信息失败,Err:" + errDetail.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = detail
}

// @Title 活动类型列表小分类筛选
// @Description 获取活动类型列表小分类筛选接口
// @Param   SearchType   query   int  true       "筛选类型 1专家/分析师电话会、2专家/分析师线下沙龙 、3公司调研/ 4、C类电话会"
// @Success 200 {object} cygx.ActivityTypeListResp
// @router /activityType/listSearch [get]
func (this *ActivityTypeCoAntroller) ListSearch() {
	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
	}
	searchType, _ := this.GetInt("SearchType")
	resp := new(cygx.ActivityTypeListResp)
	var condition string
	if searchType == 1 {
		condition += ` AND  activity_type_id  IN (1,2)`
	} else if searchType == 2 {
		condition += ` AND activity_type_id  IN (5,6)`
	} else if searchType == 3 {
		condition += ` AND activity_type_id  IN (3,4)`
	} else {
		condition += ` AND activity_type_id  IN (7)`
	}
	list, err := cygx.GetActivityTypeSearchList(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 线上、线下会议类型
// @Description 线上、线下会议类型
// @Param   MeetType   query   int  true       "会议类型 1线上、2线下"
// @Param   IsResearch   query   bool  true       "是否为研选"
// @Success 200 {object} cygx.ActivityTypeListResp
// @router /activityType/meetType [get]
func (this *ActivityTypeCoAntroller) MeetType() {
	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
	}
	meetType, _ := this.GetInt("MeetType")
	isResearch, _ := this.GetBool("IsResearch", false) // 是否为研选 查研观向11.0 （研选活动独立显示）
	resp := new(cygx.ActivityTypeListResp)
	var condition string
	if meetType == 1 {
		condition += ` AND activity_type =1 `
	} else {
		condition += ` AND activity_type  = 0 `
	}
	if isResearch {
		condition += " AND  source_type IN (0,2) "
	} else {
		condition += " AND source_type IN (0,1) "
	}
	list, err := cygx.GetActivityTypeSearchList(condition)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
