package message

import (
	"spaceofcheng/services/models"
	"spaceofcheng/services/models/model"
	"spaceofcheng/services/utils"
	"strconv"
	"strings"
	"time"

	"github.com/astaxie/beego"

	"github.com/go-xorm/builder"
)

// CmsCaseInfo 项目专题
type CmsCaseInfo struct {
	model.TaCmsCase `xorm:"extends"`
	CaseName        string
}

const (
	STATUS_NOSHOW = 0
)

// GetCmsCaseList 获取项目专题列表
func (m *MessageDAO) GetCmsCaseList(name, caseids string, page int, pageSize int) ([]CmsCaseInfo, error) {
	var cases []CmsCaseInfo
	sql := `select a.*,b.case_name from ta_cms_case a inner join sys_case b on a.case_id = b.case_id 
	where a.case_id in ('` + strings.Replace(caseids, ",", "','", -1) + `') and a.name like '%` + name + `%' and a.status > ` + strconv.Itoa(models.STATUS_DEL) + `
	and b.status > ` + strconv.Itoa(models.STATUS_DEL) + `
	order by create_date desc limit ` + strconv.Itoa((page-1)*pageSize) + `, ` + strconv.Itoa(page*pageSize)
	err := m.db.Sql(sql).Find(&cases)
	return cases, err
}

// GetCmsCaseByOrg 获取列表
func (m *MessageDAO) GetCmsCaseByOrg(orgid string) ([]CmsCase, error) {
	var cases []CmsCase
	sql := `select * from ta_cms_case where org_id='` + orgid + `' and status=` + strconv.Itoa(models.STATUS_NORMAL)
	err := m.db.Sql(sql).Find(&cases)
	// err := m.db.Where("org_id=?", orgid).Find(&cases)
	return cases, err
}

// GetCmsCaseListCount 获取项目专题Count
func (m *MessageDAO) GetCmsCaseListCount(name, caseids string) (int, error) {
	var cases []CmsCaseInfo
	sql := `select a.*,b.case_name from ta_cms_case a inner join sys_case b on a.case_id = b.case_id 
	where a.case_id in ('` + strings.Replace(caseids, ",", "','", -1) + `') and a.name like '%` + name + `%' 
	and a.status > ` + strconv.Itoa(models.STATUS_DEL) + ` and b.status > ` + strconv.Itoa(models.STATUS_DEL)

	err := m.db.Sql(sql).Find(&cases)
	if err != nil {
		return 0, err
	}
	return len(cases), nil
}

// CmsCase 项目详情
type CmsCase struct {
	model.TaCmsCase `xorm:"extends"`
	Courses         []model.TaCourse
	CmsCaseImgs     []model.TaCmsCaseImage
}

// GetCmsCaseByID 获取项目专题详情
func (m *MessageDAO) GetCmsCaseByID(cmscaseid string) (*CmsCase, error) {
	var cases []CmsCase
	sql := `select * from ta_cms_case where cms_case_id='` + cmscaseid + `' and status>` + strconv.Itoa(models.STATUS_DEL)
	err := m.db.Sql(sql).Find(&cases)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	if len(cases) > 0 {
		return &cases[0], nil
	}
	return nil, nil
}

// GetCoursesByCmsCase 根据项目专题ID获取课程信息
func (m *MessageDAO) GetCoursesByCmsCase(cmscaseid string) ([]model.TaCourse, error) {
	var course []model.TaCourse
	err := m.db.Where("status>"+strconv.Itoa(models.STATUS_DEL)).In("course_id", builder.Select("course_id").From("ta_cms_case_course").Where(builder.Eq{"cms_case_id": cmscaseid})).Find(&course)
	return course, err
}

// GetCmsCaseImgs 根据项目专题ID获取课程图片
func (m *MessageDAO) GetCmsCaseImgs(cmscaseid string) ([]model.TaCmsCaseImage, error) {
	var imgs []model.TaCmsCaseImage
	err := m.db.Where("cms_case_id=?", cmscaseid).And("status>" + strconv.Itoa(models.STATUS_DEL)).Find(&imgs)
	return imgs, err
}

