package cygx

import (
	"encoding/json"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/cygx"
	cygxService "hongze/hz_crm_api/services/cygx"
	"hongze/hz_crm_api/services/elastic"
	"hongze/hz_crm_api/utils"
	"strconv"
	"time"
)

// YanxuanSpecialController 研选专栏
type YanxuanSpecialController struct {
	controllers.BaseAuthController
}

// @Title 新增研选专栏作者
// @Description 新增研选专栏作者
// @Param	request	body help_doc.AddHelpDocReq true "type json string"
// @Success 200 {object} models.AddEnglishReportResp
// @router /yanxuan_special/author/add [post]
func (this *YanxuanSpecialController) Add() {
	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
	}
	var req cygx.AddCygxYanxuanSpecialAuthorReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.UserId <= 0 {
		br.Msg = "请输入用户信息"
		return
	}
	if req.RealName == "" {
		br.Msg = "请输入真实姓名"
		return
	}
	if req.Mobile == "" {
		br.Msg = "请输入手机号"
		return
	}

	//获取关联公司的用户信息
	infoUser, err := cygx.GetUserAndCompanyNameList(req.UserId)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取数据失败，Err:" + err.Error()
		return
	}

	rnd := utils.GetRandInt(1, 5)
	item := cygx.CygxYanxuanSpecialAuthor{
		UserId:      req.UserId,
		RealName:    req.RealName,
		Mobile:      req.Mobile,
		CompanyName: infoUser.CompanyName,
		CompanyId:   infoUser.CompanyId,
		CreateTime:  time.Now(),
		ModifyTime:  time.Now(),
		HeadImg:     utils.CYGX_YANXUAN_SPECIAL_HEAD_IMG_URL + strconv.Itoa(rnd) + ".png",
		BgImg:       utils.CYGX_YANXUAN_SPECIAL_BG_IMG_URL + strconv.Itoa(rnd) + "a.png",
		BgImgDown:   utils.CYGX_YANXUAN_SPECIAL_BG_IMG_URL + strconv.Itoa(rnd) + "b.png",
		BgImgPc:     utils.CYGX_YANXUAN_SPECIAL_BG_IMG_URL_PC + strconv.Itoa(rnd) + ".png",
		Status:      1,
	}

	_, err = cygx.AddCygxYanxuanSpecialAuthor(&item)
	if err != nil {
		br.Msg = "新增失败"
		br.ErrMsg = "新增失败，Err:" + err.Error()
		return
	}

	br.Ret = 200
	br.Success = true
	br.Msg = "新增成功"
}

// @Title 禁用/启用研选专栏作者
// @Description 禁用/启用研选专栏作者
// @Param	request	body help_doc.AddHelpDocReq true "type json string"
// @Success 200 {object} models.AddEnglishReportResp
// @router /yanxuan_special/author/enable [post]
func (this *YanxuanSpecialController) AuthorEnable() {
	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
	}
	var req cygx.EnableCygxYanxuanSpecialAuthorReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.UserId <= 0 {
		br.Msg = "用户id错误"
		return
	}
	if req.Status <= 0 {
		br.Msg = "参数错误"
		return
	}
	if tmpErr := cygx.EnableYanxuanSpecialAuthor(req.UserId, req.Status); tmpErr != nil {
		br.Msg = "启用/禁用作者失败"
		br.ErrMsg = "启用/禁用作者失败, Err:" + tmpErr.Error()
		return
	}

	if req.Status == 1 {
		br.Msg = "启用成功"
	} else {
		br.Msg = "禁用成功"
	}

	br.Ret = 200
	br.Success = true
}

// @Title 作者列表
// @Description 作者列表
// @Param	request	body help_doc.AddHelpDocReq true "type json string"
// @Success 200 {object} models.AddEnglishReportResp
// @router /yanxuan_special/author/list [get]
func (this *YanxuanSpecialController) AuthorList() {
	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
	}

	list, tmpErr := cygx.GetYanxuanSpecialAuthorList()
	if tmpErr != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败, Err:" + tmpErr.Error()
		return
	}

	br.Data = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}

