苏州瑞玛APS项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

291 lines
8.0 KiB

3 years ago
3 years ago
  1. package grmi
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/kataras/iris/v12/core/router"
  6. "leit.com/LAPP_CHEERSSON_BACKEND/container"
  7. "leit.com/LAPP_CHEERSSON_BACKEND/global"
  8. "leit.com/LAPP_CHEERSSON_BACKEND/rpc"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type GoType int
  14. const (
  15. URLDateTimeFormat = "2006-01-02 15:04:05"
  16. URLDateFormat = "2006-01-02"
  17. URLTimeFormat = "2006-01-02 15:04:05"
  18. DateTimeOutFormat = "2006-01-02 15:04:05"
  19. DateOutFormat = "2006-01-02"
  20. TimeOutFormat = "15:04:05"
  21. )
  22. const (
  23. TypeInt GoType = iota + 1
  24. TypeInt8
  25. TypeInt16
  26. TypeInt32
  27. TypeInt64
  28. TypeUint
  29. TypeUint8
  30. TypeUint16
  31. TypeUint32
  32. TypeUint64
  33. TypeFloat32
  34. TypeFloat64
  35. TypeDate
  36. TypeTime
  37. TypeDateTime
  38. TypeBool
  39. TypeString
  40. )
  41. var componentContainer = container.NewComponentContainer()
  42. var routeManager = rpc.NewRouteManager(componentContainer)
  43. var routeMapping = make(map[string]func(router.Party), 10)
  44. func RegisterRouteMapping(path string, factory func(router.Party)) {
  45. routeMapping[path] = factory
  46. }
  47. func BindRoutes(party router.Party) {
  48. for path, factory := range routeMapping {
  49. party.PartyFunc(path, factory)
  50. }
  51. var pathMapping = make(map[string][]string)
  52. for _, serviceRouteManager := range routeManager.ServiceRouteManagers() {
  53. serviceRouteManager.BindRoutes(pathMapping, party)
  54. }
  55. for path, group := range pathMapping {
  56. if len(group) > 1 {
  57. panic(fmt.Sprintf("服务路径冲突! 路径: \"%s\" 服务: %s", path, group))
  58. }
  59. }
  60. }
  61. func RepeatStrings(content string, count int) []string {
  62. result := make([]string, count)
  63. for index := range result {
  64. result[index] = content
  65. }
  66. return result
  67. }
  68. func Equal(predicate Predicate) (string, error) {
  69. if predicate.Values == nil || len(predicate.Values) != 1 {
  70. return "", errors.New("相等性比较只支持一个参数")
  71. }
  72. return fmt.Sprintf(" and %s = ?", predicate.ColumnName), nil
  73. }
  74. func NotEqual(predicate Predicate) (string, error) {
  75. if predicate.Values == nil || len(predicate.Values) != 1 {
  76. return "", errors.New("相等性比较只支持一个参数")
  77. }
  78. return fmt.Sprintf(" and %s <> ?", predicate.ColumnName), nil
  79. }
  80. func Like(predicate Predicate) (string, error) {
  81. if predicate.Values == nil || len(predicate.Values) != 1 {
  82. return "", errors.New("相似性比较只支持一个参数")
  83. }
  84. return fmt.Sprintf(" and %s like concat('%%', ?, '%%')", predicate.ColumnName), nil
  85. }
  86. func NotLike(predicate Predicate) (string, error) {
  87. if predicate.Values == nil || len(predicate.Values) != 1 {
  88. return "", errors.New("相似性比较只支持一个参数")
  89. }
  90. return fmt.Sprintf(" and %s not like concat('%%', ?, '%%')", predicate.ColumnName), nil
  91. }
  92. func IsNull(predicate Predicate) (string, error) {
  93. if predicate.Values != nil && len(predicate.Values) > 0 {
  94. return "", errors.New("为空比较比较不支持参数")
  95. }
  96. return fmt.Sprintf(" and %s is null", predicate.ColumnName), nil
  97. }
  98. func IsNotNull(predicate Predicate) (string, error) {
  99. if predicate.Values != nil && len(predicate.Values) > 0 {
  100. return "", errors.New("为空比较比较不支持参数")
  101. }
  102. return fmt.Sprintf(" and %s is not null", predicate.ColumnName), nil
  103. }
  104. func Include(predicate Predicate) (string, error) {
  105. if predicate.Values != nil && len(predicate.Values) > 0 {
  106. return fmt.Sprintf(" and %s in (%s)", predicate.ColumnName, strings.Join(RepeatStrings("?", len(predicate.Values)), ",")), nil
  107. } else {
  108. return " and 1 <> 1", nil
  109. }
  110. }
  111. func Exclude(predicate Predicate) (string, error) {
  112. if predicate.Values != nil && len(predicate.Values) > 0 {
  113. return fmt.Sprintf(" and %s not in (%s)", predicate.ColumnName, strings.Join(RepeatStrings("?", len(predicate.Values)), ",")), nil
  114. } else {
  115. return " and 1 = 1", nil
  116. }
  117. }
  118. func Approximate(predicate Predicate) (string, error) {
  119. if predicate.Values == nil || len(predicate.Values) != 1 {
  120. return "", errors.New("约等于比较比较只支持一个参数")
  121. }
  122. return fmt.Sprintf(" and datediff(second, %s, ?) = 0", predicate.ColumnName), nil
  123. }
  124. func NotApproximate(predicate Predicate) (string, error) {
  125. if predicate.Values == nil || len(predicate.Values) != 1 {
  126. return "", errors.New("约等于比较比较只支持一个参数")
  127. }
  128. return fmt.Sprintf(" and datediff(second, %s, ?) <> 0", predicate.ColumnName), nil
  129. }
  130. func GreaterThen(predicate Predicate) (string, error) {
  131. if predicate.Values == nil || len(predicate.Values) != 1 {
  132. return "", errors.New("大于比较比较只支持一个参数")
  133. }
  134. return fmt.Sprintf(" and %s > ?", predicate.ColumnName), nil
  135. }
  136. func GreaterOrEqual(predicate Predicate) (string, error) {
  137. if predicate.Values == nil || len(predicate.Values) != 1 {
  138. return "", errors.New("大于等于比较比较只支持一个参数")
  139. }
  140. return fmt.Sprintf(" and %s >= ?", predicate.ColumnName), nil
  141. }
  142. func LessThen(predicate Predicate) (string, error) {
  143. if predicate.Values == nil || len(predicate.Values) != 1 {
  144. return "", errors.New("小于比较比较只支持一个参数")
  145. }
  146. return fmt.Sprintf(" and %s < ?", predicate.ColumnName), nil
  147. }
  148. func LessOrEqual(predicate Predicate) (string, error) {
  149. if predicate.Values == nil || len(predicate.Values) != 1 {
  150. return "", errors.New("小于等于比较比较只支持一个参数")
  151. }
  152. return fmt.Sprintf(" and %s <= ?", predicate.ColumnName), nil
  153. }
  154. func Log(user *global.User, codeFile string, function string, message string) {
  155. //logs := new(models.LeitServerLog)
  156. //logs.File = codeFile
  157. //logs.Level = "info"
  158. //logs.Function = function
  159. //logs.Message = message
  160. //logs.Operator = user.UserId
  161. //logs.TimeStamp = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss")
  162. //logs.InsertRecord()
  163. }
  164. func intParser(value string) (interface{}, error) {
  165. predicateValue, err := strconv.ParseInt(value, 10, 64)
  166. if err != nil {
  167. return nil, err
  168. }
  169. return predicateValue, nil
  170. }
  171. func uint64Parser(value string) (interface{}, error) {
  172. predicateValue, err := strconv.ParseUint(value, 10, 64)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return predicateValue, nil
  177. }
  178. func floatParser(value string) (interface{}, error) {
  179. predicateValue, err := strconv.ParseFloat(value, 64)
  180. if err != nil {
  181. return nil, err
  182. }
  183. return predicateValue, nil
  184. }
  185. func dateTimeParser(value string) (interface{}, error) {
  186. predicateValue, err := time.Parse(URLDateTimeFormat, value)
  187. if err != nil {
  188. return nil, err
  189. }
  190. return predicateValue, nil
  191. }
  192. func timeParser(value string) (interface{}, error) {
  193. predicateValue, err := time.Parse(URLTimeFormat, value)
  194. if err != nil {
  195. return nil, err
  196. }
  197. return predicateValue, nil
  198. }
  199. func dateParser(value string) (interface{}, error) {
  200. predicateValue, err := time.Parse(URLDateFormat, value)
  201. if err != nil {
  202. return nil, err
  203. }
  204. return predicateValue, nil
  205. }
  206. func boolParser(value string) (interface{}, error) {
  207. predicateValue, err := strconv.ParseBool(value)
  208. if err != nil {
  209. return nil, err
  210. }
  211. return predicateValue, nil
  212. }
  213. func stringParser(value string) (interface{}, error) {
  214. return strings.TrimSpace(value), nil
  215. }
  216. var valueParsers = map[GoType]func(string) (interface{}, error){
  217. TypeInt: intParser,
  218. TypeInt8: intParser,
  219. TypeInt16: intParser,
  220. TypeInt32: intParser,
  221. TypeInt64: intParser,
  222. TypeUint: intParser,
  223. TypeUint8: intParser,
  224. TypeUint16: intParser,
  225. TypeUint32: intParser,
  226. TypeUint64: uint64Parser,
  227. TypeFloat32: floatParser,
  228. TypeFloat64: floatParser,
  229. TypeDateTime: dateTimeParser,
  230. TypeDate: dateParser,
  231. TypeTime: timeParser,
  232. TypeBool: boolParser,
  233. TypeString: stringParser,
  234. }
  235. /******************************************************************************
  236. *
  237. * @Function Name : DeleteUrlParametersEmpty
  238. *-----------------------------------------------------------------------------
  239. *
  240. * @Description : 删除urlParameters的空字符串
  241. *
  242. * @Function Parameters : 删除urlParameters的空字符串
  243. *
  244. * @Author : 张鑫
  245. *
  246. * @Date : 2021-03-24
  247. *
  248. ******************************************************************************/
  249. func DeleteUrlParametersEmpty(urlParameters *map[string]string) {
  250. for key, value := range *urlParameters {
  251. if value == "" {
  252. delete(*urlParameters, key)
  253. }
  254. }
  255. }