// @Description   订阅设置相关
package service

import (
	"errors"
	"fmt"
	"jyBXSubscribe/entity"
	IC "jyBXSubscribe/rpc/init"
	"strconv"

	"app.yhyue.com/moapp/jybase/common"
	"app.yhyue.com/moapp/jybase/mongodb"
	"github.com/zeromicro/go-zero/core/logx"
)

//
type SubseribeService struct {
	Mgo          mongodb.MongodbSim
	UserId       string //mongodb 的用户id
	Types        string //订阅信息产品类型 超级订阅、大会员、商机管理、免费订阅...
	PositionType int64
	*entity.SubscribeUpdate
}

// @description  	订阅设置相关修改
// @param     		SubseribeService  用户信息相关
// @return    		int64 是否成功 1成功 -1失败
// @return    		error 错误信息
//TODO 后期需要实现 查询各个身份的订阅设置修改[目前只实现了超级订阅]
func (this *SubseribeService) Update() (int64, error) {
	if this.UserId == "" {
		return -1, errors.New(entity.UPDATE_ERROR_MSG)
	}
	//查询条件
	//查询字段
	fields := `{"i_vip_status":1,"o_vipjy":1}`
	//查询用户信息
	fmt.Println("111111", IC.Compatible)
	r := IC.Compatible.Select(this.UserId, fields)
	if r == nil || len(*r) == 0 {
		logx.Error("未找到用户")
		return -1, errors.New(entity.UPDATE_ERROR_MSG)
	}
	//判断用户权益
	vip_status := common.Int64All((*r)["i_vip_status"])
	if vip_status <= 0 {
		logx.Error("用户暂无权益")
		return -1, errors.New(entity.UPDATE_ERROR_MSG)
	}
	//权益通过，获取要修改的参数
	setMap := map[string]interface{}{}
	if this.Area != nil {
		setMap["o_vipjy.o_area"] = this.Area
	}
	if this.Buyerclass != nil {
		setMap["o_vipjy.a_buyerclass"] = this.Buyerclass
	}
	if this.Items != nil {
		setMap["o_vipjy.a_items"] = this.Items
	}
	if this.Infotype != nil {
		setMap["o_vipjy.a_infotype"] = this.Infotype
	}
	if this.Matchway != "" {
		i_matchway, _ := strconv.Atoi(this.Matchway)
		setMap["o_vipjy.i_matchway"] = i_matchway
	}
	if this.Projectmatch != "" {
		i_projectmatch, _ := strconv.Atoi(this.Projectmatch)
		setMap["o_vipjy.i_projectmatch"] = i_projectmatch
	}
	if this.Ratemode != "" {
		i_ratemode, _ := strconv.Atoi(this.Ratemode)
		setMap["o_vipjy.i_ratemode"] = i_ratemode
	}
	if this.Apppush != "" {
		i_apppush, _ := strconv.Atoi(this.Apppush)
		setMap["o_vipjy.i_apppush"] = i_apppush
	}
	if this.Mailpush != "" {
		i_mailpush, _ := strconv.Atoi(this.Mailpush)
		setMap["o_vipjy.i_mailpush"] = i_mailpush
	}
	if this.Mail != "" {
		setMap["o_vipjy.s_email"] = this.Mail
	}
	if this.Otherbuyerclass != "" {
		i_otherbuyerclass, _ := strconv.Atoi(this.Otherbuyerclass)
		setMap["o_vipjy.i_matchbuyerclass_other"] = i_otherbuyerclass
	}
	if ok := IC.Compatible.Update(this.UserId, map[string]interface{}{
		"$set": setMap,
	}); ok {
		return 1, nil
	}
	return -1, errors.New(entity.UPDATE_ERROR_MSG)
}
