package roadshow

import (
	"encoding/json"
	"fmt"
	"hongze/hz_crm_api/controllers"
	"hongze/hz_crm_api/models"
	"hongze/hz_crm_api/models/cygx"
	"hongze/hz_crm_api/models/roadshow"
	"hongze/hz_crm_api/utils"
	"strconv"
	"strings"
	"time"
)

// 日历参会名单
type CalendarMeetingUserController struct {
	controllers.BaseAuthController
}

// @Title 新增路演活动参会名单接口
// @Description 新增路演活动参会名单接口
// @Param	request	body roadshow.AddRsCalendarMeetingUserReq true "type json string"
// @Success Ret=200 保存成功
// @router /rs_calendar_meeting_user/add [post]
func (this *CalendarMeetingUserController) Add() {
	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
	}

	var req roadshow.AddRsCalendarMeetingUserReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	rsCalendarId := req.RsCalendarId
	userIds := req.UserId
	tagList := req.TagList

	if rsCalendarId == 0 {
		br.Msg = "路演信息错误！"
		return
	}

	var itemsGroup []*roadshow.RsCalendarMeetingLabelGroup
	for _, v := range tagList {
		itemGroup := new(roadshow.RsCalendarMeetingLabelGroup)
		itemGroup.RsCalendarId = rsCalendarId
		itemGroup.TagType = v.TagType
		itemGroup.TagId = v.TagId
		itemGroup.AdminId = sysUser.AdminId
		itemGroup.AdminName = sysUser.RealName
		itemGroup.CreateTime = time.Now()
		itemGroup.ModifyTime = time.Now()
		itemsGroup = append(itemsGroup, itemGroup)
	}
	err = roadshow.AddRsCalendarMeetingLabelGroupMulti(itemsGroup, rsCalendarId)
	if err != nil {
		br.Msg = "获取信息失败！"
		br.ErrMsg = "获取信息失败-AddRsCalendarMeetingLabelGroupMulti！Err:" + err.Error()
		return
	}

	if len(userIds) > 0 {
		var userIdsStr []string
		for _, v := range userIds {
			userIdsStr = append(userIdsStr, strconv.Itoa(v))
		}
		listUser, err := models.GetWxUserListByUserIdsHaveCompany(strings.Join(userIdsStr, ","))
		if err != nil {
			br.Msg = "获取用户信息失败！"
			br.ErrMsg = "获取用户信息失败-GetWxUserListByUserIdsHaveCompany！Err:" + err.Error()
			return
		}

		var items []*roadshow.RsCalendarMeetingUser
		for _, v := range listUser {
			item := new(roadshow.RsCalendarMeetingUser)
			item.RsCalendarId = rsCalendarId
			item.UserId = v.UserId
			item.Mobile = v.Mobile
			item.Email = v.Email
			item.CompanyId = v.CompanyId
			item.CompanyName = v.CompanyName
			item.RealName = v.RealName
			item.Position = v.Position
			item.AdminId = sysUser.AdminId
			item.AdminName = sysUser.RealName
			item.CreateTime = time.Now()
			item.ModifyTime = time.Now()
			items = append(items, item)
		}
		err = roadshow.MultiAddRsCalendarMeetingUser(items)
		if err != nil {
			br.Msg = "操作失败！"
			br.ErrMsg = "操作失败-MultiAddRsCalendarMeetingUser！Err:" + err.Error()
			return
		}
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "添加成功"
	br.IsAddLog = true
}

