package course

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

	"github.com/yl10/kit/guid"
)

// SaveCourseOrder 保存订单
func (m *CourseDAO) SaveCourseOrder(order *model.TaCourseOrders) error {
	order.OrdersId = guid.NewGUIDString()
	// 当前订单号的随机方式 = 时间 + 个人ID
	order.OrdersNo = "C-" + time.Now().Local().Format("20060102150405") + "-" + strings.Join(utils.GUIID2IntString(order.CustomerId), "")
	order.CreateDate = time.Now()
	_, err := m.db.Insert(order)
	return 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(details []model.TaCourseOrdersDetail) error {
	for i, d := range details {
		d.OrdersDetailId = guid.NewGUIDString()

		details[i] = d
	}

	_, err := m.db.Insert(details)
	return err
}
