package yb

import (
	"encoding/json"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/yb/request"
	"hongze/hz_crm_api/models/yb/response"
	"hongze/hz_crm_api/services/yb"
	"hongze/hz_crm_api/utils"
)

// PriceDrivenController 价格驱动模块
type PriceDrivenController struct {
	controllers.BaseAuthController
}

// GetPriceDrivenDetail
// @Title 获取价格驱动详情
// @Description 获取价格驱动详情
// @Param   VarietyTagId  query  int  false  "标签ID"
// @Success 200 {object} response.PriceDrivenDetail
// @router /price_driven/detail [get]
func (this *PriceDrivenController) GetPriceDrivenDetail() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	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
	}
	varietyTagId, _ := this.GetInt("VarietyTagId")
	if varietyTagId <= 0 {
		br.Msg = "参数有误"
		br.ErrMsg = "参数有误"
		return
	}
	detail, msg, err := yb.GetPriceDrivenDetail(varietyTagId)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = "获取价格驱动详情失败, Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = detail
}

// UpdatePriceDriven
// @Title 更新价格驱动
// @Description 更新价格驱动
// @Param	request  body  request.UpdatePriceDrivenReq  true  "type json string"
// @Success 200 {object} response.PriceDrivenDetail
// @router /price_driven/update [post]
func (this *PriceDrivenController) UpdatePriceDriven() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	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
	}
	var req request.UpdatePriceDrivenReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败, Err:" + err.Error()
		return
	}
	if req.VarietyTagId <= 0 {
		br.Msg = "参数有误"
		return
	}
	if req.AutoSave != 1 {
		if req.MainVariable == "" {
			br.Msg = "请输入关键变量"
			return
		}
		if req.CoreDrivenContent == "" {
			br.Msg = "请输入核心驱动"
			return
		}
		if req.CoreContent == "" {
			br.Msg = "请输入核心内容"
			return
		}
	}
	detail, msg, err := yb.UpdatePriceDriven(req)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = "更新价格驱动失败, Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
	br.Data = detail
}

// GetPriceDrivenPermissionTree
// @Title 获取价格驱动分类列表
// @Description 获取价格驱动分类列表
// @Success 200 {object} response.ChartPermissionTree
// @router /price_driven/permission_tree [get]
func (this *PriceDrivenController) GetPriceDrivenPermissionTree() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	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
	}
	list, err := yb.GetPriceDrivenPermissionTree()
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取价格驱动分类列表失败, Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = list
}

// SwitchPriceDrivenPermissionState
// @Title 切换价格驱动分类状态
// @Description 切换价格驱动分类状态
// @Param	request  body  request.SwitchPriceDrivenPermissionStateReq  true  "type json string"
// @Success 200 string "操作成功"
// @router /price_driven/switch_permission_state [post]
func (this *PriceDrivenController) SwitchPriceDrivenPermissionState() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	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
	}
	var req request.SwitchPriceDrivenPermissionStateReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败, Err:" + err.Error()
		return
	}
	if req.VarietyTagId <= 0 {
		br.Msg = "参数有误"
		return
	}
	msg, err := yb.SwitchPriceDrivenPermissionState(req.VarietyTagId, req.PriceDrivenState)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = "切换价格驱动分类状态失败, Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// SendPriceDrivenTemplateMsg
// @Title 推送价格驱动模板消息
// @Description 推送价格驱动模板消息
// @Param	request  body  request.PriceDrivenSendMsgReq  true  "type json string"
// @Success 200 string "操作成功"
// @router /price_driven/send_template_msg [post]
func (this *PriceDrivenController) SendPriceDrivenTemplateMsg() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	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
	}
	var req request.PriceDrivenSendMsgReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败, Err:" + err.Error()
		return
	}
	if req.PriceDrivenId <= 0 {
		br.Msg = "参数有误"
		return
	}
	msg, err := yb.SendPriceDrivenTemplateMsg(req.PriceDrivenId)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = "推送价格驱动模板消息失败, Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// SendPriceDrivenThsMsg
// @Title 推送价格驱动同花顺客群消息
// @Description 推送价格驱动同花顺客群消息
// @Param	request  body  request.PriceDrivenSendMsgReq  true  "type json string"
// @Success 200 string "操作成功"
// @router /price_driven/send_ths_msg [post]
func (this *PriceDrivenController) SendPriceDrivenThsMsg() {
	br := new(models.BaseResponse).Init()
	br.IsSendEmail = false
	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
	}
	var req request.PriceDrivenSendMsgReq
	if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败, Err:" + err.Error()
		return
	}
	if req.PriceDrivenId <= 0 {
		br.Msg = "参数有误"
		return
	}
	msg, err := yb.SendPriceDrivenThsMsg(req.PriceDrivenId)
	if err != nil {
		br.Msg = msg
		br.ErrMsg = "推送价格驱动客群消息失败, Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "操作成功"
}

// ClickCensusList
// @Title 价格驱动点击量列表
// @Description 价格驱动点击量列表
// @Param   SortField	query	int	false	"排序字段：1-按照累计点击量排序(默认) 2-按照最近点击量排序"
// @Param   SortRule	query	int	false	"排序规则：1-升序 2-降序"
// @Success 200 {object} response.PriceDrivenClickCensusResp
// @router /price_driven/click_census_list [get]
func (this *PriceDrivenController) ClickCensusList() {
	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
	}
	sortField, _ := this.GetInt("SortField")
	sortRule, _ := this.GetInt("SortRule")

	var startSize int
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	resp := new(response.PriceDrivenClickCensusResp)
	total, list, err := yb.GetPriceDrivenClickCensusList(startSize, pageSize, sortField, sortRule)
	if err != nil {
		br.Msg = "获取点击量统计列表失败"
		br.ErrMsg = "获取价格驱动统计列表失败-ClickCensusList ErrMsg:" + err.Error()
		return
	}
	resp.List = list

	page := paging.GetPaging(currentIndex, pageSize, int(total))
	resp.Paging = page
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// ClickCensusDetail
// @Title 价格驱动点击量详情
// @Description 价格驱动点击量详情
// @Param   VarietyTagId	query	int	false	"标签ID：查询累计点击量详情"
// @Param   PriceDrivenId	query	int	false	"价格驱动ID：查询最近点击量详情"
// @Success 200 {object} response.PriceDrivenClickCensusDetailResp
// @router /price_driven/click_census_detail [get]
func (this *PriceDrivenController) ClickCensusDetail() {
	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
	}
	varietyTagId, _ := this.GetInt("VarietyTagId")
	priceDrivenId, _ := this.GetInt("PriceDrivenId")
	if varietyTagId <= 0 && priceDrivenId <= 0 {
		br.Msg = "参数有误"
		return
	}

	var startSize int
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = paging.StartIndex(currentIndex, pageSize)

	resp := new(response.PriceDrivenClickCensusDetailResp)
	total, pageTotal, list, err := yb.GetPriceDrivenClickCensusDetail(startSize, pageSize, varietyTagId, priceDrivenId)
	if err != nil {
		br.Msg = "获取点击量统计详情列表失败"
		br.ErrMsg = "获取价格驱动统计详情列表失败-ClickCensusDetail ErrMsg:" + err.Error()
		return
	}
	resp.List = list

	page := paging.GetPaging(currentIndex, pageSize, total)
	resp.Paging = page
	resp.PageTotal = pageTotal
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
