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.

503 lines
12 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
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
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
2 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
2 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
2 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
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
2 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
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
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
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. func (TempLowHead3A60 *Temp3A60) joinTable() (types, sqlx, condition, alias string) {
  49. //todo
  50. return "LEFT", "", "Temp3A60.id=Temp3A60_id", "tmp"
  51. }
  52. // saveAllCols 保存时是否全量保存
  53. func (TempLowHead3A60 *Temp3A60) isSaveAllCols() bool {
  54. return false
  55. }
  56. // insertPretreatment 新增前预处理
  57. func (TempLowHead3A60 *Temp3A60) insertPretreatment() error {
  58. //todo
  59. return nil
  60. }
  61. // updatePretreatment 修改前预处理
  62. func (TempLowHead3A60 *Temp3A60) updatePretreatment() error {
  63. //todo
  64. return nil
  65. }
  66. // deletePretreatment 删除前预处理
  67. func (TempLowHead3A60 *Temp3A60) deletePretreatment() error {
  68. //todo
  69. return nil
  70. }
  71. func (TempLowHead3A60 *Temp3A60) getXorm() *xorm.Engine {
  72. //todo
  73. return hskdb.GetXormAuto()
  74. }
  75. // 队列状态
  76. const (
  77. TempLowHead3A60_AutoQueueWait = 2 * time.Second
  78. TempLowHead3A60_AutoQueueStatusNone = "None"
  79. TempLowHead3A60_AutoQueueStatusWait = "Wait"
  80. TempLowHead3A60_AutoQueueStatusIng = "Ing"
  81. TempLowHead3A60_AutoQueueStatusSuccess = "Success"
  82. TempLowHead3A60_AutoQueueStatusFail = "Fail"
  83. )
  84. // 不同service对应的不同队列状态
  85. var TempLowHead3A60_AutoQueueStatusFieldMap = map[string]string{
  86. "service": "queue_status",
  87. }
  88. // 队列事件
  89. func (TempLowHead3A60 *Temp3A60) queueEvent() error {
  90. //todo
  91. return nil
  92. }
  93. // ----------------------------temp------------------------------------
  94. // Save 保存或者新增,通过wherePrimaryKey查不到数据时,新增数据,否则修改数据,参数:isAllCols...
  95. func (TempLowHead3A60 *Temp3A60) Save(mustCols ...string) (err error) {
  96. if TempLowHead3A60.service == "" {
  97. TempLowHead3A60.service = "none"
  98. }
  99. tmp := Temp3A60{}
  100. whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
  101. whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
  102. session := TempLowHead3A60.getXorm().NewSession()
  103. defer session.Close()
  104. if whereQuery != "" {
  105. session = session.Where(whereQuery, whereArgs...)
  106. }
  107. if whereCond != nil {
  108. session = session.Where(whereCond)
  109. }
  110. _, err = session.Get(&tmp)
  111. if err != nil {
  112. return
  113. }
  114. if tmp.Id == 0 {
  115. err = TempLowHead3A60.insertPretreatment()
  116. if err != nil {
  117. return
  118. }
  119. _, err = TempLowHead3A60.getXorm().Insert(TempLowHead3A60)
  120. } else {
  121. err = TempLowHead3A60.updatePretreatment()
  122. if err != nil {
  123. return
  124. }
  125. if len(mustCols) > 0 {
  126. _, err = TempLowHead3A60.getXorm().MustCols(mustCols...).Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
  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. types, sqlx, condition, alias := TempLowHead3A60.joinTable()
  147. if sqlx != "" {
  148. session = session.Join(types, sqlx, condition, alias)
  149. }
  150. if whereQuery != "" {
  151. session = session.Where(whereQuery, whereArgs...)
  152. }
  153. if whereCond != nil {
  154. session = session.Where(whereCond)
  155. }
  156. _, err = session.Get(TempLowHead3A60)
  157. if err != nil {
  158. return
  159. }
  160. TempLowHead3A60.init()
  161. return
  162. }
  163. // Data 不进行分页的数据列表
  164. func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) {
  165. if TempLowHead3A60.service == "" {
  166. TempLowHead3A60.service = "none"
  167. }
  168. q, args := TempLowHead3A60.whereData()
  169. _xorm := TempLowHead3A60.getXorm()
  170. session := _xorm.NewSession()
  171. defer func() {
  172. session.Close()
  173. }()
  174. types, sqlx, condition, alias := TempLowHead3A60.joinTable()
  175. if sqlx != "" {
  176. session = session.Join(types, sqlx, condition, alias)
  177. }
  178. if q != "" {
  179. session = session.Where(q, args...)
  180. }
  181. whereCond := TempLowHead3A60.whereDataWithCond()
  182. if whereCond != nil {
  183. session = session.Where(whereCond)
  184. }
  185. order := TempLowHead3A60.orderData()
  186. if order != "" {
  187. session = session.OrderBy(order)
  188. }
  189. session.Find(&data)
  190. for i := range data {
  191. data[i].init()
  192. }
  193. return
  194. }
  195. // PageData 进行分页的数据列表,page从0开始
  196. func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60, count int64) {
  197. if TempLowHead3A60.service == "" {
  198. TempLowHead3A60.service = "none"
  199. }
  200. q, args := TempLowHead3A60.whereData()
  201. _xorm := TempLowHead3A60.getXorm()
  202. session := _xorm.NewSession()
  203. defer func() {
  204. session.Close()
  205. }()
  206. types, sqlx, condition, alias := TempLowHead3A60.joinTable()
  207. if sqlx != "" {
  208. session = session.Join(types, sqlx, condition, alias)
  209. }
  210. if q != "" {
  211. session = session.Where(q, args...)
  212. }
  213. whereCond := TempLowHead3A60.whereDataWithCond()
  214. if whereCond != nil {
  215. session = session.Where(whereCond)
  216. }
  217. order := TempLowHead3A60.orderData()
  218. if order != "" {
  219. session = session.OrderBy(order)
  220. }
  221. count, _ = session.Limit(pageSize, page*pageSize).FindAndCount(&data)
  222. for i := range data {
  223. data[i].init()
  224. }
  225. return
  226. }
  227. // Delete 删除数据
  228. func (TempLowHead3A60 *Temp3A60) Delete() (err error) {
  229. if TempLowHead3A60.service == "" {
  230. TempLowHead3A60.service = "none"
  231. }
  232. whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
  233. whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
  234. session := TempLowHead3A60.getXorm().NewSession()
  235. defer session.Close()
  236. if whereQuery != "" {
  237. session = session.Where(whereQuery, whereArgs...)
  238. }
  239. if whereCond != nil {
  240. session = session.Where(whereCond)
  241. }
  242. err = TempLowHead3A60.deletePretreatment()
  243. if err != nil {
  244. return
  245. }
  246. _, err = session.Delete(TempLowHead3A60)
  247. return
  248. }
  249. // ----------------------------temp------------------------------------
  250. func StartQueueTaskPrefixHigHead3A60Temp3A60() {
  251. serviceName := "Prefix3A60"
  252. f := TempLowHead3A60_AutoQueueStatusFieldMap[serviceName]
  253. go func() {
  254. defer func() {
  255. err := recover()
  256. if err != nil {
  257. L.Error("StartQueueTaskPrefixHigHead3A60Temp3A60 意外退出:", err)
  258. }
  259. time.Sleep(time.Second * 5)
  260. StartQueueTaskPrefixHigHead3A60Temp3A60()
  261. }()
  262. for {
  263. onec := &Temp3A60{}
  264. hskdb.GetXormAuto().Where(fmt.Sprintf("`%s`=?", f), TempLowHead3A60_AutoQueueStatusWait).OrderBy("rand()").Get(onec)
  265. session := onec.getXorm().NewSession()
  266. defer session.Close()
  267. session = session.Table(onec)
  268. whereQuery, whereArgs := onec.wherePrimaryKey()
  269. whereCond := onec.wherePrimaryKeyWithCond()
  270. if whereQuery != "" {
  271. session = session.Where(whereQuery, whereArgs...)
  272. }
  273. if whereCond != nil {
  274. session = session.Where(whereCond)
  275. }
  276. i, err := session.Where(fmt.Sprintf("`%s`=?", f), TempLowHead3A60_AutoQueueStatusWait).Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusIng})
  277. if err == nil && i == 1 {
  278. func() {
  279. {
  280. onec.service = serviceName
  281. err := onec.queueEvent()
  282. session := onec.getXorm().NewSession()
  283. defer session.Close()
  284. session = session.Table(onec)
  285. whereQuery, whereArgs := onec.wherePrimaryKey()
  286. whereCond := onec.wherePrimaryKeyWithCond()
  287. if whereQuery != "" {
  288. session = session.Where(whereQuery, whereArgs...)
  289. }
  290. if whereCond != nil {
  291. session = session.Where(whereCond)
  292. }
  293. if err != nil {
  294. session.Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusFail})
  295. } else {
  296. session.Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusSuccess})
  297. }
  298. }
  299. }()
  300. }
  301. time.Sleep(TempLowHead3A60_AutoQueueWait)
  302. }
  303. }()
  304. }
  305. func InitializationHttpCrudPrefixHigHead3A60Temp3A60(router *hskgin.GinHelper, authoritys ...func(ctx *hskgin.GinContextHelper, TempLowHead3A60 *Temp3A60)) {
  306. var authority = func(ctx *hskgin.GinContextHelper, TempLowHead3A60 *Temp3A60) {
  307. }
  308. if len(authoritys) > 0 {
  309. authority = authoritys[0]
  310. }
  311. /*
  312. @yapi
  313. @group TempLowHead3A60
  314. @name 获取(Prefix3A60-TempLowHead3A60)单个数据
  315. @path /Prefix3A60/TempLowHead3A60/get
  316. @markdown
  317. ##
  318. ```
  319. 传入主键获取对应数据
  320. ```
  321. @end
  322. */
  323. router.Any("/Prefix3A60/TempLowHead3A60/get", func(ctx *hskgin.GinContextHelper) {
  324. req := &Temp3A60{}
  325. ctx.Bind(req)
  326. authority(ctx, req)
  327. req.service = "Prefix3A60"
  328. err := req.Load()
  329. ctx.CheckErrDisplayByError(err)
  330. ctx.DisplayByData(req)
  331. })
  332. /*
  333. @yapi
  334. @group TempLowHead3A60
  335. @name 保存(Prefix3A60-TempLowHead3A60)单个数据
  336. @path /Prefix3A60/TempLowHead3A60/save
  337. @markdown
  338. ##
  339. ```
  340. 传入整体数据插入或者保存(取决于主键是否存在)
  341. ```
  342. @end
  343. */
  344. router.Any("/Prefix3A60/TempLowHead3A60/save", func(ctx *hskgin.GinContextHelper) {
  345. req := &Temp3A60{}
  346. ctx.Bind(&req)
  347. authority(ctx, req)
  348. req.service = "Prefix3A60"
  349. err := req.Save()
  350. ctx.CheckErrDisplayByError(err)
  351. err = req.Load()
  352. ctx.CheckErrDisplayByError(err)
  353. ctx.DisplayByData(req)
  354. })
  355. /*
  356. @yapi
  357. @group TempLowHead3A60
  358. @name 获取(Prefix3A60-TempLowHead3A60)无分页数据
  359. @path /Prefix3A60/TempLowHead3A60/data
  360. @markdown
  361. ##
  362. ```
  363. 获取数据列表,无分页
  364. ```
  365. @end
  366. */
  367. router.Any("/Prefix3A60/TempLowHead3A60/data", func(ctx *hskgin.GinContextHelper) {
  368. req := &Temp3A60{}
  369. ctx.Bind(&req)
  370. authority(ctx, req)
  371. req.service = "Prefix3A60"
  372. data := req.Data()
  373. ctx.DisplayByData(data)
  374. })
  375. /*
  376. @yapi
  377. @group TempLowHead3A60
  378. @name 获取(Prefix3A60-TempLowHead3A60)分页数据
  379. @path /Prefix3A60/TempLowHead3A60/list
  380. @markdown
  381. ##
  382. ```
  383. 获取数据列表,分页,page参数
  384. ```
  385. @end
  386. */
  387. router.Any("/Prefix3A60/TempLowHead3A60/list", func(ctx *hskgin.GinContextHelper) {
  388. req := &Temp3A60{}
  389. ctx.Bind(&req)
  390. authority(ctx, req)
  391. page := ctx.ParamRequired2Page(true)
  392. req.service = "Prefix3A60"
  393. data, count := req.PageData(page, 10)
  394. ctx.DisplayByPage(data, count)
  395. })
  396. /*
  397. @yapi
  398. @group TempLowHead3A60
  399. @name 删除一条(Prefix3A60-TempLowHead3A60)数据
  400. @path /Prefix3A60/TempLowHead3A60/delete
  401. @markdown
  402. ##
  403. ```
  404. 传入主键参数,删除一条数据
  405. ```
  406. @end
  407. */
  408. router.Any("/Prefix3A60/TempLowHead3A60/delete", func(ctx *hskgin.GinContextHelper) {
  409. req := &Temp3A60{}
  410. ctx.Bind(&req)
  411. authority(ctx, req)
  412. req.service = "Prefix3A60"
  413. err := req.Delete()
  414. ctx.CheckErrDisplayByError(err)
  415. ctx.DisplayBySuccess("删除成功")
  416. })
  417. }