package message

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

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

// CmsNewsInfo 咨询信息
type CmsNewsInfo struct {
	model.TaCmsNews `xorm:"extends"`
	LocationNames   string
	LocationIds     string
}

// GetNewsList 获取咨询列表
func (m *MessageDAO) GetNewsList(locationid, orgid string, page int, pageSize int) ([]CmsNewsInfo, error) {
	var news []CmsNewsInfo
	sql := `select * from ta_cms_news new left join (
		select b.news_id,GROUP_CONCAT(a.location_name) location_names,GROUP_CONCAT(a.location_id) location_ids 
		from td_cms_image_location a inner join ta_cms_location b on a.location_id=b.location_id and a.status>` + strconv.Itoa(models.STATUS_DEL) +
		` and a.org_id='` + orgid + `' group by b.news_id
		) c on new.news_id = c.news_id WHERE new.status>` + strconv.Itoa(models.STATUS_DEL) + ` and new.org_id='` + orgid + `'`

	if locationid != "" {
		sql += ` and new.news_id in (select news_id from ta_cms_location where location_id='` + locationid + `')`
	}

	sql = sql + " order by new.create_date desc limit " + strconv.Itoa((page-1)*pageSize) + ", " + strconv.Itoa(page*pageSize)
	err := m.db.Sql(sql).Find(&news)
	return news, err
}

// GetNewsListCount 获取咨询列表count
func (m *MessageDAO) GetNewsListCount(locationid, orgid string) (int, error) {
	var news []model.TaCmsNews
	dao := m.db.Where("status>"+strconv.Itoa(models.STATUS_DEL)).And("org_id=?", orgid)
	if locationid != "" {
		dao.In("news_id", builder.Select("news_id").From("ta_cms_location").Where(builder.Eq{"location_id": locationid}))
	}
	err := dao.Find(&news)
	if err != nil {
		return 0, nil
	}
	return len(news), nil
}

// GetNewsByLocation 根据位置获取咨询信息
func (m *MessageDAO) GetNewsByLocation(locationid, orgid string) ([]model.TaCmsNews, error) {
	var news []model.TaCmsNews
	err := m.db.Where("status>"+strconv.Itoa(models.STATUS_DEL)).And("org_id=?", orgid).In("news_id", builder.Select("news_id").From("ta_cms_location").Where(builder.Eq{"location_id": locationid})).Find(&news)
	return news, err
}

// CmsNews 咨询详情
type CmsNews struct {
	model.TaCmsNews `xorm:"extends"`
	NewsLocations   []model.TaCmsLocation
}

// GetNewsByID 获取咨询详情
func (m *MessageDAO) GetNewsByID(newid string) (*CmsNews, error) {
	var news []CmsNews
	sql := `select * from ta_cms_news where news_id='` + newid + `' and status>` + strconv.Itoa(models.STATUS_DEL)
	err := m.db.Sql(sql).Find(&news)
	if err != nil {
		return nil, err
	}
	if len(news) > 0 {
		return &news[0], err
	}
	return nil, nil
}

// GetNewsLocationByID 根据咨询id获取咨询位置
func (m *MessageDAO) GetNewsLocationByID(newsid string) ([]model.TaCmsLocation, error) {
	var newslocation []model.TaCmsLocation
	err := m.db.Where("news_id=?", newsid).Find(&newslocation)
	return newslocation, err
}

// AddNews 新增咨询详细
func (m *MessageDAO) AddNews(news model.TaCmsNews) (*model.TaCmsNews, error) {
	news.NewsId = utils.GetGUID()
	news.CreateDate = time.Now()
	user := m.ctx.Get("user").(model.SysUser)
	news.CreateUser = user.UserId
	_, err := m.db.Insert(news)
	return &news, err
}

// DelNewsLocation 删除咨询与位置映射
func (m *MessageDAO) DelNewsLocation(newsid string) error {
	sql := "delete from ta_cms_location where news_id='" + newsid + "'"
	_, err := m.db.Exec(sql)
	return err
}

// SaveNewsLocation 保存咨询与位置映射
func (m *MessageDAO) SaveNewsLocation(newsid, locationsid string) error {
	sql := `insert into ta_cms_location(news_id,location_id) select '` + newsid + `', location_id from td_cms_image_location where location_id in ('` + strings.Replace(locationsid, ",", "','", -1) + `') and status > ` + strconv.Itoa(models.STATUS_DEL)
	_, err := m.db.Exec(sql)
	return err
}

// UpdateNews 编辑咨询详情
func (m *MessageDAO) UpdateNews(news model.TaCmsNews) error {
	var col = []string{
		"image_url",
		"forward_type",
		"forward_url",
		"forward_course_id",
		"status",
		"title",
		"public_date",
	}
	_, err := m.db.Cols(col...).Where("news_id=?", news.NewsId).Update(news)
	return err
}

// DelNews 删除咨询
func (m *MessageDAO) DelNews(newsid string) error {
	var news = model.TaCmsNews{}
	news.Status = models.STATUS_DEL
	news.NewsId = newsid
	var cols = []string{
		"status",
	}
	_, err := m.db.Cols(cols...).Where("news_id=?", news.NewsId).Update(news)
	return err
}
