package cygx

import (
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/company"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/utils"
	"strings"
)

// 到会管理
type ActivityLossCoAntroller struct {
	controllers.BaseAuthController
}

// @Title 获取流失且参加过会议的客户
// @Description 获取流失且参加过会议的客户接口
// @Param   KeyWord   query   string  true       "搜索关键词"
// @Success 200 {object} cygx.CompanyNameAndIdListResp
// @router /activityLoss/company/list [get]
func (this *ActivityLossCoAntroller) CompanyList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	keyWord := this.GetString("KeyWord")
	keyWord = strings.Trim(keyWord, " ")
	keyWord = strings.Replace(keyWord, "'", "", -1)
	if keyWord == "" {
		br.Msg = "请输入关键词"
		br.IsSendEmail = false
		return
	}
	resp := new(company.CompanyNameAndIdListResp)
	var companyCondition string
	var companypars []interface{}
	var listResp []*company.CompanyNameAndId
	companyCondition += ` AND b.product_name = ? `
	companypars = append(companypars, "权益")

	companyCondition += ` AND b.status = ? `
	companypars = append(companypars, "流失")

	companyCondition += ` AND a.company_name LIKE '%` + keyWord + `%' `
	companyList, err := company.GetCompanyIdListByproductName(companyCondition, companypars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	companyArr := make([]int, 0)
	for _, v := range companyList {
		companyArr = append(companyArr, v.CompanyId)
	}
	companyLen := len(companyArr)
	if companyLen == 0 {
		goto Loop
	} else {
		var condition string
		var pars []interface{}
		condition += ` AND s.company_id IN (` + utils.GetOrmInReplace(companyLen) + `)`
		pars = append(pars, companyArr)
		companyUserSignUpList, err := cygx.GetCompanyByUserSignUp(condition, pars)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
		mapcompanyUserSignUp := make(map[int]int)
		for _, v := range companyUserSignUpList {
			mapcompanyUserSignUp[v.CompanyId] = v.CompanyId
		}
		for _, v := range companyList {
			if _, ok := mapcompanyUserSignUp[v.CompanyId]; ok {
				listResp = append(listResp, v)
			}
		}
	}
Loop:
	if len(listResp) == 0 {
		listResp = make([]*company.CompanyNameAndId, 0)
	}
	resp.List = listResp
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 获取流失且参加过会议的客户
// @Description 获取流失且参加过会议的客户接口
// @Param   CompanyName   query   string  true       "搜索关键词"
// @Success 200 {object} cygx.UserSignUpLossListResp
// @router /activityLoss/user/list [get]
func (this *ActivityLossCoAntroller) UserList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	companyName := this.GetString("CompanyName")
	companyName = strings.Trim(companyName, " ")
	companyName = strings.Replace(companyName, "'", "", -1)
	resp := new(cygx.UserSignUpLossListResp)
	nameItem, err := company.CheckCompanyProductByName(companyName, 2)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "校验客户名称是否存在失败"
		br.ErrMsg = "校验客户名称是否存在失败,Err:" + err.Error()
		return
	}
	var listResp []*cygx.UserSignUpLossResp
	if nameItem == nil {
		goto Loop
	} else {
		companyId := nameItem.CompanyId
		var condition string
		var pars []interface{}

		condition += ` AND s.company_id =  ? `
		pars = append(pars, companyId)
		companyUserSignUpList, err := cygx.GetUserSignUpList(condition, pars)
		if err != nil {
			br.Msg = "获取失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
		mapUserSignUp := make(map[string]string)
		mapTotalMeeting := make(map[string]int)
		for _, v := range companyUserSignUpList {
			mapTotalMeeting[v.Mobile]++
		}
		for _, v := range companyUserSignUpList {
			if _, ok := mapUserSignUp[v.Mobile]; !ok {
				item := new(cygx.UserSignUpLossResp)
				item.RealName = v.RealName
				item.Mobile = v.Mobile
				item.TotalMeeting = mapTotalMeeting[v.Mobile]
				listResp = append(listResp, item)
			}
			mapUserSignUp[v.Mobile] = v.Mobile
		}
		resp.CompanyId = nameItem.CompanyId
		resp.CompanyName = nameItem.CompanyName
		resp.SellerName = nameItem.SellerName
	}
Loop:
	if len(listResp) == 0 {
		listResp = make([]*cygx.UserSignUpLossResp, 0)
	}
	resp.List = listResp
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 参会明细
// @Description 获取流失且参加过会议的参会明细接口
// @Param   Mobile   query   int  true       "手机号"
// @Success 200 {object} cygx.UserSignUpMeetLossListResp
// @router /activityLoss/meet/list [get]
func (this *ActivityLossCoAntroller) MeetList() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	mobile := this.GetString("Mobile")
	if mobile == "" {
		br.Msg = "手机号不能为空"
		br.IsSendEmail = false
		return
	}
	resp := new(cygx.UserSignUpMeetLossListResp)
	userInfo, err := models.GetWxUserByMobile(mobile)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败，Err:" + err.Error()
		return
	}
	var list []*cygx.AttendanceDetail
	if userInfo == nil {
		goto Loop
	} else {
		var condition string
		condition = ` AND s.is_meeting = 1  GROUP BY s.activity_id `
		list, err = cygx.GetAttendanceDetailListGroupByUser(mobile, condition)
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "获取失败"
			br.ErrMsg = "获取失败，Err:" + err.Error()
			return
		}
		resp.RealName = userInfo.RealName
		resp.Mobile = userInfo.Mobile
	}
Loop:
	if len(list) == 0 {
		list = make([]*cygx.AttendanceDetail, 0)
	}
	resp.List = list
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}
