package controllers

import (
	"encoding/json"
	"hongze/hongze_cygx/models"
	"hongze/hongze_cygx/services"
	"hongze/hongze_cygx/utils"
	"strconv"
	"time"
)

// 报告
type IndustryController struct {
	BaseAuthController
}

// @Title 关注/取消关注产业
// @Description 关注/取消关注 接口
// @Param	request	body models.CygxIndustryFllowRep true "type json string"
// @Success 200
// @router /follow [post]
func (this *IndustryController) Fllow() {
	br := new(models.BaseResponse).Init()
	defer func() {
		this.Data["json"] = br
		this.ServeJSON()
	}()
	user := this.User
	if user == nil {
		br.Msg = "请重新登录"
		br.Ret = 408
		return
	}
	uid := user.UserId
	var req models.IndustryFllowArryReq
	resp := new(models.CygxIndustryFllowResp)
	err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
	if err != nil {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "参数解析失败，Err:" + err.Error()
		return
	}
	sourceId := req.SourceId
	source := req.Source
	doType := req.DoType
	var status int
	var condition string
	var pars []interface{}
	var industrialIds []int
	if source == "article" {
		articleDetail, err := models.GetArticleDetailById(sourceId)
		if err != nil {
			br.Msg = "获取信息失败"
			br.ErrMsg = "获取信息失败,Err:" + err.Error()
			return
		}
		//判读是否属于策略的文章类型
		reportMappingMap, _ := services.GetReportMappingMap()
		if reportMappingMap[articleDetail.CategoryId] {
			chooseCategoryMap, _ := services.GetChooseCategoryMap(user)
			//判断用户是否关注策略对应分类
			if !chooseCategoryMap[articleDetail.CategoryId] {
				item := new(models.CygxXzsChooseCategory)
				item.CategoryId = articleDetail.CategoryId
				item.UserId = uid
				item.Email = user.Email
				item.Mobile = user.Mobile
				item.RealName = user.RealName
				item.CompanyId = user.CompanyId
				item.CompanyName = user.CompanyName
				item.CreateTime = time.Now()
				item.ModifyTime = time.Now()
				_, err = models.AddCygxCategoryFllow(item)
				if err != nil {
					br.Msg = "操作失败"
					br.ErrMsg = "操作失败,Err:" + err.Error()
					return
				}
				resp.Status = 1
			} else {
				err = models.RemoveCygxCategoryFllow(user.Mobile, articleDetail.CategoryId)
				if err != nil {
					br.Msg = "操作失败"
					br.ErrMsg = "取消关注失败,Err:" + err.Error()
					return
				}
				resp.Status = 2
			}
			br.Msg = "操作成功"
			br.Ret = 200
			br.Success = true
			br.Data = resp
			return
		} else {
			pars = make([]interface{}, 0)
			condition = ` AND article_id  = ? `
			pars = append(pars, sourceId)
			industrialList, err := models.GetIndustrialArticleGroupManagementList(condition, pars)
			if err != nil && err.Error() != utils.ErrNoRow() {
				br.Msg = "获取信息失败"
				br.ErrMsg = "获取信息失败,Err:" + err.Error()
				return
			}
			if len(industrialList) > 0 {
				for _, v := range industrialList {
					industrialIds = append(industrialIds, v.IndustrialManagementId)
				}
			}
		}
	} else if source == "activity" {
		//处理活动关联的产业
		industrialList, err := models.GetIndustrialActivityGroupManagementList(sourceId)
		if err != nil && err.Error() != utils.ErrNoRow() {
			br.Msg = "获取信息失败"
			br.ErrMsg = "获取活动关联的产业列表信息失败，Err:" + err.Error() + "activityId：" + strconv.Itoa(sourceId)
			return
		}
		if len(industrialList) > 0 {
			for _, v := range industrialList {
				industrialIds = append(industrialIds, v.IndustrialManagementId)
			}
		}
	} else {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "资源类型有误:" + source
		return
	}
	lenindustrialIds := len(industrialIds)
	if lenindustrialIds == 0 {
		br.Msg = "操作失败！"
		br.ErrMsg = "对应的产业不存在:" + source + "ID:" + strconv.Itoa(sourceId)
		return
	}
	//批量取关与关注
	if doType == "add" {
		status = 1
		var industryFllowItems []*models.CygxIndustryFllow
		for _, v := range industrialIds {
			item := new(models.CygxIndustryFllow)
			item.IndustrialManagementId = v
			item.UserId = uid
			item.Email = user.Email
			item.Mobile = user.Mobile
			item.RealName = user.RealName
			item.CompanyId = user.CompanyId
			item.CompanyName = user.CompanyName
			item.Type = 1
			item.CreateTime = time.Now()
			item.ModifyTime = time.Now()
			industryFllowItems = append(industryFllowItems, item)
		}
		err = models.AddCygxIndustryFllowMulti(industryFllowItems)
	} else if doType == "cancel" {
		status = 2
		pars = make([]interface{}, 0)
		condition = ` AND  industrial_management_id IN (` + utils.GetOrmInReplace(lenindustrialIds) + `)`
		pars = append(pars, industrialIds)
		err = models.RemoveCygxIndustryFllowArry(uid, condition, pars)
	} else {
		br.Msg = "参数解析异常！"
		br.ErrMsg = "操作方式有误:" + doType
		return
	}
	if err != nil {
		br.Msg = "操作失败"
		br.ErrMsg = "取消关注失败,Err:" + err.Error()
		return
	}
	resp.Status = status
	//处理是否关注全部赛道字段
	//go services.IndustryFllowWithTrack(industrialManagementId, count, uid)
	br.Msg = "操作成功"
	br.Ret = 200
	br.Success = true
	br.Data = resp
}
