package course

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

// SaveCourseOrder 保存订单
func (m *CourseDAO) SaveCourseOrder(order model.TaCourseOrders) (*model.TaCourseOrders, error) {
	order.OrdersId = utils.GetGUID()
	// 当前订单号的随机方式 = 时间 + 个人ID
	order.OrdersNo = "Customer-" + time.Now().Local().Format("20060102150405") + "-" + strings.Join(utils.GUIID2IntString(order.CustomerId), "")
	order.CreateDate = time.Now()
	_, err := m.db.Insert(&order)
	return &order, err
}

// GetOrderByID 根据id获取订单明细
func (m *CourseDAO) GetOrderByID(ordersid string) (*model.TaCourseOrders, error) {
	var orders []model.TaCourseOrders
	err := m.db.Where("orders_id=?", ordersid).Find(&orders)
	if err != nil {
		return nil, err
	}
	if len(orders) > 0 {
		return &orders[0], nil
	}
	return nil, nil
}

// SaveCourseOrderDetail 保存订单明细
func (m *CourseDAO) SaveCourseOrderDetail(courseid, ordersid string) error {
	sql := `insert into (orders_detail_id,orders_id,course_id,course_detail_id,begin_date,end_date) select UUID(),'` + ordersid +
		`',course_id,detail_id,begin_date,end_date from ta_course_detail where course_id='` + courseid + `' and status>` + strconv.Itoa(models.STATUS_DEL)
	_, err := m.db.Insert(sql)
	return err
}

//
