| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\givingGifts;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\User;
- use app\models\SaasUser;
- use app\utils\Notice\NoticeSend;
- use app\modules\client\models\v1\ShareQrcodeForm;
- use app\utils\Baidu\BaiduAiTools;
- use app\utils\DeepSeek\DeepSeekAiTools;
- use app\models\AccountLog;
- use app\models\GivingGiftsOrder;
- use app\models\GivingGiftsGoods;
- use app\models\Order;
- class GivingGiftsForm extends Model {
- public $store_id;
- public $id;
- public $ids;
- public $goods_name;
- public $supplier_name;
- public $goods_id;
- public $cat_id;
- public $supplier_id;
- public $warehouse_id;
- public $warehouse_zone_id;
- public $is_delete;
- public $warning_num;
- public $eiId;
- public $eipId;
- public $nickname;
- public $phone;
- public $order_no;
- public $type;
- public $purchase_in_price;
- public function init() {
- parent::init();
- if (empty($this->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(),
- ];
- }
- }
- }
|