package service import ( "database/sql" "encoding/json" "fmt" "log" "strconv" "time" "app.yhyue.com/moapp/jybase/common" "app.yhyue.com/moapp/jybase/encrypt" "app.yhyue.com/moapp/jybase/mongodb" . "bp.jydev.jianyu360.cn/BaseService/orderCenter/entity" "bp.jydev.jianyu360.cn/BaseService/orderCenter/rpc/ordercenter" ) func GetOrderCode(*ordercenter.GetOrderCodeReq) *ordercenter.GetOrderCodeResp { id := Node.Generate() return &ordercenter.GetOrderCodeResp{ ErrorCode: 1, ErrorMsg: "", Data: &ordercenter.OrderCode{ OrderCode: fmt.Sprint(id), }, } } func OrderAdd(this *ordercenter.OrderAddUpdateReq) *ordercenter.OrderAddUpdateResp { status := 0 orderCode := fmt.Sprint(Node.Generate()) flag := false nowTime := time.Now().Format("2006-01-02 15:04:05") flag = Mysql.ExecTx("", func(tx *sql.Tx) bool { 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 (?,?,?,?,?,?,?,?,?)`, this.Appid, orderCode, this.Order.OrderMoney, this.Order.OriginalPrice, nowTime, this.Order.OrderStatus, this.Order.UserId, this.Order.EntId, 0) extendId := Mysql.InsertBySqlByTx(tx, `insert into `+OrderExtend+`(appid,order_code,pay_type,source,dis_word,iscan_invoice,iscan_evaluate,remind_status,create_time) values (?,?,?,?,?,?,?,?,?)`, this.Appid, orderCode, this.OrderExtend.PayType, this.OrderExtend.Source, this.OrderExtend.DisWord, this.OrderExtend.IscanInvoice, this.OrderExtend.IscanEvaluate, this.OrderExtend.RemindStatus, nowTime) isOk1, isOk2 := true, true for _, v := range this.OrderCommodity { commodityId := Mysql.InsertBySqlByTx(tx, `insert into `+OrderCommodity+`(appid,order_code,goods_code,goods_spec_id,num,price,total,isgift,create_time) values (?,?,?,?,?,?,?,?,?)`, this.Appid, orderCode, v.GoodsCode, v.GoodsSpecId, v.Num, v.Price, v.Total, v.Isgift, nowTime) if commodityId == 0 { isOk1 = false } } if this.OrderBusiness.GoodsSpecId != 0 { 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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`, this.Appid, orderCode, this.OrderBusiness.ServiceStatus, this.OrderBusiness.ServiceTime, this.OrderBusiness.Filter, this.OrderBusiness.FilterKeys, this.OrderBusiness.FilterPublishtime, this.OrderBusiness.DataCount, this.OrderBusiness.DataSpec, this.OrderBusiness.FilterId, this.OrderBusiness.DownloadUrl, this.OrderBusiness.GoodsCode, this.OrderBusiness.GoodsSpecId, this.OrderBusiness.UserPhone, this.OrderBusiness.UserMail) if BusinessId == 0 { isOk2 = false } } return orderId > 0 && extendId > 0 && isOk1 && isOk2 }) if flag { status = 1 } return &ordercenter.OrderAddUpdateResp{ ErrorCode: 1, Data: &ordercenter.DataStatus{Status: int64(status)}, } } func OrderUpdate(this *ordercenter.OrderAddUpdateReq) *ordercenter.OrderAddUpdateResp { status := 0 if this.OrderCode != "" { flag := false // nowTime := time.Now().Format("2006-01-02 15:04:05") flag = Mysql.ExecTx("", func(tx *sql.Tx) bool { isOk1, isOk2, isOk3 := true, true, true if this.Order.UserId > 0 { ok := Mysql.UpdateByTx(tx, Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{ "order_money": this.Order.OrderMoney, "original_price": this.Order.OriginalPrice, "order_status": this.Order.OrderStatus, "user_id": this.Order.UserId, "ent_id": this.Order.EntId, }) if !ok { isOk1 = false } } if this.OrderExtend != nil { ok := Mysql.UpdateByTx(tx, OrderExtend, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{ "pay_type": this.OrderExtend.PayType, "source": this.OrderExtend.Source, "dis_word": this.OrderExtend.DisWord, "iscan_invoice": this.OrderExtend.IscanInvoice, "iscan_evaluate": this.OrderExtend.IscanEvaluate, }) if !ok { isOk2 = false } } if this.OrderBusiness.GoodsSpecId != 0 { ok := Mysql.UpdateByTx(tx, OrderBusiness, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{ "service_status": this.OrderBusiness.ServiceStatus, "service_time": this.OrderBusiness.ServiceTime, "filter": this.OrderBusiness.Filter, "filter_keys": this.OrderBusiness.FilterKeys, "filter_publishtime": this.OrderBusiness.FilterPublishtime, "data_count": this.OrderBusiness.DataCount, "data_spec": this.OrderBusiness.DataSpec, "filter_id": this.OrderBusiness.FilterId, "download_url": this.OrderBusiness.DownloadUrl, "goods_code": this.OrderBusiness.GoodsCode, "goods_spec_id": this.OrderBusiness.GoodsSpecId, "user_phone": this.OrderBusiness.UserPhone, "user_mail": this.OrderBusiness.UserMail, }) if !ok { isOk3 = false } } return isOk1 && isOk2 && isOk3 }) if flag { status = 1 } } return &ordercenter.OrderAddUpdateResp{ ErrorCode: 1, Data: &ordercenter.DataStatus{Status: int64(status)}, } } func OrderStatusUpdate(this *ordercenter.OrderStatusUpdateReq) *ordercenter.OrderAddUpdateResp { status, ok := 0, false if this.OrderCode != "" && this.OrderStatus != 0 { if this.DataType == 1 { ok = Mysql.Update(Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{"order_status": this.OrderStatus}) } else { ok = JyMysql.Update(OrderOld, map[string]interface{}{"order_code": this.OrderCode}, map[string]interface{}{"order_status": this.OrderStatus}) } } if ok { status = 1 } return &ordercenter.OrderAddUpdateResp{ ErrorCode: 1, Data: &ordercenter.DataStatus{Status: int64(status)}, } } func OrderDel(this *ordercenter.OrderDelReq) *ordercenter.OrderAddUpdateResp { nowTime := time.Now().Format("2006-01-02 15:04:05") ok := Mysql.Update(Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{"del_time": nowTime, "del_status": 1}) status := 0 if ok { status = 1 } return &ordercenter.OrderAddUpdateResp{ ErrorCode: 1, Data: &ordercenter.DataStatus{Status: int64(status)}, } } func OrderList(this *ordercenter.OrderListReq) *ordercenter.OrderListResp { OrderLists := []*ordercenter.OrderLists{} if this.NewUserIds != "" { newUserIds, _ := strconv.Atoi(encrypt.SE.Decode4Hex(this.NewUserIds)) if newUserIds > 0 { userMongo, ok := Mgo.FindOne(User, map[string]interface{}{"base_user_id": newUserIds}) if ok && userMongo != nil && len(*userMongo) > 0 { this.NewUserId = int64(newUserIds) this.UserId = mongodb.BsonIdToSId((*userMongo)["_id"]) } } } if this.NewUserId > 0 { orderNew := Mysql.Find(Order, map[string]interface{}{"appid": this.Appid, "user_id": this.NewUserId}, "", "create_time desc", 0, 0) if orderNew != nil && len(*orderNew) > 0 { log.Println("orderNew", len(*orderNew)) for _, v := range *orderNew { orderData1 := &ordercenter.OrderLists{} orderData2 := &ordercenter.Order{} orderByte, _ := json.Marshal(v) json.Unmarshal(orderByte, orderData2) orderData1.Order = orderData2 orderData1.OrderCode = common.ObjToString(v["order_code"]) orderData1.DataType = 1 OrderLists = append(OrderLists, orderData1) } } } if this.UserId != "" { orderOld := JyMysql.Find(OrderOld, map[string]interface{}{"user_id": this.UserId}, "", "create_time desc", 0, 0) if orderOld != nil && len(*orderOld) > 0 { for _, v := range *orderOld { orderData1 := &ordercenter.OrderLists{ OrderCode: common.ObjToString(v["order_code"]), ProductType: common.ObjToString(v["product_type"]), Order: &ordercenter.Order{ OrderMoney: common.Int64All(v["order_money"]), OriginalPrice: common.Int64All(v["original_price"]), OrderStatus: int64(common.IntAll(v["order_status"])), CreateTime: common.ObjToString(v["create_time"]), }, DataType: 2, StartTime: common.ObjToString(v["vip_starttime"]), EntTime: common.ObjToString(v["vip_endtime"]), OrderBusiness: &ordercenter.OrderBusiness{ ServiceStatus: common.Int64All(v["service_status"]), Filter: common.ObjToString(v["filter"]), FilterKeys: common.ObjToString(v["filter_keys"]), FilterPublishtime: common.ObjToString(v["filter_publishtime"]), ServiceTime: common.ObjToString(v["service_time"]), DataCount: common.Int64All(v["data_count"]), DataSpec: common.ObjToString(v["data_spec"]), FilterId: common.ObjToString(v["filter_id"]), DownloadUrl: common.ObjToString(v["download_url"]), UserPhone: common.ObjToString(v["user_phone"]), UserMail: common.ObjToString(v["user_mail"]), }, } OrderLists = append(OrderLists, orderData1) } } } count := int64(len(OrderLists)) if len(OrderLists) > int(this.PageNum*this.PageSize) { OrderLists = OrderLists[(this.PageNum-1)*this.PageSize : this.PageNum*this.PageSize] } else { OrderLists = OrderLists[(this.PageNum-1)*this.PageSize : len(OrderLists)] } for k, v := range OrderLists { if v.DataType == 1 { data := Mysql.FindOne(OrderBusiness, map[string]interface{}{"appid": this.Appid, "order_code": v.OrderCode}, "", "") OrderBusinessData := &ordercenter.OrderBusiness{} OrderBusinessByte, _ := json.Marshal(*data) json.Unmarshal(OrderBusinessByte, OrderBusinessData) OrderLists[k].OrderBusiness = OrderBusinessData } } return &ordercenter.OrderListResp{ ErrorCode: 1, Data: &ordercenter.OrderList{OrderList: OrderLists, Count: count}, } } func OrderInfo(this *ordercenter.OrderInfoReq) *ordercenter.OrderInfoResp { orderData := &ordercenter.Order{} orderData1 := &ordercenter.OrderAddUpdateReq{} order := Mysql.FindOne(Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, "", "") if order != nil && len(*order) > 0 { orderByte, _ := json.Marshal(*order) json.Unmarshal(orderByte, orderData) orderData1.Order = orderData } return &ordercenter.OrderInfoResp{ ErrorCode: 1, Data: &ordercenter.OrderInfo{OrderInfo: orderData1}, } } func OrderExamine(this *ordercenter.OrderExamineReq) *ordercenter.OrderAddUpdateResp { status := 0 ok := Mysql.Update(Order, map[string]interface{}{"appid": this.Appid, "order_code": this.OrderCode}, map[string]interface{}{"audit_status": this.AuditStatus}) if ok { status = 1 } return &ordercenter.OrderAddUpdateResp{ ErrorCode: 1, ErrorMsg: "", Data: &ordercenter.DataStatus{ Status: int64(status), }, } }