package backstage_service

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

var Finance = serviceFinance{}

type serviceFinance struct{}

// FindAllFinance 查询所有，分页查询
func (s *serviceFinance) FindAllFinance(finance *backstage_model.FinanceFind) ([]*backstage_model.FinanceFindAll, int, error) {
	g.DB().SetDebug(true)
	var finances []*backstage_model.FinanceFindAll
	var num int
	if finance.Finance == nil {
		// 提现单号表和达人信息表两张表联查，查询所有
		finances, num, err := s.findAll(finance)
		if err != nil {
			return nil, 0, err
		}
		return finances, num, nil
	} else {
		if len(finance.Finance.CompleteDate) == 0 && len(finance.Finance.SubmitDate) == 0 {
			finances, num, err := s.findAll(finance)
			if err != nil {
				return nil, 0, err
			}
			return finances, num, nil
		}
		// 存在提交日期，但不存在打款日期查询
		if len(finance.Finance.CompleteDate) == 0 && len(finance.Finance.SubmitDate) != 0 {
			finances, num, err := s.findSubmitDateAll(finance)
			if err != nil {
				return nil, 0, err
			}
			return finances, num, nil
		}
		// 存在打款日期，但不存在提交日期查询
		if len(finance.Finance.CompleteDate) != 0 && len(finance.Finance.SubmitDate) == 0 {
			finances, num, err := s.findUpdateTimeAll(finance)
			if err != nil {
				return nil, 0, err
			}
			return finances, num, nil
		}
		if len(finance.Finance.CompleteDate) != 0 && len(finance.Finance.SubmitDate) != 0 {
			finances, num, err := s.findSubmissionTimeAndUpdateTimeAll(finance)
			if err != nil {
				return nil, 0, err
			}
			return finances, num, nil
		}
	}

	return finances, num, nil
}

// ChangeListStatus 批量修改打款状态，可以进行多个id的同时修改，也可以单个id单独修改
func (s *serviceFinance) ChangeListStatus(finance *backstage_model.ChangeMoneyListStatus) error {
	_, err := g.DB().Model("bobocoin_withdrawal_record").Data(g.Map{"pay_state": finance.OrderState, "pay_fail_reason": finance.OrderMessage}).Where("record_id IN(?)", finance.RecordId).Update()
	return err
}

// 查询所有,并且排除时间的查询条件
func (s *serviceFinance) findAll(finance *backstage_model.FinanceFind) ([]*backstage_model.FinanceFindAll, int, error) {
	var finances []*backstage_model.FinanceFindAll
	g.DB().SetDebug(true)
	err := g.DB().Model("bobocoin_withdrawal_record record").
		LeftJoin("talent_info info", "record.talent_id=info.id").
		Fields("record_id,talent_wx_nickname,submit_date, draw_amount,pay_state, pay_fail_reason,pay_platform,account_name,pay_account, complete_date").
		OmitEmpty().
		Where(finance.Finance).
		Page(finance.IndexPage, finance.PageSize).OrderDesc("record_id").Scan(&finances)
	if err != nil {
		return nil, 0, err
	}
	// 去掉分页，查询数据库总数
	var financesNum []*backstage_model.FinanceFindAll
	err = g.DB().Model("bobocoin_withdrawal_record record").
		LeftJoin("talent_info info", "record.talent_id=info.id").
		Fields("record_id,talent_wx_nickname,submit_date, draw_amount,pay_state, pay_fail_reason,pay_platform,account_name,pay_account, complete_date").
		OmitEmpty().Where(finance.Finance).OrderDesc("record_id").Scan(&financesNum)
	if err != nil {
		return nil, 0, err
	}
	num := len(financesNum)
	return finances, num, nil
}

// 根据条件查询所有排除时间的条件查询，存在提交日期，但不存在打款日期查询
func (s *serviceFinance) findSubmitDateAll(finance *backstage_model.FinanceFind) ([]*backstage_model.FinanceFindAll, int, error) {
	var finances []*backstage_model.FinanceFindAll
	g.DB().SetDebug(true)
	err := g.DB().Model("bobocoin_withdrawal_record record").
		LeftJoin("talent_info info", "record.talent_id=info.id").
		Fields("record_id,talent_wx_nickname,submit_date, draw_amount,pay_state, pay_fail_reason,pay_platform,account_name,pay_account, complete_date").
		OmitEmpty().
		Where(finance.Finance).WhereOrBetween("submit_date",
		finance.Finance.SubmitDate[0]+" 00:00:00", finance.Finance.SubmitDate[1]+" 23:59:59").
		Page(finance.IndexPage, finance.PageSize).OrderDesc("record_id").Scan(&finances)
	if err != nil {
		return nil, 0, err
	}
	// 去掉分页，查询数据库总数
	var financesNum []*backstage_model.FinanceFindAll
	err = g.DB().Model("bobocoin_withdrawal_record record").
		LeftJoin("talent_info info", "record.talent_id=info.id").
		Fields("record_id,talent_wx_nickname,submit_date, draw_amount,pay_state, pay_fail_reason,pay_platform,account_name,pay_account, complete_date").
		OmitEmpty().
		Where(finance.Finance).WhereOrBetween("submit_date",
		finance.Finance.SubmitDate[0]+" 00:00:00", finance.Finance.SubmitDate[1]+" 23:59:59").
		OrderDesc("record_id").Scan(&financesNum)
	if err != nil {
		return nil, 0, err
	}
	num := len(financesNum)
	return finances, num, nil
}

