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.

396 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
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
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
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(mustCols ...string) (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(mustCols) > 0 {
  122. _, err = TempLowHead3A60.getXorm().MustCols(mustCols...).Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
  123. } else {
  124. if TempLowHead3A60.isSaveAllCols() {
  125. _, err = TempLowHead3A60.getXorm().AllCols().Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
  126. } else {
  127. _, err = TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
  128. }
  129. }
  130. }
  131. return err
  132. }
  133. // Load 为实例载入数据,数据来源取决于wherePrimaryKey查找实例中的数据
  134. func (TempLowHead3A60 *Temp3A60) Load() (err error) {
  135. if TempLowHead3A60.service == "" {
  136. TempLowHead3A60.service = "none"
  137. }
  138. whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
  139. whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
  140. session := TempLowHead3A60.getXorm().NewSession()
  141. defer session.Close()
  142. if whereQuery != "" {
  143. session = session.Where(whereQuery, whereArgs...)
  144. }
  145. if whereCond != nil {
  146. session = session.Where(whereCond)
  147. }
  148. _, err = session.Get(TempLowHead3A60)
  149. if err != nil {
  150. return
  151. }
  152. TempLowHead3A60.init()
  153. return
  154. }
  155. // Data 不进行分页的数据列表
  156. func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) {
  157. if TempLowHead3A60.service == "" {
  158. TempLowHead3A60.service = "none"
  159. }
  160. q, args := TempLowHead3A60.whereData()
  161. _xorm := TempLowHead3A60.getXorm()
  162. session := _xorm.NewSession()
  163. defer func() {
  164. session.Close()
  165. }()
  166. if q != "" {
  167. session = session.Where(q, args...)
  168. }
  169. whereCond := TempLowHead3A60.whereDataWithCond()
  170. if whereCond != nil {
  171. session = session.Where(whereCond)
  172. }
  173. order := TempLowHead3A60.orderData()
  174. if order != "" {
  175. session = session.OrderBy(order)
  176. }
  177. session.Find(&data)
  178. for i := range data {
  179. data[i].init()
  180. }
  181. return
  182. }
  183. // PageData 进行分页的数据列表,page从0开始
  184. func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60, count int64) {
  185. if TempLowHead3A60.service == "" {
  186. TempLowHead3A60.service = "none"
  187. }
  188. q, args := TempLowHead3A60.whereData()
  189. _xorm := TempLowHead3A60.getXorm()
  190. session := _xorm.NewSession()
  191. defer func() {
  192. session.Close()
  193. }()
  194. if q != "" {
  195. session = session.Where(q, args...)
  196. }
  197. whereCond := TempLowHead3A60.whereDataWithCond()
  198. if whereCond != nil {
  199. session = session.Where(whereCond)
  200. }
  201. order := TempLowHead3A60.orderData()
  202. if order != "" {
  203. session = session.OrderBy(order)
  204. }
  205. count, _ = session.Limit(pageSize, page*pageSize).FindAndCount(&data)
  206. for i := range data {
  207. data[i].init()
  208. }
  209. return
  210. }
  211. // Delete 删除数据
  212. func (TempLowHead3A60 *Temp3A60) Delete() (err error) {
  213. if TempLowHead3A60.service == "" {
  214. TempLowHead3A60.service = "none"
  215. }
  216. whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
  217. whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
  218. session := TempLowHead3A60.getXorm().NewSession()
  219. defer session.Close()
  220. if whereQuery != "" {
  221. session = session.Where(whereQuery, whereArgs...)
  222. }
  223. if whereCond != nil {
  224. session = session.Where(whereCond)
  225. }
  226. err = TempLowHead3A60.deletePretreatment()
  227. if err != nil {
  228. return
  229. }
  230. _, err = session.Delete(TempLowHead3A60)
  231. return
  232. }
  233. // ----------------------------temp------------------------------------
  234. func StartQueueTaskPrefixHigHead3A60Temp3A60() {
  235. serviceName := "Prefix3A60"
  236. f := TempLowHead3A60_AutoQueueStatusFieldMap[serviceName]
  237. go func() {
  238. defer func() {
  239. err := recover()
  240. if err != nil {
  241. L.Error("StartQueueTaskPrefixHigHead3A60Temp3A60 意外退出:", err)
  242. }
  243. time.Sleep(time.Second * 5)
  244. StartQueueTaskPrefixHigHead3A60Temp3A60()
  245. }()
  246. for {
  247. onec := &Temp3A60{}
  248. hskdb.GetXormAuto().Where(fmt.Sprintf("`%s`=?", f), TempLowHead3A60_AutoQueueStatusWait).OrderBy("rand()").Get(onec)
  249. session := onec.getXorm().NewSession()
  250. defer session.Close()
  251. session = session.Table(onec)
  252. whereQuery, whereArgs := onec.wherePrimaryKey()
  253. whereCond := onec.wherePrimaryKeyWithCond()
  254. if whereQuery != "" {
  255. session = session.Where(whereQuery, whereArgs...)
  256. }
  257. if whereCond != nil {
  258. session = session.Where(whereCond)
  259. }
  260. i, err := session.Where(fmt.Sprintf("`%s`=?", f), TempLowHead3A60_AutoQueueStatusWait).Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusIng})
  261. if err == nil && i == 1 {
  262. func() {
  263. {
  264. onec.service = serviceName
  265. err := onec.queueEvent()
  266. session := onec.getXorm().NewSession()
  267. defer session.Close()
  268. session = session.Table(onec)
  269. whereQuery, whereArgs := onec.wherePrimaryKey()
  270. whereCond := onec.wherePrimaryKeyWithCond()
  271. if whereQuery != "" {
  272. session = session.Where(whereQuery, whereArgs...)
  273. }
  274. if whereCond != nil {
  275. session = session.Where(whereCond)
  276. }
  277. if err != nil {
  278. session.Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusFail})
  279. } else {
  280. session.Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusSuccess})
  281. }
  282. }
  283. }()
  284. }
  285. time.Sleep(TempLowHead3A60_AutoQueueWait)
  286. }
  287. }()
  288. }
  289. func InitializationHttpCrudPrefixHigHead3A60Temp3A60(router *hskgin.GinHelper, authoritys ...func(ctx *hskgin.GinContextHelper, TempLowHead3A60 *Temp3A60)) {
  290. var authority = func(ctx *hskgin.GinContextHelper, TempLowHead3A60 *Temp3A60) {
  291. }
  292. if len(authoritys) > 0 {
  293. authority = authoritys[0]
  294. }
  295. router.Any("/Prefix3A60/TempLowHead3A60/get", func(ctx *hskgin.GinContextHelper) {
  296. req := &Temp3A60{}
  297. ctx.Bind(req)
  298. authority(ctx, req)
  299. req.service = "Prefix3A60"
  300. err := req.Load()
  301. ctx.CheckErrDisplayByError(err)
  302. ctx.DisplayByData(req)
  303. })
  304. router.Any("/Prefix3A60/TempLowHead3A60/save", func(ctx *hskgin.GinContextHelper) {
  305. req := &Temp3A60{}
  306. ctx.Bind(&req)
  307. authority(ctx, req)
  308. req.service = "Prefix3A60"
  309. err := req.Save()
  310. ctx.CheckErrDisplayByError(err)
  311. err = req.Load()
  312. ctx.CheckErrDisplayByError(err)
  313. ctx.DisplayByData(req)
  314. })
  315. router.Any("/Prefix3A60/TempLowHead3A60/data", func(ctx *hskgin.GinContextHelper) {
  316. req := &Temp3A60{}
  317. ctx.Bind(&req)
  318. authority(ctx, req)
  319. req.service = "Prefix3A60"
  320. data := req.Data()
  321. ctx.DisplayByData(data)
  322. })
  323. router.Any("/Prefix3A60/TempLowHead3A60/list", func(ctx *hskgin.GinContextHelper) {
  324. req := &Temp3A60{}
  325. ctx.Bind(&req)
  326. authority(ctx, req)
  327. page := ctx.ParamRequired2Page(true)
  328. req.service = "Prefix3A60"
  329. data, count := req.PageData(page, 10)
  330. ctx.DisplayByPage(data, count)
  331. })
  332. router.Any("/Prefix3A60/TempLowHead3A60/delete", func(ctx *hskgin.GinContextHelper) {
  333. req := &Temp3A60{}
  334. ctx.Bind(&req)
  335. authority(ctx, req)
  336. req.service = "Prefix3A60"
  337. err := req.Delete()
  338. ctx.CheckErrDisplayByError(err)
  339. ctx.DisplayBySuccess("删除成功")
  340. })
  341. }