package marketing

import(
	"spaceofcheng/services/models/model"
	"spaceofcheng/services/utils"
	"strconv"
	"time"
	"github.com/go-xorm/xorm"
)

// 活动类型
const (
	NEW_ACTIVITIES = 0 // 注册赠券

)

// 状态
const (
	DELETE = -1 // 删除
	NORMAL = 0 // 正常
	DISABLE = 1 // 停用

)

// 赠券卡券
const (
	drink = 1 // 饮品通用卷
	course = 2 // 课程通用卷
	petty_diplomat = 3 // 小小外交官通用卷
)

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

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

// MarketingInfo 课程
type MarketingInfo struct {
	model.SysActivity `xorm:"extends"`
	Actions []model.SysActivityAction
}

// GetMarketingList 查询所有
func (m *MarketingDAO) GetMarketingList(page int, pageSize int) ([]MarketingInfo ,error){

	var news []MarketingInfo
	sql := `select * from sys_activity new order by new.create_date desc limit ` + strconv.Itoa((page-1)*pageSize) + ", " + strconv.Itoa(pageSize)
	err := m.db.Sql(sql).Find(&news)
	return news, err

}

// GetMarketingCount 获取总数
func (m *MarketingDAO) GetMarketingCount()(int64, error){
	var total []int64
	sql := `select count(*) as total from sys_activity`
	err := m.db.Sql(sql).Cols("total").Find(&total)
	if err!=nil{

	}
	return total[0], err
}


// AddMarketing 添加
func (m *MarketingDAO) AddMarketing(activity *model.SysActivity) (*model.SysActivity, error) {
	activity.ActivityId = utils.GetGUID()
	activity.CreateDate = time.Now()
	//activity.Status = models.STATUS_NORMAL
	activity.Status = NORMAL // 状态为正常
	org := m.ctx.Get("org").(model.SysOrg)
	activity.OrgId = org.OrgId
	caseinfo := m.ctx.Get("currentCase").(model.SysCase)
	activity.CaseId = caseinfo.CaseId
	//activity.CaseId = user.

	_, err := m.db.Insert(activity)

	// // 添加活动动作数据
	// var new model.SysActivityAction
	// new.ActionId = activity.ActivityId
	// new.ActiveType = activity.ActivityType
	return activity, err
}


// GetMarketingById 根据ID查询活动营销
func (m *MarketingDAO) GetMarketingById(activityId string) (*model.SysActivity,error){

	var activity []model.SysActivity
	err := m.db.Where("activity_id=?",activityId).Find(&activity)
	if err != nil {
		return nil, err
	}
	if len(activity) >0 {
		return &activity[0],nil
	}
	return nil,nil
	
}

// DelMarketing 修改状态 营销活动(删除,启用,停用)
/**
 *	DELETE = -1 // 删除
		NORMAL = 0 // 正常
		DISABLE = 1 // 停用
 **/
func (m *MarketingDAO) DelMarketing(activityId string,status int) error {

	var info = model.SysActivity{
		ActivityId: activityId,
		// Status:   models.STATUS_DEL,
		Status:   status,
	}
	var cols = []string{
		"status",
	}
	_, err := m.db.Cols(cols...).Where("activity_id=?", activityId).Update(info)
	return err
}


// UpdateMarketing 修改 营销活动
func (m *MarketingDAO) UpdateMarketing(activity model.SysActivity) error {
	// var cols = []string{
	// 	"activity_name",
	// 	"activity_type",
	// 	"case_id",
	// 	"org_id",
	// 	"case_id",
	// }

	var cols = []string{
		"activity_name",
		"activity_type",
	}
	_, err := m.db.Cols(cols...).Where("action_id=?", activity.ActivityId).Update(activity)
	return err
}





