package verify

import (
	"spaceofcheng/services/models"
	"spaceofcheng/services/models/model"
	"spaceofcheng/services/utils"
	"strconv"
	"time"

	"github.com/go-xorm/xorm"
)

// VerifyDAO 当前数据库操作对象
type VerifyDAO struct {
	ctx *utils.Context
	db  *xorm.Session
}

// NewVerifyDAO New Inst
func NewVerifyDAO(ctx *utils.Context) *VerifyDAO {
	return &VerifyDAO{
		ctx: ctx,
		db:  ctx.DB,
	}
}

type verifyCourse struct {
	targetId     string
	caseId       string
	caseName     string
	locationName string
	courseName   string
	detailName   string
	beginDate    time.Time
	endDate      time.Time
	courseType   string
}

func (m *VerifyDAO) GetExperienceCardVerifyList(customerCardId string, page, pageSize int) ([]verifyCourse, error) {
	var course []verifyCourse
	sql := `SELECT
	a.target_id,
	g.case_name,
	c.card_id,
	f.location_name,
	e.course_name,
	d.detail_name,
	d.begin_date,
	d.end_date,
	'card' as courseType 
FROM
	ta_coupon_card_target a
	INNER JOIN ta_coupon_card b ON a.card_id = b.card_id
	INNER JOIN ta_customer_card c ON c.card_id = b.card_id
	INNER JOIN ta_course_detail d ON d.detail_id = a.detail_id
	INNER JOIN ta_course e ON d.course_id = e.course_id
	INNER JOIN td_cms_image_location f ON e.location_id = f.location_id
	INNER JOIN sys_case g ON g.case_id = c.case_id 
WHERE
	DATE_FORMAT( d.begin_date, '%Y-%m-%d' ) = DATE_FORMAT( NOW(), '%Y-%m-%d' )
	AND c.customer_card_id = ` + customerCardId
	sql = sql + " limit  " + strconv.Itoa((page-1)*pageSize) + ", " + strconv.Itoa(pageSize)
	err := m.db.Sql(sql).Find(&course)
	return course, err
}

func (m *VerifyDAO) GetExperienceCardVerifyListCount(customerCardId string) (int, error) {
	var course []verifyCourse
	sql := `SELECT
	a.target_id,
	g.case_name,
	c.card_id,
	f.location_name,
	e.course_name,
	d.detail_name,
	d.begin_date,
	d.end_date,
	'card' as courseType 
FROM
	ta_coupon_card_target a
	INNER JOIN ta_coupon_card b ON a.card_id = b.card_id
	INNER JOIN ta_customer_card c ON c.card_id = b.card_id
	INNER JOIN ta_course_detail d ON d.detail_id = a.detail_id
	INNER JOIN ta_course e ON d.course_id = e.course_id
	INNER JOIN td_cms_image_location f ON e.location_id = f.location_id
	INNER JOIN sys_case g ON g.case_id = c.case_id 
WHERE
	DATE_FORMAT( d.begin_date, '%Y-%m-%d' ) = DATE_FORMAT( NOW(), '%Y-%m-%d' )
	AND c.customer_card_id = ` + customerCardId
	err := m.db.Sql(sql).Find(&course)
	return len(course), err
}

func (m *VerifyDAO) GetOrderCourseList(courseOrderId string, page, pageSize int) ([]verifyCourse, error) {
	var course []verifyCourse
	sql := `SELECT
	a.orders_detail_id AS target_id,
	b.case_id,
	f.case_name,
	e.location_name,
	c.course_name,
	d.detail_name,
	a.begin_date,
	a.end_date,
	'order' as courseType
FROM
	ta_course_orders_detail a
	INNER JOIN ta_course_orders b ON a.orders_id = b.orders_id
	INNER JOIN ta_course c ON b.course_id = c.course_id
	INNER JOIN ta_course_detail d ON a.course_detail_id = d.detail_id
	INNER JOIN td_cms_image_location e ON e.location_id = c.location_id
	INNER JOIN sys_case f ON b.case_id = f.case_id 
WHERE
	DATE_FORMAT( a.begin_date, '%Y-%m-%d' ) = DATE_FORMAT( NOW( ), '%Y-%m-%d' )
	AND b.orders_id = ` + courseOrderId
	sql = sql + " limit  " + strconv.Itoa((page-1)*pageSize) + ", " + strconv.Itoa(pageSize)
	err := m.db.Sql(sql).Find(&course)
	return course, err

}

func (m *VerifyDAO) GetOrderCourseListCount(courseOrderId string) (int, error) {
	var course []verifyCourse
	sql := `SELECT
	a.orders_detail_id AS target_id,
	b.case_id,
	f.case_name,
	e.location_name,
	c.course_name,
	d.detail_name,
	a.begin_date,
	a.end_date,
	'order' as courseType
FROM
	ta_course_orders_detail a
	INNER JOIN ta_course_orders b ON a.orders_id = b.orders_id
	INNER JOIN ta_course c ON b.course_id = c.course_id
	INNER JOIN ta_course_detail d ON a.course_detail_id = d.detail_id
	INNER JOIN td_cms_image_location e ON e.location_id = c.location_id
	INNER JOIN sys_case f ON b.case_id = f.case_id 
WHERE
	DATE_FORMAT( a.begin_date, '%Y-%m-%d' ) = DATE_FORMAT( NOW( ), '%Y-%m-%d' )
	AND b.orders_id = ` + courseOrderId
	err := m.db.Sql(sql).Find(&course)
	return len(course), err
}

