package backstage_service

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

var Zhihu = serviceZhihu{}

type serviceZhihu struct{}

// FindAllZhihu 查询所有
func (s *serviceZhihu) FindAllZhihu(zhihu *backstage_model.ZhihuFind) ([]*backstage_model.ResponseZhihuFind, int, error) {
	var res []*backstage_model.ResponseZhihuFind
	number, err := utils.RelyNumber(7)
	if err != nil {
		return nil, 0, err
	}
	g.DB().SetDebug(true)
	err = g.DB().Model("platform_zhihu_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`,`talent_id`,visit_store_region").
		Page(zhihu.IndexPage, zhihu.PageSize).OrderDesc("talent_id").Scan(&res)
	if err != nil {
		return nil, 0, err
	}
	var result []*backstage_model.ResponseZhihuFind
	err = g.DB().Model("platform_zhihu_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
}

// FindByConditionZhihu 根据查询条件查询
func (s *serviceZhihu) FindByConditionZhihu(zhihu *backstage_model.ZhihuFind) ([]*backstage_model.ResponseZhihuFind, int, error) {
	var res []*backstage_model.ResponseZhihuFind
	if zhihu.Zhihu == nil {
		res, num, err := s.FindAllZhihu(zhihu)
		if err != nil {
			return nil, 0, err
		}
		return res, num, nil
	}
	number, err := utils.RelyNumber(7)
	if err != nil {
		return nil, 0, err
	}
	g.DB().SetDebug(true)
	// 拼接数据库查询字符串语句
	contact := s.contact(zhihu.Zhihu)
	err = g.DB().Model("platform_zhihu_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`,`talent_id`,visit_store_region").
		Page(zhihu.IndexPage, zhihu.PageSize).OrderDesc("talent_id").Scan(&res)
	if err != nil {
		return nil, 0, err
	}
	var result []*backstage_model.ResponseZhihuFind
	err = g.DB().Model("platform_zhihu_account_info").LeftJoin("talent_info", "talent_id = talent_info.id").
		OmitEmpty().Where(contact).Where("talent_id IN(?)", number).
		Scan(&result)
	if err != nil {
		return nil, 0, err
	}
	num := len(result)
	return res, num, nil
}

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

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

// FindAllById 单个查询所有
func (s *serviceZhihu) FindAllById(zhihu *backstage_model.RemoveAccountZhihu) (*backstage_model.ResponsePlatformZhihuAccountInfo, error) {
	var res *backstage_model.ResponsePlatformZhihuAccountInfo
	err := g.DB().Model("platform_zhihu_account_info").Where("talent_id", zhihu.PlatformAccountId).Scan(&res)
	if err != nil {
		return nil, err
	}
	return res, nil
}
