Order.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\common\model\wow;
  3. use app\common\library\helper;
  4. use app\common\model\BaseModel;
  5. use app\common\enum\OrderType as OrderTypeEnum;
  6. use app\common\service\wechat\wow\Order as WowOrderService;
  7. /**
  8. * 好物圈订单同步记录模型
  9. * Class Order
  10. * @package app\common\model\wow
  11. */
  12. class Order extends BaseModel
  13. {
  14. protected $name = 'wow_order';
  15. protected $alias = 'wow_order';
  16. /**
  17. * 关联用户表
  18. * @return \think\model\relation\BelongsTo
  19. */
  20. public function user()
  21. {
  22. $module = self::getCalledModule() ?: 'common';
  23. return $this->belongsTo("app\\{$module}\\model\\User");
  24. }
  25. /**
  26. * 获取器:最后更新时间
  27. * @param $value
  28. * @return array
  29. */
  30. public function getLastTimeAttr($value)
  31. {
  32. return ['value' => $value, 'text' => date('Y-m-d H:i:s', $value)];
  33. }
  34. /**
  35. * 获取单条记录
  36. * @param $id
  37. * @param $with
  38. * @return static|null
  39. * @throws \think\exception\DbException
  40. */
  41. public static function detail($id, $with = ['goods.image.file', 'user'])
  42. {
  43. return static::get($id, $with);
  44. }
  45. /**
  46. * 添加好物圈订单同步记录(批量)
  47. * @param $wxappId
  48. * @param $orderList
  49. * @param $orderType
  50. * @return array|false
  51. * @throws \Exception
  52. */
  53. public function add($wxappId, $orderList, $orderType = OrderTypeEnum::MASTER)
  54. {
  55. // 批量添加
  56. $saveData = [];
  57. foreach ($orderList as $item) {
  58. $saveData[] = [
  59. 'order_id' => $item['order_id'],
  60. 'user_id' => $item['user_id'],
  61. 'order_type' => $orderType,
  62. 'status' => WowOrderService::getStatusByOrder($item),
  63. 'last_time' => time(),
  64. 'wxapp_id' => $wxappId,
  65. ];
  66. }
  67. return $this->isUpdate(false)->saveAll($saveData);
  68. }
  69. /**
  70. * 更新好物圈订单同步记录(批量)
  71. * @param $legalList
  72. * @return array|false
  73. * @throws \Exception
  74. */
  75. public function edit($legalList)
  76. {
  77. // 批量更新
  78. $saveData = [];
  79. foreach ($legalList as $id => $item) {
  80. $saveData[] = [
  81. 'id' => $id,
  82. 'status' => WowOrderService::getStatusByOrder($item),
  83. 'last_time' => time(),
  84. ];
  85. }
  86. return $this->isUpdate()->saveAll($saveData);
  87. }
  88. /**
  89. * 软删除
  90. * @return false|int
  91. */
  92. public function setDelete()
  93. {
  94. return $this->save(['is_delete' => 1]);
  95. }
  96. /**
  97. * 根据订单id集和订单类型获取记录
  98. * @param array $orderIds
  99. * @param int $orderType
  100. * @return false|\PDOStatement|string|\think\Collection
  101. * @throws \think\db\exception\DataNotFoundException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. * @throws \think\exception\DbException
  104. */
  105. public function getListByOrderIds($orderIds, $orderType = OrderTypeEnum::MASTER)
  106. {
  107. return $this->where('order_id', 'in', $orderIds)
  108. ->where('order_type', '=', $orderType)
  109. ->where('is_delete', '=', 0)
  110. ->select();
  111. }
  112. }