123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Wukc
- * Date: 2020/11/16
- * Time: 20:12
- */
- namespace app\common\model\order;
- use app\common\model\User;
- use app\common\model\BaseModel;
- use app\common\model\store\Setting;
- use app\console\library\Tools;
- use app\common\library\helper;
- use app\common\model\card\RiceCard;
- use app\common\model\card\RiceCardOrderRemark;
- /**
- * 米卡订单模型
- * Class Category
- * @package app\common\model
- */
- class RiceCardOrder extends BaseModel
- {
- protected $name = 'rice_card_order';
- const PAY_TYPE = [
- 10 => '余额支付',
- 20 => '微信支付'
- ];
- /**
- * 关联用户表
- * @return \think\model\relation\BelongsTo
- */
- public function user()
- {
- return $this->belongsTo(User::class);
- }
- /**
- * 关联米卡订单备注表
- */
- public function riceCardOrderRemark()
- {
- return $this->hasMany(RiceCardOrderRemark::class, 'order_id');
- }
- /**
- * 获取订单图片
- */
- public function getRiceCardImgAttr($v){
- $setting = Setting:: getItem('storage',10001);
- $oss_domain = $setting['engine']['aliyun']['domain']??'';
- return (stripos($v,"http") === false) ? $oss_domain.'/'.$v : $v;
- }
- /**
- * 订单详情
- * @param array|int $where
- * @param array $with
- * @return null|static
- */
- public static function detail($where, array $with = [])
- {
- return static::get($where, $with);
- }
- /**
- * 未支付订单自动关闭
- * @param int $storeId
- * @param int $closeMinutes 自定关闭订单分钟数
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function closeEvent(int $storeId, int $closeMinutes)
- {
- // 截止时间
- $deadlineTime = time() - ((int)$closeMinutes * 60);
- // 查询截止时间未支付的订单
- $model = new RiceCardOrder;
- $list = RiceCardOrder::where("pay_status",0)->where('order_status',10)->where("create_time",'<',Date("Y-m-d H:i:s",$deadlineTime))->select();
-
- // 订单ID集
- $orderIds = helper::getArrayColumn($list, 'id');
- if (!empty($orderIds)) {
- // 批量更新订单状态为已取消
- $model->onBatchUpdate($orderIds, ['order_status' => 40]);
- //把下单减的库存返回去
- $this->backStock($list);
- }
- // 记录日志
- Tools::taskLogs('RiceCardOrderCancel', 'closeEvent', [
- 'storeId' => $storeId,
- 'closeMinutes' => $closeMinutes,
- 'deadlineTime' => $deadlineTime,
- 'orderIds' => helper::jsonEncode($orderIds)
- ]);
- }
- //订单返回库存
- public function backStock($list){
- $riceCard = [];
- foreach($list as $row){
- // 更新条件: 订单已经支付或者订单商品为 "下单减库存"
- $riceCard[] = [
- 'where' => ['id' => $row->rice_card_id],
- 'data' => ['stock' => ['inc', $row->buy_num]]
- ];
- }
- if(!empty($riceCard)){
- (new RiceCard)->updateRiceCard($riceCard);
- }
- }
- /**
- * 批量更新订单
- * @param $orderIds
- * @param $data
- * @return false|int
- */
- public function onBatchUpdate($orderIds, $data)
- {
- return static::updateBase($data, [['id', 'in', $orderIds]]);
- }
- }
|