package verify

import (
	"spaceofcheng/services/models"
	"spaceofcheng/services/models/model"
	"spaceofcheng/services/models/verify"
	"spaceofcheng/services/service"
	"spaceofcheng/services/utils"
	"time"

	"github.com/astaxie/beego"
)

// CaseServ 系统处理
type VerifyServ struct {
	ctx *utils.Context
	dao *verify.VerifyDAO
}

// NewVerifyServ 初始化
func NewVerifyServ(ctx *utils.Context) *VerifyServ {
	return &VerifyServ{
		ctx: ctx,
		dao: verify.NewVerifyDAO(ctx),
	}
}

// GetCustomerCourseDetailById 根据ID获取当日可核销课程
func (s *VerifyServ) GetCustomerCourseDetailById(id, caseid string, page, pageSize int) (map[string]interface{}, error) {
	if err := utils.NewAuthEngine(s.ctx).CheckCase(caseid); err != nil {
		return nil, err
	}
	if pageSize == 0 {
		pageSize = service.PAGENUM
	}
	if page == 0 {
		page = 1
	}
	customerDetail, err := s.dao.GetCustomerCourseList(id, caseid, page, pageSize)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	total, err := s.dao.GetCustomerCourseListCount(id, caseid)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	return map[string]interface{}{
		"list":     customerDetail,
		"pagesize": pageSize,
		"pagenum":  total,
		"page":     page,
	}, err
}

// GetCustomerCourseDetailByTel根据电话号码获取当日可核销课程
func (s *VerifyServ) GetCustomerCourseDetailByTel(tel, caseid string, page, pageSize int) (map[string]interface{}, error) {
	if err := utils.NewAuthEngine(s.ctx).CheckCase(caseid); err != nil {
		return nil, err
	}
	if pageSize == 0 {
		pageSize = service.PAGENUM
	}
	if page == 0 {
		page = 1
	}
	customerDetail, err := s.dao.GetCustomerCourseListByTel(tel, caseid, page, pageSize)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	total, err := s.dao.GetCustomerCourseListByTelCount(tel, caseid)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	return map[string]interface{}{
		"list":     customerDetail,
		"pagesize": pageSize,
		"pagenum":  total,
		"page":     page,
	}, err

}
func (s *VerifyServ) GetCustomerCourseDetailByDetailId(id string) (*model.TaCustomerCourseDetail, error) {
	customerDetail, err := s.dao.GetCustomerCourseDetailById(id)
	return customerDetail, err
}

// UpdateCustomerCourseDetail更新我的课程详情
func (s *VerifyServ) UpdateCustomerCourseDetail(customerDetail *model.TaCustomerCourseDetail, userid string) error {
	var err error
	customerDetail.VerifyDate = time.Now()
	customerDetail.VerifyUser = userid
	if time.Now().After(customerDetail.EndDate) {
		customerDetail.VerifyStatus = "late"
	} else {
		customerDetail.VerifyStatus = "used"
	}
	err = s.dao.UpdateCustomerCourseDetail(customerDetail)
	return err
}

// UpdateCustomerCard 更新我的体验卡
func (s *VerifyServ) UpdateCustomerCard(customerCard *model.TaCustomerCard) error {
	var err error
	customerCard.VerifyDate = time.Now()
	err = s.dao.UpdateCustomerCard(customerCard)
	return err

}

// UpdateCustomerCourse 更新我的课程
func (s *VerifyServ) UpdateCustomerCourse(customerCourse *model.TaCustomerCourse) error {
	var err error
	customerCourse.JoinNum = customerCourse.JoinNum + 1
	if customerCourse.JoinNum >= customerCourse.CourseNum {
		customerCourse.IsDone = models.BOOL_TRUE
	}
	err = s.dao.UpdateCustomerCourse(customerCourse)
	return err

}

// GetCustomerCardByDetailId 根据我的课程详情id获取体验卡
func (s *VerifyServ) GetCustomerCardByDetailId(detailId string) (*model.TaCustomerCard, error) {
	customerCard, err := s.dao.GetCustomerCardByDetailId(detailId)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	return customerCard, err
}

// GetCustomerCourseByDetailId 根据我的课程详情id获取我的课程
func (s *VerifyServ) GetCustomerCourseByDetailId(detailId string) (*model.TaCustomerCourse, error) {
	customerCourse, err := s.dao.GetCustomerCourseByDetailId(detailId)
	if err != nil {
		beego.Error(err)
		return nil, err
	}
	return customerCourse, err
}
