package service

import (
	"XT_New/models"
	"github.com/jinzhu/gorm"
	"strconv"
	"time"
)

func FindServiceSubscibeByOrgId(orgId int64) (*models.ServeSubscibe, error) {
	subscibe := models.ServeSubscibe{}
	err = readUserDb.Model(&models.ServeOrder{}).Preload("Org", "status=1").Where("org_id=? and status=1", orgId).First(&subscibe).Error

	var count int64
	err = readDb.Model(&models.Patients{}).Where("user_org_id=? and status = 1", orgId).Count(&count).Error
	subscibe.Patients = count

	return &subscibe, err
}

func FindOrgAppByOrgId(orgId int64) (*models.OrgApp, error) {
	app := models.OrgApp{}
	err = readUserDb.Model(&models.OrgApp{}).Where("org_id=? and status=1", orgId).First(&app).Error
	return &app, err
}

func FindAllProduct() ([]*models.ServeProduct, error) {
	product := []*models.ServeProduct{}
	err := readUserDb.Model(&models.ServeProduct{}).Where("status=1").Find(&product).Error
	return product, err
}

func FindProductByID(id int64) (*models.ServeProduct, error) {
	var product models.ServeProduct
	err := readUserDb.Where("id = ?  and status=1", id).First(&product).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}
	return &product, err
}

func CreateOrderRecord(order *models.ServeOrder) {
	writeUserDb.Create(order)
}
func CreateOrderInfomation(info *models.ServeOrderInfomation) {
	writeUserDb.Create(info)
}

func FindOrderInfomationByID(orderNumber string, orgId int64) (*models.ServeOrderInfomation, error) {
	var order models.ServeOrderInfomation
	err := readUserDb.Model(&models.ServeOrderInfomation{}).Where("order_number = ? and org_id=? and status=1", orderNumber, orgId).First(&order).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}

	if err != nil {
		return nil, err
	}
	return &order, err

}

func FindServeOrderByID(orgId, id int64) (*models.ServeOrder, error) {
	var order models.ServeOrder
	err := readUserDb.Model(&models.ServeOrder{}).Where("id = ? and org_id=? and status=1", id, orgId).First(&order).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}

	if err != nil {
		return nil, err
	}
	return &order, err
}

func UpdateOrderPayType(orgId int64, id int64) error {
	err := writeUserDb.Model(&models.ServeOrder{}).Where("id = ? and org_id=? and status=1", id, orgId).Updates(map[string]interface{}{"pay_type": 2}).Error
	return err
}

func UpdateOrderStatus(orgId int64, id int64) error {
	err := writeUserDb.Model(&models.ServeOrder{}).Where("id = ? and org_id=? and status=1", id, orgId).Updates(map[string]interface{}{"order_status": 3}).Error
	return err
}

func GetOrderList(orgId int64) ([]*models.ServeOrder, error) {
	var orders []*models.ServeOrder
	err := readUserDb.Model(&models.ServeOrder{}).Where("org_id=? and status=1", orgId).Order("created_time desc").Find(&orders).Error
	for _, order := range orders {
		tm := time.Unix(order.OrderExpireTime, 0)
		order.OrderExpireTimeFormat = tm.Format("2006-01-02 15:04:05")
	}

	return orders, err
}

func FindServeOrderByOrderNumber(orderNumber string) (*models.ServeOrder, error) {
	var order models.ServeOrder
	err := readUserDb.Model(&models.ServeOrder{}).Where(" status=1 AND order_number = ? ", orderNumber).First(&order).Error
	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}

	if err != nil {
		return nil, err
	}
	return &order, err
}

func GetHetong(orgID, orderId int64) (*models.ServeOrderContract, error) {
	var contract models.ServeOrderContract
	var err error
	err = readUserDb.Model(&models.ServeOrderContract{}).Where("order_id=? and org_id=? and status=1", orderId, orgID).First(&contract).Error

	if err == gorm.ErrRecordNotFound {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}
	return &contract, nil
}

func CreateHetong(m *models.ServeOrderContract) (err error) {
	err = writeUserDb.Create(m).Error
	return
}

func UpdateOrder(orderNumber string, Quantity int64, orgId int64, payment_transaction_id string, startTimes int64, endTimes int64) {
	var startTime int64
	var endTime int64

	if startTimes == 0 && endTimes == 0 {
		startTime = time.Now().Unix()
		endTime = startTime + ((12 * 30 * 24 * 3600) * Quantity)
	} else {

		endTime = endTimes + ((12 * 30 * 24 * 3600) * Quantity)

	}

	tm := time.Unix(endTime, 0)
	serve_duration := strconv.FormatInt(12*Quantity, 10) + "月" + "到期服务时间" + tm.Format("2006-01-02 15:04:05")
	writeUserDb.Model(&models.ServeOrder{}).Where("order_number = ? and status=1", orderNumber).Updates(map[string]interface{}{"order_status": 2, "pay_type": 1, "period_start": startTime, "period_end": endTime, "serve_duration": serve_duration, "pay_time": time.Now().Unix(), "payment_transaction_id": payment_transaction_id})
	writeUserDb.Model(&models.ServeSubscibe{}).Where("org_id = ? and status=1", orgId).Updates(map[string]interface{}{"period_start": startTime, "period_end": endTime, "updated_time": time.Now().Unix(), "state": 1})
}
