package controllers

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

// 产品内测
type ProductInteriorController struct {
	BaseAuthController
}

// @Title 列表
// @Description 列表接口
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码，从1开始"
// @Success Ret=200 {object} cygx.GetCygxTacticsTimeLineResp
// @router /list [get]
func (this *ProductInteriorController) List() {
	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(models.GetCygxProductInteriorResp)
	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 := models.GetCygxProductInteriorCount(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	condition += "	ORDER BY art.publish_time DESC , art.product_interior_id DESC "
	list, err := models.GetCygxProductInteriorList(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	for _, v := range list {
		v.PublishTime = utils.TimeRemoveHms(v.PublishTime)
	}
	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   ProductInteriorId   query   int  true       "ID"
// @Success Ret=200 {object} cygx.GetCygxProductInteriorDetailResp
// @router /detail [get]
func (this *ProductInteriorController) Detail() {
	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(models.GetCygxProductInteriorDetailResp)
	productInteriorId, _ := this.GetInt("ProductInteriorId")
	if productInteriorId < 1 {
		br.Msg = "请输入详情ID"
		return
	}
	detail, err := models.GetCygxProductInteriorDetail(productInteriorId)
	if err != nil {
		br.Msg = "详情不存在"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	//判断用户权限
	hasPermission, err := services.GetUserhasPermission(user)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取用户权限信息失败,Err:" + err.Error()
	}

	if user.UserId == 0 {
		hasPermission = 1 //用户未登录(绑定登录信息)的时候也能展示 v12.2.1
	}

	//未设置全部可见的只能给弘则内部查看
	if detail.VisibleRange == 1 || user.CompanyId == utils.HZ_COMPANY_ID {
		resp.IsShow = true
	}
	resp.HasPermission = hasPermission
	if hasPermission != 1 || !resp.IsShow {
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = resp
		return
	}
	detail.PublishTime = utils.TimeRemoveHms2(detail.PublishTime)
	resp.Detail = detail
	detail.BodySlice, err = services.GetProductInteriorUrlBody(detail.Body, user)
	if err != nil {
		br.Msg = "获取信息失败"
		br.ErrMsg = "获取用户权限信息失败,Err:" + err.Error()
	}
	resp.Disclaimers = utils.DISCLAIMERS // 免责声明
	go services.AddCygxProductInteriorHistory(user, productInteriorId)
	go services.ProductInteriorHistoryUserRmind(user, productInteriorId)
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
