Forráskód Böngészése

feat: 编号规则相关接口

lanjianrong 4 éve
szülő
commit
98bd10438f

+ 64 - 0
dao/rule_dao.go

@@ -0,0 +1,64 @@
+/*
+ * @description: 编号规则数据库操作相关
+ * @Author: LanJianRong
+ * @Date: 2020-11-27
+ * @FilePath: \construction_management\dao\rule_dao.go
+ */
+
+package dao
+
+import (
+	"fmt"
+
+	"github.com/go-xorm/xorm"
+	"go.mod/models"
+)
+
+//数据库操作引擎
+type RuleDao struct {
+	engine *xorm.Engine
+}
+
+//获得一个DAO对象
+func NewRuleDao(engine *xorm.Engine) *RuleDao {
+	return &RuleDao{
+		engine: engine,
+	}
+}
+
+// 通过项目id和标段id查找
+func (d *RuleDao) FindByPidWithBid(pid int, bid int) *models.CmRule {
+	data := &models.CmRule{BidsectionId: bid, ProjectId: pid}
+	ok, err := d.engine.Get(data)
+	if ok && err == nil {
+		return data
+	} else {
+		data.Id = 0
+		return data
+	}
+}
+
+// 根据pid和bid更新、不存在则创建
+func (d *RuleDao) UpdateOrCreate(pid int, bid int, key string, value string) (bool, error) {
+	data := &models.CmRule{BidsectionId: bid, ProjectId: pid}
+	has, err := d.engine.Get(data)
+	if key == "safe_rule" {
+		data.SafeRule = value
+	} else if key == "quality_rule" {
+		data.QualityRule = value
+	} else {
+		data.ContractRule = value
+	}
+	fmt.Println("newData", data, "value", value, "key", key)
+	if has && err == nil {
+		affected, err := d.engine.Id(data.Id).Update(data)
+		fmt.Println("err2", err)
+		return affected > 0, err
+	} else if !has && err == nil {
+		affected, err := d.engine.Insert(data)
+		fmt.Println("err3", err)
+		return affected > 0, err
+	}
+	fmt.Println(err)
+	return false, err
+}

+ 71 - 0
services/rule_service.go

@@ -0,0 +1,71 @@
+package services
+
+import (
+	"log"
+
+	"github.com/kataras/iris/v12"
+	"go.mod/dao"
+	"go.mod/datasource"
+	"go.mod/web/viewmodels"
+)
+
+type RuleService interface {
+	Get(pid int, id int) viewmodels.ViewRule
+	Post(pid int, id int, key string, value string) (bool, error)
+	ValidRule(ctx iris.Context) (viewmodels.ValidField, error)
+}
+
+// //返回service操作类
+type ruleService struct {
+	daoRule *dao.RuleDao
+}
+
+//创建项目用户service
+func NewRuleService() RuleService {
+	return &ruleService{
+		daoRule: dao.NewRuleDao(datasource.InstanceDbMaster()),
+	}
+}
+
+func (s *ruleService) Get(pid int, id int) viewmodels.ViewRule {
+	data := s.daoRule.FindByPidWithBid(pid, id)
+	viewData := viewmodels.ViewRule{SafeRule: data.SafeRule, QualityRule: data.QualityRule, ContractRule: data.ContractRule}
+	return viewData
+}
+
+func (s *ruleService) Post(pid int, id int, key string, value string) (bool, error) {
+	isOk, err := s.daoRule.UpdateOrCreate(pid, id, key, value)
+	return isOk, err
+}
+
+func (s *ruleService) ValidRule(ctx iris.Context) (viewmodels.ValidField, error) {
+	safeVaild := viewmodels.ValidField{}
+	if ctx.Method() == "GET" {
+		err := ctx.ReadForm(&safeVaild)
+		if err != nil {
+			log.Println("safe-ValidRule-ReadForm转换异常, error=", err)
+			return safeVaild, err
+		}
+		err = safeVaild.Validate()
+	}
+
+	if ctx.Method() == "POST" {
+		err := ctx.ReadJSON(&safeVaild)
+		if err != nil {
+			log.Println("safe-ValidRule-ReadJson转换异常, error=", err)
+			return safeVaild, err
+		}
+		err = safeVaild.Validate()
+	}
+
+	// if ctx.Method() == "PUT" {
+	// 	err := ctx.ReadForm(&safeVaild)
+	// 	if err != nil {
+	// 		log.Println("safe-ValidRule-ReadForm转换异常, error=", err)
+	// 		return safeVaild, err
+	// 	}
+	// 	err = safeVaild.ValidateDelete()
+	// }
+	return safeVaild, nil
+
+}

