order.go 9.7 KB

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