factory.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package dao
  2. import (
  3. "demo/internal/model"
  4. "fmt"
  5. "github.com/pkg/errors"
  6. "github.com/siddontang/go/log"
  7. "strconv"
  8. "strings"
  9. "sync"
  10. "time"
  11. )
  12. func (d *Dao) AddFactory(arg model.Factory, recall []int64) error {
  13. row := d.db.NewSession()
  14. defer row.Close()
  15. row.Begin()
  16. c := time.Now().Format("20060102")
  17. count1, err := row.Where(" batch like ? ", "%"+fmt.Sprintf("EB%s", c)+"%").Count(&model.Factory{})
  18. if err != nil {
  19. err = errors.Wrap(err, "AddFactory-db-error")
  20. log.Error(err)
  21. row.Rollback()
  22. return err
  23. }
  24. count1 = count1 + 1
  25. var batch string
  26. //for i := 0; i <= int(count1); i++ {
  27. batch = fmt.Sprintf("EB%s", c)
  28. // //count1 = count1 + 1
  29. num := 3 - len(fmt.Sprintf("%d", count1))
  30. for n := 0; n < num; n++ {
  31. batch = fmt.Sprintf("%s0", batch)
  32. }
  33. batch = fmt.Sprintf("%s%d", batch, count1)
  34. //}
  35. arg.Batch = batch
  36. arg.Recall = int64(len(recall))
  37. _, err = row.InsertOne(&arg)
  38. if err != nil {
  39. err = errors.Wrap(err, "AddFactory-db-error")
  40. log.Error(err)
  41. row.Rollback()
  42. return err
  43. }
  44. err = row.Commit()
  45. if err != nil {
  46. err = errors.Wrap(err, "AddFactory-db-error")
  47. log.Error(err)
  48. row.Rollback()
  49. return err
  50. }
  51. row.Begin()
  52. now := time.Now()
  53. if arg.Genre == 0 {
  54. //count, err := row.Where(" code like ? ", "%"+fmt.Sprintf("bh%s", c)+"%").Count(&model.NeckRingListing{})
  55. //if err != nil {
  56. // err = errors.Wrap(err, "AddFactory-db-error")
  57. // log.Error(err)
  58. // row.Rollback()
  59. // return err
  60. //}
  61. var wg sync.WaitGroup
  62. dataList := make([]*model.NeckRingListing, 0)
  63. for i := 0; i < int(arg.Count); i++ {
  64. wg.Add(1)
  65. go func() {
  66. defer wg.Done()
  67. insertarg := &model.NeckRingListing{
  68. Pastureid: arg.Pastureid,
  69. Code: 0,
  70. //Status: 0,
  71. PurchaseDate: now,
  72. CreateDate: now,
  73. FactoryId: arg.Id,
  74. }
  75. dataList = append(dataList, insertarg)
  76. }()
  77. }
  78. wg.Wait()
  79. _, err := row.Insert(dataList)
  80. if err != nil {
  81. err = errors.Wrap(err, "AddFactory-db-error")
  82. log.Error(err)
  83. row.Rollback()
  84. return err
  85. }
  86. _, err = row.Exec(` update t_nz_jbqid SET REGIST_NUM_2 = id WHERE (CREATETIME = ?) `, now.Format("2006-01-02 15:04:05"))
  87. if err != nil {
  88. err = errors.Wrap(err, "AddFactory-db-error")
  89. log.Error(err)
  90. row.Rollback()
  91. return err
  92. }
  93. } else {
  94. //TODO 置换
  95. deliveryandrecallList := make([]*model.DeliveryAndRecall, 0)
  96. for _, recallId := range recall {
  97. var deliveryandrecall model.DeliveryAndRecall
  98. deliveryandrecall.FactoryId = arg.Id
  99. deliveryandrecall.Status = 0
  100. deliveryandrecall.RecallId = recallId
  101. deliveryandrecallList = append(deliveryandrecallList, &deliveryandrecall)
  102. }
  103. _, err := row.Insert(&deliveryandrecallList)
  104. if err != nil {
  105. err = errors.Wrap(err, "AddFactory-db-error")
  106. log.Error(err)
  107. row.Rollback()
  108. return err
  109. }
  110. count, err := row.In("factoryid", recall).And("status = 1").Count(new(model.DeliveryAndRecall))
  111. if err != nil {
  112. err = errors.Wrap(err, "AddFactory-db-error")
  113. log.Error(err)
  114. row.Rollback()
  115. return err
  116. }
  117. dataList := make([]*model.NeckRingListing, 0)
  118. for i := 0; i < int(count); i++ {
  119. //wg.Add(1)
  120. //go func() {
  121. // defer wg.Done()
  122. insertarg := &model.NeckRingListing{
  123. Pastureid: arg.Pastureid,
  124. Code: 0,
  125. Recall: 0,
  126. PurchaseDate: now,
  127. CreateDate: now,
  128. FactoryId: arg.Id,
  129. }
  130. //go func() {
  131. //_, err := row.Insert(insertarg)
  132. //if err != nil {
  133. // err = errors.Wrap(err, "AddFactory-db-error")
  134. // log.Error(err)
  135. // row.Rollback()
  136. // return err
  137. //}
  138. dataList = append(dataList, insertarg)
  139. //}()
  140. }
  141. _, err = row.Insert(dataList)
  142. if err != nil {
  143. err = errors.Wrap(err, "AddFactory-db-error")
  144. log.Error(err)
  145. row.Rollback()
  146. return err
  147. }
  148. //for _, item := range dataList {
  149. // item.Code = item.Id
  150. // _, err = row.Cols("REGIST_NUM_2").Where("id = ?", item.Id).Update(item)
  151. // if err != nil {
  152. // err = errors.Wrap(err, "AddFactory-db-error")
  153. // log.Error(err)
  154. // row.Rollback()
  155. // return err
  156. // }
  157. //}
  158. _, err = row.Exec(` update t_nz_jbqid SET REGIST_NUM_2 = id WHERE (CREATETIME = ?) `, now.Format("2006-01-02 15:04:05"))
  159. if err != nil {
  160. err = errors.Wrap(err, "AddFactory-db-error")
  161. log.Error(err)
  162. row.Rollback()
  163. return err
  164. }
  165. //wg.Wait()
  166. var recallstr []string
  167. for _, r := range recall {
  168. recallstr = append(recallstr, strconv.FormatInt(r, 10))
  169. }
  170. _, err = row.Exec(fmt.Sprintf(`update t_nz_jbqid set recall = 2 where id in(
  171. select recallid from deliveryandrecall where factoryid in(%s) and status = 1) and recall = 1 `, strings.Join(recallstr, ",")))
  172. if err != nil {
  173. err = errors.Wrap(err, "AddFactory-db-error")
  174. log.Error(err)
  175. row.Rollback()
  176. return err
  177. }
  178. }
  179. err = row.Commit()
  180. if err != nil {
  181. err = errors.Wrap(err, "AddFactory-db-error")
  182. log.Error(err)
  183. row.Rollback()
  184. return err
  185. }
  186. return nil
  187. }
  188. func (d *Dao) GetFactory(index, pagesize int64, pastureid, batch string, genre, startCount, endCount int64, createName, startDate, endDate string, recallID int64) ([]*model.FactoryList, int64, error) {
  189. rows := d.db.NewSession()
  190. defer rows.Close()
  191. rows.Table("factory").Alias("f")
  192. rows.Join("inner", []string{"t_nz_farm", "fa"}, "f.FARM_UUID=fa.UUID")
  193. if batch != "" {
  194. rows.Where(" f.batch like ? ", "%"+batch+"%")
  195. }
  196. if index != 0 && pagesize != 0 {
  197. first := (index - 1) * pagesize
  198. rows.Limit(int(pagesize), int(first))
  199. }
  200. if genre != -1 {
  201. rows.Where(" f.genre = ? ", genre)
  202. }
  203. if startCount > 0 {
  204. rows.Where(" f.count >= ? ", startCount)
  205. }
  206. if endCount > 0 {
  207. rows.Where(" f.count <= ? ", endCount)
  208. }
  209. if createName != "" {
  210. rows.Where(" f.createname like ? ", "%"+createName+"%")
  211. }
  212. if startDate != "" && endDate != "" {
  213. rows.Where(" f.createtime between ? and ? ", startDate, endDate)
  214. }
  215. if pastureid != "" {
  216. rows.Where(" f.FARM_UUID = ? ", pastureid)
  217. }
  218. if recallID > 0 {
  219. rows.Where("f.id in(select recallid from deliveryandrecall where factoryid = ? and status = 1)", recallID)
  220. }
  221. rows.Where("f.genre != 2")
  222. dataList := make([]*model.FactoryList, 0)
  223. count, err := rows.Desc("f.createtime").FindAndCount(&dataList)
  224. if err != nil {
  225. err = errors.Wrap(err, "GetFactory-db-error")
  226. log.Error(err)
  227. return dataList, count, err
  228. }
  229. return dataList, count, nil
  230. }