// 根据条件查询所有排除时间的条件查询，存在打款日期，但不存在提交日期查询
func (s *serviceFinance) findUpdateTimeAll(finance *backstage_model.FinanceFind) ([]*backstage_model.FinanceFindAll, int, error) {
	var finances []*backstage_model.FinanceFindAll
	g.DB().SetDebug(true)
	err := g.DB().Model("bobocoin_withdrawal_record record").
		LeftJoin("talent_info info", "record.talent_id=info.id").
		Fields("record_id,talent_wx_nickname,submit_date, draw_amount,pay_state, pay_fail_reason,pay_platform,account_name,pay_account, complete_date").
		Where(finance.Finance).OmitEmpty().WhereOrBetween("complete_date",
		finance.Finance.CompleteDate[0]+" 00:00:00", finance.Finance.CompleteDate[1]+" 23:59:59").
		Page(finance.IndexPage, finance.PageSize).OrderDesc("record_id").Scan(&finances)
	if err != nil {
		return nil, 0, err
	}
	// 去掉分页，查询数据库总数
	var financesNum []*backstage_model.FinanceFindAll
	err = g.DB().Model("bobocoin_withdrawal_record record").
		LeftJoin("talent_info info", "record.talent_id=info.id").
		Fields("record_id,talent_wx_nickname,submit_date, draw_amount,pay_state, pay_fail_reason,pay_platform,account_name,pay_account, complete_date").
		Where(finance.Finance).OmitEmpty().WhereOrBetween("complete_date",
		finance.Finance.CompleteDate[0]+" 00:00:00", finance.Finance.CompleteDate[1]+" 23:59:59").
		OrderDesc("record_id").Scan(&financesNum)
	if err != nil {
		return nil, 0, err
	}
	num := len(financesNum)
	return finances, num, nil
}

// 根据条件查询所有排除时间的条件查询，存在提交日期，但不存在打款日期查询
func (s *serviceFinance) findSubmissionTimeAndUpdateTimeAll(finance *backstage_model.FinanceFind) ([]*backstage_model.FinanceFindAll, int, error) {
	var finances []*backstage_model.FinanceFindAll
	g.DB().SetDebug(true)
	err := g.DB().Model("bobocoin_withdrawal_record record").
		LeftJoin("talent_info info", "record.talent_id=info.id").
		Fields("record_id,talent_wx_nickname,submit_date, draw_amount,pay_state, pay_fail_reason,pay_platform,account_name,pay_account, complete_date").
		OmitEmpty().
		Where(finance.Finance).WhereOrBetween("submit_date",
		finance.Finance.SubmitDate[0]+" 00:00:00", finance.Finance.SubmitDate[1]+" 23:59:59").
		Where(finance.Finance).WhereOrBetween("complete_date",
		finance.Finance.CompleteDate[0]+" 00:00:00", finance.Finance.CompleteDate[1]+" 23:59:59").
		Page(finance.IndexPage, finance.PageSize).OrderDesc("record_id").Scan(&finances)
	if err != nil {
		return nil, 0, err
	}
	// 去掉分页，查询数据库总数
	var financesNum []*backstage_model.FinanceFindAll
	err = g.DB().Model("bobocoin_withdrawal_record record").
		LeftJoin("talent_info info", "record.talent_id=info.id").
		Fields("record_id,talent_wx_nickname,submit_date, draw_amount,pay_state, pay_fail_reason,pay_platform,account_name,pay_account, complete_date").
		OmitEmpty().
		Where(finance.Finance).WhereOrBetween("submit_date",
		finance.Finance.SubmitDate[0]+" 00:00:00", finance.Finance.SubmitDate[1]+" 23:59:59").
		Where(finance.Finance).WhereOrBetween("complete_date",
		finance.Finance.CompleteDate[0]+" 00:00:00", finance.Finance.CompleteDate[1]+" 23:59:59").
		Page(finance.IndexPage, finance.PageSize).OrderDesc("record_id").Scan(&financesNum)
	if err != nil {
		return nil, 0, err
	}
	num := len(financesNum)
	return finances, num, nil
}
