order.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package service
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "time"
  7. "app.yhyue.com/moapp/jybase/common"
  8. . "bp.jydev.jianyu360.cn/BaseService/orderCenter/entity"
  9. "bp.jydev.jianyu360.cn/BaseService/orderCenter/rpc/ordercenter"
  10. )
  11. func GetOrderCode(*ordercenter.GetOrderCodeReq) *ordercenter.GetOrderCodeResp {
  12. id := Node.Generate()
  13. return &ordercenter.GetOrderCodeResp{
  14. ErrorCode: 1,
  15. ErrorMsg: "",
  16. Data: &ordercenter.OrderCode{
  17. OrderCode: fmt.Sprint(id),
  18. },
  19. }
  20. }
  21. func OrderAdd(this *ordercenter.OrderAddUpdateReq) *ordercenter.OrderAddUpdateResp {
  22. status := 0
  23. orderCode := fmt.Sprint(Node.Generate())
  24. flag := false
  25. nowTime := time.Now().Format("2006-01-02 15:04:05")
  26. flag = Mysql.ExecTx("", func(tx *sql.Tx) bool {
  27. orderId := Mysql.InsertBySqlByTx(tx, `insert into `+Order+`(appid,order_code,order_money,original_price,create_time,order_status,user_id,ent_id,del_status) values (?,?,?,?,?,?,?,?,?)`,
  28. this.Appid, orderCode, this.Order.OrderMoney, this.Order.OriginalPrice, nowTime, this.Order.OrderStatus, this.Order.UserId, this.Order.EntId, 0)
  29. extendId := Mysql.InsertBySqlByTx(tx, `insert into `+OrderExtend+`(appid,order_code,pay_type,source,dis_word,iscan_invoice,iscan_evaluate,remind_status,create_time) values (?,?,?,?,?,?,?,?,?)`,
  30. this.Appid, orderCode, this.OrderExtend.PayType, this.OrderExtend.Source, this.OrderExtend.DisWord, this.OrderExtend.IscanInvoice, this.OrderExtend.IscanEvaluate, this.OrderExtend.RemindStatus, nowTime)
  31. isOk1, isOk2 := true, true
  32. for _, v := range this.OrderCommodity {
  33. commodityId := Mysql.InsertBySqlByTx(tx, `insert into `+OrderCommodity+`(appid,order_code,goods_code,goods_spec_id,num,price,total,isgift,create_time) values (?,?,?,?,?,?,?,?,?)`,
  34. this.Appid, orderCode, v.GoodsCode, v.GoodsSpecId, v.Num, v.Price, v.Total, v.Isgift, nowTime)
  35. if commodityId == 0 {
  36. isOk1 = false
  37. }
  38. }
  39. if this.OrderBusiness.GoodsSpecId != 0 {
  40. BusinessId := Mysql.InsertBySqlByTx(tx, `insert into `+OrderBusiness+`(appid,order_code,service_status,service_time,filter,filter_keys,filter_publishtime,data_count,filter_id,download_url,goods_code,goods_spec_id,user_phone,user_mail) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,
  41. this.Appid, orderCode, this.OrderBusiness.ServiceStatus, this.OrderBusiness.ServiceTime, this.OrderBusiness.Filter, this.OrderBusiness.FilterKeys,
  42. this.OrderBusiness.FilterPublishtime, this.OrderBusiness.DataCount, this.OrderBusiness.FilterId, this.OrderBusiness.DownloadUrl,
  43. this.OrderBusiness.GoodsCode, this.OrderBusiness.GoodsSpecId, this.OrderBusiness.UserPhone, this.OrderBusiness.UserMail)
  44. if BusinessId == 0 {
  45. isOk2 = false
  46. }
  47. }
  48. return orderId > 0 && extendId > 0 && isOk1 && isOk2
  49. })
  50. if flag {
  51. status = 1
  52. }
  53. return &ordercenter.OrderAddUpdateResp{
  54. ErrorCode: 1,
  55. Data: &ordercenter.DataStatus{Status: int64(status)},
  56. }
  57. }
  58. func OrderUpdate(this *ordercenter.OrderAddUpdateReq) *ordercenter.OrderAddUpdateResp {
  59. status := 0
  60. if this.OrderCode != "" {
  61. flag := false
  62. // nowTime := time.Now().Format("2006-01-02 15:04:05")
  63. flag = Mysql.ExecTx("", func(tx *sql.Tx) bool {
  64. isOk1, isOk2, isOk3 := true, true, true
  65. if this.Order.UserId > 0 {
  66. ok := Mysql.UpdateByTx(tx, Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{
  67. "order_money": this.Order.OrderMoney,
  68. "original_price": this.Order.OriginalPrice,
  69. "order_status": this.Order.OrderStatus,
  70. "user_id": this.Order.UserId,
  71. "ent_id": this.Order.EntId,
  72. })
  73. if !ok {
  74. isOk1 = false
  75. }
  76. }
  77. if this.OrderExtend != nil {
  78. ok := Mysql.UpdateByTx(tx, OrderExtend, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{
  79. "pay_type": this.OrderExtend.PayType,
  80. "source": this.OrderExtend.Source,
  81. "dis_word": this.OrderExtend.DisWord,
  82. "iscan_invoice": this.OrderExtend.IscanInvoice,
  83. "iscan_evaluate": this.OrderExtend.IscanEvaluate,
  84. })
  85. if !ok {
  86. isOk2 = false
  87. }
  88. }
  89. if this.OrderBusiness.GoodsSpecId != 0 {
  90. ok := Mysql.UpdateByTx(tx, OrderBusiness, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{
  91. "service_status": this.OrderBusiness.ServiceStatus,
  92. "service_time": this.OrderBusiness.ServiceTime,
  93. "filter": this.OrderBusiness.Filter,
  94. "filter_keys": this.OrderBusiness.FilterKeys,
  95. "filter_publishtime": this.OrderBusiness.FilterPublishtime,
  96. "data_count": this.OrderBusiness.DataCount,
  97. "filter_id": this.OrderBusiness.FilterId,
  98. "download_url": this.OrderBusiness.DownloadUrl,
  99. "goods_code": this.OrderBusiness.GoodsCode,
  100. "goods_spec_id": this.OrderBusiness.GoodsSpecId,
  101. "user_phone": this.OrderBusiness.UserPhone,
  102. "user_mail": this.OrderBusiness.UserMail,
  103. })
  104. if !ok {
  105. isOk3 = false
  106. }
  107. }
  108. return isOk1 && isOk2 && isOk3
  109. })
  110. if flag {
  111. status = 1
  112. }
  113. }
  114. return &ordercenter.OrderAddUpdateResp{
  115. ErrorCode: 1,
  116. Data: &ordercenter.DataStatus{Status: int64(status)},
  117. }
  118. }
  119. func OrderStatusUpdate(this *ordercenter.OrderStatusUpdateReq) *ordercenter.OrderAddUpdateResp {
  120. status, ok := 0, false
  121. if this.OrderCode != "" && this.OrderStatus != 0 {
  122. if this.DataType == 1 {
  123. ok = Mysql.Update(Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{"order_status": this.OrderStatus})
  124. } else {
  125. ok = JyMysql.Update(OrderOld, map[string]interface{}{"order_code": this.OrderCode}, map[string]interface{}{"order_status": this.OrderStatus})
  126. }
  127. }
  128. if ok {
  129. status = 1
  130. }
  131. return &ordercenter.OrderAddUpdateResp{
  132. ErrorCode: 1,
  133. Data: &ordercenter.DataStatus{Status: int64(status)},
  134. }
  135. }
  136. func OrderDel(this *ordercenter.OrderDelReq) *ordercenter.OrderAddUpdateResp {
  137. nowTime := time.Now().Format("2006-01-02 15:04:05")
  138. ok := Mysql.Update(Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{"del_time": nowTime, "del_status": 1})
  139. status := 0
  140. if ok {
  141. status = 1
  142. }
  143. return &ordercenter.OrderAddUpdateResp{
  144. ErrorCode: 1,
  145. Data: &ordercenter.DataStatus{Status: int64(status)},
  146. }
  147. }
  148. func OrderList(this *ordercenter.OrderListReq) *ordercenter.OrderListResp {
  149. OrderLists := []*ordercenter.OrderLists{}
  150. orderNew := Mysql.Find(Order, map[string]interface{}{"appid": this.Appid, "user_id": this.NewUserId}, "", "create_time desc", -1, -1)
  151. orderOld := JyMysql.Find(OrderOld, map[string]interface{}{"user_id": this.UserId}, "", "create_time desc", -1, -1)
  152. if orderNew != nil && len(*orderNew) > 0 {
  153. for _, v := range *orderNew {
  154. orderData1 := &ordercenter.OrderLists{}
  155. orderData2 := &ordercenter.Order{}
  156. orderByte, _ := json.Marshal(v)
  157. json.Unmarshal(orderByte, orderData2)
  158. orderData1.Order = orderData2
  159. orderData1.OrderCode = common.ObjToString(v["order_code"])
  160. OrderLists = append(OrderLists, orderData1)
  161. }
  162. }
  163. if orderOld != nil && len(*orderOld) > 0 {
  164. for _, v := range *orderOld {
  165. orderData1 := &ordercenter.OrderLists{
  166. OrderCode: common.ObjToString(v["order_code"]),
  167. Order: &ordercenter.Order{
  168. OrderMoney: common.Int64All(v["order_money"]),
  169. OriginalPrice: common.Int64All(v["original_price"]),
  170. OrderStatus: common.Int64All(v["order_ststus"]),
  171. },
  172. OrderBusiness: &ordercenter.OrderBusiness{
  173. ServiceStatus: common.Int64All(v["service_status"]),
  174. Filter: common.ObjToString(v["filter"]),
  175. FilterKeys: common.ObjToString(v["filter_keys"]),
  176. FilterPublishtime: common.ObjToString(v["filter_publishtime"]),
  177. ServiceTime: common.ObjToString(v["service_time"]),
  178. DataCount: common.Int64All(v["data_count"]),
  179. FilterId: common.ObjToString(v["filter_id"]),
  180. DownloadUrl: common.ObjToString(v["download_url"]),
  181. UserPhone: common.ObjToString(v["user_phone"]),
  182. UserMail: common.ObjToString(v["user_mail"]),
  183. // GoodsCode: 0,
  184. // GoodsSpecId: 0,
  185. },
  186. }
  187. OrderLists = append(OrderLists, orderData1)
  188. }
  189. }
  190. return &ordercenter.OrderListResp{
  191. ErrorCode: 1,
  192. Data: &ordercenter.OrderList{OrderList: OrderLists},
  193. }
  194. }
  195. func OrderInfo(this *ordercenter.OrderInfoReq) *ordercenter.OrderInfoResp {
  196. orderData := &ordercenter.Order{}
  197. orderData1 := &ordercenter.OrderAddUpdateReq{}
  198. order := Mysql.FindOne(Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, "", "")
  199. if order != nil && len(*order) > 0 {
  200. orderByte, _ := json.Marshal(*order)
  201. json.Unmarshal(orderByte, orderData)
  202. orderData1.Order = orderData
  203. }
  204. return &ordercenter.OrderInfoResp{
  205. ErrorCode: 1,
  206. Data: &ordercenter.OrderInfo{OrderInfo: orderData1},
  207. }
  208. }