//V12.0.1 上线脚本
//func init() {
//	list, err := cygx.GetYanxuanSpecialAuthorList()
//	if err != nil {
//		fmt.Println(err)
//	}
//	for _, v := range list {
//		//获取关联公司的用户信息
//		infoUser, err := cygx.GetUserAndCompanyNameList(v.UserId)
//		if err != nil {
//			fmt.Println(err)
//		}
//		err = cygx.UpdateSpecialAuthorComapony(infoUser.UserId, infoUser.CompanyId, infoUser.CompanyName)
//		fmt.Println(infoUser.UserId)
//	}
//}

// @Title 审核列表
// @Description 审核列表
// @Param	request	body help_doc.AddHelpDocReq true "type json string"
// @Success 200 {object} models.AddEnglishReportResp
// @router /yanxuan_special/list [get]
func (this *YanxuanSpecialController) 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
	}

	userId, _ := this.GetInt("UserId", 0)
	var condition string
	var pars []interface{}

	if userId > 0 {
		condition += ` AND a.user_id = ? `
		pars = append(pars, userId)
	}

	condition += ` AND a.status = 2 `

	list, tmpErr := cygx.GetYanxuanSpecialList(condition, pars)
	if tmpErr != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败, Err:" + tmpErr.Error()
		return
	}
	for _, v := range list {
		hasImg, err := utils.ArticleHasImgUrl(v.Content)
		if err != nil {
			return
		}
		if hasImg {
			v.ContentHasImg = 1
		}
		if v.DocUrl != "" {
			var docs []cygx.Doc
			err := json.Unmarshal([]byte(v.DocUrl), &docs)
			if err != nil {
				br.Msg = "参数解析异常！"
				br.ErrMsg = "参数解析失败，Err:" + err.Error()
				return
			}
			v.Docs = docs
		}
		if v.Type == 1 {
			v.Title = "【笔记】" + v.Title
		} else if v.Type == 2 {
			v.Title = "【观点】" + v.Title
		}
		if v.CompanyTags != "" {
			v.Tags += v.CompanyTags
		}
		if v.IndustryTags != "" {
			if v.Tags != "" {
				v.Tags += ","
			}
			v.Tags += v.IndustryTags
		}
	}

	br.Data = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
}

// @Title 审批研选专栏
// @Description 审批研选专栏
// @Param	request	body help_doc.AddHelpDocReq true "type json string"
// @Success 200 {object} models.AddEnglishReportResp
// @router /yanxuan_special/enable [post]
func (this *YanxuanSpecialController) Enable() {
	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
	}
	var req cygx.EnableCygxYanxuanSpecialReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	if req.Id <= 0 {
		br.Msg = "文章id错误"
		return
	}
	if req.Status <= 0 {
		br.Msg = "参数错误"
		return
	}
	status := 0
	if req.Status == 1 {
		status = 3
	} else {
		status = 4
	}
	if tmpErr := cygx.EnableYanxuanSpecial(req.Id, status, req.Reason); tmpErr != nil {
		br.Msg = "审批失败"
		br.ErrMsg = "审批失败, Err:" + tmpErr.Error()
		return
	}
	if req.Status == 1 {
		go cygxService.SendWxMsgSpecialFollow(req.Id)
	}
	go cygxService.SendWxMsgSpecialAuthor(req.Id, req.Status) // 研选专栏审核完成时，给提交人发送模板消息
	go cygxService.UpdateYanxuanSpecialResourceData(req.Id)   //  写入首页最新  cygx_resource_data 表
	go elastic.EsAddYanxuanSpecial(req.Id)                    //  写入es 综合搜索
	br.Msg = "审批成功"
	br.Ret = 200
	br.Success = true
}
