package vipcard

import (
	"spaceofcheng/services/controllers"
	"spaceofcheng/services/models/model"
	"spaceofcheng/services/service/vipcard"
	"time"
)

// CaseController 信息
type VipcardController struct {
	dao *vipcard.VipcardServ
	controllers.BaseController
}

// Constructor 初始化 Controller
// @Title Constructor
// @Description 初始化 Controller, 系统自动调用
func (c *VipcardController) Constructor() {
	c.dao = vipcard.NewVipcardServ(c.Context)
}

// GetVipList 获取VIP卡列表
func (c *VipcardController) GetVipList() {
	cases := c.Context.Get("cases").([]model.SysUserCase)
	caseId := c.GetCaseIDs(cases)
	page, _ := c.GetInt("page")
	pageSize, _ := c.GetInt("pagesize")
	list, err := c.dao.GetVipChild(caseId, page, pageSize)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(list)
}

// AddVipCard 新增VIP卡
func (c *VipcardController) AddVipCard() {
	vipcard := model.TaVipCard{}
	if err := c.ParseForm(&vipcard); err != nil {
		c.ResponseError(err)
	}
	newVipCard, err := c.dao.AddVipCard(vipcard)
	if err != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON(newVipCard)

}

// userCharge 用户激活vip卡
func (c *VipcardController) UserCharge() {
	vipCode := c.GetString(":vipCardChildCode")
	tel := c.GetString(":tel")
	var createUser = c.Context.Get("user").(model.SysUser)
	vipChild, err := c.dao.GetVipChildByCode(vipCode)
	if err != nil {
		c.ResponseError(err)
	}

	customer, err := c.dao.FindCustomerByTel(tel)
	if err != nil {
		c.ResponseError(err)
	}
	vipChild.CustomerId = customer.CustomerId
	vipChild.CustomerTel = tel
	vipChild.ActiveDate = time.Now()
	err1 := c.dao.UpdateVipChild(vipChild)
	if err1 != nil {
		c.ResponseError(err)
	}
	err2 := c.dao.CustomerCharge(vipChild, createUser)
	if err2 != nil {
		c.ResponseError(err)
	}
	c.ResponseJSON("激活成功")

}
