package cygx

import (
	"encoding/json"
	"github.com/rdlucklib/rdluck_tools/paging"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/utils"
)

// 权益小程序
type ApplyRecordController struct {
	controllers.BaseAuthController
}

// @Title 权益申请记录列表
// @Description权益申请记录接口
// @Param   PageSize   query   int  true       "每页数据条数"
// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
// @Param   KeyWord   query   string  true       "搜索关键词"
// @Param   CustomType   query   int  true       "用户类型1:潜在用户,2:现有客户"
// @Success 200 {object} cygx.CygxApplyRecordListResp
// @router /apply/record/list [get]
func (this *ApplyRecordController) ListSysRole() {
	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"
		return
	}
	pageSize, _ := this.GetInt("PageSize")
	currentIndex, _ := this.GetInt("CurrentIndex")
	keyWord := this.GetString("KeyWord")

	var startSize int
	if pageSize <= 0 {
		pageSize = utils.PageSize20
	}
	if currentIndex <= 0 {
		currentIndex = 1
	}
	startSize = utils.StartIndex(currentIndex, pageSize)

	var condition string
	var pars []interface{}

	if keyWord != "" {
		condition += ` AND (b.seller_name LIKE '%` + keyWord + `%' OR a.mobile LIKE '%` + keyWord + `%' OR c.email LIKE '%` + keyWord + `%' )  `
	}

	customType, _ := this.GetInt("CustomType")
	if customType > 0 {
		if customType == 1 {
			condition += ` AND a.company_id_type=1  `
		} else if customType == 3 {
			condition += ` AND a.company_id_type = 3  `
		} else {
			condition += ` AND a.company_id_type NOT IN (1,3) `
		}
	}

	total, err := cygx.GetCygxApplyRecordCount(condition, pars)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,Err:" + err.Error()
		return
	}

	list, err := cygx.GetCygxApplyRecord(condition, pars, startSize, pageSize)
	if err != nil {
		br.Msg = "获取失败"
		br.ErrMsg = "获取失败,Err:" + err.Error()
		return
	}
	for i, v := range list {
		if list[i].SellerName == "/" {
			list[i].SellerName = ""
		}
		if v.InviteeCompanyNum == 1 {
			list[i].IsInviteeComanyExistence = true
		}
		if v.InviteeMobile == "" && v.InviteeEmail != "" {
			list[i].InviteeMobile = v.InviteeEmail
		}
		if v.InviteeCompany != "" {
			list[i].ApplySource = "免费送月卡"
		}
		//用户状态,1:潜在客户 、2:现有客户 、3:FICC客户 、4:现有客户(正式,无对应权限) 、5:现有客户(试用,无对应权限)  、6:现有客户(试用暂停) 、7:现有客户(冻结) 、8:现有客户(流失)
		switch v.CompanyIdType {
		case 1:
			list[i].CompanyIdTypeName = "潜在客户"
		case 2:
			list[i].CompanyIdTypeName = "现有客户"
		case 3:
			list[i].CompanyIdTypeName = "FICC客户"
		case 4:
			list[i].CompanyIdTypeName = "现有客户(正式,无对应权限) "
		case 5:
			list[i].CompanyIdTypeName = "现有客户(试用,无对应权限)"
		case 6:
			list[i].CompanyIdTypeName = "现有客户(试用暂停)"
		case 7:
			list[i].CompanyIdTypeName = "现有客户(冻结)"
		case 8:
			list[i].CompanyIdTypeName = "现有客户(流失)"
		}

		switch v.RegisterPlatform {
		case 1:
			list[i].ApplicationSource = "小程序"
		case 2:
			list[i].ApplicationSource = "网页版"
		}

		switch v.InviteCompanySource {
		case 2:
			list[i].ApplicationSource += "(络町)"
		}
	}
	page := paging.GetPaging(currentIndex, pageSize, total)
	resp := new(cygx.CygxApplyRecordListResp)
	resp.List = list
	resp.Paging = page
	br.Ret = 200
	br.Success = true
	br.Msg = "获取成功"
	br.Data = resp
}

// @Title 处理
// @Description 处理接口
// @Param	request	body  cygx.CygxDealReq true "type json string"
// @Success 200 删除成功
// @router /apply/record/deal [post]
func (this *ApplyRecordController) Delete() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	var req cygx.CygxDealReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常!"
		br.ErrMsg = "参数解析失败,Err:" + err.Error()
		return
	}
	if req.ApplyRecordId <= 0 {
		br.Msg = "参数错误"
		br.ErrMsg = "参数错误,ApplyRecordId 小于等于0 "
		return
	}
	err = cygx.DealCygxApplyRecord(req.ApplyRecordId, this.SysUser.AdminId)
	if err != nil {
		br.Msg = "处理失败"
		br.ErrMsg = "处理失败,Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "处理成功"
}