package cygx

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
	"time"
)

// 报名
type CygxActivityMeetDetailLog struct {
	AttendanceId int       `orm:"column(attendance_id);pk;"description:"主键ID"`
	ActivityId   int       `description:"活动ID"`
	RealName     string    `description:"姓名"`
	Mobile       string    `description:"手机号"`
	CompanyName  string    `description:"公司名称"`
	CompanyId    int       `description:"公司id 不在数据库的用户为0"`
	CreateTime   time.Time `description:"创建时间"`
}

func GetOfflineMeetingDetailListCompanyName(companyName string) (item []*CygxActivityMeetDetailLog, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM cygx_activity_meet_detail_log WHERE company_name IN (` + companyName + `)`
	_, err = o.Raw(sql).QueryRows(&item)
	return
}

// 添加
func AddCygxActivityMeetDetailLog(item *CygxActivityMeetDetailLog) (lastId int64, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	lastId, err = o.Insert(item)
	return
}

func GetActivityMeetDetailLog(mobileStr string) (item []*CygxActivityMeetDetailLog, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM  cygx_activity_meet_detail_log WHERE mobile IN (` + mobileStr + `)`
	_, err = o.Raw(sql).QueryRows(&item)
	return
}

func GetActivityMeetDetailLogByMobile() (item []*CygxActivityMeetDetailLog, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT * FROM  cygx_activity_meet_detail_log WHERE mobile !='' GROUP BY mobile `
	_, err = o.Raw(sql).QueryRows(&item)
	return
}

// 获取数量
func GetActivityMeetDetailLogCount(condition string) (count int, err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	sqlCount := ` SELECT COUNT(1) AS count  FROM cygx_activity_meet_detail_log WHERE 1=1 `
	if condition != "" {
		sqlCount += condition
	}
	err = o.Raw(sqlCount).QueryRow(&count)
	return
}

// 修改公司参会数量
func UpdateActivityMeetDetailLog(companyName string, num int) (err error) {
	sql := ` UPDATE cygx_activity_meet_detail_log SET  company_meet_num= ?  WHERE company_name = ?`
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Raw(sql, num, companyName).Exec()
	return
}

// 修改个人参会数量
func UpdateActivityMeetDetailLogByUser(mobile string, num int) (err error) {
	sql := ` UPDATE cygx_activity_meet_detail_log SET  user_meet_num= ?  WHERE mobile = ?`
	o := orm.NewOrmUsingDB("hz_cygx")
	_, err = o.Raw(sql, num, mobile).Exec()
	return
}

type MeetDetailLoggGroupCount struct {
	CompanyId int    `description:"公司id 不在数据库的用户为0"`
	Mobile    string `description:"手机号"`
	Count     int    `description:"数量"`
}

// 根据分组获取数量
func GetMeetDetailLogGroupCount(condition string, pars []interface{}) (items []*MeetDetailLoggGroupCount, err error) {
	if condition == "" {
		return
	}
	o := orm.NewOrmUsingDB("hz_cygx")
	sql := `SELECT COUNT(1) AS count,company_id,mobile FROM cygx_activity_meet_detail_log WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	_, err = o.Raw(sql, pars).QueryRows(&items)
	return
}

// 添加线下到会记录
func UpdatecygxActivityMeetDetailLogOffline(items []*CygxActivityMeetDetailLog, itemsUpdateCompany []*MeetDetailLoggGroupCount, itemsUpdateMobile []*MeetDetailLoggGroupCount, activityId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			fmt.Println(err)
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()

	//删除原有数据
	sql := ` DELETE FROM cygx_activity_meet_detail_log WHERE activity_id = ?`
	_, err = to.Raw(sql, activityId).Exec()
	if err != nil {
		return
	}

	if len(items) > 0 {
		//批量添加记录
		_, err = to.InsertMulti(len(items), items)
		if err != nil {
			return
		}
	}
	return
}

// 添加线上到会记录
func AddCygxActivityMeetDetailLogOnline(list []*CygxActivityAttendanceDetail, activityId int) (err error) {
	o := orm.NewOrmUsingDB("hz_cygx")
	to, err := o.Begin()
	if err != nil {
		return
	}
	defer func() {
		if err != nil {
			fmt.Println(err)
			_ = to.Rollback()
		} else {
			_ = to.Commit()
		}
	}()
	var CompanyName string
	var MobileStr string
	var items []*CygxActivityMeetDetailLog
	//删除原有数据
	sql := ` DELETE FROM cygx_activity_meet_detail_log WHERE activity_id = ?`
	_, err = to.Raw(sql, activityId).Exec()
	if err != nil {
		return
	}
	//插入新的数据
	for _, v := range list {
		item := new(CygxActivityMeetDetailLog)
		item.ActivityId = v.ActivityId
		item.Mobile = v.Mobile
		item.CompanyName = v.CompanyName
		item.CompanyId = v.CompanyId
		item.CreateTime = v.CreateTime
		CompanyName += "'" + v.CompanyName + "',"
		MobileStr += v.Mobile + ","
		items = append(items, item)
	}
	CompanyName = strings.TrimRight(CompanyName, ",")
	MobileStr = strings.TrimRight(MobileStr, ",")
	for _, v := range items {
		_, err = to.Insert(v)
		if err != nil {
			return
		}
	}
	listCompany, err := GetOfflineMeetingDetailListCompanyName(CompanyName)
	if err != nil {
		return
	}
	//修改公司对应的数量
	var condition string
	for _, v := range listCompany {
		var total int
		condition = ` AND company_name = '` + v.CompanyName + `' `
		total, err = GetActivityMeetDetailLogCount(condition)
		if err != nil {
			return
		}
		sql := ` UPDATE cygx_activity_meet_detail_log SET  company_meet_num= ?  WHERE company_name = ?`
		_, err = to.Raw(sql, total, v.CompanyName).Exec()
		if err != nil {
			return
		}
	}
	listMobile, err := GetActivityMeetDetailLog(MobileStr)
	if err != nil {
		return
	}
	//修改个人对应的数量
	for _, v := range listMobile {
		var total int
		condition = ` AND mobile = '` + v.Mobile + `' `
		total, err = GetActivityMeetDetailLogCount(condition)
		if err != nil {
			return
		}
		sql := ` UPDATE cygx_activity_meet_detail_log SET  user_meet_num= ?  WHERE mobile = ?`
		_, err = to.Raw(sql, total, v.Mobile).Exec()
		if err != nil {
			return
		}
	}
	return
}