+ 106 - 0
web/api/rule_api.go

@@ -0,0 +1,106 @@
+/*
+ * @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.Safe "{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 {object} viewmodels.Safe "{code:0成功,data:viewmodels.Safe,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
+	}
+
+	isOk, err := c.RuleService.Post(pid, bidsectionId, safeData.Type, safeData.Rule)
+	fmt.Println(err)
+	if isOk && err == nil {
+		c.Ctx.JSON(iris.Map{
+			"code": 0,
+			"msg":  "请求成功",
+		})
+	} else {
+		c.Ctx.JSON(iris.Map{
+			"code": -1,
+			"msg":  err,
+		})
+	}
+}

+ 111 - 0
web/docs/docs.go

@@ -1596,6 +1596,117 @@ var doc = `{
                 }
             }
         },
+        "/api/rule": {
+            "get": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "description": "获得制定pid、bid的编号规则",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "编号规则"
+                ],
+                "summary": "获取编号规则",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "标段ID",
+                        "name": "bidsectionId",
+                        "in": "path",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "项目ID",
+                        "name": "projectId",
+                        "in": "path",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "{code:0成功,data:viewmodels.Safe,msg:}",
+                        "schema": {
+                            "$ref": "#/definitions/viewmodels.Safe"
+                        }
+                    },
+                    "400": {
+                        "description": "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            },
+            "post": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "description": "提交规则",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "编号规则"
+                ],
+                "summary": "提交规则",
+                "parameters": [
+                    {
+                        "description": "标段ID",
+                        "name": "bidsectionId",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "string"
+                        }
+                    },
+                    {
+                        "description": "规则类型",
+                        "name": "type",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "string"
+                        }
+                    },
+                    {
+                        "description": "编号规则",
+                        "name": "value",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "{code:0成功,data:viewmodels.Safe,msg:}",
+                        "schema": {
+                            "$ref": "#/definitions/viewmodels.Safe"
+                        }
+                    },
+                    "400": {
+                        "description": "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/api/safe": {
             "get": {
                 "security": [

+ 111 - 0
web/docs/swagger.json

@@ -1579,6 +1579,117 @@
                 }
             }
         },
+        "/api/rule": {
+            "get": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "description": "获得制定pid、bid的编号规则",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "编号规则"
+                ],
+                "summary": "获取编号规则",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "标段ID",
+                        "name": "bidsectionId",
+                        "in": "path",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "项目ID",
+                        "name": "projectId",
+                        "in": "path",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "{code:0成功,data:viewmodels.Safe,msg:}",
+                        "schema": {
+                            "$ref": "#/definitions/viewmodels.Safe"
+                        }
+                    },
+                    "400": {
+                        "description": "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            },
+            "post": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "description": "提交规则",
+                "consumes": [
+                    "application/json"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "编号规则"
+                ],
+                "summary": "提交规则",
+                "parameters": [
+                    {
+                        "description": "标段ID",
+                        "name": "bidsectionId",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "string"
+                        }
+                    },
+                    {
+                        "description": "规则类型",
+                        "name": "type",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "string"
+                        }
+                    },
+                    {
+                        "description": "编号规则",
+                        "name": "value",
+                        "in": "body",
+                        "required": true,
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "{code:0成功,data:viewmodels.Safe,msg:}",
+                        "schema": {
+                            "$ref": "#/definitions/viewmodels.Safe"
+                        }
+                    },
+                    "400": {
+                        "description": "{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}",
+                        "schema": {
+                            "type": "string"
+                        }
+                    }
+                }
+            }
+        },
         "/api/safe": {
             "get": {
                 "security": [

+ 71 - 0
web/docs/swagger.yaml

@@ -1217,6 +1217,77 @@ paths:
       summary: 保存项目信息
       tags:
       - 项目设置-管理员
+  /api/rule:
+    get:
+      consumes:
+      - application/json
+      description: 获得制定pid、bid的编号规则
+      parameters:
+      - description: 标段ID
+        in: path
+        name: bidsectionId
+        required: true
+        type: string
+      - description: 项目ID
+        in: path
+        name: projectId
+        required: true
+        type: string
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: '{code:0成功,data:viewmodels.Safe,msg:}'
+          schema:
+            $ref: '#/definitions/viewmodels.Safe'
+        "400":
+          description: '{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}'
+          schema:
+            type: string
+      security:
+      - ApiKeyAuth: []
+      summary: 获取编号规则
+      tags:
+      - 编号规则
+    post:
+      consumes:
+      - application/json
+      description: 提交规则
+      parameters:
+      - description: 标段ID
+        in: body
+        name: bidsectionId
+        required: true
+        schema:
+          type: string
+      - description: 规则类型
+        in: body
+        name: type
+        required: true
+        schema:
+          type: string
+      - description: 编号规则
+        in: body
+        name: value
+        required: true
+        schema:
+          type: string
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: '{code:0成功,data:viewmodels.Safe,msg:}'
+          schema:
+            $ref: '#/definitions/viewmodels.Safe'
+        "400":
+          description: '{code:0成功,-1参数类错误,-2服务端内部错误,msg:错误信息}'
+          schema:
+            type: string
+      security:
+      - ApiKeyAuth: []
+      summary: 提交规则
+      tags:
+      - 编号规则
   /api/safe:
     delete:
       consumes:

+ 13 - 6
web/routes/routes.go

@@ -24,8 +24,9 @@ func Configure(b *bootstrap.Bootstrapper) {
 	BidsectionService := services.NewBidsectionService()
 	BidAccountService := services.NewBidAccountService()
 	ContractService := services.NewContractService()
-	RpcService := services.NewRpcService()
+	// RpcService := services.NewRpcService()
 	SafeService := services.NewSafeService()
+	RuleService := services.NewRuleService()
 	//CSRF相关
 	b.Use(middleware.SetCsrf)
 
@@ -125,11 +126,11 @@ func Configure(b *bootstrap.Bootstrapper) {
 	apiContract.Handle(new(api.ContractApi))
 
 	// rpc相关
-	rpc := mvc.New(b.Party("/api/rpc/test"))
-	rpc.Register(RpcService)
-	rpc.Router.Use(middleware.SessionsAuth)
-	rpc.Router.Use(middleware.AccessAuth)
-	rpc.Handle(new(api.RpcApi))
+	// rpc := mvc.New(b.Party("/api/rpc/test"))
+	// rpc.Register(RpcService)
+	// rpc.Router.Use(middleware.SessionsAuth)
+	// rpc.Router.Use(middleware.AccessAuth)
+	// rpc.Handle(new(api.RpcApi))
 
 	// safe
 	apiSafe := mvc.New(b.Party("/api/safe"))
@@ -138,4 +139,10 @@ func Configure(b *bootstrap.Bootstrapper) {
 	apiSafe.Router.Use(middleware.AccessAuth)
 	apiSafe.Handle(new(api.SafeApi))
 
+	// rule
+	apiRule := mvc.New(b.Party("/api/rule"))
+	apiRule.Register(RuleService)
+	apiRule.Router.Use(middleware.SessionsAuth)
+	apiRule.Router.Use(middleware.AccessAuth)
+	apiRule.Handle(new(api.RuleApi))
 }

+ 38 - 0
web/viewmodels/rule.go

@@ -0,0 +1,38 @@
+package viewmodels
+
+import validation "github.com/go-ozzo/ozzo-validation/v3"
+
+/*
+ * @description: 编号规则
+ * @Author: LanJianRong
+ * @Date: 2020-11-27
+ * @FilePath: \construction_management\web\viewmodels\safe.go
+ */
+
+type Rule struct {
+	Id           string `form:"id" json:"id" `
+	ProjectId    string `form:"projectId" json:"projectId" `
+	BidsectionId string `form:"bidsectionId" json:"bidsectionId" `
+	SafeRule     string `form:"safeRule" json:"safeRule" `
+	QualityRule  string `form:"qualityRule" json:"qualityRule" `
+	ContractRule string `form:"contractRule" json:"contractRule" `
+}
+
+// 页面所需字段
+type ViewRule struct {
+	SafeRule     string `form:"safeRule" json:"safeRule" `
+	QualityRule  string `form:"qualityRule" json:"qualityRule" `
+	ContractRule string `form:"contractRule" json:"contractRule" `
+}
+
+type ValidField struct {
+	BidsectionId string `form:"bidsectionId" json:"bidsectionId" `
+	Type         string `form:"type" json:"type"`
+	Rule         string `form:"rule" json:"rule"`
+}
+
+func (l ValidField) Validate() error {
+	return validation.ValidateStruct(&l,
+		validation.Field(&l.BidsectionId, validation.Required.Error("标段ID不能为空")),
+	)
+}