package jd

import (
	"git.rosy.net.cn/baseapi/platformapi/jdapi"
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"time"
)

func MemberCreateMsg(msg *jdapi.CallbackMemberMsg) (retVal *jdapi.CallbackResponse) {
	var (
		db = dao.GetDB()
	)
	//result, err := api.JdAPI.GetCommonMemberRegisteredInfo(msg.Billid)
	userMembers, _ := dao.GetUserMember(db, "", "", msg.Outbillid, model.VendorIDJD, 0, -1)
	if len(userMembers) > 0 {
		//应该是以前开过卡，后面没开了，现在又开了
		userMembers[0].DeletedAt = time.Now()
		dao.UpdateEntity(db, userMembers[0], "DeletedAt")
	}
	userMember := &model.UserMember{
		VendorOrderID: msg.Billid,
		VendorID:      model.VendorIDJD,
		Mobile:        msg.Outbillid,
		MemberType:    1,
	}
	dao.WrapAddIDCULDEntity(userMember, "jxadmin")
	userMember.CreatedAt = utils.Str2Time(msg.Timestamp)
	userMember.EndAt = utils.Str2Time(utils.Time2Str(userMember.CreatedAt.AddDate(0, 1, 0))[:len(utils.Time2Str(userMember.CreatedAt.AddDate(0, 1, 0)))-8] + "23:59:59")
	dao.CreateEntity(db, userMember)
	return jdapi.SuccessResponse
}

func MemberRenewMsg(msg *jdapi.CallbackMemberMsg) (retVal *jdapi.CallbackResponse) {
	return jdapi.SuccessResponse
}
