package controllers

import (
	"encoding/json"
	"oppmg/common/log"
	"oppmg/models"
	"oppmg/protocol"
	serverank "oppmg/services/rank"
)

type RankController struct {
	BaseController
}

//GetRankType  ..
//@router /rank/list
func (c RankController) GetRankType() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	companyid := c.GetCompanyId()
	rspData := serverank.GetRankList(companyid)
	msg = protocol.NewReturnResponse(rspData, nil)
	return
}

//EditRankType ...
//@router /rank/edit
func (c RankController) EditRankType() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	type Parameter struct {
		Id   int64  `json:"id"`
		Name string `json:"name"`
	}
	var param Parameter
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	companyid := c.GetCompanyId()
	err := serverank.UpdateRankType(param.Id, param.Name, companyid)
	msg = protocol.NewReturnResponse(nil, err)
	return
}

//EditRankType ...
//@router /rank/forbid_allow
func (c RankController) RankTypeForbidAllow() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	type Parameter struct {
		Id     int64 `json:"id"`
		status int   `json:"status"`
	}
	var param Parameter
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	var err error
	companyid := c.GetCompanyId()
	switch param.status {
	case 1:
		err = serverank.ForbidRank(param.Id, companyid)
	case 2:
		err = serverank.AllowRank(param.Id, companyid)
	}
	msg = protocol.NewReturnResponse(nil, err)
	return
}

//RankSeasonList ...
//@router /rank/season/list
func (c RankController) RankSeasonList() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()

	var param protocol.RequestPageInfo
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	companyid := c.GetCompanyId()
	rspData, err := serverank.RankSeasonList(param.PageIndex, param.PageSize, companyid)
	msg = protocol.NewReturnResponse(rspData, err)
	return
}

//RankSeasonAdd ...
//@router /rank/season/add
func (c RankController) RankSeasonAdd() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	type Parameter struct {
		Name      string `json:"name"`
		BeginTime string `json:"begin_time"`
		EndTime   string `json:"end_time"`
	}
	var param Parameter
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	var (
		beginTime int64
		endTime   int64
	)
	companyid := c.GetCompanyId()
	err := serverank.AddRankSeason(beginTime, endTime, param.Name, companyid)
	msg = protocol.NewReturnResponse(nil, err)
	return
}

//RankSeasonEdit ...
//@router /rank/season/edit
func (c RankController) RankSeasonEdit() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	type Parameter struct {
		Id        int64  `json:"id"`
		Name      string `json:"name"`
		BeginTime string `json:"begin_time"`
		EndTime   string `json:"end_time"`
	}
	var param Parameter
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	var (
		beginTime int64
		endTime   int64
	)
	companyid := c.GetCompanyId()
	err := serverank.EditRankSeason(param.Id, beginTime, endTime, param.Name, companyid)
	msg = protocol.NewReturnResponse(nil, err)
	return
}

//RankRangeList ...
//@router /rank/range/list
func (c RankController) RankRangeList() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()

	companyid := c.GetCompanyId()
	rspData := serverank.GetRankRangeList(companyid)
	msg = protocol.NewReturnResponse(rspData, nil)
	return
}

//RankRangeAdd ...
//@router /rank/range/add
func (c RankController) RankRangeAdd() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	type Parameter struct {
		Name       string  `json:"name"`
		RangeType  int8    `json:"range_type"`
		RelationId []int64 `json:"relation_id"`
	}
	var param Parameter
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	switch param.RangeType {
	case 1:
		//员工
		if len(param.RelationId) == 0 {
			param.RangeType = models.RANK_RANGE_TYPE_EMPLAYEEALL
		} else {
			param.RangeType = models.RANK_RANGE_TYPE_EMPLAYEE
		}
	case 2:
		//部门
		if len(param.RelationId) == 0 {
			param.RangeType = models.RANK_RANGE_TYPE_DEPARTMENTALL
		} else {
			param.RangeType = models.RANK_RANGE_TYPE_DEPARTMENT
		}
	default:
		log.Error("param.RangeType err")
		msg = protocol.BadRequestParam("1")
		return
	}
	companyid := c.GetCompanyId()
	err := serverank.AddRankRange(param.Name, param.RangeType, param.RelationId, companyid)
	msg = protocol.NewReturnResponse(nil, err)
	return
}

//RankRangeEdit ...
//@router /rank/range/edit
func (c RankController) RankRangeEdit() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	type Parameter struct {
		Id         int64   `json:"id"`
		Name       string  `json:"name"`
		RangeType  int8    `json:"range_type"`
		RelationId []int64 `json:"relation_id"`
	}
	var param Parameter
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	switch param.RangeType {
	case 1:
		//员工
		if len(param.RelationId) == 0 {
			param.RangeType = models.RANK_RANGE_TYPE_EMPLAYEEALL
		} else {
			param.RangeType = models.RANK_RANGE_TYPE_EMPLAYEE
		}
	case 2:
		//部门
		if len(param.RelationId) == 0 {
			param.RangeType = models.RANK_RANGE_TYPE_DEPARTMENTALL
		} else {
			param.RangeType = models.RANK_RANGE_TYPE_DEPARTMENT
		}
	default:
		log.Error("param.RangeType err")
		msg = protocol.BadRequestParam("1")
		return
	}
	companyid := c.GetCompanyId()
	err := serverank.EditRankRange(param.Id, param.Name, param.RangeType, param.RelationId, companyid)
	msg = protocol.NewReturnResponse(nil, err)
	return
}

//RankRangeInfo ...
//@router /rank/range/info
func (c RankController) RankRangeInfo() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	type Parameter struct {
		Id int64 `json:"id"`
	}
	var param Parameter
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	// companyid := c.GetCompanyId()
	rspData := serverank.GetRankRangeInfo(param.Id)
	switch rspData.RangeType {
	case models.RANK_RANGE_TYPE_DEPARTMENT, models.RANK_RANGE_TYPE_DEPARTMENTALL:
		//部门
		rspData.RangeType = 2
	case models.RANK_RANGE_TYPE_EMPLAYEE, models.RANK_RANGE_TYPE_EMPLAYEEALL:
		//员工
		rspData.RangeType = 1

	}
	msg = protocol.NewReturnResponse(rspData, nil)
	return
}