package card

import (
	"fmt"
	"log-server/global"
	"log-server/model/card"
	cardReq "log-server/model/card/request"
	"math/rand"
	"time"
)

type IdCardService struct {
}

//查找身份证信息
func (s *IdCardService) GetIdCard(idCard cardReq.IdCardRequest) (list []card.IdCard, err error) {
	var unionIdCardList []card.UnionIdCard
	var idCardList = make([]card.IdCard, idCard.Rows)
	var total int64
	//查找此渠道和此id_idCard记录是否存在， 1是创建，2是更新
	var status int
	//取一个0-1000的随机数作为偏移值
	//将时间戳设置成种子数
	rand.Seed(time.Now().UnixNano())
	//生成10个0-99之间的随机数
	randomOffset := rand.Intn(1000)
	limit := idCard.Rows
	//联合查询符合条件的身份证信息
	//查找channel != channel 的表单信息
	db := global.GVA_DB.Table("(select * from shuyou_id_card where id not in (select id_idCard from `shuyou_id_card_usage` where channel = ?) and flag = ?) as a", idCard.Channel, idCard.Flag)
	db = db.Select("a.id, id_name, a.id_number, flag, IFNULL(use_count, 0) as use_count, b.id as usage_id, channel")
	db = db.Joins("left JOIN shuyou_id_card_usage as b on b.id_idCard = a.id")
	db.Count(&total)
	db = db.Order("use_count").Limit(limit).Offset(randomOffset)
	err = db.Find(&unionIdCardList).Error
	if err != nil {
		return nil, err
	}
	fmt.Println(unionIdCardList)
	//没有非这个渠道的记录
	if total == 0 {
		dbFail := global.GVA_DB.Table("shuyou_id_card as a")
		dbFail = dbFail.Select("a.id, id_name, a.id_number, flag, IFNULL(use_count, 0) as use_count, b.id as usage_id, channel")
		dbFail = dbFail.Joins("left JOIN shuyou_id_card_usage as b on b.id_idCard = a.id")
		dbFail = dbFail.Where("channel = ? and flag = ?", idCard.Channel, idCard.Flag)
		dbFail = dbFail.Order("use_count").Limit(limit)
		err = dbFail.Find(&unionIdCardList).Error
		fmt.Println("---------没查到----------")
		fmt.Println(unionIdCardList)
		//1是创建，2是更新
		status = 2
	} else {
		//1是创建，2是更新
		status = 1
	}
	for i, unionInfo := range unionIdCardList {
		//将idCard内容赋予idCardList
		idCardList[i] = unionInfo.IdCard
		if status == 1 {
			err = s.CreateIdCardUsage(unionInfo, idCard.Channel)
		} else if status == 2 {
			err = s.UpdateIdCardUsage(unionInfo)
		}
		if err != nil {
			return nil, err
		}
	}
	return idCardList, err
}

//更改身份证使用表
//func (s *IdCardService) ChangeIdCardUsage(unionInfo card.UnionIdCard, channel string) (err error){
//	var usageEntity card.IdCardUsage
//	if unionInfo.UseCount == 0 {
//		usageEntity = card.IdCardUsage{
//			IdIdCard: unionInfo.Id,
//			IdNumber: unionInfo.IdNumber,
//			Channel: channel,
//			UseCount: 1,
//		}
//		err = global.GVA_DB.Model(&card.IdCardUsage{}).Create(&usageEntity).Error
//	} else {
//		usageEntity = card.IdCardUsage{
//			UseCount: unionInfo.UseCount + 1,
//		}
//		err = global.GVA_DB.Model(&card.IdCardUsage{}).Updates(&usageEntity).Error
//	}
//	return err
//}

//添加身份证使用记录信息
func (s *IdCardService) CreateIdCardUsage(unionInfo card.UnionIdCard, channel string) (err error) {
	var usageEntity = card.IdCardUsage{
		IdIdCard: unionInfo.Id,
		IdNumber: unionInfo.IdNumber,
		Channel:  channel,
		UseCount: 1,
	}
	return global.GVA_DB.Model(&card.IdCardUsage{}).Omit("createTime", "updateTime").Create(&usageEntity).Error
}

//更新身份证使用记录信息
func (s *IdCardService) UpdateIdCardUsage(unionInfo card.UnionIdCard) (err error) {
	useCount := unionInfo.UseCount + 1
	//更新记录
	return global.GVA_DB.Model(&card.IdCardUsage{}).Where("id = ? ", unionInfo.UsageId).Update("use_count", useCount).Error
}

//判断是更新记录还是创建记录, 1是创建，2是更新
//func(s *IdCardService)	judgeOperate(unionInfo card.UnionIdCard, channel string) (status int, err error){
//	//查找此channel和此id_number账号是否存在
//	var total int64
//	err = global.GVA_DB.Model(&card.IdCardUsage{}).Where("channel = ? and id_idCard = ?", channel, unionInfo.Id).Count(&total).Error
//	fmt.Println(total)
//	if total == 0 {
//		status = 1
//	} else {
//		status = 2
//	}
//	return status, err
//}
