RiceCardOrder.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Wukc
  5. * Date: 2020/11/16
  6. * Time: 20:12
  7. */
  8. namespace app\common\model\order;
  9. use app\common\model\User;
  10. use app\common\model\BaseModel;
  11. use app\common\model\store\Setting;
  12. use app\console\library\Tools;
  13. use app\common\library\helper;
  14. use app\common\model\card\RiceCard;
  15. use app\common\model\card\RiceCardOrderRemark;
  16. /**
  17. * 米卡订单模型
  18. * Class Category
  19. * @package app\common\model
  20. */
  21. class RiceCardOrder extends BaseModel
  22. {
  23. protected $name = 'rice_card_order';
  24. const PAY_TYPE = [
  25. 10 => '余额支付',
  26. 20 => '微信支付'
  27. ];
  28. /**
  29. * 关联用户表
  30. * @return \think\model\relation\BelongsTo
  31. */
  32. public function user()
  33. {
  34. return $this->belongsTo(User::class);
  35. }
  36. /**
  37. * 关联米卡订单备注表
  38. */
  39. public function riceCardOrderRemark()
  40. {
  41. return $this->hasMany(RiceCardOrderRemark::class, 'order_id');
  42. }
  43. /**
  44. * 获取订单图片
  45. */
  46. public function getRiceCardImgAttr($v){
  47. $setting = Setting:: getItem('storage',10001);
  48. $oss_domain = $setting['engine']['aliyun']['domain']??'';
  49. return (stripos($v,"http") === false) ? $oss_domain.'/'.$v : $v;
  50. }
  51. /**
  52. * 订单详情
  53. * @param array|int $where
  54. * @param array $with
  55. * @return null|static
  56. */
  57. public static function detail($where, array $with = [])
  58. {
  59. return static::get($where, $with);
  60. }
  61. /**
  62. * 未支付订单自动关闭
  63. * @param int $storeId
  64. * @param int $closeMinutes 自定关闭订单分钟数
  65. * @throws \think\db\exception\DataNotFoundException
  66. * @throws \think\db\exception\DbException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. */
  69. public function closeEvent(int $storeId, int $closeMinutes)
  70. {
  71. // 截止时间
  72. $deadlineTime = time() - ((int)$closeMinutes * 60);
  73. // 查询截止时间未支付的订单
  74. $model = new RiceCardOrder;
  75. $list = RiceCardOrder::where("pay_status",0)->where('order_status',10)->where("create_time",'<',Date("Y-m-d H:i:s",$deadlineTime))->select();
  76. // 订单ID集
  77. $orderIds = helper::getArrayColumn($list, 'id');
  78. if (!empty($orderIds)) {
  79. // 批量更新订单状态为已取消
  80. $model->onBatchUpdate($orderIds, ['order_status' => 40]);
  81. //把下单减的库存返回去
  82. $this->backStock($list);
  83. }
  84. // 记录日志
  85. Tools::taskLogs('RiceCardOrderCancel', 'closeEvent', [
  86. 'storeId' => $storeId,
  87. 'closeMinutes' => $closeMinutes,
  88. 'deadlineTime' => $deadlineTime,
  89. 'orderIds' => helper::jsonEncode($orderIds)
  90. ]);
  91. }
  92. //订单返回库存
  93. public function backStock($list){
  94. $riceCard = [];
  95. foreach($list as $row){
  96. // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
  97. $riceCard[] = [
  98. 'where' => ['id' => $row->rice_card_id],
  99. 'data' => ['stock' => ['inc', $row->buy_num]]
  100. ];
  101. }
  102. if(!empty($riceCard)){
  103. (new RiceCard)->updateRiceCard($riceCard);
  104. }
  105. }
  106. /**
  107. * 批量更新订单
  108. * @param $orderIds
  109. * @param $data
  110. * @return false|int
  111. */
  112. public function onBatchUpdate($orderIds, $data)
  113. {
  114. return static::updateBase($data, [['id', 'in', $orderIds]]);
  115. }
  116. }