package service

import (
	"context"
	"fmt"
	log "github.com/sirupsen/logrus"
	"time"
	"youngee_b_api/db"
	"youngee_b_api/model/gorm_model"
	"youngee_b_api/model/http_model"
)

var SubAccount *subaccount

type subaccount struct {
}

// CreateSubAccount 新增子账号
func (*subaccount) CreateSubAccount(ctx context.Context, request http_model.AddNewSubAccountRequest) (*gorm_model.YounggeeSubAccount, error) {
	user := gorm_model.YounggeeUser{
		Phone:         request.PhoneNumber,
		User:          "1002",
		Username:      request.PhoneNumber,
		Password:      "1002",
		RealName:      "",
		Role:          "7",
		Email:         "",
		LastLoginTime: time.Now().UTC().Local(),
	}
	userId, err := db.CreateUser(ctx, user)
	if err != nil {
		log.Infof("[CreateEnterpriseSubUser] fail,err:%+v", err)
		return nil, err
	}
	fmt.Println("userId: ", userId)
	var curr = int(*userId)

	var newSubAccount = gorm_model.YounggeeSubAccount{
		PhoneNumber:    request.PhoneNumber,
		SubAccountName: request.SubAccountName,
		JobId:          request.JobId,
		SupplierId:     request.SupplierId,
		AccountStatus:  1,
		UserId:         curr,
		SubAccountType: 3,
	}
	err1 := db.CreateSubAccount(ctx, newSubAccount)
	if err1 != nil {
		return nil, err1
	}
	return &newSubAccount, nil
}

// UpdateSubAccount 修改子账号
func (*subaccount) UpdateSubAccount(ctx context.Context, request http_model.UpdateJobRequest) error {
	var newSubAccount = gorm_model.YounggeeSubAccount{}
	err := db.UpdateSubAccount(ctx, newSubAccount)
	if err != nil {
		return err
	}
	return nil
}

// DeleteSubAccount 删除子账号
func (*subaccount) DeleteSubAccount(ctx context.Context, request http_model.DeleteJobRequest) error {
	var newSubAccount = gorm_model.YounggeeSubAccount{
		JobId: request.JobId,
	}
	err := db.DeleteSubAccount(ctx, newSubAccount)
	if err != nil {
		return err
	}
	return nil
}