// @Title 路演活动参会名单列表接口
// @Description 路演活动参会名单列表接口
// @Param   RsCalendarId   query   int  true       "路演活动id"
// @Success 200 {object} roadshow.CalendarListResp
// @router /rs_calendar_meeting_user/list [get]
func (this *CalendarMeetingUserController) List() {
	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
	}

	rsCalendarId, _ := this.GetInt("RsCalendarId")
	resp := new(roadshow.RsCalendarMeetingUserListResp)
	list, err := roadshow.GetRsCalendarMeetingUserListByRsCalendarId(rsCalendarId)
	if err != nil {
		br.Msg = "获取失败！"
		br.ErrMsg = "操作失败-GetRsCalendarMeetingUserListByRsCalendarId！Err:" + err.Error()
		return
	}
	totalGroup, err := roadshow.GetRsCalendarMeetingLabelGroupByRsCalendarId(rsCalendarId)
	if err != nil {
		br.Msg = "获取信息失败！"
		br.ErrMsg = "获取用户信息失败-GetRsCalendarMeetingLabelGroupByRsCalendarId！Err:" + err.Error()
		return
	}

	var lebelDetail []*cygx.RaiServeTagResp
	if totalGroup > 0 {
		listGroup, err := roadshow.GetRsCalendarMeetingLabelGroupListByRsCalendarId(rsCalendarId)
		if err != nil {
			br.Msg = "获取信息失败！"
			br.ErrMsg = "获取用户信息失败-GetRsCalendarMeetingLabelGroupListByRsCalendarId！Err:" + err.Error()
			return
		}

		var industrialManagementIds []int
		var industrialSubjectIds []int

		mapindustrialManagementName := make(map[int]string)
		mapindustrialSubjectName := make(map[int]string)
		mapCelueName := make(map[int]string)
		for _, v := range listGroup {
			switch v.TagType {
			case 1:
				industrialManagementIds = append(industrialManagementIds, v.TagId)
			case 2:
				industrialSubjectIds = append(industrialSubjectIds, v.TagId)
			case 3:
				mapCelueName[v.TagId] = "策略"
			}
		}

		if len(industrialManagementIds) > 0 {
			IndustryList, err := cygx.GetIndustryListByConditionByIds(industrialManagementIds)
			if err != nil {
				br.Msg = "获取信息失败！"
				br.ErrMsg = "获取用户信息失败-GetIndustryListByConditionByIds！Err:" + err.Error()
				return
			}

			for _, v := range IndustryList {
				mapindustrialManagementName[v.IndustrialManagementId] = v.IndustryName
			}
		}

		if len(industrialSubjectIds) > 0 {
			SubjectList, err := cygx.GetIndustrialSubjectDetailByIds(industrialSubjectIds)
			if err != nil {
				br.Msg = "获取信息失败！"
				br.ErrMsg = "获取用户信息失败-GetIndustryListByConditionByIds！Err:" + err.Error()
				return
			}
			for _, v := range SubjectList {
				mapindustrialSubjectName[v.IndustrialSubjectId] = v.SubjectName
			}
		}

		for _, v := range listGroup {
			item := new(cygx.RaiServeTagResp)
			item.TagType = v.TagType
			item.TagId = v.TagId
			item.Md5Key = utils.MD5(fmt.Sprintf("K_", v.TagType, "V_", v.TagId))
			switch v.TagType {
			case 1:
				item.TagName = mapindustrialManagementName[v.TagId]
			case 2:
				item.TagName = mapindustrialSubjectName[v.TagId]
			case 3:
				item.TagName = mapCelueName[v.TagId]
			}
			lebelDetail = append(lebelDetail, item)
		}
	}
	if len(list) == 0 {
		list = make([]*roadshow.RsCalendarMeetingUserResp, 0)
	}
	if len(lebelDetail) == 0 {
		lebelDetail = make([]*cygx.RaiServeTagResp, 0)
	}
	resp.List = list
	resp.ListLebel = lebelDetail
	br.Ret = 200
	br.Success = true
	br.Data = resp
	br.Msg = "获取成功"
}

// @Title 路演活动参会删除接口
// @Description 路演活动参会删除接口
// @Param   RsCalendarMeetingUserId   query   int  true       "参会名单主键ID"
// @Success Ret=200 删除成功
// @router /rs_calendar_meeting_user/delete [post]
func (this *CalendarMeetingUserController) Delete() {
	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
	}
	var req roadshow.DeleteRsCalendarMeetingUserReq
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}

	rsCalendarMeetingUserId := req.RsCalendarMeetingUserId
	err = roadshow.DeleteRsCalendarMeetingUser(rsCalendarMeetingUserId)
	if err != nil {
		br.Msg = "删除失败！"
		br.ErrMsg = "删除失败-DeleteRsCalendarMeetingUser！Err:" + err.Error()
		return
	}
	br.Ret = 200
	br.Success = true
	br.Msg = "删除成功"
	br.IsAddLog = true
}
