package controllers

import (
	"encoding/json"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/models/time_line"
	"hongze/hongze_cygx/services"
	"hongze/hongze_cygx/utils"
	"time"
)

// 固收
type GushouController struct {
	BaseAuthController
}

// @Title 固收时间线列表
// @Description 固收时间线列表接口
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Success 200 {object} models.GetCygxGushouTimeLineResp
// @router /gushouTimeLine/list [get]
func (this *GushouController) GushouTimeLineList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}

	resp := new(time_line.GetCygxGushouTimeLineResp)
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	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 art.status = 1 `
	total, err := time_line.GetCygxGushouTimeLineCount(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	condition += "	ORDER BY art.publish_time DESC , art.time_line_id DESC "
	list, err := time_line.GetCygxGushouTimeLineList(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	for _, v := range list {
		v.PublishTime = utils.TimeRemoveHms2(v.PublishTime)
		v.Resource = 1
		v.Content = services.AnnotationHtml(v.Content)
		v.ChartPermissionName = utils.GU_SHOU_NAME
	}
	if len(list) == 0 {
		list = make([]*time_line.CygxGushouTimeLineResp, 0)
	}
	cf, err := models.GetConfigByCode(utils.CYGX_GUSHOU_TIME_LINE_STATUS)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}
	//如果不是弘则用户，并且设置了内部可见，那么数据就隐藏
	if user.CompanyId != utils.HZ_COMPANY_ID && cf.ConfigValue != "1" {
		list = make([]*time_line.CygxGushouTimeLineResp, 0)
	}
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp.List = list
	resp.Paging = page
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 固收时间线点击记录
// @Description 固收时间线点击记录接口
// @Param	request	body models.GushouTimeLineTimeLineIdReq true "type json string"
// @Success Ret=200 新增成功
// @router /gushouTimeLine/history [post]
func (this *GushouController) History() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，用户信息为空"
		br.Ret = 408
		return
	}
	var req models.TacticsTimeLineTimeLineIdReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	timeLineId := req.TimeLineId
	if timeLineId == 0 {
		br.Msg = "时间线ID错误"
		return
	}
	var sellerName string
	sellerName, err = models.GetCompanySellerNameRai(user.CompanyId)
	if err != nil {
		br.Msg = "记录失败！"
		br.ErrMsg = "获取对应销售失败，Err:" + err.Error()
		return
	}
	item := time_line.CygxGushouTimeLineHistory{
		TimeLineId:  timeLineId,
		UserId:      user.UserId,
		Mobile:      user.Mobile,
		Email:       user.Email,
		CompanyId:   user.CompanyId,
		CompanyName: user.CompanyName,
		RealName:    user.RealName,
		SellerName:  sellerName,
		CreateTime:  time.Now(),
		ModifyTime:  time.Now(),
	}
	err = time_line.AddCygxGushouTimeLineHistory(&item)
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}