// GetCourseByCaseID 根据案场ID获取所有的课程信息
func (m *MessageDAO) GetCourseByCaseID(caseid string) ([]model.TaCourse, error) {
	var course []model.TaCourse
	err := m.db.Where("status>"+strconv.Itoa(models.STATUS_DEL)).And("case_id=?", caseid).Find(&course)
	return course, err
}

// AddCmsCase 新增项目专题
func (m *MessageDAO) AddCmsCase(cmscase model.TaCmsCase) (*model.TaCmsCase, error) {
	cmscase.CreateDate = time.Now()
	cmscase.CmsCaseId = utils.GetGUID()
	user := m.ctx.Get("user").(model.SysUser)
	cmscase.CreateUser = user.UserId
	_, err := m.db.Insert(cmscase)
	return &cmscase, err
}

// UpdateCmsCase 修改项目专题
func (m *MessageDAO) UpdateCmsCase(cmscase model.TaCmsCase) error {
	var col = []string{
		"name",
		"title",
		"detail_content",
		"is_all_course",
		"status",
		"sort",
	}
	_, err := m.db.Cols(col...).Where("cms_case_id=?", cmscase.CmsCaseId).Update(cmscase)
	return err
}

// DelCmsCase 删除项目专题
func (m *MessageDAO) DelCmsCase(cmscaseid string) error {
	var cmscase = model.TaCmsCase{}
	cmscase.Status = models.STATUS_DEL
	cmscase.CmsCaseId = cmscaseid
	var cols = []string{
		"status",
	}
	_, err := m.db.Cols(cols...).Where("cms_case_id=?", cmscase.CmsCaseId).Update(cmscase)
	return err
}

// DelCmsCaseCourse 删除项目专题对应的课程
func (m *MessageDAO) DelCmsCaseCourse(cmscaseid string) error {
	sql := "delete from ta_cms_case_course where cms_case_id='" + cmscaseid + "'"
	_, err := m.db.Exec(sql)
	return err
}

// SaveCmsCaseCourse 保存项目对应的课程
func (m *MessageDAO) SaveCmsCaseCourse(cmscaseid, courseids string) error {
	sql := `insert into ta_cms_case_course(cms_case_id,course_id) select '` + cmscaseid + `', course_id from ta_course where course_id in ('` + strings.Replace(courseids, ",", "','", -1) + `') and status > ` + strconv.Itoa(models.STATUS_DEL)
	_, err := m.db.Exec(sql)
	return err
}

// DelCmsCaseImg 删除项目专题对应的图片
func (m *MessageDAO) DelCmsCaseImg(cmscaseid string) error {
	sql := "delete from ta_cms_case_image where cms_case_id='" + cmscaseid + "'"
	_, err := m.db.Exec(sql)
	return err
}

// SaveCmsCaseImg 保存项目专题对应的图片
func (m *MessageDAO) SaveCmsCaseImg(img model.TaCmsCaseImage) error {
	img.CmsCaseImageId = utils.GetGUID()
	img.Status = models.STATUS_NORMAL
	_, err := m.db.Insert(img)
	return err
}

// CmsCaseShow 前台展示
func (m *MessageDAO) CmsCaseShow(cmscaseid string) error {
	var cmscase = model.TaCmsCase{}
	cmscase.Status = models.STATUS_NORMAL
	cmscase.CmsCaseId = cmscaseid
	var cols = []string{
		"status",
	}
	_, err := m.db.Cols(cols...).Where("cms_case_id=?", cmscase.CmsCaseId).Update(cmscase)
	return err
}

// CmsCaseHide 前台隐藏
func (m *MessageDAO) CmsCaseHide(cmscaseid string) error {
	var cmscase = model.TaCmsCase{}
	cmscase.Status = STATUS_NOSHOW
	cmscase.CmsCaseId = cmscaseid
	var cols = []string{
		"status",
	}
	_, err := m.db.Cols(cols...).Where("cms_case_id=?", cmscase.CmsCaseId).Update(cmscase)
	return err
}
