store_id)) { $this->store_id = get_store_id(); } } public static function paramsClient($params, $client_user_id, $client_saas_user_id) { return array_merge($params, ['is_client' => 1, 'client_user_id' => $client_user_id, 'client_saas_user_id' => $client_saas_user_id]); } public static $confs = []; public static function conf($store_id = 0, $refresh = 0) { if (isset(self::$confs[$store_id]) && !$refresh) { return self::$confs[$store_id]; } $confDef = [ 'is_open' => 0, // 'gift_timeout' => 0, 'pic_gift_face' => '', 'pic_gift_open' => '', 'pic_gift_open_bg' => '', 'pic_gift_bg' => '', 'share_link_pic' => '', 'color_content' => '', 'color_tip' => '', ]; $conf = Option::get(OptionSetting::GIVING_GIFTS_SETTING, $store_id, 'store')['value']; if ($conf) { $conf = array_merge($confDef, json_decode($conf, true)); } else { $conf = $confDef; } self::$confs[$store_id] = $conf; return $conf; } public static function confSave($store_id = 0, $config = []) { $oldConf = self::conf($store_id, 1); $conf = array_merge($oldConf, $config); $set = Option::set(OptionSetting::GIVING_GIFTS_SETTING, json_encode($conf, JSON_UNESCAPED_UNICODE), $store_id, 'store'); self::conf($store_id, 1); return $set; } public static function isopen($store_id = 0) { $conf = self::conf($store_id); $isopen = $conf['is_open']; return empty($isopen) ? 0 : 1; } public static function confGoodsSave($store_id = 0, $goodsIds = [], $del = 0) { if($del){ $rows = GivingGiftsGoods::deleteAll(['store_id' => $store_id, 'goods_id' => $goodsIds]); }else{ $rows = 0; foreach($goodsIds as $gid){ try{ $m = new GivingGiftsGoods(); $m->store_id = $store_id; $m->goods_id = $gid; if(!$m->save()){ \Yii::error([__METHOD__, $m->getErrors()]); continue; } $rows++; } catch (\Exception $ex) { debug_log([__METHOD__, $ex->getMessage()], __CLASS__ . '.log'); } } } return [ 'code' => 0, 'msg' => 'ok', 'data' => [ 'rows' => $rows, ], ]; } public static function isopenGoods($store_id = 0, $goodsId = 0) { if(!self::isopen($store_id)){ return false; } return GivingGiftsGoods::findOne(['store_id' => $store_id, 'goods_id' => $goodsId]); } public static function givingGiftsOrderData($order) { if($order['order_type'] != Order::ORDER_TYPE_GIVING_GIFTS){ return null; } return [ 'isReceivedTimeout' => self::isReceivedTimeout($order), 'giving_gifts_received_user_id' => $order['giving_gifts_received_user_id'], 'GivingGiftsOrder' => GivingGiftsOrder::findOne(['order_id' => $order['id']]), 'giving_gifts_received_user' => $order['giving_gifts_received_user_id'] ? SaasUser::findOne(['mobile' => User::findOne($order['giving_gifts_received_user_id'])['binding']]) : null, ]; } public static function orderSubmit($order, $data) { try{ if(is_string($data)){ $data = json_decode($data, true); } $order->order_type = Order::ORDER_TYPE_GIVING_GIFTS; $order->mobile = ''; $order->name = ''; if (!$order->save()) { throw new \Exception(array_shift($order->getFirstErrors())); } $orderExt = new GivingGiftsOrder(); $orderExt->store_id = $order->store_id; $orderExt->order_id = $order->id; $orderExt->from = (string)$data['from']; $orderExt->to = (string)$data['to']; $orderExt->text = (string)$data['text']; $orderExt->hide_goods = (int)$data['hide_goods']; if (!$orderExt->save()) { throw new \Exception(array_shift($orderExt->getFirstErrors())); } return [ 'code' => 0, ]; } catch (\Exception $ex) { \Yii::error($ex); debug_log([__METHOD__, __LINE__, $order->id, $data, $ex->getMessage()], __CLASS__ . '.log'); return [ 'code' => 1, 'msg' => $ex->getMessage(), ]; } } public static function isReceivedTimeout($order) { return 0; $conf = self::conf($order['store_id']); if($order['giving_gifts_received_user_id']){ return 0; } return ($order['pay_time'] ? (time() - 3600 * $conf['gift_timeout']) - $order['pay_time'] : 0); } public static function orderReceivedTimeoutRefund($order) { debug_log([__METHOD__, __LINE__, $order['id']], __CLASS__ . '.log'); if($order['giving_gifts_received_user_id']){ return; } $timeout = self::isReceivedTimeout($order); if($timeout < 0){ $queueId = queue_push(new \app\jobs\GivingGiftsReceivedTimeoutRefundJob(['order_id' => $order['id']]), abs($timeout)); debug_log([__METHOD__, __LINE__, $order['id'], $queueId, $timeout], __CLASS__ . '.log'); return $queueId; } //退单 $form = new \app\modules\admin\models\OrderRevokeForm(); $form->order_id = $order['id']; $form->delete_pass = true; $form->user_id = $order['user_id']; $form->store_id = $order['store_id']; $res = $form->save(); debug_log([__METHOD__, __LINE__, $order['id'], $res], __CLASS__ . '.log'); return $res; } public static function afterOrderSave($order, $insert, $changedAttributes) { try{ if (!$insert && isset($changedAttributes['is_pay']) && $order->is_pay == 1) { //定时退单 // self::orderReceivedTimeoutRefund($order); } } catch (\Exception $ex) { \Yii::error($ex); debug_log([__METHOD__, __LINE__, $order->id, $ex->getMessage()], __CLASS__ . '.log'); return [ 'code' => 1, 'msg' => $ex->getMessage(), ]; } } }