func (m *VerifyDAO) GetCourseListByTel(tel string, page, pageSize int) ([]verifyCourse, error) {
	var course []verifyCourse
	sql := `SELECT
	a.target_id,
	g.case_name,
	c.card_id,
	f.location_name,
	e.course_name,
	d.detail_name,
	d.begin_date,
	d.end_date,
	'card' as courseType 
FROM
	ta_coupon_card_target a
	INNER JOIN ta_coupon_card b ON a.card_id = b.card_id
	INNER JOIN ta_customer_card c ON c.card_id = b.card_id
	INNER JOIN ta_course_detail d ON d.detail_id = a.detail_id
	INNER JOIN ta_course e ON d.course_id = e.course_id
	INNER JOIN td_cms_image_location f ON e.location_id = f.location_id
	INNER JOIN sys_case g ON g.case_id = c.case_id 
	INNER JOIN ta_customer h ON c.customer_id = h.customer_id
WHERE
	DATE_FORMAT( d.begin_date, '%Y-%m-%d' ) = DATE_FORMAT( NOW(), '%Y-%m-%d' )
	AND h.phone = ` + tel + `
	union all
	SELECT
	a.orders_detail_id AS target_id,
	b.case_id,
	f.case_name,
	e.location_name,
	c.course_name,
	d.detail_name,
	a.begin_date,
	a.end_date,
	'order' as courseType
FROM
	ta_course_orders_detail a
	INNER JOIN ta_course_orders b ON a.orders_id = b.orders_id
	INNER JOIN ta_course c ON b.course_id = c.course_id
	INNER JOIN ta_course_detail d ON a.course_detail_id = d.detail_id
	INNER JOIN td_cms_image_location e ON e.location_id = c.location_id
	INNER JOIN sys_case f ON b.case_id = f.case_id 
	INNER JOIN ta_customer g ON b.customer_id = g.customer_id
WHERE
	DATE_FORMAT( a.begin_date, '%Y-%m-%d' ) = DATE_FORMAT( NOW( ), '%Y-%m-%d' )
	AND g.phone = ` + tel
	sql = sql + " limit  " + strconv.Itoa((page-1)*pageSize) + ", " + strconv.Itoa(pageSize)
	err := m.db.Sql(sql).Find(&course)
	return course, err
}

func (m *VerifyDAO) GetCourseListByTelCount(tel string) (int, error) {
	var course []verifyCourse
	sql := `SELECT
	a.target_id,
	g.case_name,
	c.card_id,
	f.location_name,
	e.course_name,
	d.detail_name,
	d.begin_date,
	d.end_date,
	'card' as courseType 
FROM
	ta_coupon_card_target a
	INNER JOIN ta_coupon_card b ON a.card_id = b.card_id
	INNER JOIN ta_customer_card c ON c.card_id = b.card_id
	INNER JOIN ta_course_detail d ON d.detail_id = a.detail_id
	INNER JOIN ta_course e ON d.course_id = e.course_id
	INNER JOIN td_cms_image_location f ON e.location_id = f.location_id
	INNER JOIN sys_case g ON g.case_id = c.case_id 
	INNER JOIN ta_customer h ON c.customer_id = h.customer_id
WHERE
	DATE_FORMAT( d.begin_date, '%Y-%m-%d' ) = DATE_FORMAT( NOW(), '%Y-%m-%d' )
	AND h.phone = ` + tel + `
	union all
	SELECT
	a.orders_detail_id AS target_id,
	b.case_id,
	f.case_name,
	e.location_name,
	c.course_name,
	d.detail_name,
	a.begin_date,
	a.end_date,
	'order' as courseType
FROM
	ta_course_orders_detail a
	INNER JOIN ta_course_orders b ON a.orders_id = b.orders_id
	INNER JOIN ta_course c ON b.course_id = c.course_id
	INNER JOIN ta_course_detail d ON a.course_detail_id = d.detail_id
	INNER JOIN td_cms_image_location e ON e.location_id = c.location_id
	INNER JOIN sys_case f ON b.case_id = f.case_id 
	INNER JOIN ta_customer g ON b.customer_id = g.customer_id
WHERE
	DATE_FORMAT( a.begin_date, '%Y-%m-%d' ) = DATE_FORMAT( NOW( ), '%Y-%m-%d' )
	AND g.phone = ` + tel
	err := m.db.Sql(sql).Find(&course)
	return len(course), err
}

func (m *VerifyDAO) AddExperienceCardVerification(verify model.TaExperienceCardVerification) (*model.TaExperienceCardVerification, error) {
	verify.ExperienceCardVerifyTime = time.Now()
	verify.Status = models.STATUS_NORMAL
	verify.ExperienceCardVerifyId = utils.GetGUID()
	_, err := m.db.Insert(verify)
	return &verify, err
}

func (m *VerifyDAO) updateExperienceCardTarget(verify model.TaCouponCardTarget) error {
	var cols = []string{
		"card_state",
	}
	_, err := m.db.Cols(cols...).Where("target_id=?", verify.TargetId).Update(verify)
	return err
}

func (m *VerifyDAO) updateCustomerExperienceCard(verify model.TaCustomerCard) error {
	var cols = []string{
		"card_state",
		"join_num",
		"verify_date",
	}
	_, err := m.db.Cols(cols...).Where("customer_card_id=?", verify.CustomerCardId).Update(verify)
	return err
}

func (m *VerifyDAO) updateCustomerOrderCourse(verify model.TaCourseOrders) error {
	var cols = []string{
		"join_num",
	}
	_, err := m.db.Cols(cols...).Where("orders_id = ?", verify.OrdersId).Update(verify)
	return err
}

func (m *VerifyDAO) AddOrdersVerification(verify model.TaCourseVerifcation) (*model.TaCourseVerifcation, error) {
	verify.CreateDate = time.Now()
	verify.VerifcationId = utils.GetGUID()
	verify.Status = models.STATUS_NORMAL
	_, err := m.db.Insert(verify)
	return &verify, err

}
