ScoreExpansionLogic.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. <?php
  2. /**
  3. * 积分拓客
  4. */
  5. namespace addons\ScoreExpansion\common\logic;
  6. use addons\Redpack\common\enums\RedpackEnum;
  7. use addons\Redpack\common\models\Redpack;
  8. use addons\Redpack\common\models\RedpackGoodsSetting;
  9. use addons\Redpack\common\models\RedpackUserCountDay;
  10. use addons\Redpack\common\models\RedpackUserGrab;
  11. use addons\ScoreExpansion\common\enums\ScoreExpansionEnum;
  12. use addons\ScoreExpansion\common\models\ScoreExpansion;
  13. use addons\ScoreExpansion\common\models\ScoreExpansionGoodsSetting;
  14. use addons\ScoreExpansion\common\models\ScoreExpansionRateTask;
  15. use addons\ScoreExpansion\common\models\ScoreExpansionUserCountDay;
  16. use addons\ScoreExpansion\common\models\ScoreExpansionUserGrab;
  17. use addons\ScoreExpansion\common\models\ScoreExpansionWallet;
  18. use addons\ScoreExpansion\common\models\ScoreExpansionWalletLog;
  19. use addons\ScoreExpansion\common\services\SettingService;
  20. use addons\Store\common\models\Store;
  21. use addons\Store\common\models\StoreCashierOrder;
  22. use addons\Store\common\models\StoreOrder;
  23. use addons\Store\common\models\StoreOrderDetail;
  24. use common\enums\AddonsEnum;
  25. use common\enums\StatusEnum;
  26. use common\models\order\Order;
  27. use common\models\order\OrderDetail;
  28. use common\models\order\OrderMarketing;
  29. use common\models\user\User;
  30. use common\traits\ErrorTrait;
  31. use yii\db\Exception;
  32. use Yii;
  33. use yii\helpers\Json;
  34. class ScoreExpansionLogic
  35. {
  36. use ErrorTrait;
  37. /**
  38. * 判断活动限制
  39. * @Author: lun
  40. * @DateTime: 2022/3/21 0021 17:24
  41. * @Copyright: copyright (c) 2021 广东七件事集团
  42. */
  43. public static function checkUserLimit($redpack, $user_level)
  44. {
  45. if (empty($redpack['limit_user_level'])) throw new \Exception('您暂无参与活动权限');
  46. $limit_user_level = Json::decode($redpack['limit_user_level'], true);
  47. if (!in_array($user_level, $limit_user_level)) throw new \Exception('您暂无参与活动权限');
  48. }
  49. /**
  50. * 红包金额变动
  51. * @Author: lun
  52. * @DateTime: 2022/3/22 0022 9:56
  53. * @Copyright: copyright (c) 2021 广东七件事集团
  54. * @param int $user_id
  55. */
  56. public static function gradRedpack($user_id, ScoreExpansion $redpack)
  57. {
  58. $trans = Yii::$app->db->beginTransaction();
  59. try {
  60. $time_type = $redpack['time_type'];// 活动类型[1=每日红包,2=时间段红包]
  61. $date = date('ymd', time());
  62. // 活动-每日抢红包,每天都有一次无需邀请人抢红包机会
  63. // 活动-时间段抢红包,在这个时间段内,最多只有一次无需邀请人可抢红包机会
  64. // 获取用户是否首次抢红包
  65. if ($time_type == 1) {
  66. $is_first_grad = ScoreExpansionUserCountDay::find()->where(['rid' => $redpack->id, 'user_id' => $user_id, 'date' => $date, 'status' => StatusEnum::ENABLED])->asArray()->one();
  67. } else {
  68. $is_first_grad = ScoreExpansionUserCountDay::find()->where(['rid' => $redpack->id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
  69. }
  70. $award = $redpack->award;// 活动奖金池剩余金额
  71. // 今日抢红包统计
  72. $today_grad = ScoreExpansionUserCountDay::find()->where(['rid' => $redpack->id, 'user_id' => $user_id, 'date' => $date, 'status' => StatusEnum::ENABLED])->asArray()->one();
  73. // 解析红包规则内容
  74. $rule_values = Json::decode($redpack->rule_values, true);
  75. if (empty($is_first_grad)) {
  76. // 首次抢红包金额配置
  77. $money = rand($rule_values['first_integral_start'] * 100, $rule_values['first_integral_end'] * 100);
  78. } else {
  79. // 非首次抢到红包,检查最后一次抢红包次数是否是第一次抢,不是则找到今日最后一次抢的统计内容
  80. // 判断今日抢红包次数是否已经超出
  81. if ($today_grad['grab_num_total'] >= $rule_values['times']) throw new \Exception('今日抢积分次数已用完');
  82. // 判断可抢次数是否还足够
  83. $user_grad_num = empty($today_grad['inviter_num']) ? 0 : floor($today_grad['inviter_num'] / $rule_values['everyday_invitation']);// 用户已抢红包次数
  84. if ($user_grad_num - $today_grad['grab_num_total'] + 1 <= 0) throw new \Exception('今日抢积分次数不足');
  85. $money = rand($rule_values['everyday_invitation_start'] * 100, $rule_values['everyday_invitation_end'] * 100);
  86. }
  87. // 将奖励金额单位改为元
  88. $money = $money / 100;
  89. // 扣减奖金池
  90. if ($redpack->integral_type != ScoreExpansionEnum::FREEZE_TO_ACTIVE) {
  91. // 判断可得金额是否大于奖金池剩余金额
  92. if ($money >= $award) $money = $award;
  93. $redpack->award = $redpack->award - $money;
  94. }
  95. // 判断用户曾经是否抢过红包
  96. $has_grab = ScoreExpansionUserGrab::find()->where(['mall_id' => $redpack->mall_id, 'rid' => $redpack->id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
  97. empty($has_grab) && $redpack->grab_user_num = $redpack->grab_user_num + 1;
  98. // 红包表数据统计汇总
  99. $redpack->grab_integral = $redpack->grab_integral + $money;
  100. if (!$redpack->save()) throw new Exception($redpack->getErrorMessage());
  101. // 增加用户红包金额
  102. switch ($redpack->integral_type) {
  103. case ScoreExpansionEnum::FREEZE_TO_ACTIVE :
  104. $is_frozen = false;
  105. $desc = '抢冻结到激活积分';
  106. // 将冻结红包的金额先减扣掉
  107. $wallet = ScoreExpansionWallet::setData([
  108. 'mall_id' => $redpack->mall_id,
  109. 'user_id' => $user_id,
  110. 'is_frozen' => true,
  111. 'is_deduct' => true,
  112. 'integral' => -$money,
  113. 'source_table' => ScoreExpansion::briefTableName(),
  114. 'source_table_id' => $redpack->id,
  115. 'from_type' => ScoreExpansionEnum::FROM_RECEIVE,
  116. 'desc' => $desc,
  117. ]);
  118. if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  119. $money = $wallet['money'];
  120. if ($money <= 0) throw new \Exception("暂无可激活的冻结积分");
  121. break;
  122. case ScoreExpansionEnum::INCREMENT_FREEZE :
  123. $is_frozen = true;
  124. $desc = '抢冻结积分';
  125. break;
  126. case ScoreExpansionEnum::INCREMENT_ACTIVE :
  127. $is_frozen = false;
  128. $desc = '抢激活积分';
  129. break;
  130. }
  131. // 增加用户的红包余额
  132. $res = ScoreExpansionWallet::setData([
  133. 'mall_id' => $redpack->mall_id,
  134. 'user_id' => $user_id,
  135. 'is_frozen' => $is_frozen,
  136. 'integral' => $money,
  137. 'source_table' => ScoreExpansion::briefTableName(),
  138. 'source_table_id' => $redpack->id,
  139. 'from_type' => ScoreExpansionEnum::FROM_RECEIVE,
  140. 'desc' => $desc,
  141. ]);
  142. if ($res == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  143. // 增加抢红包次数
  144. $count_day = [
  145. 'mall_id' => $redpack->mall_id,
  146. 'rid' => $redpack->id,
  147. 'user_id' => $user_id,
  148. 'grab_num' => empty($today_grad['grab_num_total']) ? $rule_values['times'] - 1 : $rule_values['times'] - $today_grad['grab_num_total'] - 1,
  149. 'grab_num_total' => empty($today_grad['grab_num_total']) ? 1 : $today_grad['grab_num_total'] + 1,
  150. ];
  151. !empty($today_grad['id']) && $count_day['id'] = $today_grad['id'];
  152. $res = ScoreExpansionUserCountDay::setData($count_day);
  153. if ($res == false) throw new Exception(ScoreExpansionUserCountDay::getStaticError());
  154. // 增加抢红包数据
  155. $res = ScoreExpansionUserGrab::setData([
  156. 'mall_id' => $redpack->mall_id,
  157. 'rid' => $redpack->id,
  158. 'user_id' => $user_id,
  159. 'grab_money' => $money,
  160. ]);
  161. if ($res == false) throw new Exception(ScoreExpansionUserGrab::getStaticError());
  162. $trans->commit();
  163. return $money;
  164. } catch (\Exception $e) {
  165. self::setStaticError($e->getMessage());
  166. $trans->rollBack();
  167. return false;
  168. }
  169. }
  170. /**
  171. * 赠送积分
  172. * @Author: lun
  173. * @DateTime: 2022/3/26 0026 14:51
  174. * @Copyright: copyright (c) 2021 广东七件事集团
  175. * @param $order_id
  176. * @param $order_status 1=支付完成,2=订单完成
  177. * @param $order_type order=平台, StoreOrder=商户
  178. * @return bool
  179. */
  180. public static function giveRedpack($order_id, $order_status = 2, $order_type = "order")
  181. {
  182. try {
  183. // 查询订单
  184. $select = "mall_id,goods_id,user_id,goods_price,adjust_money,is_evaluate,num";
  185. $source_table = Order::tableName();
  186. //判断来源 订单表
  187. if ($order_type == 'order') {
  188. $orderData = Order::findOne(['id' => $order_id]);
  189. $order_details = OrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all();
  190. $source_table = Order::tableName();
  191. } elseif ($order_type == 'StoreOrder') {
  192. $orderData = StoreOrder::findOne(['order_no' => $order_id]);
  193. $order_id = $orderData->id;
  194. $orderData = StoreOrder::findOne(['id' => $order_id]);
  195. $order_details = StoreOrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all();
  196. $source_table = StoreOrder::tableName();
  197. }
  198. if (empty($orderData)) throw new \Exception("订单id={$order_id}的订单数据为空");
  199. if (empty($order_details)) throw new \Exception("订单id={$order_id}的订单详情数据为空");
  200. $user_id = $orderData->user_id;
  201. $mall_id = $orderData->mall_id;
  202. //20230404
  203. $user_data = User::findOne(['id' => $user_id]);
  204. //2024/09/13 liuzhantao 加上注释且优化条件
  205. // “赠送积分方式” 配置说明:
  206. // 当配置为“1”:订单支付后,有两种条件
  207. // 条件一. 订单确认收货且完成评价后,这笔订单赠送的冻结积分变为激活积分(释放所有积分)
  208. // 条件二. 订单确认收货但未完成评价,过n天后,释放所有积分
  209. // 当配置为“2”:订单支付后将不能获得任何积分,要订单完成后才能获得
  210. // 获取配置【1=支付完成,2=订单完成】
  211. $give_type = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic.give_type');
  212. if ($give_type == 2 && $order_status != 2) { // 当配置为“2”,需要订单完成才能有积分,所以就直接return,反之配置为“1”,继续执行下面的
  213. return true;
  214. }
  215. //循环订单商品列表,没有单独设置则应用全局设置20230404
  216. //获取全局设置
  217. $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic');
  218. $setting_data = [];
  219. if (!empty($config['buy_frozen_integral'])) {
  220. $config['buy_frozen_integral'] = json_decode($config['buy_frozen_integral'], true);
  221. $config['buy_frozen_integral']['parent'] = 0;
  222. $setting_data[] = $config['buy_frozen_integral'];
  223. }
  224. if (!empty($config['buy_integral'])) {
  225. $config['buy_integral'] = json_decode($config['buy_integral'], true);
  226. $config['buy_integral']['parent'] = 0;
  227. $setting_data[] = $config['buy_integral'];
  228. }
  229. if (!empty($config['parent_frozen_integral'])) {
  230. $config['parent_frozen_integral'] = json_decode($config['parent_frozen_integral'], true);
  231. $config['parent_frozen_integral']['parent'] = 1;
  232. $setting_data[] = $config['parent_frozen_integral'];
  233. }
  234. if (!empty($config['parent_integral'])) {
  235. $config['parent_integral'] = json_decode($config['parent_integral'], true);
  236. $config['parent_integral']['parent'] = 1;
  237. $setting_data[] = $config['parent_integral'];
  238. }
  239. foreach ($order_details as $goodsDetail) {
  240. //判断单独设置
  241. $list = '';
  242. if ($order_type == 'order') {
  243. $list = ScoreExpansionGoodsSetting::find()->where(['goods_id' => $goodsDetail['goods_id'], 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->asArray()->all();
  244. }
  245. if (!empty($list)) {
  246. foreach ($list as $item) {
  247. // 计算可赠送的红包金额
  248. $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money'];
  249. $money = $item['is_percent'] == 0 ? $item['give'] : bcmul(($item['give'] / 100), $pay_money, 2);
  250. if ($money <= 0) continue;
  251. //判断受益者20230404
  252. $desc = '购物赠送';
  253. if ($item['parent'] == 1) {
  254. $user_id = $user_data['parent_id'];
  255. $desc = '直推下级购物赠送';
  256. if($user_id <= 0) continue;
  257. }
  258. // 订单完成 && 完成评价 && 符合条件一,把冻结的积分转换为激活积分
  259. if ($order_status == 2 && $give_type == 1) {
  260. if ($orderData->is_comment != 1 || $config['change_activation_integral'] != 1) return true; // 没有评价 或 不符合条件一,就return
  261. if ($item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) {
  262. // 增加激活积分
  263. $frozenWallet = ScoreExpansionWallet::setData([
  264. 'mall_id' => $item['mall_id'],
  265. 'user_id' => $user_id,
  266. 'is_frozen' => false,
  267. 'integral' => $money,
  268. 'source_table' => Order::tableName(),
  269. 'source_table_id' => $order_id,
  270. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  271. 'desc' => $desc,
  272. ]);
  273. if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  274. //扣减冻结积分
  275. $activationWallet = ScoreExpansionWallet::setData([
  276. 'mall_id' => $item['mall_id'],
  277. 'user_id' => $user_id,
  278. 'is_frozen' => true,
  279. 'integral' => -$money,
  280. 'source_table' => Order::tableName(),
  281. 'source_table_id' => $order_id,
  282. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  283. 'desc' => $desc,
  284. ]);
  285. if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  286. //修改定时任务状态
  287. $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
  288. $task->is_work = 1;
  289. if (!$task->save()) {
  290. throw new Exception(ScoreExpansionRateTask::getStaticError());
  291. }
  292. }
  293. } else {
  294. // 插入用户积分表
  295. $wallet = ScoreExpansionWallet::setData([
  296. 'mall_id' => $item['mall_id'],
  297. 'user_id' => $user_id,
  298. 'is_frozen' => $item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
  299. 'integral' => $money,
  300. 'source_table' => Order::tableName(),
  301. 'source_table_id' => $order_id,
  302. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  303. 'desc' => $desc,
  304. ]);
  305. if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  306. if ($give_type == 2 && $order_status == 2) {
  307. //修改定时任务状态
  308. $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
  309. $task->is_work = 1;
  310. if (!$task->save()) {
  311. throw new Exception(ScoreExpansionRateTask::getStaticError());
  312. }
  313. }
  314. }
  315. }
  316. } else {
  317. foreach ($setting_data as $setting) {
  318. // 计算可赠送的红包金额
  319. $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money'];
  320. $money = $setting['is_percent'] == 0 ? $setting['give'] * $goodsDetail['num'] : bcmul(($setting['give'] / 100), $pay_money, 2);
  321. if ($money <= 0) continue;
  322. //判断受益者20230404
  323. $desc = '购物赠送';
  324. if ($setting['parent'] == 1) {
  325. $user_id = $user_data['parent_id'];
  326. $desc = '直推下级购物赠送';
  327. if($user_id <= 0) continue;
  328. }
  329. //订单完成并且完成评价时 把冻结的积分转换为激活积分
  330. if ($order_status == 2 && $give_type == 1) {
  331. if ($orderData->is_comment != 1) return true;
  332. if ($setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) {
  333. // 增加激活积分
  334. $frozenWallet = ScoreExpansionWallet::setData([
  335. 'mall_id' => $goodsDetail['mall_id'],
  336. 'user_id' => $user_id,
  337. 'is_frozen' => false,
  338. 'integral' => $money,
  339. 'source_table' => $source_table,
  340. 'source_table_id' => $order_id,
  341. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  342. 'desc' => $desc,
  343. ]);
  344. if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  345. //扣减冻结积分
  346. $activationWallet = ScoreExpansionWallet::setData([
  347. 'mall_id' => $goodsDetail['mall_id'],
  348. 'user_id' => $user_id,
  349. 'is_frozen' => true,
  350. 'integral' => -$money,
  351. 'source_table' => $source_table,
  352. 'source_table_id' => $order_id,
  353. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  354. 'desc' => $desc,
  355. ]);
  356. if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  357. //修改定时任务状态
  358. $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
  359. $task->is_work = 1;
  360. if (!$task->save()) {
  361. throw new Exception(ScoreExpansionRateTask::getStaticError());
  362. }
  363. }
  364. } else {
  365. // 插入用户积分表
  366. $wallet = ScoreExpansionWallet::setData([
  367. 'mall_id' => $goodsDetail['mall_id'],
  368. 'user_id' => $user_id,
  369. 'is_frozen' => $setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
  370. 'integral' => $money,
  371. 'source_table' => $source_table,
  372. 'source_table_id' => $order_id,
  373. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  374. 'desc' => $desc,
  375. ]);
  376. if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  377. if ($give_type == 2 && $order_status == 2) {
  378. //修改定时任务状态
  379. $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
  380. $task->is_work = 1;
  381. if (!$task->save()) {
  382. throw new Exception(ScoreExpansionRateTask::getStaticError());
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }
  389. //循环订单商品列表,没有单独设置则应用全局设置20230404 end
  390. // 获取设置了红包的商品
  391. // $list = ScoreExpansionGoodsSetting::find()->where(['goods_id' => $goods_ids, 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->asArray()->all();true;
  392. // if (empty($list)) return true;
  393. // $orderData = Order::findOne(['id'=>$order_id]);
  394. // foreach ($list as $item) {
  395. // $is_evaluate = $order_details[$item['goods_id']]['is_evaluate'];
  396. // // 计算可赠送的红包金额
  397. // $pay_money = $order_details[$item['goods_id']]['goods_price'] + $order_details[$item['goods_id']]['adjust_money'];
  398. // $money = $item['is_percent'] == 0 ? $item['give'] : bcmul(($item['give'] / 100), $pay_money, 2);
  399. // if ($money <= 0) continue;
  400. //
  401. // //判断受益者20230404
  402. // if($item['parent'] == 1) $user_id = $user_data['parent_id'];
  403. //
  404. // //订单完成并且完成评价时 把冻结的积分转换为激活积分
  405. // if($order_status == 2 && $give_type == 1){
  406. // if($orderData->is_comment != 1) return true;
  407. // if($item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN){
  408. // // 增加激活积分
  409. //// $money = $item['give'];
  410. // $frozenWallet = ScoreExpansionWallet::setData([
  411. // 'mall_id' => $item['mall_id'],
  412. // 'user_id' => $user_id,
  413. // 'is_frozen' => false,
  414. // 'integral' => $money,
  415. // 'source_table' => Order::tableName(),
  416. // 'source_table_id' => $order_id,
  417. // 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  418. // 'desc' => '购物赠送',
  419. // ]);
  420. // if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  421. // //扣减冻结积分
  422. // $activationWallet = ScoreExpansionWallet::setData([
  423. // 'mall_id' => $item['mall_id'],
  424. // 'user_id' => $user_id,
  425. // 'is_frozen' => true,
  426. // 'integral' => -$money,
  427. // 'source_table' => Order::tableName(),
  428. // 'source_table_id' => $order_id,
  429. // 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  430. // 'desc' => '购物赠送',
  431. // ]);
  432. // if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  433. // //修改定时任务状态
  434. // $task = ScoreExpansionRateTask::findOne(['order_id'=>$order_id]);
  435. // $task->is_work = 1;
  436. // if(!$task->save()){
  437. // throw new Exception(ScoreExpansionRateTask::getStaticError());
  438. // }
  439. // }
  440. // }else{
  441. // // 插入用户积分表
  442. // $wallet = ScoreExpansionWallet::setData([
  443. // 'mall_id' => $item['mall_id'],
  444. // 'user_id' => $user_id,
  445. // 'is_frozen' => $item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
  446. // 'integral' => $money,
  447. // 'source_table' => Order::tableName(),
  448. // 'source_table_id' => $order_id,
  449. // 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  450. // 'desc' => '购物赠送',
  451. // ]);
  452. // if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  453. //
  454. // if($give_type == 2 && $order_status == 2){
  455. // //修改定时任务状态
  456. // $task = ScoreExpansionRateTask::findOne(['order_id'=>$order_id]);
  457. // $task->is_work = 1;
  458. // if(!$task->save()){
  459. // throw new Exception(ScoreExpansionRateTask::getStaticError());
  460. // }
  461. // }
  462. // }
  463. // }
  464. return true;
  465. } catch (\Exception $e) {
  466. self::setStaticError($e->getMessage());
  467. return false;
  468. }
  469. }
  470. /**
  471. * 红包返回
  472. * @Author: lun
  473. * @DateTime: 2022/9/21 0021 9:54
  474. * @Copyright: copyright (c) 2021 广东七件事集团
  475. * @param $order_detail_id
  476. * @param array $params
  477. * @return bool
  478. */
  479. public static function backRedpack($order_detail_id, $params = [])
  480. {
  481. $trans = Yii::$app->db->beginTransaction();
  482. try {
  483. $deduct = OrderMarketing::find()->select('deduct_currency')->where(['order_detail_id' => $order_detail_id, 'status' => StatusEnum::ENABLED])->scalar();
  484. if (empty($deduct)) {
  485. $trans->commit();
  486. return true;
  487. }
  488. $deduct_arr = Json::decode($deduct, true);
  489. foreach ($deduct_arr as $key => $item) {
  490. // 判断里面是否存在激活积分跟冻结积分
  491. if (in_array($key, [ScoreExpansionEnum::WALLET_INTEGRAL, ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN]) && $item['money'] > 0) {
  492. $wallet = ScoreExpansionWallet::setData([
  493. 'mall_id' => $params['mall_id'],
  494. 'user_id' => $params['user_id'],
  495. 'is_frozen' => $key == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
  496. 'integral' => $item['money'],
  497. 'source_table' => $params['source_table'],
  498. 'source_table_id' => $params['source_table_id'],
  499. 'from_type' => $params['from_type'],
  500. 'desc' => $params['desc'] ?? '',
  501. 'no_icr_total' => true,// 不增加积分累计总额
  502. ]);
  503. if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  504. }
  505. }
  506. $trans->commit();
  507. return true;
  508. } catch (\Exception $e) {
  509. $trans->rollBack();
  510. self::setStaticError($e->getMessage());
  511. return false;
  512. }
  513. }
  514. /**
  515. * 收银台赠送积分
  516. * @Author: lun
  517. * @DateTime: 2022/3/26 0026 14:51
  518. * @Copyright: copyright (c) 2021 广东七件事集团
  519. * @param $order_id
  520. * @param $order_status 1=支付完成,2=订单完成
  521. * @param $order_type order=平台, StoreOrder=商户
  522. * @return bool
  523. */
  524. public static function giveCashierRedpack($order_id, $order_status = 2)
  525. {
  526. try {
  527. // 查询订单
  528. $source_table = StoreCashierOrder::tableName();
  529. $orderData = StoreCashierOrder::findOne(['id' => $order_id]);
  530. if (empty($orderData)) throw new \Exception("订单id={$order_id}的订单数据为空");
  531. //判断来源 商户 门店 平台20230404
  532. $user_id = $orderData->user_id;
  533. $mall_id = $orderData->mall_id;
  534. //20230404
  535. $user_data = User::findOne(['id' => $user_id]);
  536. //循环订单商品列表,没有单独设置则应用全局设置20230404
  537. //获取全局设置
  538. $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic');
  539. $setting_data = [];
  540. if (!empty($config['buy_frozen_integral'])) {
  541. $config['buy_frozen_integral'] = json_decode($config['buy_frozen_integral'], true);
  542. $config['buy_frozen_integral']['parent'] = 0;
  543. $setting_data[] = $config['buy_frozen_integral'];
  544. }
  545. if (!empty($config['buy_integral'])) {
  546. $config['buy_integral'] = json_decode($config['buy_integral'], true);
  547. $config['buy_integral']['parent'] = 0;
  548. $setting_data[] = $config['buy_integral'];
  549. }
  550. if (!empty($config['parent_frozen_integral'])) {
  551. $config['parent_frozen_integral'] = json_decode($config['parent_frozen_integral'], true);
  552. $config['parent_frozen_integral']['parent'] = 1;
  553. $setting_data[] = $config['parent_frozen_integral'];
  554. }
  555. if (!empty($config['parent_integral'])) {
  556. $config['parent_integral'] = json_decode($config['parent_integral'], true);
  557. $config['parent_integral']['parent'] = 1;
  558. $setting_data[] = $config['parent_integral'];
  559. }
  560. foreach ($setting_data as $setting) {
  561. // 计算可赠送的红包金额
  562. $pay_money = $orderData['pay_money'];
  563. $money = $setting['is_percent'] == 0 ? $setting['give'] : bcmul(($setting['give'] / 100), $pay_money, 2);
  564. if ($money <= 0) continue;
  565. //判断受益者
  566. $desc = '购物赠送';
  567. if ($setting['parent'] == 1) {
  568. $user_id = $user_data['parent_id'];
  569. $desc = '直推下级购物赠送';
  570. }
  571. // 插入用户积分表
  572. $wallet = ScoreExpansionWallet::setData([
  573. 'mall_id' => $mall_id,
  574. 'user_id' => $user_id,
  575. 'is_frozen' => $setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
  576. 'integral' => $money,
  577. 'source_table' => $source_table,
  578. 'source_table_id' => $order_id,
  579. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  580. 'desc' => $desc,
  581. ]);
  582. if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  583. }
  584. //循环订单商品列表,没有单独设置则应用全局设置20230404 end
  585. return true;
  586. } catch (\Exception $e) {
  587. self::setStaticError($e->getMessage());
  588. return false;
  589. }
  590. }
  591. /**
  592. * 店铺赠送积分
  593. * @param $order_id
  594. * @param $order_status
  595. * @param $order_type
  596. * @return bool
  597. */
  598. public static function giveStoreRedpack($order_id, $order_status = 2, $order_type = 'order')
  599. {
  600. try {
  601. // 查询订单
  602. $select = "mall_id,goods_id,user_id,goods_price,adjust_money,is_evaluate";
  603. $source_table = Order::tableName();
  604. //判断来源 订单表
  605. if ($order_type == 'order') {
  606. $orderData = Order::findOne(['id' => $order_id]);
  607. $order_details = OrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all();
  608. $source_table = Order::tableName();
  609. } elseif ($order_type == 'StoreOrder') {
  610. $orderData = StoreOrder::findOne(['id' => $order_id]);
  611. $order_details = StoreOrderDetail::find()->select($select)->where(['order_id' => $order_id])->indexBy("goods_id")->asArray()->all();
  612. $source_table = StoreOrder::tableName();
  613. }
  614. if (empty($orderData)) throw new \Exception("订单id={$order_id}的订单数据为空");
  615. if (empty($order_details)) throw new \Exception("订单id={$order_id}的订单详情数据为空");
  616. //查找店铺持有者的user_id
  617. if(empty($orderData->store_id)) throw new \Exception("订单id={$order_id}的店铺id为空");
  618. $storeData = Store::findOne(['id'=>$orderData->store_id]);
  619. $user_id = $storeData->user_id;
  620. $mall_id = $storeData->mall_id;
  621. //20230404
  622. $user_data = User::findOne(['id' => $user_id]);
  623. //当配置为1时 订单完成 将订单获得的冻结积分转变为激活积分
  624. //当配置为2时 订单支付完成将不能获得任何积分
  625. // 获取配置【1=支付完成,2=订单完成】
  626. $give_type = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic.give_type');
  627. if ($give_type == 2 && $order_status != 2) {
  628. return true;
  629. }
  630. //循环订单商品列表,没有单独设置则应用全局设置20230404
  631. //获取全局设置
  632. $config = Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_SCORE_EXPANSION, 'basic');
  633. $setting_data = [];
  634. //门店设置
  635. if(!empty($config['store_frozen_integral'])){
  636. $config['store_frozen_integral'] = json_decode($config['store_frozen_integral'],true);
  637. $config['store_frozen_integral']['parent'] = 0;
  638. $setting_data[] = $config['store_frozen_integral'];
  639. }
  640. if(!empty($config['store_integral'])){
  641. $config['store_integral'] = json_decode($config['store_integral'],true);
  642. $config['store_integral']['parent'] = 0;
  643. $setting_data[] = $config['store_integral'];
  644. }
  645. if(!empty($config['parent_store_frozen_integral'])){
  646. $config['parent_store_frozen_integral'] = json_decode($config['parent_store_frozen_integral'],true);
  647. $config['parent_store_frozen_integral']['parent'] = 1;
  648. $setting_data[] = $config['parent_store_frozen_integral'];
  649. }
  650. if(!empty($config['parent_store_integral'])){
  651. $config['parent_store_integral'] = json_decode($config['parent_store_integral'],true);
  652. $config['parent_store_integral']['parent'] = 1;
  653. $setting_data[] = $config['parent_store_integral'];
  654. }
  655. foreach ($order_details as $goodsDetail) {
  656. //判断单独设置
  657. $list = '';
  658. if ($order_type == 'order') {
  659. $list = ScoreExpansionGoodsSetting::find()->where(['goods_id' => $goodsDetail['goods_id'], 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->asArray()->all();
  660. }
  661. if (!empty($list)) {
  662. foreach ($list as $item) {
  663. // 计算可赠送的红包金额
  664. $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money'];
  665. $money = $item['is_percent'] == 0 ? $item['give'] : bcmul(($item['give'] / 100), $pay_money, 2);
  666. if ($money <= 0) continue;
  667. //判断受益者20230404
  668. $desc = '购物赠送';
  669. if ($item['parent'] == 1) {
  670. $user_id = $user_data['parent_id'];
  671. $desc = '直推下级购物赠送';
  672. }
  673. //订单完成并且完成评价时 把冻结的积分转换为激活积分
  674. if ($order_status == 2 && $give_type == 1) {
  675. if ($orderData->is_comment != 1) return true;
  676. if ($item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) {
  677. // 增加激活积分
  678. $frozenWallet = ScoreExpansionWallet::setData([
  679. 'mall_id' => $item['mall_id'],
  680. 'user_id' => $user_id,
  681. 'is_frozen' => false,
  682. 'integral' => $money,
  683. 'source_table' => Order::tableName(),
  684. 'source_table_id' => $order_id,
  685. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  686. 'desc' => $desc,
  687. ]);
  688. if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  689. //扣减冻结积分
  690. $activationWallet = ScoreExpansionWallet::setData([
  691. 'mall_id' => $item['mall_id'],
  692. 'user_id' => $user_id,
  693. 'is_frozen' => true,
  694. 'integral' => -$money,
  695. 'source_table' => Order::tableName(),
  696. 'source_table_id' => $order_id,
  697. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  698. 'desc' => $desc,
  699. ]);
  700. if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  701. //修改定时任务状态
  702. $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
  703. $task->is_work = 1;
  704. if (!$task->save()) {
  705. throw new Exception(ScoreExpansionRateTask::getStaticError());
  706. }
  707. }
  708. } else {
  709. // 插入用户积分表
  710. $wallet = ScoreExpansionWallet::setData([
  711. 'mall_id' => $item['mall_id'],
  712. 'user_id' => $user_id,
  713. 'is_frozen' => $item['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
  714. 'integral' => $money,
  715. 'source_table' => Order::tableName(),
  716. 'source_table_id' => $order_id,
  717. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  718. 'desc' => $desc,
  719. ]);
  720. if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  721. if ($give_type == 2 && $order_status == 2) {
  722. //修改定时任务状态
  723. $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
  724. $task->is_work = 1;
  725. if (!$task->save()) {
  726. throw new Exception(ScoreExpansionRateTask::getStaticError());
  727. }
  728. }
  729. }
  730. }
  731. } else {
  732. foreach ($setting_data as $setting) {
  733. // 计算可赠送的红包金额
  734. $pay_money = $goodsDetail['goods_price'] + $goodsDetail['adjust_money'];
  735. $money = $setting['is_percent'] == 0 ? $setting['give'] : bcmul(($setting['give'] / 100), $pay_money, 2);
  736. if ($money <= 0) continue;
  737. //判断受益者20230404
  738. $desc = '购物赠送';
  739. if ($setting['parent'] == 1) {
  740. $user_id = $user_data['parent_id'];
  741. $desc = '直推下级购物赠送';
  742. }
  743. //订单完成并且完成评价时 把冻结的积分转换为激活积分
  744. if ($order_status == 2 && $give_type == 1) {
  745. if ($orderData->is_comment != 1) return true;
  746. if ($setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN) {
  747. // 增加激活积分
  748. $frozenWallet = ScoreExpansionWallet::setData([
  749. 'mall_id' => $goodsDetail['mall_id'],
  750. 'user_id' => $user_id,
  751. 'is_frozen' => false,
  752. 'integral' => $money,
  753. 'source_table' => $source_table,
  754. 'source_table_id' => $order_id,
  755. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  756. 'desc' => $desc,
  757. ]);
  758. if ($frozenWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  759. //扣减冻结积分
  760. $activationWallet = ScoreExpansionWallet::setData([
  761. 'mall_id' => $goodsDetail['mall_id'],
  762. 'user_id' => $user_id,
  763. 'is_frozen' => true,
  764. 'integral' => -$money,
  765. 'source_table' => $source_table,
  766. 'source_table_id' => $order_id,
  767. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  768. 'desc' => $desc,
  769. ]);
  770. if ($activationWallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  771. //修改定时任务状态
  772. $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
  773. $task->is_work = 1;
  774. if (!$task->save()) {
  775. throw new Exception(ScoreExpansionRateTask::getStaticError());
  776. }
  777. }
  778. } else {
  779. // 插入用户积分表
  780. $wallet = ScoreExpansionWallet::setData([
  781. 'mall_id' => $goodsDetail['mall_id'],
  782. 'user_id' => $user_id,
  783. 'is_frozen' => $setting['integral_wallet_type'] == ScoreExpansionEnum::WALLET_INTEGRAL_FROZEN ? true : false,
  784. 'integral' => $money,
  785. 'source_table' => $source_table,
  786. 'source_table_id' => $order_id,
  787. 'from_type' => ScoreExpansionEnum::FROM_GOODS,
  788. 'desc' => $desc,
  789. ]);
  790. if ($wallet == false) throw new Exception(ScoreExpansionWallet::getStaticError());
  791. if ($give_type == 2 && $order_status == 2) {
  792. //修改定时任务状态
  793. $task = ScoreExpansionRateTask::findOne(['order_id' => $order_id]);
  794. $task->is_work = 1;
  795. if (!$task->save()) {
  796. throw new Exception(ScoreExpansionRateTask::getStaticError());
  797. }
  798. }
  799. }
  800. }
  801. }
  802. }
  803. //循环订单商品列表,没有单独设置则应用全局设置20230404 end
  804. return true;
  805. } catch (\Exception $e) {
  806. self::setStaticError($e->getMessage());
  807. return false;
  808. }
  809. }
  810. }