package cygx

import (
	"errors"
	"fmt"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/services/alarm_msg"
	"hongze/hz_crm_api/utils"
	"time"
)

// 添加更新日志记录
func AddCygxUserYanxuanPermissionLog(item *cygx.CygxUserYanxuanPermission) {
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg(fmt.Sprint(item, err.Error()), 2)
		}
	}()

	itemLog := new(cygx.CygxUserYanxuanPermissionLog)
	itemLog.Status = item.Status
	itemLog.UserId = item.UserId
	itemLog.CompanyId = item.CompanyId
	itemLog.CompanyName = item.CompanyName
	itemLog.StartDate = item.StartDate
	itemLog.EndDate = item.EndDate
	itemLog.Mobile = item.Mobile
	itemLog.Email = item.Email
	itemLog.RealName = item.RealName
	itemLog.AdminId = item.AdminId
	itemLog.AdminRealName = item.AdminRealName
	itemLog.CreatedTime = time.Now()
	itemLog.LastUpdatedTime = time.Now()
	itemLog.ModifyTime = time.Now()
	_, e := cygx.AddCygxUserYanxuanPermissionLog(itemLog)
	if e != nil {
		err = errors.New("AddCygxUserYanxuanPermissionLog, Err: " + e.Error())
		return
	}
	return
}

// GetCygxUserYanxuanPermissionItem 根据用户ID 获取对应的研选权限信息
func GetCygxUserYanxuanPermissionMapItem(userIds []int) (mapResp map[int]*cygx.CygxUserYanxuanPermission) {
	lenArr := len(userIds)
	if lenArr == 0 {
		return
	}
	var err error
	defer func() {
		if err != nil {
			fmt.Println(err)
			go alarm_msg.SendAlarmMsg(fmt.Sprint("userIds", userIds, err.Error()), 2)
		}
	}()
	var condition string
	var pars []interface{}
	condition += "  AND	status = '试用' AND user_id IN(" + utils.GetOrmInReplace(lenArr) + ") "
	pars = append(pars, userIds)
	list, e := cygx.GetCygxUserYanxuanPermissionList(condition, pars, 0, lenArr)
	if e != nil {
		err = errors.New("GetCygxUserYanxuanPermissionList, Err: " + e.Error())
		return
	}
	mapResp = make(map[int]*cygx.CygxUserYanxuanPermission, 0)
	for _, v := range list {
		mapResp[v.UserId] = v
	}
	return
}
