/*
 * @description: 编号规则
 * @Author: LanJianRong
 * @Date: 2020-11-27
 * @FilePath: \construction_management\web\api\rule_api.go
 */

package api

import (
	"fmt"

	"github.com/kataras/iris/v12"
	"go.mod/services"
	"go.mod/web/utils"
)

type RuleApi struct {
	//框架-web应用上下文环境
	Ctx iris.Context
	// 需要用的service
	RuleService services.RuleService
}

// @Summary 获取编号规则
// @Tags 编号规则
// @Description 获得制定pid、bid的编号规则
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param   bidsectionId     path    string     true        "标段ID"
// @Param   projectId     path    string     true        "项目ID"
// @Success 200 {object} viewmodels.ViewRule "{code:0成功,data:viewmodels.Safe,msg:请求成功}"
// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Router /api/rule [get]
func (c *RuleApi) Get() {
	// 1.规则验证
	safeData, err := c.RuleService.ValidRule(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
		return
	}
	pid, err := utils.GetProjectId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "项目id不存在, 请重新登录"})
		return
	}
	bidsectionId, err := utils.GetDecryptId(safeData.BidsectionId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}

	rule := c.RuleService.Get(pid, bidsectionId)

	c.Ctx.JSON(iris.Map{
		"code": 0,
		"msg":  "请求成功",
		"data": rule,
	})
}

// @Summary 提交编号规则
// @Tags 编号规则
// @Description 提交规则
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param   bidsectionId     body    string     true        "标段ID"
// @Param   type 			body     string  true   "规则类型" eg:"safeRule、qualityRule、contractRule"
// @Param   value 			body     string  true   "编号规则" eg:"'['202011', 'cc3']'"
// @Success 200 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Router /api/rule [post]
func (c *RuleApi) Post() {
	// 1.规则验证
	safeData, err := c.RuleService.ValidRule(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
		return
	}
	pid, err := utils.GetProjectId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "项目id不存在, 请重新登录"})
		return
	}
	bidsectionId, err := utils.GetDecryptId(safeData.BidsectionId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}

	err = c.RuleService.Post(pid, bidsectionId, safeData.Type, safeData.Rule)
	if err == nil {
		c.Ctx.JSON(iris.Map{
			"code": 0,
			"msg":  "请求成功",
		})
	} else {
		c.Ctx.JSON(iris.Map{
			"code": -1,
			"msg":  fmt.Sprintf("%s", err),
		})
	}
}

// @Summary 生成编号
// @Tags 编号规则
// @Description 提交规则
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param   bidsectionId     body    string     true        "标段ID"
// @Param   type 			body     string  true   "规则类型" eg:"safeRule、qualityRule、contractRule"
// @Success 200 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Router /api/rule/auto [post]
func (c *RuleApi) PostAuto() {
	// 1.规则验证
	safeData, err := c.RuleService.ValidRule(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
		return
	}
	pid, err := utils.GetProjectId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "项目id不存在, 请重新登录"})
		return
	}
	bidsectionId, err := utils.GetDecryptId(safeData.BidsectionId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}
	code, err := c.RuleService.AutoCode(bidsectionId, pid, safeData.Type)
	if err != nil {
		c.Ctx.JSON(iris.Map{
			"code": -1,
			"msg":  fmt.Sprintf("%s", err),
		})
	} else {
		c.Ctx.JSON(iris.Map{
			"code": 0,
			"msg":  "请求成功",
			"data": code,
		})
	}
}
