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.

399 lines
9.9 KiB

4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "git.ouxuan.net/hasaki-service/hasaki-sdk/hskdb"
  6. "git.ouxuan.net/hasaki-service/hasaki-sdk/hskgin"
  7. "git.ouxuan.net/hasaki-service/hasaki-sdk/hsklogger/L"
  8. "xorm.io/builder"
  9. "xorm.io/xorm"
  10. )
  11. type Temp3A60 struct {
  12. Id int
  13. service string
  14. QueueStatus string
  15. }
  16. // ----------------------------temp------------------------------------
  17. // wherePrimaryKey 主键查询
  18. func (TempLowHead3A60 *Temp3A60) wherePrimaryKey() (q interface{}, args []interface{}) {
  19. //todo 根据需求修改
  20. return "`id`=?", []interface{}{
  21. TempLowHead3A60.Id,
  22. }
  23. }
  24. // wherePrimaryKeyWithCond 主键查询
  25. func (TempLowHead3A60 *Temp3A60) wherePrimaryKeyWithCond() builder.Cond {
  26. //todo 根据需求修改
  27. return nil
  28. }
  29. // whereDataWithCond 列表查询
  30. func (TempLowHead3A60 *Temp3A60) whereDataWithCond() builder.Cond {
  31. //todo 根据需求修改
  32. return nil
  33. }
  34. // whereData 列表查询筛选
  35. func (TempLowHead3A60 *Temp3A60) whereData() (q interface{}, args []interface{}) {
  36. //todo
  37. return "", nil
  38. }
  39. // orderData 列表查询排序
  40. func (TempLowHead3A60 *Temp3A60) orderData() string {
  41. //todo
  42. return "`id` desc"
  43. }
  44. // init 数据初始化,可附加更多数据
  45. func (TempLowHead3A60 *Temp3A60) init() {
  46. //todo
  47. }
  48. // saveAllCols 保存时是否全量保存
  49. func (TempLowHead3A60 *Temp3A60) isSaveAllCols() bool {
  50. return false
  51. }
  52. // insertPretreatment 新增前预处理
  53. func (TempLowHead3A60 *Temp3A60) insertPretreatment() error {
  54. //todo
  55. return nil
  56. }
  57. // updatePretreatment 修改前预处理
  58. func (TempLowHead3A60 *Temp3A60) updatePretreatment() error {
  59. //todo
  60. return nil
  61. }
  62. // deletePretreatment 删除前预处理
  63. func (TempLowHead3A60 *Temp3A60) deletePretreatment() error {
  64. //todo
  65. return nil
  66. }
  67. func (TempLowHead3A60 *Temp3A60) getXorm() *xorm.Engine {
  68. //todo
  69. return hskdb.GetXormAuto()
  70. }
  71. // 队列状态
  72. const (
  73. TempLowHead3A60_AutoQueueWait = 2 * time.Second
  74. TempLowHead3A60_AutoQueueStatusNone = "None"
  75. TempLowHead3A60_AutoQueueStatusWait = "Wait"
  76. TempLowHead3A60_AutoQueueStatusIng = "Ing"
  77. TempLowHead3A60_AutoQueueStatusSuccess = "Success"
  78. TempLowHead3A60_AutoQueueStatusFail = "Fail"
  79. )
  80. // 不同service对应的不同队列状态
  81. var TempLowHead3A60_AutoQueueStatusFieldMap = map[string]string{
  82. "service": "queue_status",
  83. }
  84. // 队列事件
  85. func (TempLowHead3A60 *Temp3A60) queueEvent() error {
  86. //todo
  87. return nil
  88. }
  89. // ----------------------------temp------------------------------------
  90. // Save 保存或者新增,通过wherePrimaryKey查不到数据时,新增数据,否则修改数据,参数:isAllCols...
  91. func (TempLowHead3A60 *Temp3A60) Save(isAllCols ...bool) (err error) {
  92. if TempLowHead3A60.service == "" {
  93. TempLowHead3A60.service = "none"
  94. }
  95. tmp := Temp3A60{}
  96. whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
  97. whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
  98. session := TempLowHead3A60.getXorm().NewSession()
  99. defer session.Close()
  100. if whereQuery != "" {
  101. session = session.Where(whereQuery, whereArgs...)
  102. }
  103. if whereCond != nil {
  104. session = session.Where(whereCond)
  105. }
  106. _, err = session.Get(&tmp)
  107. if err != nil {
  108. return
  109. }
  110. if tmp.Id == 0 {
  111. err = TempLowHead3A60.insertPretreatment()
  112. if err != nil {
  113. return
  114. }
  115. _, err = TempLowHead3A60.getXorm().Insert(TempLowHead3A60)
  116. } else {
  117. err = TempLowHead3A60.updatePretreatment()
  118. if err != nil {
  119. return
  120. }
  121. if len(isAllCols) > 0 {
  122. if isAllCols[0] {
  123. _, err = TempLowHead3A60.getXorm().AllCols().Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
  124. } else {
  125. _, err = TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
  126. }
  127. } else {
  128. if TempLowHead3A60.isSaveAllCols() {
  129. _, err = TempLowHead3A60.getXorm().AllCols().Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
  130. } else {
  131. _, err = TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
  132. }
  133. }
  134. }
  135. return err
  136. }
  137. // Load 为实例载入数据,数据来源取决于wherePrimaryKey查找实例中的数据
  138. func (TempLowHead3A60 *Temp3A60) Load() (err error) {
  139. if TempLowHead3A60.service == "" {
  140. TempLowHead3A60.service = "none"
  141. }
  142. whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
  143. whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
  144. session := TempLowHead3A60.getXorm().NewSession()
  145. defer session.Close()
  146. if whereQuery != "" {
  147. session = session.Where(whereQuery, whereArgs...)
  148. }
  149. if whereCond != nil {
  150. session = session.Where(whereCond)
  151. }
  152. _, err = session.Get(TempLowHead3A60)
  153. if err != nil {
  154. return
  155. }
  156. TempLowHead3A60.init()
  157. return
  158. }
  159. // Data 不进行分页的数据列表
  160. func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) {
  161. if TempLowHead3A60.service == "" {
  162. TempLowHead3A60.service = "none"
  163. }
  164. q, args := TempLowHead3A60.whereData()
  165. _xorm := TempLowHead3A60.getXorm()
  166. session := _xorm.NewSession()
  167. defer func() {
  168. session.Close()
  169. }()
  170. if q != "" {
  171. session = session.Where(q, args...)
  172. }
  173. whereCond := TempLowHead3A60.whereDataWithCond()
  174. if whereCond != nil {
  175. session = session.Where(whereCond)
  176. }
  177. order := TempLowHead3A60.orderData()
  178. if order != "" {
  179. session = session.OrderBy(order)
  180. }
  181. session.Find(&data)
  182. for i := range data {
  183. data[i].init()
  184. }
  185. return
  186. }
  187. // PageData 进行分页的数据列表,page从0开始
  188. func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60, count int64) {
  189. if TempLowHead3A60.service == "" {
  190. TempLowHead3A60.service = "none"
  191. }
  192. q, args := TempLowHead3A60.whereData()
  193. _xorm := TempLowHead3A60.getXorm()
  194. session := _xorm.NewSession()
  195. defer func() {
  196. session.Close()
  197. }()
  198. if q != "" {
  199. session = session.Where(q, args...)
  200. }
  201. whereCond := TempLowHead3A60.whereDataWithCond()
  202. if whereCond != nil {
  203. session = session.Where(whereCond)
  204. }
  205. order := TempLowHead3A60.orderData()
  206. if order != "" {
  207. session = session.OrderBy(order)
  208. }
  209. count, _ = session.Limit(pageSize, page*pageSize).FindAndCount(&data)
  210. for i := range data {
  211. data[i].init()
  212. }
  213. return
  214. }
  215. // Delete 删除数据
  216. func (TempLowHead3A60 *Temp3A60) Delete() (err error) {
  217. if TempLowHead3A60.service == "" {
  218. TempLowHead3A60.service = "none"
  219. }
  220. whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
  221. whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
  222. session := TempLowHead3A60.getXorm().NewSession()
  223. defer session.Close()
  224. if whereQuery != "" {
  225. session = session.Where(whereQuery, whereArgs...)
  226. }
  227. if whereCond != nil {
  228. session = session.Where(whereCond)
  229. }
  230. err = TempLowHead3A60.deletePretreatment()
  231. if err != nil {
  232. return
  233. }
  234. _, err = session.Delete(TempLowHead3A60)
  235. return
  236. }
  237. // ----------------------------temp------------------------------------
  238. func StartQueueTaskPrefixHigHead3A60Temp3A60() {
  239. serviceName := "Prefix3A60"
  240. f := TempLowHead3A60_AutoQueueStatusFieldMap[serviceName]
  241. go func() {
  242. defer func() {
  243. err := recover()
  244. if err != nil {
  245. L.Error("StartQueueTaskPrefixHigHead3A60Temp3A60 意外退出:", err)
  246. }
  247. time.Sleep(time.Second * 5)
  248. StartQueueTaskPrefixHigHead3A60Temp3A60()
  249. }()
  250. for {
  251. onec := &Temp3A60{}
  252. hskdb.GetXormAuto().Where(fmt.Sprintf("`%s`=?", f), TempLowHead3A60_AutoQueueStatusWait).OrderBy("rand()").Get(onec)
  253. session := onec.getXorm().NewSession()
  254. defer session.Close()
  255. session = session.Table(onec)
  256. whereQuery, whereArgs := onec.wherePrimaryKey()
  257. whereCond := onec.wherePrimaryKeyWithCond()
  258. if whereQuery != "" {
  259. session = session.Where(whereQuery, whereArgs...)
  260. }
  261. if whereCond != nil {
  262. session = session.Where(whereCond)
  263. }
  264. i, err := session.Where(fmt.Sprintf("`%s`=?", f), TempLowHead3A60_AutoQueueStatusWait).Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusIng})
  265. if err == nil && i == 1 {
  266. func() {
  267. {
  268. err := onec.queueEvent()
  269. session := onec.getXorm().NewSession()
  270. defer session.Close()
  271. session = session.Table(onec)
  272. whereQuery, whereArgs := onec.wherePrimaryKey()
  273. whereCond := onec.wherePrimaryKeyWithCond()
  274. if whereQuery != "" {
  275. session = session.Where(whereQuery, whereArgs...)
  276. }
  277. if whereCond != nil {
  278. session = session.Where(whereCond)
  279. }
  280. if err != nil {
  281. session.Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusFail})
  282. } else {
  283. session.Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusSuccess})
  284. }
  285. }
  286. }()
  287. }
  288. time.Sleep(TempLowHead3A60_AutoQueueWait)
  289. }
  290. }()
  291. }
  292. func InitializationHttpCrudPrefixHigHead3A60Temp3A60(router *hskgin.GinHelper, authoritys ...func(ctx *hskgin.GinContextHelper, TempLowHead3A60 *Temp3A60)) {
  293. var authority = func(ctx *hskgin.GinContextHelper, TempLowHead3A60 *Temp3A60) {
  294. }
  295. if len(authoritys) > 0 {
  296. authority = authoritys[0]
  297. }
  298. router.Any("/Prefix3A60/TempLowHead3A60/get", func(ctx *hskgin.GinContextHelper) {
  299. req := &Temp3A60{}
  300. ctx.Bind(req)
  301. authority(ctx, req)
  302. req.service = "Prefix3A60"
  303. err := req.Load()
  304. ctx.CheckErrDisplayByError(err)
  305. ctx.DisplayByData(req)
  306. })
  307. router.Any("/Prefix3A60/TempLowHead3A60/save", func(ctx *hskgin.GinContextHelper) {
  308. req := &Temp3A60{}
  309. ctx.Bind(&req)
  310. authority(ctx, req)
  311. req.service = "Prefix3A60"
  312. err := req.Save()
  313. ctx.CheckErrDisplayByError(err)
  314. err = req.Load()
  315. ctx.CheckErrDisplayByError(err)
  316. ctx.DisplayByData(req)
  317. })
  318. router.Any("/Prefix3A60/TempLowHead3A60/data", func(ctx *hskgin.GinContextHelper) {
  319. req := &Temp3A60{}
  320. ctx.Bind(&req)
  321. authority(ctx, req)
  322. req.service = "Prefix3A60"
  323. data := req.Data()
  324. ctx.DisplayByData(data)
  325. })
  326. router.Any("/Prefix3A60/TempLowHead3A60/list", func(ctx *hskgin.GinContextHelper) {
  327. req := &Temp3A60{}
  328. ctx.Bind(&req)
  329. authority(ctx, req)
  330. page := ctx.ParamRequired2Page(true)
  331. req.service = "Prefix3A60"
  332. data, count := req.PageData(page, 10)
  333. ctx.DisplayByPage(data, count)
  334. })
  335. router.Any("/Prefix3A60/TempLowHead3A60/delete", func(ctx *hskgin.GinContextHelper) {
  336. req := &Temp3A60{}
  337. ctx.Bind(&req)
  338. authority(ctx, req)
  339. req.service = "Prefix3A60"
  340. err := req.Delete()
  341. ctx.CheckErrDisplayByError(err)
  342. ctx.DisplayBySuccess("删除成功")
  343. })
  344. }