ElectronCouponUserCoupon.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. namespace addons\ElectronCoupon\common\models;
  3. use addons\ElectronCoupon\common\enums\ElectronCouponEnums;
  4. use common\models\goods\Goods;
  5. use common\models\user\User;
  6. use common\models\user\UserPartitionRelationship;
  7. use Yii;
  8. use common\enums\StatusEnum;
  9. use yii\db\Exception;
  10. /**
  11. * This is the model class for table "{{%addons_electron_coupon_user_coupon}}".
  12. *
  13. * @property int $id
  14. * @property int $e_coupon_id 电子券id
  15. * @property string|null $receive_begin_time 领取开始时间
  16. * @property string|null $receive_end_time 领取结束时间
  17. * @property string|null $use_begin_time 使用开始时间
  18. * @property string|null $use_end_time 使用结束时间
  19. * @property float $price 金额
  20. * @property int $get_type 获取方式,1是领取,2是购买商品获得,3是系统发放
  21. * @property int $use_way 适用范围;1扫描核销
  22. * @property int $receive_times 领取次数
  23. * @property int $receive_amount 领取数量;未激活
  24. * @property int $active_amount 激活数量;
  25. * @property int $used_amount 兑换数量;即使用数量
  26. * @property int $num 电子卷总数
  27. * @property int $created_at 创建时间
  28. * @property int $updated_at 更新时间
  29. * @property int $mall_id 商城id
  30. * @property int $user_id 用户id
  31. * @property string|null $last_used_time 最新兑换时间
  32. * @property string|null $last_receive_time 最新领取时间
  33. * @property string|null $last_active_time 最新激活时间
  34. * @property int $is_proper 是否专有券
  35. * @property int $giver_id 赠送者id
  36. * @property int $proper_id 专有者id
  37. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  38. */
  39. class ElectronCouponUserCoupon extends \common\models\base\BaseActiveRecord
  40. {
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public static function tableName()
  45. {
  46. return '{{%addons_electron_coupon_user_coupon}}';
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function rules()
  52. {
  53. return [
  54. [['e_coupon_id', 'user_id'], 'required'],
  55. [['e_coupon_id', 'get_type', 'use_way', 'receive_times', 'receive_amount', 'active_amount', 'used_amount', 'num', 'created_at', 'updated_at', 'goods_id', 'mall_id', 'user_id', 'is_proper', 'giver_id', 'proper_id', 'status'], 'integer'],
  56. [['receive_begin_time', 'receive_end_time', 'use_begin_time', 'use_end_time', 'last_used_time', 'last_receive_time', 'last_active_time'], 'safe'],
  57. [['price'], 'number'],
  58. ];
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function attributeLabels()
  64. {
  65. return [
  66. 'id' => 'ID',
  67. 'e_coupon_id' => '电子券id',
  68. 'receive_begin_time' => '领取开始时间',
  69. 'receive_end_time' => '领取结束时间',
  70. 'use_begin_time' => '使用开始时间',
  71. 'use_end_time' => '使用结束时间',
  72. 'price' => '金额',
  73. 'get_type' => '获取方式,1是领取,2是购买商品获得,3是系统发放',
  74. 'use_way' => '适用范围;1扫描核销',
  75. 'receive_times' => '领取次数',
  76. 'receive_amount' => '领取数量;未激活',
  77. 'active_amount' => '激活数量;',
  78. 'used_amount' => '兑换数量;即使用数量',
  79. 'num' => '电子卷总数',
  80. 'created_at' => '创建时间',
  81. 'updated_at' => '更新时间',
  82. 'mall_id' => '商城id',
  83. 'user_id' => '用户id',
  84. 'last_used_time' => '最新兑换时间',
  85. 'last_receive_time' => '最新领取时间',
  86. 'last_active_time' => '最新激活时间',
  87. 'is_proper' => '是否专有券',
  88. 'giver_id' => '赠送者id',
  89. 'proper_id' => '专有者id',
  90. 'goods_id' => '商品id',
  91. 'status' => '状态[-1:删除;0:禁用;1启用]',
  92. ];
  93. }
  94. public function getUser()
  95. {
  96. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,mobile,nickname,avatar_url,username');
  97. }
  98. // 专有者
  99. public function getProperUser()
  100. {
  101. return $this->hasOne(User::class, ['id' => 'proper_id'])->select('id,mobile,nickname,avatar_url,username');
  102. }
  103. // 赠送者
  104. public function getGiver()
  105. {
  106. return $this->hasOne(User::class, ['id' => 'giver_id'])->select('id,mobile,nickname,avatar_url,username');
  107. }
  108. // 购买商品
  109. public function getGoods()
  110. {
  111. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select('id,goods_name');
  112. }
  113. // 获取分页列表
  114. public static function getListPage($condition, $mall_id)
  115. {
  116. $page_data = self::listPage($condition, false, true);
  117. $page_data['list'] = is_array($page_data['list']) ? $page_data['list'] : [];
  118. if ($page_data['list']) {
  119. // 关联电子券
  120. $coupon_id_array = array_unique(array_filter(array_column($page_data['list'], 'e_coupon_id')));
  121. $e_coupon = ElectronCoupon::find()
  122. ->select('id,name,pic,rules,is_give')
  123. ->where(['>=', 'status', StatusEnum::DISABLED])
  124. ->where(['mall_id' => $mall_id])
  125. ->where(['in', 'id', $coupon_id_array])
  126. ->asArray()
  127. ->all();
  128. $coupon_name_data = array_column($e_coupon, null, 'id');
  129. } else {
  130. $coupon_name_data = [];
  131. }
  132. foreach ($page_data['list'] as $index => $item) {
  133. // 电子券
  134. $item['e_coupon'] = $coupon_name_data[$item['e_coupon_id']] ?? '';
  135. $page_data['list'][$index] = $item;
  136. }
  137. return $page_data;
  138. }
  139. /**
  140. * 数据设置
  141. */
  142. public static function setData(array $params)
  143. {
  144. try{
  145. if (!isset($params['mall_id']) || empty($params['mall_id'])) throw new Exception('缺少商城ID');
  146. $mall_id = $params['mall_id'];
  147. if (isset($params['id']) && !empty($params['id'])) {
  148. $model = self::findOne(['id' => $params['id'], 'mall_id' => $mall_id, 'status' => StatusEnum::ENABLED]);
  149. if(empty($model)) throw new Exception('会员电子券不存在或已删除');
  150. $bcmath = ['receive_times', 'receive_amount', 'active_amount', 'used_amount', 'num'];
  151. foreach ($bcmath as $field) {
  152. if (isset($params[$field])) {
  153. $model->$field += $params[$field];
  154. if ($model->$field < 0) {
  155. throw new Exception('保存失败');
  156. }
  157. unset($params[$field]);
  158. }
  159. }
  160. } else {
  161. $model = new self();
  162. $model->loadDefaultValues();
  163. $model->mall_id = $mall_id;
  164. }
  165. if (!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  166. return $model;
  167. }catch(Exception $e){
  168. self::setStaticError($e->getMessage());
  169. return false;
  170. }
  171. }
  172. /**
  173. * 用户之间赠送电子券
  174. * @param array $params
  175. * user_coupon_id 主键id
  176. * user_id 赠送者用户id
  177. * e_coupon_id 电子券id
  178. * receiver_user_id 获赠者会员id
  179. * amount 赠送数量
  180. * status 赠送哪种状态的电子券
  181. * mall_id 商城id
  182. * @return bool
  183. */
  184. public static function userGiveCoupon(array $params)
  185. {
  186. $tran = Yii::$app->db->beginTransaction();
  187. try {
  188. $must_keys = ['user_coupon_id', 'e_coupon_id', 'receiver_user_id', 'amount', 'status', 'mall_id', 'user_id'];
  189. foreach ($must_keys as $key) {
  190. if (!in_array($key, array_keys($params))) {
  191. throw new Exception('缺少必要的参数' . $key);
  192. }
  193. }
  194. $config = Yii::$app->services->setting->addons->get($params['mall_id'], ElectronCouponEnums::ADDONS_NAME, 'basic');
  195. $is_open = $config['is_open'] ?? 0;
  196. if (!$is_open) {
  197. throw new Exception('暂时无法赠送好友');
  198. }
  199. $is_allow_deal = $config['is_allow_deal'] ?? 0;
  200. if (!$is_allow_deal) {
  201. throw new Exception('暂时无法赠送好友');
  202. }
  203. $customize_name = $config['customize_name'] ?? '电子券';
  204. $source_transfer_permission = $config['source_transfer_permission'] ?? 0;
  205. if ($source_transfer_permission == 1) {
  206. $parent_give = UserPartitionRelationship::getParentIdArr($params['user_id']);
  207. $parent_receive = UserPartitionRelationship::getParentIdArr($params['receiver_user_id']);
  208. if (!in_array($params['receiver_user_id'], $parent_give) && !in_array($params['user_id'], $parent_receive)) {
  209. throw new Exception($customize_name . '仅支持赠送给团队成员');
  210. }
  211. }
  212. $user_coupon = self::findOne(['id' => $params['user_coupon_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED]);
  213. if (!$user_coupon) {
  214. throw new Exception($customize_name . '不存在');
  215. }
  216. // 电子券信息
  217. $coupon = ElectronCoupon::getOne([['id' => $user_coupon['e_coupon_id']], ['mall_id' => $params['mall_id']], ['<>', 'status', StatusEnum::DELETE]], true, '', '', ['id', 'name', 'is_give','share_expiry']);
  218. if (!$coupon) {
  219. throw new Exception($customize_name . '不存在');
  220. }
  221. if(!empty($coupon['share_expiry'])&&!empty($user_coupon['giver_id'])){
  222. throw new Exception($customize_name . '赠送券不支持赠送给好友');
  223. }
  224. if (!$coupon['is_give']) {
  225. throw new Exception($customize_name . '不支持赠送给好友');
  226. }
  227. if ($params['user_id'] != $user_coupon['user_id']) {
  228. throw new Exception($customize_name . '不存在');
  229. }
  230. if (!in_array($params['status'], [ElectronCouponEnums::COUPON_STATUS_RECEIVE, ElectronCouponEnums::COUPON_STATUS_ACTIVE])) {
  231. throw new Exception('不支持的' . $customize_name . '状态');
  232. }
  233. if ($params['status'] == ElectronCouponEnums::COUPON_STATUS_RECEIVE && $params['amount'] > $user_coupon['receive_amount']) { // 未激活
  234. throw new Exception('未激活的' . $customize_name . '数量不足');
  235. }
  236. if ($params['status'] == ElectronCouponEnums::COUPON_STATUS_ACTIVE && $params['amount'] > $user_coupon['active_amount']) { // 已激活
  237. throw new Exception('激活的' . $customize_name . '数量不足');
  238. }
  239. if ($user_coupon['use_end_time'] < time()) {
  240. throw new Exception($customize_name . '已过期');
  241. }
  242. $receive_user = User::getOne([['id' => $params['receiver_user_id']]], true, '', '', 'id');
  243. if (!$receive_user) {
  244. throw new Exception('用户不存在');
  245. }
  246. // 获赠者的数据
  247. $data = [
  248. 'e_coupon_id' => $user_coupon['e_coupon_id'],
  249. 'receive_begin_time' => $user_coupon['receive_begin_time'],
  250. 'receive_end_time' => $user_coupon['receive_end_time'],
  251. 'use_begin_time' => $user_coupon['use_begin_time'],
  252. 'use_end_time' => $user_coupon['use_end_time'],
  253. 'price' => $user_coupon['price'],
  254. 'get_type' => $user_coupon['get_type'],
  255. 'use_way' => $user_coupon['use_way'],
  256. 'receive_times' => 0,
  257. 'receive_amount' => 0,
  258. 'active_amount' => 0,
  259. 'used_amount' => 0,
  260. 'num' => $params['amount'],
  261. 'mall_id' => $params['mall_id'],
  262. 'user_id' => $params['receiver_user_id'],
  263. 'last_used_time' => 0,
  264. 'is_proper' => $user_coupon['is_proper'],
  265. 'giver_id' => $user_coupon['user_id'],
  266. 'proper_id' => $user_coupon['proper_id'], // 专有者不发生改变
  267. 'goods_id' => $user_coupon['goods_id'],
  268. 'status' => $user_coupon['status']
  269. ];
  270. // 赠送者的数据
  271. $give_data = [
  272. 'id' => $user_coupon['id'],
  273. 'mall_id' => $user_coupon['mall_id'],
  274. 'num' => -1 * $params['amount'],
  275. ];
  276. if ($params['status'] == ElectronCouponEnums::COUPON_STATUS_RECEIVE) {
  277. $data['receive_amount'] = $params['amount'];
  278. $data['last_receive_time'] = time();
  279. $give_data['receive_amount'] = -1 * $params['amount'];
  280. }
  281. if ($params['status'] == ElectronCouponEnums::COUPON_STATUS_ACTIVE) {
  282. $data['active_amount'] = $params['amount'];
  283. $data['last_active_time'] = time();
  284. $give_data['active_amount'] = -1 * $params['amount'];
  285. }
  286. // 赠送记录的数据
  287. $log_data = [
  288. 'mall_id' => $params['mall_id'],
  289. 'e_coupon_id' => $user_coupon['e_coupon_id'],
  290. 'user_id' => $params['receiver_user_id'],
  291. 'give_user_id' => $user_coupon['user_id'],
  292. 'e_coupon_name' => $coupon['name'], // 电子券名称
  293. 'amount' => $params['amount'],
  294. 'is_proper' => $user_coupon['is_proper'],
  295. 'proper_id' => $user_coupon['proper_id'],
  296. 'status' => StatusEnum::ENABLED
  297. ];
  298. // 同一张电子券可转让给同一个会员多少张电子券,填 0 不限制
  299. $transfer_num = $config['transfer_num'] ?? 0;
  300. if ($transfer_num > 0) {
  301. $log_give_num = (int) ElectronCouponGiveLog::find()->where([
  302. 'mall_id' => $params['mall_id'],
  303. 'e_coupon_id' => $user_coupon['e_coupon_id'],
  304. 'user_id' => $params['receiver_user_id'],
  305. 'give_user_id' => $user_coupon['user_id']
  306. ])->sum('amount');
  307. if ($log_give_num >= $transfer_num) {
  308. throw new Exception('该' . $customize_name . '赠送给该好友的次数已达上限');
  309. }
  310. }
  311. // 同一张电子券可接收不同会员的多少张电子券,填 0 不限制
  312. $receive_num = $config['receive_num'] ?? 0;
  313. if ($receive_num > 0) {
  314. $log_receive_num = (int) ElectronCouponGiveLog::find()->where([
  315. 'mall_id' => $params['mall_id'],
  316. 'e_coupon_id' => $user_coupon['e_coupon_id'],
  317. 'user_id' => $params['receiver_user_id']
  318. ])->sum('amount');
  319. if ($log_receive_num >= $receive_num) {
  320. throw new Exception('该好友获赠该' . $customize_name . '的次数已达上限');
  321. }
  322. }
  323. // 获赠者有没有对应的电子券
  324. $receive_user_coupon = self::getOne([
  325. ['mall_id' => $data['mall_id']],
  326. ['user_id' => $data['user_id']],
  327. ['e_coupon_id' => $data['e_coupon_id']],
  328. ['get_type' => $data['get_type']],
  329. ['is_proper' => $data['is_proper']],
  330. ['proper_id' => $data['proper_id']], // 同一专有者
  331. ['giver_id' => $data['giver_id']], // 同一赠送人
  332. ['goods_id' => (int) $data['goods_id']], // 同一商品赠送
  333. ['use_begin_time' => $data['use_begin_time']], // 同一使用开始时间
  334. ['receive_end_time' => $data['receive_end_time']], // 同一使用结束时间
  335. ['status' => StatusEnum::ENABLED]
  336. ], true, '', '', 'id');
  337. if ($receive_user_coupon) {
  338. $data['id'] = $receive_user_coupon['id'];
  339. } else {
  340. $data['last_receive_time'] = time();
  341. $data['last_active_time'] = time();
  342. }
  343. // 赠送
  344. $update_receive_coupon = self::setData($data);
  345. if ($update_receive_coupon == false) throw new Exception('赠送失败 ' . self::getStaticError());
  346. // 赠送者减去赠送数量
  347. $update_give_coupon = self::setData($give_data);
  348. if ($update_give_coupon == false) throw new Exception('赠送失败 ' . self::getStaticError());
  349. // 赠送记录
  350. $log = ElectronCouponGiveLog::setData($log_data);
  351. if ($log == false) throw new Exception('赠送失败 ' . ElectronCouponGiveLog::getStaticError());
  352. // 赠送记录的数据
  353. $time = time();
  354. $log_data_s = [
  355. 'mall_id' => $params['mall_id'],
  356. 'e_coupon_id' => $user_coupon['e_coupon_id'],
  357. 'user_coupon_id' => $user_coupon['id'],
  358. 'accept_user_e_coupon_id' => $update_receive_coupon['id'],
  359. 'user_id' => $params['receiver_user_id'],
  360. 'give_user_id' => $user_coupon['user_id'],
  361. 'active_time' => $params['status']==1?$user_coupon['last_active_time']:0,
  362. 'use_time' => 0,
  363. 'share_expiry' => $coupon['share_expiry'],
  364. 'created_at'=>$time,
  365. 'updated_at'=>$time,
  366. 'status' => StatusEnum::ENABLED
  367. ];
  368. $res = ElectronCouponGiveLogs::setData($log_data_s,$params['amount']);
  369. if ($res == false) throw new Exception('赠送失败 ' . ElectronCouponGiveLogs::getStaticError());
  370. $tran->commit();
  371. return true;
  372. } catch (Exception $e) {
  373. $tran->rollBack();
  374. self::setStaticError($e->getMessage());
  375. return false;
  376. }
  377. }
  378. /**
  379. * 领券电子券操作
  380. * e_coupon_id 电子券id
  381. * mall_id 商城id
  382. * user_id 领取人
  383. * goods_id 购买的商品id
  384. * get_type 获取方式
  385. * receive_times 领取次数,默认是领取一次
  386. *
  387. */
  388. public static function userReceiveCoupon(array $params)
  389. {
  390. $tran = Yii::$app->db->beginTransaction();
  391. try {
  392. $must_keys = ['e_coupon_id', 'mall_id', 'user_id', 'get_type'];
  393. foreach ($must_keys as $key) {
  394. if (!in_array($key, array_keys($params))) {
  395. throw new Exception('缺少必要的参数' . $key);
  396. }
  397. }
  398. if (!in_array($params['get_type'], [
  399. ElectronCouponEnums::COUPON_GET_TYPE_RECEIVE,
  400. ElectronCouponEnums::COUPON_GET_TYPE_GOODS,
  401. ElectronCouponEnums::COUPON_GET_TYPE_SYSTEM
  402. ])) {
  403. throw new Exception('不支持的获取方式');
  404. }
  405. // 默认领取一次
  406. $receive_times = $params['receive_times'] ?? 1;
  407. $config = Yii::$app->services->setting->addons->get($params['mall_id'], ElectronCouponEnums::ADDONS_NAME, 'basic');
  408. $is_open = $config['is_open'] ?? 0;
  409. if (!$is_open) {
  410. throw new Exception('暂时无法领取');
  411. }
  412. $customize_name = $config['customize_name'] ?? '电子券';
  413. $coupon_info = ElectronCoupon::getOne([['id' => $params['e_coupon_id']], ['mall_id' => $params['mall_id']], ['>=', 'status', StatusEnum::DISABLED]], true);
  414. if (!$coupon_info) {
  415. throw new Exception($customize_name . '不存在或已下架');
  416. }
  417. if($receive_times>($coupon_info['amount'] -$coupon_info['receive_amount'])){
  418. throw new Exception($coupon_info['name'].'数量不足');
  419. }
  420. $goods_id = $params['goods_id'] ?? 0;
  421. if ($params['get_type'] == ElectronCouponEnums::COUPON_GET_TYPE_GOODS && !$goods_id) {
  422. throw new Exception('商品ID不能为空');
  423. }
  424. // 是否可以被领取
  425. if ($params['get_type'] == ElectronCouponEnums::COUPON_GET_TYPE_RECEIVE && $coupon_info['is_can_receive'] == ElectronCouponEnums::IS_NOT_RECEIVE) {
  426. throw new Exception('该' . $customize_name . '暂时不支持领取');
  427. }
  428. if ($coupon_info['receive_begin_time'] > time()) {
  429. throw new Exception('该' . $customize_name . '未到领取时间');
  430. }
  431. if ($coupon_info['receive_end_time'] < time()) {
  432. throw new Exception('该' . $customize_name . '已过领取时间');
  433. }
  434. $user_info = User::getOne([
  435. ['id' => $params['user_id']],
  436. ['mall_id' => $params['mall_id']],
  437. ['status' => StatusEnum::ENABLED]
  438. ], true);
  439. if (empty($user_info)) {
  440. throw new Exception('领取会员ID不正确');
  441. }
  442. // 领取级别
  443. if ($coupon_info['receive_codition'] == ElectronCouponEnums::RECEIVE_CONDITION_LEVEL) {
  444. $level = $coupon_info['receive_member_level'];
  445. if ($level != $user_info['level']) {
  446. throw new Exception('您不符合领取条件');
  447. }
  448. }
  449. $proper_id = $coupon_info['is_proper'] ? $params['user_id'] : 0;
  450. $pieces_amount = $params['pieces_amount'] ?? $coupon_info['pieces_amount'];
  451. $pieces_amount = $receive_times * $pieces_amount;
  452. // 判断领取的电子券+已领取的电子券是否大于总电子券数量
  453. if ($coupon_info['receive_amount'] + $pieces_amount > $coupon_info['amount']) {
  454. throw new Exception('该' . $customize_name . '已超出领取数量');
  455. }
  456. $user_coupon_data = [
  457. 'e_coupon_id' => $params['e_coupon_id'],
  458. 'receive_begin_time' => $coupon_info['receive_begin_time'],
  459. 'receive_end_time' => $coupon_info['receive_end_time'],
  460. 'use_begin_time' => $coupon_info['use_begin_time'],
  461. 'use_end_time' => $coupon_info['use_end_time'],
  462. 'price' => $coupon_info['price'],
  463. 'get_type' => ElectronCouponEnums::COUPON_GET_TYPE_RECEIVE,
  464. 'use_way' => ElectronCouponEnums::USE_WAY,
  465. 'receive_times' => $receive_times,
  466. 'receive_amount' => 0,
  467. 'active_amount' => 0,
  468. 'used_amount' => 0,
  469. 'num' => $pieces_amount,
  470. 'mall_id' => $params['mall_id'],
  471. 'user_id' => $params['user_id'],
  472. 'is_proper' => $coupon_info['is_proper'],
  473. 'giver_id' => 0,
  474. 'proper_id' => $proper_id,
  475. 'goods_id' => $goods_id,
  476. 'status' => StatusEnum::ENABLED,
  477. 'last_receive_time' => 0,
  478. 'last_used_time' => 0,
  479. 'last_active_time' => 0
  480. ];
  481. // 如果是购买商品赠送的 || 后台送的,则无需激活
  482. if (in_array($params['get_type'], [ElectronCouponEnums::COUPON_GET_TYPE_GOODS, ElectronCouponEnums::COUPON_GET_TYPE_SYSTEM]) && $coupon_info['price']<=0) {
  483. $user_coupon_data['active_amount'] = $pieces_amount;
  484. $user_coupon_data['last_active_time'] = time();
  485. } else {
  486. $user_coupon_data['receive_amount'] = $pieces_amount;
  487. $user_coupon_data['last_receive_time'] = time();
  488. }
  489. $user_coupon = ElectronCouponUserCoupon::getOne([
  490. ['mall_id' => $user_coupon_data['mall_id']],
  491. ['user_id' => $user_coupon_data['user_id']],
  492. ['e_coupon_id' => $user_coupon_data['e_coupon_id']],
  493. ['get_type' => $user_coupon_data['get_type']],
  494. ['is_proper' => $user_coupon_data['is_proper']],
  495. ['proper_id' => $user_coupon_data['proper_id']], // 同一专有者
  496. ['giver_id' => $user_coupon_data['giver_id']],
  497. ['goods_id' => $user_coupon_data['goods_id']], // 同一商品赠送
  498. ['use_begin_time' => $user_coupon_data['use_begin_time']], // 同一使用开始时间
  499. ['receive_end_time' => $user_coupon_data['receive_end_time']], // 同一使用结束时间
  500. ['status' => StatusEnum::ENABLED]
  501. ], true);
  502. if ($user_coupon) {
  503. $user_coupon_data['id'] = $user_coupon['id'];
  504. // 有限领取次数
  505. if ($coupon_info['unlimit_receive_times'] != ElectronCouponEnums::UNLIMIT_RECEIVE_TIMES &&
  506. $user_coupon['receive_times'] >= $coupon_info['receive_times']
  507. ) {
  508. throw new Exception('该' . $customize_name . '仅限领取' . $coupon_info['receive_times'] . '次, 您已超出次数');
  509. }
  510. // 更新数据的话,去掉默认值
  511. if ($user_coupon_data['last_receive_time'] == 0) {
  512. unset($user_coupon_data['last_receive_time']);
  513. }
  514. if ($user_coupon_data['last_active_time'] == 0) {
  515. unset($user_coupon_data['last_active_time']);
  516. }
  517. } else if ($params['get_type'] == ElectronCouponEnums::COUPON_GET_TYPE_GOODS) {
  518. $user_coupon_data['last_receive_time'] = time();
  519. }
  520. // 插入赠送的电子券数据
  521. $insert_user_coupon = self::setData($user_coupon_data);
  522. if ($insert_user_coupon == false) throw new Exception(self::getStaticError());
  523. // 电子券更新已领取数量
  524. $give_quantity= $coupon_info['give_quantity'];
  525. if(!empty($params['get_type'])&&$params['get_type']==ElectronCouponEnums::COUPON_GET_TYPE_SYSTEM){
  526. $give_quantity+= $receive_times;
  527. }
  528. $update_coupon_data = ElectronCoupon::setData([
  529. 'id' => $coupon_info['id'],
  530. 'mall_id' => $params['mall_id'],
  531. 'receive_amount' => $pieces_amount,
  532. 'give_quantity' => $give_quantity,
  533. ]);
  534. if ($update_coupon_data == false) {
  535. throw new Exception(ElectronCoupon::getStaticError());
  536. }
  537. $coupon_info['receive_amount'] += $pieces_amount;
  538. $coupon_info['receive_percent'] = $coupon_info['amount'] > 0 ? bcdiv($coupon_info['receive_amount'] * 100, $coupon_info['amount'], 2) : 0;
  539. $tran->commit();
  540. if ($params['get_type'] == ElectronCouponEnums::COUPON_GET_TYPE_RECEIVE) {
  541. return compact('coupon_info');
  542. }
  543. } catch (Exception $e) {
  544. $tran->rollBack();
  545. self::setStaticError($e->getMessage());
  546. return false;
  547. }
  548. }
  549. }