/*
 * @description: 质量巡检审批相关
 * @Author: LanJianRong
 * @Date: 2020-12-18
 * @FilePath: \construction_management\web\api\quality_audit_api.go
 */

package api

import (
	"fmt"

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

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

// @Summary 开始审批
// @Tags 质量巡检-审批
// @Description 开始审批
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param   quality_id     body    string     true        "质量巡检id"
// @Param   bidsection_id     body    string     true        "标段id"
// @Param   auditors     body    array     true        "审核组"
// @Param   reAuditors     body    array     true        "复查组"
// @Success 200 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Router /api/quality_audit/start [post]
func (c *QualityAuditApi) PostStart() {
	// 1.规则验证
	qualityAuditData, err := c.QualityAuditService.ValidStart(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 2.获取bid
	bidsectionId, err := utils.GetDecryptId(qualityAuditData.BidsectionId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}
	// 3.获取qualityId
	qualityId, err := utils.GetDecryptId(qualityAuditData.QualityId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}
	// 4.解析审批组id、复查组id
	var auditors []int
	for _, item := range qualityAuditData.Auditors {
		auditId, _ := utils.GetDecryptId(item)
		auditors = append(auditors, auditId)
	}
	var reAuditors []int
	for _, item := range qualityAuditData.ReAuditors {
		auditId, _ := utils.GetDecryptId(item)
		reAuditors = append(reAuditors, auditId)
	}
	// 5.获取当前用户id
	uid, err := utils.GetProjectAccountId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 6.获取当前项目id
	pid, err := utils.GetProjectId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 7.执行sql
	err = c.QualityAuditService.StartAudit(qualityId, bidsectionId, auditors, reAuditors, uid, pid, qualityAuditData.Inspection, qualityAuditData.InspectionDetail, qualityAuditData.Demand, qualityAuditData.CreateTime)
	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
// @Param   id     body    string     true        "审批流程id"
// @Param   quality_id     body    string     true        "巡检id"
// @Param   opinion     body    string     true        "审批意见"
// @Success 200 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Router /api/quality_audit/close [post]
func (c *QualityAuditApi) PostClose() {
	// 1.规则验证
	qualityAuditData, err := c.QualityAuditService.ValidRule(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 2.获取审批记录id
	id, err := utils.GetDecryptId(qualityAuditData.Id)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}
	// 3.获取质量巡检记录id
	qualityId, err := utils.GetDecryptId(qualityAuditData.QualityId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}
	// 4.解析登录id
	uid, err := utils.GetProjectAccountId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 5. 执行sql
	err = c.QualityAuditService.CloseAudit(id, qualityAuditData.Opinion, uid, qualityId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 6. 返回结果
	c.Ctx.JSON(iris.Map{
		"code": 0,
		"msg":  "请求成功",
	})
}

// @Summary 审批退回
// @Tags 质量巡检-审批
// @Description
// @Accept  json
// @Produce  json
// @Param   id     body    string     true        "审批id"
// @Param   quality_id     body    string     true        "巡检id"
// @Param   audit_id     body    string     false        "回滚的审批流程id, 不传则退回到上报人"
// @Param   opinion     body    string     true        "审批意见"
// @Success 200 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Router /api/quality_audit/back [post]
func (c *QualityAuditApi) PostBack() {
	// 1.规则验证
	qualityAuditData, err := c.QualityAuditService.ValidRule(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 2.获取当前审批流程id
	id, err := utils.GetDecryptId(qualityAuditData.Id)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}
	// 3.解析登录id
	uid, err := utils.GetProjectAccountId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 4.获取质量巡检记录id、回滚的审批流程id
	qualityId, err := utils.GetDecryptId(qualityAuditData.QualityId)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}
	// 说明退回到了检查人
	if qualityAuditData.AuditId == "" {
		err = c.QualityAuditService.BackAudit(id, qualityAuditData.Opinion, uid, qualityId, 0)
		if err != nil {
			c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
			return
		}
	} else {
		auditId, err := utils.GetDecryptId(qualityAuditData.AuditId)
		if err != nil {
			c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
			return
		}
		err = c.QualityAuditService.BackAudit(id, qualityAuditData.Opinion, uid, qualityId, auditId)
		if err != nil {
			c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
			return
		}
	}

	// 6. 返回结果
	c.Ctx.JSON(iris.Map{
		"code": 0,
		"msg":  "请求成功",
	})
}

// @Summary 审批通过
// @Tags 质量巡检-审批
// @Description
// @Accept  json
// @Produce  json
// @Param   id     			body    string		true		"审批id"
// @Param   quality_id     body    string		true		"巡检id"
// @Param   audit_id    body    string		false		"整改人id"
// @Param 	opinion			body 		string		false		"审批意见"
// @Param   rectifiedInfo body string false "整改情况"
// @Param		rectifiedTime body string false "整改日期"
// @Success 200 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Failure 400 {string} string	"{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}"
// @Router /api/quality_audit/pass [post]
func (c *QualityAuditApi) PostPass() {
	// 1.规则验证
	qualityAuditData, err := c.QualityAuditService.ValidRule(c.Ctx)
	fmt.Println("---------------------------------------------------------")
	fmt.Println(qualityAuditData.RectifiedTime)
	fmt.Println("---------------------------------------------------------")
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 2.获取审批记录id
	id, err := utils.GetDecryptId(qualityAuditData.Id)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": "ReadJSON转换异常,请检查参数"})
		return
	}
	var auditId = 0
	if qualityAuditData.AuditId != "" {
		aid, err := utils.GetDecryptId(qualityAuditData.AuditId)
		if err != nil {
			c.Ctx.JSON(iris.Map{"code": -1, "msg": "解析参数,请检查参数"})
			return
		}
		auditId = aid
	}
	// 3.解析登录id
	uid, err := utils.GetProjectAccountId(c.Ctx)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 4. 执行sql
	err = c.QualityAuditService.PassAudit(id, uid, auditId, qualityAuditData.Opinion, qualityAuditData.RectifiedInfo, qualityAuditData.RectifiedTime)
	if err != nil {
		c.Ctx.JSON(iris.Map{"code": -1, "msg": fmt.Sprintf("%s", err)})
		return
	}
	// 6. 返回结果
	c.Ctx.JSON(iris.Map{
		"code": 0,
		"msg":  "请求成功",
	})
}
