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.

611 lines
14 KiB

4 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
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
2 years ago
2 years ago
2 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
2 years ago
2 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
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
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
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
4 years ago
4 years ago
2 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
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
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. "reflect"
  5. "time"
  6. "git.ouxuan.net/hasaki-service/hasaki-sdk/hskdb"
  7. "git.ouxuan.net/hasaki-service/hasaki-sdk/hskgin"
  8. "git.ouxuan.net/hasaki-service/hasaki-sdk/hsklogger/L"
  9. "git.ouxuan.net/hasaki-service/hasaki-sdk/hskutils"
  10. "github.com/spf13/cast"
  11. "github.com/tidwall/gjson"
  12. "xorm.io/builder"
  13. "xorm.io/xorm"
  14. )
  15. type Temp3A60 struct {
  16. Id int
  17. service string
  18. QueueStatus string
  19. }
  20. // ----------------------------temp------------------------------------
  21. // wherePrimaryKey 主键查询
  22. func (TempLowHead3A60 *Temp3A60) wherePrimaryKey() (q interface{}, args []interface{}) {
  23. //todo 根据需求修改
  24. return "`id`=?", []interface{}{
  25. TempLowHead3A60.Id,
  26. }
  27. }
  28. // wherePrimaryKeyWithCond 主键查询
  29. func (TempLowHead3A60 *Temp3A60) wherePrimaryKeyWithCond() builder.Cond {
  30. //todo 根据需求修改
  31. return nil
  32. }
  33. // whereDataWithCond 列表查询
  34. func (TempLowHead3A60 *Temp3A60) whereDataWithCond() builder.Cond {
  35. //todo 根据需求修改
  36. return nil
  37. }
  38. // whereData 列表查询筛选
  39. func (TempLowHead3A60 *Temp3A60) whereData() (q interface{}, args []interface{}) {
  40. //todo
  41. return "", nil
  42. }
  43. // orderData 列表查询排序
  44. func (TempLowHead3A60 *Temp3A60) orderData() string {
  45. //todo
  46. return "`id` desc"
  47. }
  48. // init 数据初始化,可附加更多数据
  49. func (TempLowHead3A60 *Temp3A60) init() {
  50. //todo
  51. }
  52. func (TempLowHead3A60 *Temp3A60) joinTable() (typesAndsqlxAndcondition [][]string) {
  53. //todo
  54. return [][]string{
  55. // {"LEFT", "(SELECT id as external_brand_id,name as brand_name FROM `ox_brand`) as tmp", "tmp.external_brand_id=brand_id"},
  56. }
  57. }
  58. // saveAllCols 保存时是否全量保存
  59. func (TempLowHead3A60 *Temp3A60) isSaveAllCols() bool {
  60. return false
  61. }
  62. // insertPretreatment 新增前预处理
  63. func (TempLowHead3A60 *Temp3A60) insertPretreatment() error {
  64. //todo
  65. return nil
  66. }
  67. // updatePretreatment 修改前预处理
  68. func (TempLowHead3A60 *Temp3A60) updatePretreatment() error {
  69. //todo
  70. return nil
  71. }
  72. // deletePretreatment 删除前预处理
  73. func (TempLowHead3A60 *Temp3A60) deletePretreatment() error {
  74. //todo
  75. return nil
  76. }
  77. func (TempLowHead3A60 *Temp3A60) getXorm() *xorm.Engine {
  78. //todo
  79. return hskdb.GetXormAuto()
  80. }
  81. // 队列状态
  82. const (
  83. TempLowHead3A60_AutoQueueWait = 2 * time.Second
  84. TempLowHead3A60_AutoQueueStatusNone = "None"
  85. TempLowHead3A60_AutoQueueStatusWait = "Wait"
  86. TempLowHead3A60_AutoQueueStatusIng = "Ing"
  87. TempLowHead3A60_AutoQueueStatusSuccess = "Success"
  88. TempLowHead3A60_AutoQueueStatusFail = "Fail"
  89. )
  90. // 不同service对应的不同队列状态
  91. var TempLowHead3A60_AutoQueueStatusFieldMap = map[string]string{
  92. "service": "queue_status",
  93. }
  94. // 队列事件
  95. func (TempLowHead3A60 *Temp3A60) queueEvent() error {
  96. //todo
  97. return nil
  98. }
  99. // ----------------------------temp------------------------------------
  100. // Save 保存或者新增,通过wherePrimaryKey查不到数据时,新增数据,否则修改数据,参数:isAllCols...
  101. func (TempLowHead3A60 *Temp3A60) Save(mustCols ...string) (err error) {
  102. if TempLowHead3A60.service == "" {
  103. TempLowHead3A60.service = "none"
  104. }
  105. tmp := Temp3A60{}
  106. whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
  107. whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
  108. session := TempLowHead3A60.getXorm().NewSession()
  109. defer session.Close()
  110. if whereQuery != "" {
  111. session = session.Where(whereQuery, whereArgs...)
  112. }
  113. if whereCond != nil {
  114. session = session.Where(whereCond)
  115. }
  116. _, err = session.Get(&tmp)
  117. if err != nil {
  118. return
  119. }
  120. if tmp.Id == 0 {
  121. err = TempLowHead3A60.insertPretreatment()
  122. if err != nil {
  123. return
  124. }
  125. _, err = TempLowHead3A60.getXorm().Insert(TempLowHead3A60)
  126. } else {
  127. err = TempLowHead3A60.updatePretreatment()
  128. if err != nil {
  129. return
  130. }
  131. if len(mustCols) > 0 {
  132. _, err = TempLowHead3A60.getXorm().MustCols(mustCols...).Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
  133. } else {
  134. if TempLowHead3A60.isSaveAllCols() {
  135. _, err = TempLowHead3A60.getXorm().AllCols().Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
  136. } else {
  137. _, err = TempLowHead3A60.getXorm().Where(whereQuery, whereArgs...).Update(TempLowHead3A60)
  138. }
  139. }
  140. }
  141. return err
  142. }
  143. func (TempLowHead3A60 *Temp3A60) Service(key string) *Temp3A60 {
  144. TempLowHead3A60.service = key
  145. return TempLowHead3A60
  146. }
  147. func (TempLowHead3A60 *Temp3A60) getFieldTypes() map[string]reflect.Kind {
  148. types := reflect.TypeOf(TempLowHead3A60)
  149. fieldType := map[string]reflect.Kind{}
  150. for i := 0; i < types.Elem().NumField(); i++ {
  151. fieldType[string(types.Elem().Field(i).Tag.Get("json"))] = types.Elem().Field(i).Type.Kind()
  152. }
  153. return fieldType
  154. }
  155. func (TempLowHead3A60 *Temp3A60) fixSqlRowMap(fieldType map[string]reflect.Kind, raw map[string]interface{}) map[string]interface{} {
  156. for i := range raw {
  157. switch fieldType[i] {
  158. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  159. raw[i] = cast.ToInt64(raw[i])
  160. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  161. raw[i] = cast.ToUint64(raw[i])
  162. case reflect.Float32, reflect.Float64:
  163. raw[i] = cast.ToFloat64(raw[i])
  164. case reflect.Bool:
  165. raw[i] = cast.ToBool(raw[i])
  166. case reflect.String:
  167. raw[i] = cast.ToString(raw[i])
  168. case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct, reflect.Ptr:
  169. raw[i] = gjson.Parse(cast.ToString(raw[i])).Value()
  170. default:
  171. raw[i] = cast.ToString(raw[i])
  172. }
  173. }
  174. return raw
  175. }
  176. // Load 为实例载入数据,数据来源取决于wherePrimaryKey查找实例中的数据
  177. func (TempLowHead3A60 *Temp3A60) Load() (err error) {
  178. if TempLowHead3A60.service == "" {
  179. TempLowHead3A60.service = "none"
  180. }
  181. whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
  182. whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
  183. session := TempLowHead3A60.getXorm().NewSession()
  184. defer session.Close()
  185. typesAndSqlxAndCndition := TempLowHead3A60.joinTable()
  186. isJoin := false
  187. for _, v := range typesAndSqlxAndCndition {
  188. isJoin = true
  189. session = session.Join(v[0], v[1], v[2])
  190. }
  191. if whereQuery != "" {
  192. session = session.Where(whereQuery, whereArgs...)
  193. }
  194. if whereCond != nil {
  195. session = session.Where(whereCond)
  196. }
  197. if !isJoin {
  198. _, err = session.Get(TempLowHead3A60)
  199. if err != nil {
  200. return
  201. }
  202. } else {
  203. fieldType := TempLowHead3A60.getFieldTypes()
  204. session = session.Table(TempLowHead3A60)
  205. raw := map[string]interface{}{}
  206. session.Get(&raw)
  207. raw = TempLowHead3A60.fixSqlRowMap(fieldType, raw)
  208. hskutils.Interface2Struct(raw, TempLowHead3A60)
  209. }
  210. TempLowHead3A60.init()
  211. return
  212. }
  213. // Data 不进行分页的数据列表
  214. func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) {
  215. if TempLowHead3A60.service == "" {
  216. TempLowHead3A60.service = "none"
  217. }
  218. q, args := TempLowHead3A60.whereData()
  219. _xorm := TempLowHead3A60.getXorm()
  220. session := _xorm.NewSession()
  221. defer func() {
  222. session.Close()
  223. }()
  224. typesAndSqlxAndCndition := TempLowHead3A60.joinTable()
  225. isJoin := false
  226. for _, v := range typesAndSqlxAndCndition {
  227. isJoin = true
  228. session = session.Join(v[0], v[1], v[2])
  229. }
  230. if q != "" {
  231. session = session.Where(q, args...)
  232. }
  233. whereCond := TempLowHead3A60.whereDataWithCond()
  234. if whereCond != nil {
  235. session = session.Where(whereCond)
  236. }
  237. order := TempLowHead3A60.orderData()
  238. if order != "" {
  239. session = session.OrderBy(order)
  240. }
  241. if !isJoin {
  242. session.Find(&data)
  243. } else {
  244. fieldType := TempLowHead3A60.getFieldTypes()
  245. session = session.Table(TempLowHead3A60)
  246. raw := []map[string]interface{}{}
  247. session.Find(&raw)
  248. for i := range raw {
  249. item := Temp3A60{}
  250. raw[i] = TempLowHead3A60.fixSqlRowMap(fieldType, raw[i])
  251. hskutils.Interface2Struct(raw[i], &item)
  252. data = append(data, item)
  253. }
  254. }
  255. for i := range data {
  256. data[i].init()
  257. }
  258. return
  259. }
  260. // PageData 进行分页的数据列表,page从0开始
  261. func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60, count int64) {
  262. if TempLowHead3A60.service == "" {
  263. TempLowHead3A60.service = "none"
  264. }
  265. q, args := TempLowHead3A60.whereData()
  266. _xorm := TempLowHead3A60.getXorm()
  267. session := _xorm.NewSession()
  268. defer func() {
  269. session.Close()
  270. }()
  271. typesAndSqlxAndCndition := TempLowHead3A60.joinTable()
  272. isJoin := false
  273. for _, v := range typesAndSqlxAndCndition {
  274. isJoin = true
  275. session = session.Join(v[0], v[1], v[2])
  276. }
  277. if q != "" {
  278. session = session.Where(q, args...)
  279. }
  280. whereCond := TempLowHead3A60.whereDataWithCond()
  281. if whereCond != nil {
  282. session = session.Where(whereCond)
  283. }
  284. order := TempLowHead3A60.orderData()
  285. if order != "" {
  286. session = session.OrderBy(order)
  287. }
  288. session = session.Limit(pageSize, page*pageSize)
  289. if !isJoin {
  290. count, _ = session.FindAndCount(&data)
  291. } else {
  292. fieldType := TempLowHead3A60.getFieldTypes()
  293. session = session.Table(TempLowHead3A60)
  294. raw := []map[string]interface{}{}
  295. count, _ = session.FindAndCount(&raw)
  296. //xorm兼容性处理
  297. for i := range raw {
  298. for k := range raw[i] {
  299. value := gjson.Parse(cast.ToString(raw[i][k])).Value()
  300. if value != nil {
  301. raw[i][k] = value
  302. } else {
  303. raw[i][k] = cast.ToString(raw[i][k])
  304. }
  305. }
  306. }
  307. for i := range raw {
  308. item := Temp3A60{}
  309. raw[i] = TempLowHead3A60.fixSqlRowMap(fieldType, raw[i])
  310. hskutils.Interface2Struct(raw[i], &item)
  311. data = append(data, item)
  312. }
  313. }
  314. for i := range data {
  315. data[i].init()
  316. }
  317. return
  318. }
  319. // Delete 删除数据
  320. func (TempLowHead3A60 *Temp3A60) Delete() (err error) {
  321. if TempLowHead3A60.service == "" {
  322. TempLowHead3A60.service = "none"
  323. }
  324. whereQuery, whereArgs := TempLowHead3A60.wherePrimaryKey()
  325. whereCond := TempLowHead3A60.wherePrimaryKeyWithCond()
  326. session := TempLowHead3A60.getXorm().NewSession()
  327. defer session.Close()
  328. if whereQuery != "" {
  329. session = session.Where(whereQuery, whereArgs...)
  330. }
  331. if whereCond != nil {
  332. session = session.Where(whereCond)
  333. }
  334. err = TempLowHead3A60.deletePretreatment()
  335. if err != nil {
  336. return
  337. }
  338. _, err = session.Delete(TempLowHead3A60)
  339. return
  340. }
  341. // ----------------------------temp------------------------------------
  342. func StartQueueTaskPrefixHigHead3A60Temp3A60() {
  343. serviceName := "Prefix3A60"
  344. f := TempLowHead3A60_AutoQueueStatusFieldMap[serviceName]
  345. go func() {
  346. defer func() {
  347. err := recover()
  348. if err != nil {
  349. L.Error("StartQueueTaskPrefixHigHead3A60Temp3A60 意外退出:", err)
  350. }
  351. time.Sleep(time.Second * 5)
  352. StartQueueTaskPrefixHigHead3A60Temp3A60()
  353. }()
  354. for {
  355. onec := &Temp3A60{}
  356. hskdb.GetXormAuto().Where(fmt.Sprintf("`%s`=?", f), TempLowHead3A60_AutoQueueStatusWait).OrderBy("rand()").Get(onec)
  357. session := onec.getXorm().NewSession()
  358. defer session.Close()
  359. session = session.Table(onec)
  360. whereQuery, whereArgs := onec.wherePrimaryKey()
  361. whereCond := onec.wherePrimaryKeyWithCond()
  362. if whereQuery != "" {
  363. session = session.Where(whereQuery, whereArgs...)
  364. }
  365. if whereCond != nil {
  366. session = session.Where(whereCond)
  367. }
  368. i, err := session.Where(fmt.Sprintf("`%s`=?", f), TempLowHead3A60_AutoQueueStatusWait).Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusIng})
  369. if err == nil && i == 1 {
  370. func() {
  371. {
  372. onec.service = serviceName
  373. err := onec.queueEvent()
  374. session := onec.getXorm().NewSession()
  375. defer session.Close()
  376. session = session.Table(onec)
  377. whereQuery, whereArgs := onec.wherePrimaryKey()
  378. whereCond := onec.wherePrimaryKeyWithCond()
  379. if whereQuery != "" {
  380. session = session.Where(whereQuery, whereArgs...)
  381. }
  382. if whereCond != nil {
  383. session = session.Where(whereCond)
  384. }
  385. if err != nil {
  386. session.Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusFail})
  387. } else {
  388. session.Update(map[string]interface{}{f: TempLowHead3A60_AutoQueueStatusSuccess})
  389. }
  390. }
  391. }()
  392. }
  393. time.Sleep(TempLowHead3A60_AutoQueueWait)
  394. }
  395. }()
  396. }
  397. func InitializationHttpCrudPrefixHigHead3A60Temp3A60(router *hskgin.GinHelper, authoritys ...func(ctx *hskgin.GinContextHelper, TempLowHead3A60 *Temp3A60)) {
  398. var authority = func(ctx *hskgin.GinContextHelper, TempLowHead3A60 *Temp3A60) {
  399. }
  400. if len(authoritys) > 0 {
  401. authority = authoritys[0]
  402. }
  403. /*
  404. @yapi
  405. @group TempLowHead3A60
  406. @name 获取(Prefix3A60-TempLowHead3A60)单个数据
  407. @path /Prefix3A60/TempLowHead3A60/get
  408. @markdown
  409. ##
  410. ```
  411. 传入主键获取对应数据
  412. ```
  413. @end
  414. */
  415. router.Any("/Prefix3A60/TempLowHead3A60/get", func(ctx *hskgin.GinContextHelper) {
  416. req := &Temp3A60{}
  417. ctx.Bind(req)
  418. authority(ctx, req)
  419. req.service = "Prefix3A60"
  420. err := req.Load()
  421. ctx.CheckErrDisplayByError(err)
  422. ctx.DisplayByData(req)
  423. })
  424. /*
  425. @yapi
  426. @group TempLowHead3A60
  427. @name 保存(Prefix3A60-TempLowHead3A60)单个数据
  428. @path /Prefix3A60/TempLowHead3A60/save
  429. @markdown
  430. ##
  431. ```
  432. 传入整体数据插入或者保存(取决于主键是否存在)
  433. ```
  434. @end
  435. */
  436. router.Any("/Prefix3A60/TempLowHead3A60/save", func(ctx *hskgin.GinContextHelper) {
  437. req := &Temp3A60{}
  438. ctx.Bind(&req)
  439. authority(ctx, req)
  440. req.service = "Prefix3A60"
  441. err := req.Save()
  442. ctx.CheckErrDisplayByError(err)
  443. err = req.Load()
  444. ctx.CheckErrDisplayByError(err)
  445. ctx.DisplayByData(req)
  446. })
  447. /*
  448. @yapi
  449. @group TempLowHead3A60
  450. @name 获取(Prefix3A60-TempLowHead3A60)无分页数据
  451. @path /Prefix3A60/TempLowHead3A60/data
  452. @markdown
  453. ##
  454. ```
  455. 获取数据列表,无分页
  456. ```
  457. @end
  458. */
  459. router.Any("/Prefix3A60/TempLowHead3A60/data", func(ctx *hskgin.GinContextHelper) {
  460. req := &Temp3A60{}
  461. ctx.Bind(&req)
  462. authority(ctx, req)
  463. req.service = "Prefix3A60"
  464. data := req.Data()
  465. ctx.DisplayByData(data)
  466. })
  467. /*
  468. @yapi
  469. @group TempLowHead3A60
  470. @name 获取(Prefix3A60-TempLowHead3A60)分页数据
  471. @path /Prefix3A60/TempLowHead3A60/list
  472. @markdown
  473. ##
  474. ```
  475. 获取数据列表,分页,page参数
  476. ```
  477. @end
  478. */
  479. router.Any("/Prefix3A60/TempLowHead3A60/list", func(ctx *hskgin.GinContextHelper) {
  480. req := &Temp3A60{}
  481. ctx.Bind(&req)
  482. authority(ctx, req)
  483. page := ctx.ParamRequired2Page(true)
  484. req.service = "Prefix3A60"
  485. data, count := req.PageData(page, 10)
  486. ctx.DisplayByPage(data, count)
  487. })
  488. /*
  489. @yapi
  490. @group TempLowHead3A60
  491. @name 删除一条(Prefix3A60-TempLowHead3A60)数据
  492. @path /Prefix3A60/TempLowHead3A60/delete
  493. @markdown
  494. ##
  495. ```
  496. 传入主键参数,删除一条数据
  497. ```
  498. @end
  499. */
  500. router.Any("/Prefix3A60/TempLowHead3A60/delete", func(ctx *hskgin.GinContextHelper) {
  501. req := &Temp3A60{}
  502. ctx.Bind(&req)
  503. authority(ctx, req)
  504. req.service = "Prefix3A60"
  505. err := req.Delete()
  506. ctx.CheckErrDisplayByError(err)
  507. ctx.DisplayBySuccess("删除成功")
  508. })
  509. }