|
@@ -1,7 +1,14 @@
|
|
|
package services
|
|
|
|
|
|
import (
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
"log"
|
|
|
+ "reflect"
|
|
|
+ "strconv"
|
|
|
+
|
|
|
+ "go.mod/web/utils"
|
|
|
|
|
|
"github.com/kataras/iris/v12"
|
|
|
"go.mod/dao"
|
|
@@ -11,19 +18,24 @@ import (
|
|
|
|
|
|
type RuleService interface {
|
|
|
Get(pid int, id int) viewmodels.ViewRule
|
|
|
- Post(pid int, id int, key string, value string) (bool, error)
|
|
|
+ Post(pid int, id int, key string, value string) error
|
|
|
+ AutoCode(bid int, pid int, codeType string) (string, error)
|
|
|
ValidRule(ctx iris.Context) (viewmodels.ValidField, error)
|
|
|
}
|
|
|
|
|
|
// //返回service操作类
|
|
|
type ruleService struct {
|
|
|
- daoRule *dao.RuleDao
|
|
|
+ daoRule *dao.RuleDao
|
|
|
+ daoSafe *dao.SafeDao
|
|
|
+ validAutoPath string
|
|
|
}
|
|
|
|
|
|
//创建项目用户service
|
|
|
func NewRuleService() RuleService {
|
|
|
return &ruleService{
|
|
|
- daoRule: dao.NewRuleDao(datasource.InstanceDbMaster()),
|
|
|
+ validAutoPath: "/api/rule/auto",
|
|
|
+ daoRule: dao.NewRuleDao(datasource.InstanceDbMaster()),
|
|
|
+ daoSafe: dao.NewSafeDao(datasource.InstanceDbMaster()),
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -33,9 +45,56 @@ func (s *ruleService) Get(pid int, id int) viewmodels.ViewRule {
|
|
|
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) Post(pid int, id int, key string, value string) error {
|
|
|
+ err := s.daoRule.UpdateOrCreate(pid, id, key, value)
|
|
|
+ return err
|
|
|
+}
|
|
|
+
|
|
|
+// type Code struct {
|
|
|
+// Date string `from:"date" json:"date"`
|
|
|
+// Text string `from:"text" json:"text"`
|
|
|
+// Name string `from:"name" json:"name"`
|
|
|
+// Code string `from:"code" json:"code"`
|
|
|
+// }
|
|
|
+
|
|
|
+// 生成code
|
|
|
+func (s *ruleService) AutoCode(bid int, pid int, codeType string) (string, error) {
|
|
|
+ // 获取该标段的规则
|
|
|
+ rule := s.daoRule.FindByPidWithBid(pid, bid)
|
|
|
+ if codeType == "safeRule" {
|
|
|
+ var code viewmodels.RuleCode
|
|
|
+ err := json.Unmarshal([]byte(rule.SafeRule), &code)
|
|
|
+ // fmt.Println(code)
|
|
|
+ fmt.Println(err)
|
|
|
+ if err == nil {
|
|
|
+ total, err := s.daoSafe.CountRuleCode(bid)
|
|
|
+ value := reflect.ValueOf(code)
|
|
|
+ for i := 0; i < value.NumField(); i++ {
|
|
|
+ b := fmt.Sprint(value.Field(i))
|
|
|
+ if b == code.Code {
|
|
|
+ // fmt.Println(value.Field(i))
|
|
|
+ k, _ := strconv.Atoi(b)
|
|
|
+ code.Code = utils.CreateRuleCode(int64(k), total, len(code.Code))
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ e, err := json.Marshal(code)
|
|
|
+ if err != nil {
|
|
|
+ return "", err
|
|
|
+ }
|
|
|
+ // fmt.Println()
|
|
|
+ // var v interface{}
|
|
|
+ // for key, val := range code {
|
|
|
+ // fmt.Printf("%v===>%v\n", key, val)
|
|
|
+ // }
|
|
|
+ // c, _ := strconv.Atoi(code.Code)
|
|
|
+ // l := len(code.Code)
|
|
|
+ // fmt.Println(c, l, total)
|
|
|
+ // fmt.Println("total", total)
|
|
|
+ return string(e), err
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "", errors.New("生成code失败")
|
|
|
}
|
|
|
|
|
|
func (s *ruleService) ValidRule(ctx iris.Context) (viewmodels.ValidField, error) {
|
|
@@ -55,7 +114,11 @@ func (s *ruleService) ValidRule(ctx iris.Context) (viewmodels.ValidField, error)
|
|
|
log.Println("safe-ValidRule-ReadJson转换异常, error=", err)
|
|
|
return safeVaild, err
|
|
|
}
|
|
|
- err = safeVaild.Validate()
|
|
|
+ if ctx.Path() == s.validAutoPath {
|
|
|
+ err = safeVaild.ValidateAuto()
|
|
|
+ } else {
|
|
|
+ err = safeVaild.Validate()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// if ctx.Method() == "PUT" {
|