package backstage_service

import (
	"github.com/gogf/gf/frame/g"
	"youngmini_server/app/model/backstage_model"
	"youngmini_server/app/utils"
)

var Bilibili = serviceBilibili{}

type serviceBilibili struct{}

// FindAllBilibili 查询所有
func (s *serviceBilibili) FindAllBilibili(bilibili *backstage_model.BilibiliFind) ([]*backstage_model.ResponseBilibiliFind, int, error) {
	var res []*backstage_model.ResponseBilibiliFind
	g.DB().SetDebug(true)
	number, err := utils.RelyNumber(5)
	if err != nil {
		return nil, 0, err
	}
	err = g.DB().Model("platform_bilibili_account_info").LeftJoin("talent_info", "talent_id = talent_info.id").
		OmitEmpty().Where("talent_id IN(?)", number).
		Fields("`platform_id`,`platform_account_id`,`platform_nickname`,`submit_date`,`fans_count`,`got_like_count`,`home_page_url`,`skilled_at`,`talent_id`,visit_store_region").
		Page(bilibili.IndexPage, bilibili.PageSize).OrderDesc("talent_id").Scan(&res)
	if err != nil {
		return nil, 0, err
	}
	var result []*backstage_model.ResponseBilibiliFind
	err = g.DB().Model("platform_bilibili_account_info").LeftJoin("talent_info", "talent_id = talent_info.id").OmitEmpty().
		Where("talent_id IN(?)", number).
		OrderDesc("talent_id").Scan(&result)
	if err != nil {
		return nil, 0, err
	}
	num := len(result)
	return res, num, nil
}

// FindByConditionBilibili 根据查询条件查询
func (s *serviceBilibili) FindByConditionBilibili(bilibili *backstage_model.BilibiliFind) ([]*backstage_model.ResponseBilibiliFind, int, error) {
	var res []*backstage_model.ResponseBilibiliFind
	if bilibili.Bilibili == nil {
		res, num, err := s.FindAllBilibili(bilibili)
		if err != nil {
			return nil, 0, err
		}
		return res, num, nil
	}
	g.DB().SetDebug(true)
	number, err := utils.RelyNumber(5)
	if err != nil {
		return nil, 0, err
	}
	// 拼接数据库查询字符串语句
	contact := s.contact(bilibili.Bilibili)
	err = g.DB().Model("platform_bilibili_account_info").LeftJoin("talent_info", "talent_id = talent_info.id").
		OmitEmpty().Where(contact).Where("talent_id IN(?)", number).Fields("`platform_id`,`platform_account_id`,`platform_nickname`,`submit_date`,`fans_count`,`got_like_count`,`home_page_url`,`skilled_at`,`talent_id`,visit_store_region").
		Page(bilibili.IndexPage, bilibili.PageSize).OrderDesc("talent_id").Scan(&res)
	if err != nil {
		return nil, 0, err
	}
	var result []*backstage_model.ResponseBilibiliFind
	err = g.DB().Model("platform_bilibili_account_info").LeftJoin("talent_info", "talent_id = talent_info.id").
		OmitEmpty().Where("talent_id IN(?)", number).Where(contact).
		Scan(&result)
	if err != nil {
		return nil, 0, err
	}
	num := len(result)
	return res, num, nil
}

// RemoveAccount 账号下架操作
func (s *serviceBilibili) RemoveAccount(status *backstage_model.RemoveAccountBilibili) error {
	g.DB().SetDebug(true)
	_, err := g.DB().Model("platform_bilibili_account_info").
		Data(g.Map{"examine_status": "3", "examine_fail_reason": status.Message}).
		Where("platform_account_id", status.PlatformAccountId).OmitEmpty().Update()
	if err != nil {
		return err
	}
	return nil
}

// 拼接数据库数据库where语句字符串
func (s *serviceBilibili) contact(bilibili *backstage_model.RequestBilibiliFind) string {
	var res []string
	if bilibili.PlatformAccountId != "" {
		res = append(res, "platform_account_id="+bilibili.PlatformAccountId)
	}
	if bilibili.TalentId != "" {
		res = append(res, "talent_id="+bilibili.TalentId)
	}
	if len(bilibili.SubmitDate) != 0 {
		res = append(res, "submit_date>'"+bilibili.SubmitDate[0]+" 00:00:00' and "+
			"submit_date<'"+bilibili.SubmitDate[1]+" 23:59:59'")
	}
	if len(bilibili.FansCount) != 0 {
		res = append(res, "fans_count>="+bilibili.FansCount[0]+" and fans_count<="+bilibili.FansCount[1])
	}
	if bilibili.VisitStoreRegion != "" {
		res = append(res, "visit_store_region="+bilibili.VisitStoreRegion)
	}
	if len(bilibili.SkilledAt) != 0 {
		// JSON_CONTAINS包含语句
		var temp string
		for index, value := range bilibili.SkilledAt {
			if index == 0 {
				temp = "JSON_CONTAINS(skilled_at,'" + value + "')"
			} else {
				temp = " OR JSON_CONTAINS(skilled_at,'" + value + "')"
			}
		}
		res = append(res, "("+temp+")")
	}
	var result string
	for _, value := range res {
		result += " and " + value
	}
	return result
}

// FindAllById 单个查询所有
func (s *serviceBilibili) FindAllById(bilibili *backstage_model.RemoveAccountBilibili) (*backstage_model.ResponsePlatformBilibiliAccountInfo, error) {
	var res *backstage_model.ResponsePlatformBilibiliAccountInfo
	err := g.DB().Model("platform_bilibili_account_info").Where("talent_id", bilibili.PlatformAccountId).Scan(&res)
	if err != nil {
		return nil, err
	}
	return res, nil
}
