/*
 * @description: rpc - 质量巡检
 * @Author: LanJianRong
 * @Date: 2020-11-18
 * @FilePath: \construction_management\web\api\quality_api.go
 */

package api

import (
	"fmt"
	"time"

	"github.com/kataras/iris/v12"
	"go.mod/conf"
	"go.mod/lib"
	"go.mod/models"
	"go.mod/services"
	"go.mod/web/utils"
	"go.mod/web/viewmodels"
)

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

// @Summary 质量巡检列表
// @Tags 质量巡检
// @Description 获得列表数据
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param   bidsectionId     path    string     true        "标段ID"
// @Param   pageNo     path    int     true        "页码" eg:1
// @Param   pageSize     path    int     true        "页数" eg:15
// @Success 200 {object} viewmodels.Quality "{code:0成功,data:viewmodels.Quality,msg:}"
// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Router /api/quality [get]
func (c *QualityApi) Get() {
	// 1.规则验证
	qualityData, err := c.ServiceQuality.ValidRule(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", 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(qualityData.BidsectionId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}
	QualityData, total := c.ServiceQuality.Get(bidsectionId, pid, qualityData.PageNo, qualityData.PageSize)

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

// @Summary 创建新的质量巡检记录
// @Tags 质量巡检
// @Description 创建新的质量巡检记录
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param   bidsectionId     body    string     true        "标段ID"
// @Param   code     					body    string     true        "编号"
// @Param   createTime     body    string     true        "日期"
// @Param   inspection     body    string     true        "检查部位"
// @Param   position     body    string     true        "部位"
// @Success 200 {object} viewmodels.Quality "{code:0成功,msg:}"
// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Router /api/quality [post]
func (c *QualityApi) Post() {
	// 1.规则验证
	qualityData, err := c.ServiceQuality.ValidRule(c.Ctx)
	// fmt.Println("-------------------------------", err)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	bidsectionId, err := utils.GetDecryptId(qualityData.BidsectionId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}
	data := models.CmQuality{}
	data.BidsectionId = bidsectionId
	data.Code = qualityData.Code
	data.Inspection = qualityData.Inspection
	data.Times = 1
	uid, err := utils.GetProjectAccountId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "未登录或账号失效，请重新登录"})
		return
	}
	data.Uid = uid
	createTime, err := time.Parse(conf.SysTimeform, qualityData.CreateTime)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "日期转换异常,请检查参数"})
		return
	}
	pid, err := utils.GetProjectId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	data.ProjectId = pid
	data.CreateTime = createTime
	err = c.ServiceQuality.Post(data)
	if err != nil {
		c.Ctx.JSON(iris.Map{
			"code": -1,
			"msg":  fmt.Sprintf("%s", err),
		})
		return
	}
	c.Ctx.JSON(iris.Map{
		"code": 0,
		"msg":  "插入成功",
	})
}

// @Summary 删除记录
// @Tags 质量巡检
// @Description 删除质量巡检记录
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param   id     body    string     true        "质量巡检ID"
// @Success 200 {object} viewmodels.Quality "{code:0成功,msg:}"
// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Router /api/quality [delete]
func (c *QualityApi) Delete() {
	queryId := c.Ctx.URLParam("id")
	if queryId == "" {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "id不存在"})
		return
	}
	id, err := utils.GetDecryptId(queryId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}

	err = c.ServiceQuality.Del(id)
	if err != nil {
		c.Ctx.JSON(iris.Map{
			"code": -1,
			"msg":  fmt.Sprintf("%s", err),
		})
		return
	}
	c.Ctx.JSON(iris.Map{
		"code": 0,
		"msg":  "删除成功",
	})
}

// @Summary 获取质量巡检详情
// @Tags 质量巡检
// @Description 获得质量巡检详情页面数据
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param   id     path    string     true        "巡检ID"
// @Success 200 {object} viewmodels.QualityDetail "{code:0成功,data:viewmodels.QualityDetail,msg:}"
// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Router /api/quality/detail [get]
func (c *QualityApi) GetDetail() {
	// 1.规则验证
	qualityData, err := c.ServiceQuality.ValidRule(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}

	id, err := utils.GetDecryptId(qualityData.Id)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadForm转换异常,请检查参数"})
		return
	}
	pid, _ := utils.GetProjectId(c.Ctx)
	quality := c.ServiceQuality.GetDetail(id, pid)

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

// @Summary 质量概述
// @Tags 质量巡检
// @Description 质量概述
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param   bidsectionId     path    string     true        "标段ID"
// @Success 200 {object} viewmodels.TreeSectionContract "{code:0成功,-1参数类错误,msg:错误信息}"
// @Router /api/quality/survey [get]
func (c *QualityApi) GetSurvey() {
	sectionData := viewmodels.Quality{}
	err := c.Ctx.ReadForm(&sectionData)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}
	err = sectionData.ValidateBidsectionId()
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}

	// 标段ID
	bidsectionId, err := utils.GetDecryptId(sectionData.BidsectionId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}

	// 项目ID
	projectId, err := utils.GetProjectId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": err})
		return
	}

	// 账号ID
	accountId, err := utils.GetProjectAccountId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 缓存标段ID-用于权限
	key := fmt.Sprintf("pm_%d_%d", projectId, accountId)
	lib.NewRedis().SetBidsectionIdByCache(key, bidsectionId)

	QualityData := c.ServiceQuality.GetSurvey(projectId, bidsectionId)

	c.Ctx.JSON(iris.Map{
		"code":         0,
		"msg":          "",
		"bidsectionId": sectionData.BidsectionId,
		"data":         QualityData,
	})
}
