order.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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,data_spec,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.DataSpec, 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. "data_spec": this.OrderBusiness.DataSpec,
  102. "filter_id": this.OrderBusiness.FilterId,
  103. "download_url": this.OrderBusiness.DownloadUrl,
  104. "goods_code": this.OrderBusiness.GoodsCode,
  105. "goods_spec_id": this.OrderBusiness.GoodsSpecId,
  106. "user_phone": this.OrderBusiness.UserPhone,
  107. "user_mail": this.OrderBusiness.UserMail,
  108. })
  109. if !ok {
  110. isOk3 = false
  111. }
  112. }
  113. return isOk1 && isOk2 && isOk3
  114. })
  115. if flag {
  116. status = 1
  117. }
  118. }
  119. return &ordercenter.OrderAddUpdateResp{
  120. ErrorCode: 1,
  121. Data: &ordercenter.DataStatus{Status: int64(status)},
  122. }
  123. }
  124. func OrderStatusUpdate(this *ordercenter.OrderStatusUpdateReq) *ordercenter.OrderAddUpdateResp {
  125. status, ok := 0, false
  126. if this.OrderCode != "" && this.OrderStatus != 0 {
  127. if this.DataType == 1 {
  128. ok = Mysql.Update(Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{"order_status": this.OrderStatus})
  129. } else {
  130. ok = JyMysql.Update(OrderOld, map[string]interface{}{"order_code": this.OrderCode}, map[string]interface{}{"order_status": this.OrderStatus})
  131. }
  132. }
  133. if ok {
  134. status = 1
  135. }
  136. return &ordercenter.OrderAddUpdateResp{
  137. ErrorCode: 1,
  138. Data: &ordercenter.DataStatus{Status: int64(status)},
  139. }
  140. }
  141. func OrderDel(this *ordercenter.OrderDelReq) *ordercenter.OrderAddUpdateResp {
  142. nowTime := time.Now().Format("2006-01-02 15:04:05")
  143. ok := Mysql.Update(Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{"del_time": nowTime, "del_status": 1})
  144. status := 0
  145. if ok {
  146. status = 1
  147. }
  148. return &ordercenter.OrderAddUpdateResp{
  149. ErrorCode: 1,
  150. Data: &ordercenter.DataStatus{Status: int64(status)},
  151. }
  152. }
  153. func OrderList(this *ordercenter.OrderListReq) *ordercenter.OrderListResp {
  154. OrderLists := []*ordercenter.OrderLists{}
  155. if this.NewUserIds != "" {
  156. newUserIds, _ := strconv.Atoi(encrypt.SE.Decode4Hex(this.NewUserIds))
  157. if newUserIds > 0 {
  158. userMongo, ok := Mgo.FindOne(User, map[string]interface{}{"base_user_id": newUserIds})
  159. if ok && userMongo != nil && len(*userMongo) > 0 {
  160. this.NewUserId = int64(newUserIds)
  161. this.UserId = mongodb.BsonIdToSId((*userMongo)["_id"])
  162. }
  163. }
  164. }
  165. if this.NewUserId > 0 {
  166. orderNew := Mysql.Find(Order, map[string]interface{}{"appid": this.Appid, "user_id": this.NewUserId}, "", "create_time desc", 0, 0)
  167. if orderNew != nil && len(*orderNew) > 0 {
  168. log.Println("orderNew", len(*orderNew))
  169. for _, v := range *orderNew {
  170. orderData1 := &ordercenter.OrderLists{}
  171. orderData2 := &ordercenter.Order{}
  172. orderByte, _ := json.Marshal(v)
  173. json.Unmarshal(orderByte, orderData2)
  174. orderData1.Order = orderData2
  175. orderData1.OrderCode = common.ObjToString(v["order_code"])
  176. orderData1.DataType = 1
  177. OrderLists = append(OrderLists, orderData1)
  178. }
  179. }
  180. }
  181. if this.UserId != "" {
  182. orderOld := JyMysql.Find(OrderOld, map[string]interface{}{"user_id": this.UserId}, "", "create_time desc", 0, 0)
  183. if orderOld != nil && len(*orderOld) > 0 {
  184. for _, v := range *orderOld {
  185. orderData1 := &ordercenter.OrderLists{
  186. OrderCode: common.ObjToString(v["order_code"]),
  187. ProductType: common.ObjToString(v["product_type"]),
  188. Order: &ordercenter.Order{
  189. OrderMoney: common.Int64All(v["order_money"]),
  190. OriginalPrice: common.Int64All(v["original_price"]),
  191. OrderStatus: int64(common.IntAll(v["order_status"])),
  192. CreateTime: common.ObjToString(v["create_time"]),
  193. },
  194. DataType: 2,
  195. StartTime: common.ObjToString(v["vip_starttime"]),
  196. EntTime: common.ObjToString(v["vip_endtime"]),
  197. OrderBusiness: &ordercenter.OrderBusiness{
  198. ServiceStatus: common.Int64All(v["service_status"]),
  199. Filter: common.ObjToString(v["filter"]),
  200. FilterKeys: common.ObjToString(v["filter_keys"]),
  201. FilterPublishtime: common.ObjToString(v["filter_publishtime"]),
  202. ServiceTime: common.ObjToString(v["service_time"]),
  203. DataCount: common.Int64All(v["data_count"]),
  204. DataSpec: common.ObjToString(v["data_spec"]),
  205. FilterId: common.ObjToString(v["filter_id"]),
  206. DownloadUrl: common.ObjToString(v["download_url"]),
  207. UserPhone: common.ObjToString(v["user_phone"]),
  208. UserMail: common.ObjToString(v["user_mail"]),
  209. },
  210. }
  211. OrderLists = append(OrderLists, orderData1)
  212. }
  213. }
  214. }
  215. count := int64(len(OrderLists))
  216. if len(OrderLists) > int(this.PageNum*this.PageSize) {
  217. OrderLists = OrderLists[(this.PageNum-1)*this.PageSize : this.PageNum*this.PageSize]
  218. } else {
  219. OrderLists = OrderLists[(this.PageNum-1)*this.PageSize : len(OrderLists)]
  220. }
  221. for k, v := range OrderLists {
  222. if v.DataType == 1 {
  223. data := Mysql.FindOne(OrderBusiness, map[string]interface{}{"appid": this.Appid, "order_code": v.OrderCode}, "", "")
  224. OrderBusinessData := &ordercenter.OrderBusiness{}
  225. OrderBusinessByte, _ := json.Marshal(*data)
  226. json.Unmarshal(OrderBusinessByte, OrderBusinessData)
  227. OrderLists[k].OrderBusiness = OrderBusinessData
  228. }
  229. }
  230. return &ordercenter.OrderListResp{
  231. ErrorCode: 1,
  232. Data: &ordercenter.OrderList{OrderList: OrderLists, Count: count},
  233. }
  234. }
  235. func OrderInfo(this *ordercenter.OrderInfoReq) *ordercenter.OrderInfoResp {
  236. orderData := &ordercenter.Order{}
  237. orderData1 := &ordercenter.OrderAddUpdateReq{}
  238. order := Mysql.FindOne(Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, "", "")
  239. if order != nil && len(*order) > 0 {
  240. orderByte, _ := json.Marshal(*order)
  241. json.Unmarshal(orderByte, orderData)
  242. orderData1.Order = orderData
  243. }
  244. return &ordercenter.OrderInfoResp{
  245. ErrorCode: 1,
  246. Data: &ordercenter.OrderInfo{OrderInfo: orderData1},
  247. }
  248. }
  249. func OrderExamine(this *ordercenter.OrderExamineReq) *ordercenter.OrderAddUpdateResp {
  250. status := 0
  251. ok := Mysql.Update(Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{"audit_status": this.AuditStatus})
  252. if ok {
  253. status = 1
  254. }
  255. return &ordercenter.OrderAddUpdateResp{
  256. ErrorCode: 1,
  257. ErrorMsg: "",
  258. Data: &ordercenter.DataStatus{
  259. Status: int64(status),
  260. },
  261. }
  262. }