package cases

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

	"github.com/astaxie/beego"
)

// GetTagCount 获取标签总数
func (m *CaseDAO) GetTagCount(caseid, caseids string) (int, error) {
	var tags []TagInfo
	sql := `select tag.*,c.case_names from sys_tag tag inner join (
	select b.tag_id,GROUP_CONCAT(a.case_name) case_names from sys_case a inner join sys_tag_case b on a.case_id=b.case_id and a.status > ` + strconv.Itoa(models.STATUS_DEL) + `
	where b.tag_id in (select tag_id from sys_tag_case where case_id in ('` + strings.Replace(caseid, ",", "','", -1) + `')) 
	and b.tag_id not in (select tag_id from sys_tag_case where case_id not in ('` +
		strings.Replace(caseids, ",", "','", -1) + `'))
	group by b.tag_id ) c on tag.tag_id = c.tag_id where tag.status>` + strconv.Itoa(models.STATUS_DEL)
	err := m.db.Sql(sql).Find(&tags)

	return len(tags), err
}

// TagInfo 标签
type TagInfo struct {
	model.SysTag `xorm:"extends"`
	CaseNames    string
}

// GetTagList 根据案场获取标签
func (m *CaseDAO) GetTagList(caseid, caseids string, page int, pageSize int) ([]TagInfo, error) {
	var tags []TagInfo
	offset := (page - 1) * pageSize
	sql := `select tag.*,c.case_names from sys_tag tag inner join (
	select b.tag_id,GROUP_CONCAT(a.case_name) case_names from sys_case a inner join sys_tag_case b on a.case_id=b.case_id and a.status > ` + strconv.Itoa(models.STATUS_DEL) + `
	where b.tag_id in (select tag_id from sys_tag_case where case_id in ('` + strings.Replace(caseid, ",", "','", -1) + `')) 
	and b.tag_id not in (select tag_id from sys_tag_case where case_id not in ('` +
		strings.Replace(caseids, ",", "','", -1) + `'))
	group by b.tag_id ) c on tag.tag_id = c.tag_id where tag.status>` + strconv.Itoa(models.STATUS_DEL) + ` LIMIT ` + strconv.Itoa(pageSize) + ` OFFSET ` + strconv.Itoa(offset)
	err := m.db.Sql(sql).Find(&tags)
	return tags, err
}

// Tag 标签
type Tag struct {
	model.SysTag `xorm:"extends"`
	TagCases     []model.SysTagCase
}

// GetTagByID 根据ID获取标签
func (m *CaseDAO) GetTagByID(tagid string) (*Tag, error) {
	var tags []Tag
	sql := `select * from sys_tag where tag_id='` + tagid + `'`
	err := m.db.Sql(sql).Find(&tags)
	if err != nil {
		return nil, err
	}
	if len(tags) > 0 {
		return &tags[0], err
	}
	return nil, nil
}

// GetTagCaseByTagID 根据标签ID获取标签对应案场
func (m *CaseDAO) GetTagCaseByTagID(tagid string) ([]model.SysTagCase, error) {
	var tagcase []model.SysTagCase
	err := m.db.Where("tag_id=?", tagid).Find(&tagcase)
	return tagcase, err
}

// AddTag 新增标签
func (m *CaseDAO) AddTag(tag model.SysTag) (*model.SysTag, error) {
	tag.TagId = utils.GetGUID()
	tag.Status = models.STATUS_NORMAL
	tag.CreateDate = time.Now()
	_, err := m.db.Insert(tag)
	return &tag, err
}

// UpdateTag 修改标签
func (m *CaseDAO) UpdateTag(tag model.SysTag) error {
	var cols = []string{
		"tag_name",
	}
	_, err := m.db.Cols(cols...).Where("tag_id=?", tag.TagId).Update(tag)
	return err
}

// DeleteTag 刪除标签
func (m *CaseDAO) DeleteTag(tagid string) error {
	beego.Error(tagid)
	var tag = model.SysTag{
		TagId:  tagid,
		Status: models.STATUS_DEL,
	}
	var cols = []string{
		"status",
	}
	_, err := m.db.Cols(cols...).Where("tag_id=?", tag.TagId).Update(tag)
	return err
}

// DelTagCase 删除标签对应的案场
func (m *CaseDAO) DelTagCase(tagid string) error {
	sql := "delete from sys_tag_case where tag_id='" + tagid + "'"
	_, err := m.db.Exec(sql)
	return err
}

// SaveTagCase 保存标签对应的案场
func (m *CaseDAO) SaveTagCase(tagid, caseids string) error {
	sql := `insert into sys_tag_case(tag_id,case_id) select '` + tagid + `', case_id from sys_case where case_id in ('` + strings.Replace(caseids, ",", "','", -1) + `') and status > ` + strconv.Itoa(models.STATUS_DEL)
	_, err := m.db.Exec(sql)
	return err
}
