package backstage_service

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

var Account = accountService{}

type accountService struct {
}

func (a *accountService) FindAll(account *backstage_model.AccountFindAll) ([]*backstage_model.AccountManagement, int, error) {
	g.DB().SetDebug(true)
	var res []*backstage_model.AccountManagement
	err := g.DB().Model("r_talent_platform_table").
		LeftJoin("talent_info", "talent_info.id = r_talent_platform_table.tid").
		Fields("tid", "p_id", "p_account_id", "talent_info.talent_wx_nickname", "r_talent_platform_table.create_date",
			"examine_date", "fans_count", "home_page_url", "examine_state", "p_nickname").
		Where(account.AccountManagement).
		Page(account.IndexPage, account.PageSize).OmitEmpty().Scan(&res)
	if err != nil {
		return nil, 0, err
	}
	var result []*backstage_model.AccountManagement
	err = g.DB().Model("r_talent_platform_table").
		Fields("tid", "p_account_id", "r_talent_platform_table.create_date",
			"examine_date", "fans_count", "home_page_url", "examine_state").Where(account.AccountManagement).OmitEmpty().Scan(&result)
	if err != nil {
		return nil, 0, err
	}
	num := len(result)
	return res, num, nil
}

func (a *accountService) FindTimeRange(account *backstage_model.RequestAccount) ([]*backstage_model.AccountManagement, int, error) {
	var res []*backstage_model.AccountManagement
	g.DB().SetDebug(true)
	err := g.DB().Model("r_talent_platform_table").
		LeftJoin("talent_info", "talent_info.id = r_talent_platform_table.tid").
		Fields("tid", "pid", "p_account_id", "talent_wx_nickname", "r_talent_platform_table.create_date",
			"examine_date", "fans_count", "home_page_url", "examine_state", "p_nickname").Where(account.RequestParam).
		OmitEmpty().
		WhereBetween("r_talent_platform_table.create_date",
			account.RequestParam.CreateDate[0]+" 00:00:00", account.RequestParam.CreateDate[1]+" 23:59:59").
		Page(account.IndexPage, account.PageSize).OrderDesc("r_talent_platform_table.create_date").Scan(&res)
	if err != nil {
		return nil, 0, err
	}
	var result []*backstage_model.AccountManagement
	err = g.DB().Model("r_talent_platform_table").
		LeftJoin("talent_info", "talent_info.id = r_talent_platform_table.tid").
		Fields("tid", "pid", "p_account_id", "talent_wx_nickname", "r_talent_platform_table.create_date",
			"examine_date", "fans_count", "home_page_url", "examine_state", "p_nickname").OmitEmpty().
		Where(account.RequestParam).WhereBetween("r_talent_platform_table.create_date",
		account.RequestParam.CreateDate[0]+" 00:00:00", account.RequestParam.CreateDate[1]+" 23:59:59").
		Scan(&result)
	if err != nil {
		return nil, 0, err
	}
	num := len(result)
	return res, num, nil
}

// ChangeListStatus 批量审核成功/或失败
func (a *accountService) ChangeListStatus(account *backstage_model.RequestAccountList) error {
	g.DB().SetDebug(true)
	_, err := g.DB().Model("r_talent_platform_table").
		Data(g.Map{"examine_state": account.Status, "examine_date": gtime.Now().Format("Y-m-d H:i:s"), "fail_reason": account.FailReason}).
		OmitEmpty().Where("Tid IN(?)", account.Tid).Where("p_account_id IN(?)", account.PAccountId).Update()
	if err != nil {
		return err
	}
	return nil
}

// FindFailReason 查询审核时间和失败原因
func (a *accountService) FindFailReason(pId int, pAccountId string) (*backstage_model.ResponseFailReason, error) {
	g.DB().SetDebug(true)
	var res *backstage_model.ResponseFailReason
	err := g.DB().Model("r_talent_platform_table").Where("p_id", pId).Where("tid", pAccountId).
		Scan(&res)
	if err != nil {
		return nil, err
	}
	return res, nil
}
