package cygx

import (
	//"github.com/beego/beego/v2/client/orm"
	//"github.com/rdlucklib/rdluck_tools/paging"
	//"strconv"
	//"strings"
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"strconv"
	"strings"
	"time"
)

type CygxAskserieVideo struct {
	AskserieVideoId     int       `orm:"column(askserie_video_id);pk"description:"视频id"`
	VideoName           string    `description:"视频标题"`
	VideoUrl            string    `description:"视频地址"`
	VideoDuration       string    `description:"视频时长"`
	ChartPermissionId   int       `description:"行业ID"`
	ChartPermissionName string    `description:"行业名称"`
	PublishStatus       int       `description:"发布状态 1发布 0没有"`
	VideoCounts         int       `description:"播放量"`
	BackgroundImg       string    `description:"封面图片"`
	ShareImg            string    `description:"分享图片"`
	AdminId             int       `description:"管理员、销售ID"`
	ModifyDate          time.Time `description:"更新时间"`
	PublishDate         time.Time `description:"发布时间"`
	CreateTime          time.Time `description:"创建时间"`
	ActivityId          int       ` description:"活动ID"`
}

type AddAskserieVideoReq struct {
	AskserieVideoId         int    `description:"askserie_video_id"`
	VideoName               string `description:"视频标题"`
	VideoUrl                string `description:"视频地址"`
	VideoDuration           string `description:"视频时长"`
	ChartPermissionId       int    `description:"行业ID"`
	ChartPermissionName     string `description:"行业名称"`
	IndustrialManagementIds string `description:"产业id 多个用 , 隔开"`
	BackgroundImg           string `description:"封面图片"`
	ShareImg                string `description:"分享图片"`
}

type CygxAskserieVideoResp struct {
	AskserieVideoId     int    `orm:"column(askserie_video_id);pk"description:"视频id"`
	VideoName           string `description:"视频标题"`
	VideoUrl            string `description:"视频地址"`
	VideoDuration       string `description:"视频时长"`
	ChartPermissionId   int    `description:"行业ID"`
	ChartPermissionName string `description:"行业名称"`
	PublishStatus       int    `description:"发布状态 1发布 0没有"`
	VideoCounts         int    `description:"播放量"`
	CommentNum          int    `description:"留言总数"`
	BackgroundImg       string `description:"封面图片"`
	ShareImg            string `description:"分享图片"`
	AdminId             int    `description:"管理员、销售ID"`
	IndustryName        string `description:"产业名称"`
	ModifyDate          string `description:"更新时间"`
	PublishDate         string `description:"发布时间"`
	CreateTime          string `description:"创建时间"`
	ListIndustrial      []*IndustrialActivityGroupManagementRep
}

type GetCygxAskserieVideoDetailResp struct {
	Detail *CygxAskserieVideoResp
}

// 通过ID获取详情
func GetCygxAskserieVideoDetail(askserieVideoId int) (item *CygxAskserieVideoResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_askserie_video  WHERE askserie_video_id=? `
	err = o.Raw(sql, askserieVideoId).QueryRow(&item)
	return
}

// 添加
func AddCygxAskserieVideo(item *CygxAskserieVideo, industrialManagementIds string) (newId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	newId, err = to.Insert(item)
	if err != nil {
		return
	}
	//添加与产业的关联
	//new(cygx.CygxIndustrialAskserieVideoGroupManagement),
	industrialManagementIdList := strings.Split(industrialManagementIds, ",")
	for _, v := range industrialManagementIdList {
		itemIndustrialManagementGroup := new(CygxIndustrialAskserieVideoGroupManagement)
		newIndustrialId, _ := strconv.Atoi(v)
		if newIndustrialId == 0 {
			continue
		}
		itemIndustrialManagementGroup.CreateTime = time.Now()
		itemIndustrialManagementGroup.AskserieVideoId = int(newId)
		itemIndustrialManagementGroup.IndustrialManagementId = newIndustrialId
		_, err = to.Insert(itemIndustrialManagementGroup)
		if err != nil {
			return
		}
	}
	return
}

// 修改
func UpdateCygxAskserieVideo(item *CygxAskserieVideo, industrialManagementIds string) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	updateParams := make(map[string]interface{})
	updateParams["VideoName"] = item.VideoName
	updateParams["VideoUrl"] = item.VideoUrl
	updateParams["VideoDuration"] = item.VideoDuration
	updateParams["ChartPermissionId"] = item.ChartPermissionId
	updateParams["ChartPermissionName"] = item.ChartPermissionName
	updateParams["BackgroundImg"] = item.BackgroundImg
	updateParams["ShareImg"] = item.ShareImg
	updateParams["ModifyDate"] = time.Now()
	ptrStructOrTableName := "cygx_askserie_video"
	whereParam := map[string]interface{}{"askserie_video_id": item.AskserieVideoId}
	qs := to.QueryTable(ptrStructOrTableName)
	for expr, exprV := range whereParam {
		qs = qs.Filter(expr, exprV)
	}
	_, err = qs.Update(updateParams)
	if err != nil {
		return
	}

	//删除关联产业 （避免截断表时产生的脏数据，故不用update而是先删除再增加）
	sql := `	DELETE FROM cygx_industrial_askserie_video_group_management WHERE askserie_video_id = ?`
	_, err = to.Raw(sql, item.AskserieVideoId).Exec()
	if err != nil {
		return
	}

	industrialManagementIdList := strings.Split(industrialManagementIds, ",")
	for _, v := range industrialManagementIdList {
		itemIndustrialManagementGroup := new(CygxIndustrialAskserieVideoGroupManagement)
		newIndustrialId, _ := strconv.Atoi(v)
		itemIndustrialManagementGroup.CreateTime = time.Now()
		itemIndustrialManagementGroup.AskserieVideoId = item.AskserieVideoId
		itemIndustrialManagementGroup.IndustrialManagementId = newIndustrialId
		_, err = to.Insert(itemIndustrialManagementGroup)
		if err != nil {
			return
		}
	}

	return
}

// 获取数量
func GetCygxAskserieVideoCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_askserie_video as art WHERE 1= 1  `
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount, pars).QueryRow(&count)
	return
}

// 列表
func GetCygxAskserieVideoList(condition string, pars []interface{}, startSize, pageSize int) (items []*CygxAskserieVideoResp, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_askserie_video as art WHERE 1= 1 `
	if condition != "" {
		sql += condition
	}
	sql += ` LIMIT ?,?  `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
	return
}

type GetCygxAskserieVideoRespListResp struct {
	Paging *paging.PagingItem `description:"分页数据"`
	List   []*CygxAskserieVideoResp
}

// 修改是否展示
func EditCygxAskserieVideoStatus(status, askserieVideoId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_askserie_video SET publish_status=?,publish_date = NOW() , modify_date=NOW()   WHERE askserie_video_id=? `
	_, err = o.Raw(sql, status, askserieVideoId).Exec()
	return
}

// 修改分享图片
func EditCygxAskserieVideoShareImg(shareImg string, askserieVideoId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_askserie_video SET share_img=?   WHERE askserie_video_id=? `
	_, err = o.Raw(sql, shareImg, askserieVideoId).Exec()
	return
}

// 修改文件名称
func EditCygxAskserieVideoVideoName(videoName string, askserieVideoId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `UPDATE cygx_askserie_video SET video_name=?   WHERE askserie_video_id=? `
	_, err = o.Raw(sql, videoName, askserieVideoId).Exec()
	return
}
