package marketing

import(
	"spaceofcheng/services/models/marketing"
	"spaceofcheng/services/utils"
	"spaceofcheng/services/service"
	"spaceofcheng/services/models/model"
	"github.com/astaxie/beego"
)

// MarketingServ 系统处理
type MarketingServ struct {
	ctx     *utils.Context
	dao     *marketing.MarketingDAO
	wdao    *marketing.WorkDAO
}

// NewMarketingServ 初始化
func NewMarketingServ(ctx *utils.Context) *MarketingServ {
	return &MarketingServ{
		ctx:     ctx,
		dao:     marketing.NewMarketingDAO(ctx),
	}
}

// GetCustWithWXList 获取客户列表
func (s *MarketingServ) GetMarketingList(page int, pageSize int) (map[string]interface{}, error) {

	if pageSize == 0 {
		pageSize = service.PAGENUM
	}
	if page == 0 {
		page = 1
	}

	list,err := s.dao.GetMarketingList(page,pageSize)
	if err != nil {
		beego.Error(err)
		return nil, err
	}

	total, err := s.dao.GetMarketingCount()

	return map[string]interface{}{
		"list":     list,
		"pagesize": pageSize,
		"pagenum":  total,
		"page":     page,
	}, err
}

// 保存 营销活动
/**
	* resource_desc 活动描述
	*/
func (s *MarketingServ) SaveMarketing(activity model.SysActivity,resourceDesc string) (*model.SysActivity,*model.SysActivityAction, error) {

	var newInfo *model.SysActivity
	var newWoke *model.SysActivityAction

	var err error
	// 存储 营销活动
	newInfo,err = s.dao.AddMarketing(&activity)
	if err != nil {
		return nil,nil, err
	}
	
	woke := model.SysActivityAction{}
	woke.ActivityId = newInfo.ActivityId
	woke.ActiveType = newInfo.ActivityType
	woke.ResourceDesc = resourceDesc

	// 存储 活动动作
	newWoke,err = s.wdao.AddWork(&woke)
	if err != nil {
		return nil,nil, err
	}

	return newInfo,newWoke,err

}

// GetMarketingById 根据ID查询数据
func (s *MarketingServ) GetMarketingById(activityId string) (*model.SysActivity,error){

	if activityId == "" {
		return nil,utils.LogError("不存在营销信息!")
	}

	info,err:= s.dao.GetMarketingById(activityId)
	if err != nil {
		return nil, err
	}
	if info == nil {
		return nil,utils.LogError("不存在营销信息!")
	}
	if info.Status == marketing.DELETE {
		return nil, utils.LogError("营销信息已被删除！")
	}

	return info,err

}

// DelMarketing 删除 根据ID删除数据
func (s *MarketingServ) DelMarketing(activityId string) error {

	if activityId == "" {
		return utils.LogError("没有对应的营销活动")
	}

	info,err := s.dao.GetMarketingById(activityId)
	if err != nil {
		return err
	}
	// 判断状态是否被删除
	if info.Status == marketing.DELETE {
		return utils.LogError("营销状态异常")
	}
	err = s.dao.DelMarketing(activityId,marketing.DELETE)
	return err;

}

// DelMarketing  根据ID 修改状态为 正常
func (s *MarketingServ) DelMarketingNormal(activityId string) error {

	if activityId == "" {
		return utils.LogError("没有对应的营销活动")
	}

	info,err := s.dao.GetMarketingById(activityId)
	if err != nil {
		return err
	}
	// 判断状态是否已经是正常
	if info.Status == marketing.NORMAL {
		return utils.LogError("营销状态异常")
	}
	err = s.dao.DelMarketing(activityId,marketing.NORMAL)
	return err;

}

// DelMarketing  根据ID 修改状态为 停用
func (s *MarketingServ) DelMarketingDisable(activityId string) error {

	if activityId == "" {
		return utils.LogError("没有对应的营销活动")
	}

	info,err := s.dao.GetMarketingById(activityId)
	if err != nil {
		return err
	}
	// 判断状态是否已经是停用
	if info.Status == marketing.DISABLE {
		return utils.LogError("营销状态异常")
	}
	err = s.dao.DelMarketing(activityId,marketing.DISABLE)
	return err;

}


// UpdateMarketing 修改 营销活动
func (s *MarketingServ) UpdateMarketing(activity model.SysActivity) error {
	if activity.ActivityId == "" {
		return utils.LogError("没有对应的营销活动")
	}
	err := s.dao.UpdateMarketing(activity)
	return err
}




