GivingGiftsForm.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\givingGifts;
  8. use app\models\Option;
  9. use app\constants\OptionSetting;
  10. use app\models\User;
  11. use app\models\SaasUser;
  12. use app\utils\Notice\NoticeSend;
  13. use app\modules\client\models\v1\ShareQrcodeForm;
  14. use app\utils\Baidu\BaiduAiTools;
  15. use app\utils\DeepSeek\DeepSeekAiTools;
  16. use app\models\AccountLog;
  17. use app\models\GivingGiftsOrder;
  18. use app\models\GivingGiftsGoods;
  19. use app\models\Order;
  20. class GivingGiftsForm extends Model {
  21. public $store_id;
  22. public $id;
  23. public $ids;
  24. public $goods_name;
  25. public $supplier_name;
  26. public $goods_id;
  27. public $cat_id;
  28. public $supplier_id;
  29. public $warehouse_id;
  30. public $warehouse_zone_id;
  31. public $is_delete;
  32. public $warning_num;
  33. public $eiId;
  34. public $eipId;
  35. public $nickname;
  36. public $phone;
  37. public $order_no;
  38. public $type;
  39. public $purchase_in_price;
  40. public function init() {
  41. parent::init();
  42. if (empty($this->store_id)) {
  43. $this->store_id = get_store_id();
  44. }
  45. }
  46. public static function paramsClient($params, $client_user_id, $client_saas_user_id) {
  47. return array_merge($params, ['is_client' => 1, 'client_user_id' => $client_user_id, 'client_saas_user_id' => $client_saas_user_id]);
  48. }
  49. public static $confs = [];
  50. public static function conf($store_id = 0, $refresh = 0) {
  51. if (isset(self::$confs[$store_id]) && !$refresh) {
  52. return self::$confs[$store_id];
  53. }
  54. $confDef = [
  55. 'is_open' => 0,
  56. // 'gift_timeout' => 0,
  57. 'pic_gift_face' => '',
  58. 'pic_gift_open' => '',
  59. 'pic_gift_open_bg' => '',
  60. 'pic_gift_bg' => '',
  61. 'share_link_pic' => '',
  62. 'color_content' => '',
  63. 'color_tip' => '',
  64. ];
  65. $conf = Option::get(OptionSetting::GIVING_GIFTS_SETTING, $store_id, 'store')['value'];
  66. if ($conf) {
  67. $conf = array_merge($confDef, json_decode($conf, true));
  68. } else {
  69. $conf = $confDef;
  70. }
  71. self::$confs[$store_id] = $conf;
  72. return $conf;
  73. }
  74. public static function confSave($store_id = 0, $config = []) {
  75. $oldConf = self::conf($store_id, 1);
  76. $conf = array_merge($oldConf, $config);
  77. $set = Option::set(OptionSetting::GIVING_GIFTS_SETTING, json_encode($conf, JSON_UNESCAPED_UNICODE), $store_id, 'store');
  78. self::conf($store_id, 1);
  79. return $set;
  80. }
  81. public static function isopen($store_id = 0) {
  82. $conf = self::conf($store_id);
  83. $isopen = $conf['is_open'];
  84. return empty($isopen) ? 0 : 1;
  85. }
  86. public static function confGoodsSave($store_id = 0, $goodsIds = [], $del = 0) {
  87. if($del){
  88. $rows = GivingGiftsGoods::deleteAll(['store_id' => $store_id, 'goods_id' => $goodsIds]);
  89. }else{
  90. $rows = 0;
  91. foreach($goodsIds as $gid){
  92. try{
  93. $m = new GivingGiftsGoods();
  94. $m->store_id = $store_id;
  95. $m->goods_id = $gid;
  96. if(!$m->save()){
  97. \Yii::error([__METHOD__, $m->getErrors()]);
  98. continue;
  99. }
  100. $rows++;
  101. } catch (\Exception $ex) {
  102. debug_log([__METHOD__, $ex->getMessage()], __CLASS__ . '.log');
  103. }
  104. }
  105. }
  106. return [
  107. 'code' => 0,
  108. 'msg' => 'ok',
  109. 'data' => [
  110. 'rows' => $rows,
  111. ],
  112. ];
  113. }
  114. public static function isopenGoods($store_id = 0, $goodsId = 0) {
  115. if(!self::isopen($store_id)){
  116. return false;
  117. }
  118. return GivingGiftsGoods::findOne(['store_id' => $store_id, 'goods_id' => $goodsId]);
  119. }
  120. public static function givingGiftsOrderData($order) {
  121. if($order['order_type'] != Order::ORDER_TYPE_GIVING_GIFTS){
  122. return null;
  123. }
  124. return [
  125. 'isReceivedTimeout' => self::isReceivedTimeout($order),
  126. 'giving_gifts_received_user_id' => $order['giving_gifts_received_user_id'],
  127. 'GivingGiftsOrder' => GivingGiftsOrder::findOne(['order_id' => $order['id']]),
  128. 'giving_gifts_received_user' => $order['giving_gifts_received_user_id'] ? SaasUser::findOne(['mobile' => User::findOne($order['giving_gifts_received_user_id'])['binding']]) : null,
  129. ];
  130. }
  131. public static function orderSubmit($order, $data) {
  132. try{
  133. if(is_string($data)){
  134. $data = json_decode($data, true);
  135. }
  136. $order->order_type = Order::ORDER_TYPE_GIVING_GIFTS;
  137. $order->mobile = '';
  138. $order->name = '';
  139. if (!$order->save()) {
  140. throw new \Exception(array_shift($order->getFirstErrors()));
  141. }
  142. $orderExt = new GivingGiftsOrder();
  143. $orderExt->store_id = $order->store_id;
  144. $orderExt->order_id = $order->id;
  145. $orderExt->from = (string)$data['from'];
  146. $orderExt->to = (string)$data['to'];
  147. $orderExt->text = (string)$data['text'];
  148. $orderExt->hide_goods = (int)$data['hide_goods'];
  149. if (!$orderExt->save()) {
  150. throw new \Exception(array_shift($orderExt->getFirstErrors()));
  151. }
  152. return [
  153. 'code' => 0,
  154. ];
  155. } catch (\Exception $ex) {
  156. \Yii::error($ex);
  157. debug_log([__METHOD__, __LINE__, $order->id, $data, $ex->getMessage()], __CLASS__ . '.log');
  158. return [
  159. 'code' => 1,
  160. 'msg' => $ex->getMessage(),
  161. ];
  162. }
  163. }
  164. public static function isReceivedTimeout($order) {
  165. return 0;
  166. $conf = self::conf($order['store_id']);
  167. if($order['giving_gifts_received_user_id']){
  168. return 0;
  169. }
  170. return ($order['pay_time'] ? (time() - 3600 * $conf['gift_timeout']) - $order['pay_time'] : 0);
  171. }
  172. public static function orderReceivedTimeoutRefund($order) {
  173. debug_log([__METHOD__, __LINE__, $order['id']], __CLASS__ . '.log');
  174. if($order['giving_gifts_received_user_id']){
  175. return;
  176. }
  177. $timeout = self::isReceivedTimeout($order);
  178. if($timeout < 0){
  179. $queueId = queue_push(new \app\jobs\GivingGiftsReceivedTimeoutRefundJob(['order_id' => $order['id']]), abs($timeout));
  180. debug_log([__METHOD__, __LINE__, $order['id'], $queueId, $timeout], __CLASS__ . '.log');
  181. return $queueId;
  182. }
  183. //退单
  184. $form = new \app\modules\admin\models\OrderRevokeForm();
  185. $form->order_id = $order['id'];
  186. $form->delete_pass = true;
  187. $form->user_id = $order['user_id'];
  188. $form->store_id = $order['store_id'];
  189. $res = $form->save();
  190. debug_log([__METHOD__, __LINE__, $order['id'], $res], __CLASS__ . '.log');
  191. return $res;
  192. }
  193. public static function afterOrderSave($order, $insert, $changedAttributes) {
  194. try{
  195. if (!$insert && isset($changedAttributes['is_pay']) && $order->is_pay == 1) {
  196. //定时退单
  197. // self::orderReceivedTimeoutRefund($order);
  198. }
  199. } catch (\Exception $ex) {
  200. \Yii::error($ex);
  201. debug_log([__METHOD__, __LINE__, $order->id, $ex->getMessage()], __CLASS__ . '.log');
  202. return [
  203. 'code' => 1,
  204. 'msg' => $ex->getMessage(),
  205. ];
  206. }
  207. }
  208. }