package roadshow

import (
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/company"
	"hongze/hz_crm_api/models/roadshow"
	"hongze/hz_crm_api/services"
	"hongze/hz_crm_api/utils"
	"strings"
)

// @Title 我的日历列表
// @Description 我的日历列表接口
// @Param   KeyWord   query   string  true       "搜索关键词"
// @Success 200 {object} roadshow.CalendarListResp
// @router /company/search [get]
func (this *CalendarController) CompanySearch() {
	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")
	if KeyWord == "" {
		br.Msg = "请输入搜索词！"
		return
	}
	KeyWord = "%" + KeyWord + "%"
	var groupId int
	if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_GROUP || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_GROUP {
		groupId = sysUser.GroupId
	} else {
		groupId = -1
	}

	list, err := roadshow.CompanySearchV2(sysUser.AdminId, groupId, KeyWord)
	if err != nil && err.Error() != utils.ErrNoRow() {
		br.Msg = "搜索客户失败！"
		br.ErrMsg = "搜索客户失败！Err:" + err.Error()
		return
	}
	if list == nil {
		list = make([]*roadshow.CompanySearchView, 0)
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = list
}

// CompanyDetail
// @Title 我的日历列表
// @Description 我的日历列表接口
// @Param   CompanyId   query   int  true       "公司id"
// @Param   EnglishCompany   query   int  true       "是否为英文客户"
// @Param   CompanyType   query   string  true       "客户类型：'ficc','权益',传空默认为ficc,"
// @Success 200 {object} roadshow.CompanyDetailView
// @router /company/detail [get]
func (this *CalendarController) CompanyDetail() {
	br := new(models.BaseResponse).Init()
	defer func() {
		if br.ErrMsg == "" {
			br.IsSendEmail = false
		}
		this.Data["json"] = br
		this.ServeJSON()
	}()
	sysUser := this.SysUser
	if sysUser == nil {
		br.Msg = "请登录"
		br.ErrMsg = "请登录，SysUser Is Empty"
		br.Ret = 408
		return
	}
	companyId, _ := this.GetInt("CompanyId")
	if companyId <= 0 {
		br.Msg = "参数错误！"
		return
	}
	englishCompany, _ := this.GetInt("EnglishCompany")
	companyType := this.GetString("CompanyType")
	productId := services.GetProductId(sysUser.RoleTypeCode)
	if productId == 0 {
		productId = 1
	}
	if companyType == utils.COMPANY_CLASSIFY_RAI {
		productId = 2
	}
	detailView := new(roadshow.CompanyDetailView)
	if englishCompany == 0 {
		companyProductItem, err := company.GetCompanyProductByCompanyIdAndProductId(companyId, productId)
		if err != nil {
			if err.Error() == utils.ErrNoRow() {
				br.Msg = "该客户已被删除"
				return
			}
			br.Msg = "搜索客户失败！"
			br.ErrMsg = "搜索客户失败！Err:" + err.Error()
			return
		}
		permissionList, err := company.GetCompanyProductReportPermissionList(companyId, productId)
		if err != nil {
			br.Msg = "搜索客户权限失败！"
			br.ErrMsg = "搜索客户权限失败！Err:" + err.Error()
			return
		}
		var permissionArr []string
		for _, v := range permissionList {
			permissionArr = append(permissionArr, v.PermissionName)
		}
		detailView.CompanyId = companyProductItem.CompanyId
		detailView.CompanyName = companyProductItem.CompanyName
		detailView.Status = companyProductItem.Status
		detailView.IndustryId = companyProductItem.IndustryId
		detailView.IndustryName = companyProductItem.IndustryName
		detailView.PermissionName = strings.Join(permissionArr, "/")
		detailView.ReportReadTotal = companyProductItem.ViewTotal //ficc报告-累计阅读次数
		br.Ret = 200
		br.Success = true
		br.Msg = "获取成功"
		br.Data = detailView
		return
	}

	// 英文客户
	enItem, e := models.GetEnglishCompanyById(companyId)
	if e != nil {
		br.Msg = "搜索英文客户失败！"
		br.ErrMsg = "获取英文客户详情失败！Err:" + e.Error()
		return
	}
	detailView.CompanyId = enItem.CompanyId
	detailView.CompanyName = enItem.CompanyName
	detailView.Status = "正常"
	detailView.EnglishCompany = 1
	detailView.EnglishCountry = enItem.Country
	detailView.EnglishViewTotal = enItem.ViewTotal
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = detailView
}
