StoreCouponUserRelate.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. namespace addons\Store\common\models;
  3. use addons\Coupon\common\enums\AddonsCouponEnum;
  4. use addons\Coupon\common\models\AddonsCoupon;
  5. use addons\Coupon\common\models\AddonsCouponMemberRelate;
  6. use addons\Coupon\common\models\AddonsCouponRemind;
  7. use addons\Coupon\common\models\AddonsCouponStatistics;
  8. use addons\Store\common\enums\StoreEnum;
  9. use common\enums\AddonsEnum;
  10. use common\enums\RedisKeyEnum;
  11. use common\enums\StatusEnum;
  12. use common\helpers\LockHelper;
  13. use common\models\base\BaseActiveRecord;
  14. use common\models\mall\MallAddons;
  15. use common\models\user\User;
  16. use Yii;
  17. use yii\db\Exception;
  18. /**
  19. * This is the model class for table "qimall_addons_store_coupon_user_relate".
  20. *
  21. * @property int $id
  22. * @property int $mall_id
  23. * @property string $store_id 门店id
  24. * @property int $from_type 类型:1:平台优惠券;2:门店优惠券
  25. * @property int $user_id 用户
  26. * @property int $coupon_id 优惠卷
  27. * @property int $coupon_status 优惠券状态,2:领取中,3:已过期,4:已失效(后台操作失效、转赠被领取后、使用之后)
  28. * @property int $start_time 优惠券开始生效时间
  29. * @property int $end_time 优惠券过期时间
  30. * @property int $is_use 是否已使用:0=未使用,1=已使用
  31. * @property int $is_giving 是否已赠送:0-否;1-是;
  32. * @property int $giving_status 转赠状态,0:未转赠,1:转赠中,2:已被领取(转赠完成)
  33. * @property string $receive_type 获取方式,如"商品详情页领取"
  34. * @property string $receive_platform 领取来源,从哪个平台领取的,wechat:微信,mp-wx:微信小程序,h5,app,work-wx:企业微信
  35. * @property int $is_force_failure 是否强制失效,后台操作用户已领取的优惠券失效
  36. * @property int $is_send_reward 是否已经发放优惠券奖励,1:是,0:否
  37. * @property int $status 状态[-1:删除;0:禁用;1启用]
  38. * @property int $created_at 创建时间
  39. * @property int $updated_at
  40. */
  41. class StoreCouponUserRelate extends BaseActiveRecord
  42. {
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public static function tableName()
  47. {
  48. return '{{%addons_store_coupon_user_relate}}';
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function rules()
  54. {
  55. return [
  56. [['mall_id', 'from_type', 'user_id', 'coupon_id'], 'required'],
  57. [['mall_id', 'user_id', 'coupon_id', 'coupon_status', 'start_time', 'end_time', 'is_use', 'is_giving', 'giving_status', 'is_force_failure', 'is_send_reward', 'status', 'created_at', 'updated_at'], 'integer'],
  58. [['receive_type'], 'string', 'max' => 255],
  59. [['receive_platform'], 'string', 'max' => 25],
  60. [['store_id'], 'string'],
  61. ];
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function attributeLabels()
  67. {
  68. return [
  69. 'id' => 'ID',
  70. 'mall_id' => 'Mall ID',
  71. 'store_id' => '门店id',
  72. 'from_type' => '类型:1:平台优惠券;2:门店优惠券',
  73. 'user_id' => '用户',
  74. 'coupon_id' => '优惠卷',
  75. 'coupon_status' => '优惠券状态,2:领取中,3:已过期,4:已失效(后台操作失效、转赠被领取后、使用之后)',
  76. 'start_time' => '优惠券开始生效时间',
  77. 'end_time' => '优惠券过期时间',
  78. 'is_use' => '是否已使用:0=未使用,1=已使用',
  79. 'is_giving' => '是否已赠送:0-否;1-是;',
  80. 'giving_status' => '转赠状态,0:未转赠,1:转赠中,2:已被领取(转赠完成)',
  81. 'receive_type' => '获取方式,如\"商品详情页领取\"',
  82. 'receive_platform' => '领取来源,从哪个平台领取的,wechat:微信,mp-wx:微信小程序,h5,app,work-wx:企业微信',
  83. 'is_force_failure' => '是否强制失效,后台操作用户已领取的优惠券失效',
  84. 'is_send_reward' => '是否已经发放优惠券奖励,1:是,0:否',
  85. 'status' => '状态[-1:删除;0:禁用;1启用]',
  86. 'created_at' => '创建时间',
  87. 'updated_at' => 'Updated At',
  88. ];
  89. }
  90. public static function setData($params)
  91. {
  92. $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_COUPON . ':' . $params['from_type'] . ':', $params['coupon_id']);
  93. $identification = uniqid();
  94. $t = Yii::$app->db->beginTransaction();
  95. try {
  96. $time = time();
  97. if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new Exception('当前访问人数过多');
  98. switch ($params['from_type']) {
  99. case 1:
  100. $where[] = ['id' => $params['coupon_id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]];
  101. $coupon_model = AddonsCoupon::getOne($where);
  102. if (empty($coupon_model['store_id'])) throw new Exception('该优惠券不是平台指的给门店领取');
  103. $relate_id = self::receive($coupon_model, $params, $time, $t);
  104. AddonsCouponStatistics::setData(['mall_id'=>$params['mall_id'], 'coupon_id'=>$params['coupon_id'],'received_number'=>1,]);
  105. break;
  106. case 2:
  107. $where[] = ['id' => $params['coupon_id'], 'mall_id' => $params['mall_id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]];
  108. $coupon_model = StoreCoupon::getOne($where);
  109. $relate_id = self::receive($coupon_model, $params, $time, $t);
  110. StoreCouponStatistics::setData(['mall_id'=>$params['mall_id'], 'coupon_id'=>$params['coupon_id'],
  111. 'store_id'=>$coupon_model['store_id'], 'received_number'=>1,]);
  112. break;
  113. default:
  114. throw new Exception('领取类型有误');
  115. break;
  116. }
  117. LockHelper::ulock($lock_key, $identification);
  118. $t->commit();
  119. return $relate_id;
  120. } catch (Exception $e) {
  121. LockHelper::ulock($lock_key, $identification);
  122. $t->rollBack();
  123. self::$static_error = $e->getMessage();
  124. return false;
  125. }
  126. }
  127. //领取优惠券
  128. public static function receive($coupon_model, $params, $time, $t)
  129. {
  130. if (empty($coupon_model)) throw new Exception('该优惠券不存在');
  131. if ($coupon_model["coupon_status"] == AddonsCouponEnum::STATUS_NOT_BEGIN) throw new Exception('该优惠券不能领取');
  132. if ($coupon_model["coupon_status"] == AddonsCouponEnum::STATUS_EXPIRED) throw new Exception('该优惠券已过期');
  133. if ($coupon_model["coupon_status"] == AddonsCouponEnum::STATUS_FAILURE) throw new Exception('该优惠券已失效');
  134. if ($coupon_model["total_count"] <= 0) {
  135. $coupon_model->status = StatusEnum::DISABLED;
  136. $coupon_model->save();
  137. $t->commit();
  138. throw new Exception('该优惠券被领完啦');
  139. }
  140. if ($params['num'] > $coupon_model['total_count']) throw new Exception('该优惠券数量不足');
  141. if ($coupon_model['is_receive_limit']) {
  142. $counts = self::find()->where(['mall_id' => $params['mall_id'], 'from_type' => $params['from_type'], 'user_id' => $params['user_id'], 'coupon_id' => $params['coupon_id']])->count();
  143. if (($counts + $params['num']) > $coupon_model['receive_count']) throw new Exception('每人只可以领取' . $coupon_model['receive_count'] . '张');
  144. }
  145. if ($coupon_model['is_member']) {
  146. if($coupon_model instanceof AddonsCoupon){
  147. $member_relate_list = AddonsCouponMemberRelate::lists(['where' => [['mall_id' => $params['mall_id']], ['coupon_id' => $coupon_model['id']]], 'select' => 'member_level']);
  148. }else{
  149. $member_relate_list = StoreCouponMemberRelate::lists(['where' => [['mall_id' => $params['mall_id']], ['coupon_id' => $coupon_model['id']]], 'select' => 'member_level']);
  150. }
  151. $level_arr = array_column($member_relate_list, 'member_level');
  152. if (!in_array($params['user']['level'], $level_arr)) throw new Exception('该优惠券指定会员等级');
  153. }
  154. switch ($coupon_model['expire_type']) {
  155. case AddonsCouponEnum::EXPIRE_TYPE2:
  156. $params['start_time'] = $coupon_model['begin_at'];
  157. $params['end_time'] = $coupon_model['end_at'];
  158. break;
  159. case AddonsCouponEnum::EXPIRE_TYPE1:
  160. $params['start_time'] = $time;
  161. $params['end_time'] = $coupon_model['expire_day'] * 86400 + $time;
  162. break;
  163. case AddonsCouponEnum::EXPIRE_TYPE3:
  164. $params['start_time'] = $coupon_model['effect_days'] * 86400 + $time;
  165. $params['end_time'] = $coupon_model['expire_day'] * 86400 + $params['start_time'];
  166. break;
  167. default:
  168. throw new Exception('有效期时间设置不对');
  169. }
  170. $insert = [];
  171. $remind = [];
  172. for ($i = 0; $i < $params['num']; $i++) {
  173. $insert[] = [$params['mall_id'], $coupon_model['store_id'], $params['from_type'], $params['user_id'], $params['coupon_id'], $coupon_model['coupon_status'], 1, $params['start_time'], $params['end_time'], 0, 0, 0, StoreEnum::getSendFromMap($params['receive_type'], $params['mall_id']), $params['receive_platform'], 0, 0, $time, $time];
  174. $remind[] = [$params['mall_id'], $params['user_id'], $params['coupon_id'], $params['end_time'], 1, 1, $time, $time];
  175. }
  176. $field = ['mall_id', 'store_id', 'from_type', 'user_id', 'coupon_id', 'coupon_status', 'status', 'start_time', 'end_time', 'is_use', 'is_giving', 'giving_status', 'receive_type', 'receive_platform', 'is_force_failure', 'is_send_reward', 'created_at', 'updated_at'];
  177. // 批量插入数据
  178. $res = Yii::$app->db->createCommand()->batchInsert(self::tableName(), $field, $insert)->execute();
  179. $newId = Yii::$app->db->getLastInsertID();
  180. if ($res == false) throw new Exception('领取失败');
  181. $total_count = $coupon_model->total_count - $params['num'];
  182. $coupon_model->total_count = $total_count >= 0 ? $total_count : 0;
  183. $res = $coupon_model->save();
  184. if ($res == false) throw new Exception('领取失败 ' . $coupon_model->getErrotMessage());
  185. if (1 == $coupon_model->is_remind && !empty($remind)) {
  186. $remind_fields = ['mall_id', 'user_id', 'coupon_id', 'expire_time', 'status', 'send_status', 'created_at', 'updated_at'];
  187. $remind_table_name = (new StoreCouponRemind())->tableSchema->name;
  188. Yii::$app->db->createCommand()->batchInsert($remind_table_name, $remind_fields, $remind)->execute();
  189. }
  190. return $newId;
  191. }
  192. /**
  193. * @desc:领取别人赠与
  194. * @Author: hua
  195. * @Date: 2022/7/16
  196. * @Time: 10:22
  197. * @Copyright: copyright (c) 2021 广东七件事集团
  198. * @param $params
  199. * @return StoreCouponUserRelate|false
  200. */
  201. public static function receiveByGive($params)
  202. {
  203. $t = Yii::$app->db->beginTransaction();
  204. try {
  205. $share_log_model = StoreCouponShareLog::findOne(['id' => $params['coupon_share_log_id']]);
  206. if (empty($share_log_model)) throw new Exception('未找到转赠记录');
  207. $params['user_coupon_id'] = $share_log_model->user_coupon_id;
  208. $where[] = ['id' => $params['user_coupon_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED];
  209. $coupon_user_relate_model = StoreCouponUserRelate::getOne($where);
  210. if (empty($coupon_user_relate_model)) throw new Exception('该优惠券不存在');
  211. if ($coupon_user_relate_model['user_id'] == $params['user_id']) throw new Exception('不能领取自己分享的优惠券');
  212. if ($coupon_user_relate_model["coupon_status"] == StoreEnum::STATUS_EXPIRED) throw new Exception('该优惠券已过期');
  213. if ($coupon_user_relate_model["coupon_status"] == StoreEnum::STATUS_FAILURE) throw new Exception('该优惠券已失效');
  214. if ($coupon_user_relate_model["is_use"] == 1) throw new Exception('该优惠券已被赠送者使用');
  215. if ($coupon_user_relate_model["is_giving"] == 0) throw new Exception('该优惠券没有被赠送者送出');
  216. if ($coupon_user_relate_model["giving_status"] == StoreEnum::GIVING_STATUS2) throw new Exception('该优惠券已被领取');
  217. if($coupon_user_relate_model['from_type']==1){
  218. $coupon_class = AddonsCoupon::class;
  219. $coupon_member_relate_class = AddonsCouponMemberRelate::class;
  220. }else{
  221. $coupon_class = StoreCoupon::class;
  222. $coupon_member_relate_class = StoreCouponMemberRelate::class;
  223. }
  224. $coupon_model = $coupon_class::findOne(['id' => $coupon_user_relate_model['coupon_id']]);
  225. if ($coupon_model['is_member']) {
  226. $member_relate_list = $coupon_member_relate_class::lists(['where' => [['mall_id' => $params['mall_id']], ['coupon_id' => $coupon_model['id']]], 'select' => 'member_level']);
  227. $level_arr = array_column($member_relate_list, 'member_level');
  228. if (!in_array($params['user']['level'], $level_arr)) throw new Exception('该优惠券指定会员等级');
  229. }
  230. if ($coupon_model['is_receive_limit'] ) {
  231. $received_counts = StoreCouponUserRelate::find()
  232. ->where(['mall_id' => $params['mall_id'], 'user_id' => $params['user_id'], 'status' => StatusEnum::ENABLED, 'coupon_id' => $coupon_model['id']])
  233. ->andWhere(['or', ['is_giving' => 0], ['is_giving' => 1, 'giving_status' => [StoreEnum::GIVING_STATUS0, StoreEnum::GIVING_STATUS1]]])
  234. ->count();
  235. if ($received_counts >= $coupon_model['receive_count']) throw new Exception('你已经领取过了');
  236. }
  237. $now = time();
  238. $coupon_user_model = new self();
  239. $coupon_user_model->mall_id = $params['mall_id'];
  240. $coupon_user_model->user_id = $params['user_id'];
  241. $coupon_user_model->store_id = $coupon_user_relate_model['store_id'];
  242. $coupon_user_model->coupon_id = $coupon_user_relate_model['coupon_id'];
  243. $coupon_user_model->from_type = $coupon_user_relate_model['from_type'];
  244. $coupon_user_model->coupon_status = $coupon_user_relate_model['coupon_status'];
  245. $coupon_user_model->status = 1;
  246. $coupon_user_model->start_time = $coupon_user_relate_model['start_time'];
  247. $coupon_user_model->end_time = $coupon_user_relate_model['end_time'];
  248. $coupon_user_model->is_use = 0;
  249. $coupon_user_model->is_giving = 0;
  250. $coupon_user_model->giving_status = 0;
  251. $coupon_user_model->receive_type = StoreEnum::getSendFromMap(StoreEnum::SEND_FROM6);
  252. $coupon_user_model->receive_platform = $params['receive_platform']??'o2o';
  253. $coupon_user_model->is_force_failure = 0;
  254. $coupon_user_model->is_send_reward = 0;
  255. $coupon_user_model->created_at = $now;
  256. $coupon_user_model->updated_at = $now;
  257. $res = $coupon_user_model->save();
  258. if ($res == false) throw new Exception('领取失败');
  259. $share_data = [
  260. 'mall_id' => $params['mall_id'],
  261. 'coupon_share_log_id' => $params['coupon_share_log_id'],
  262. 'get_user_coupon_id' => $coupon_user_model->id,
  263. 'receive_user_id' => $params['user_id'],
  264. ];
  265. $res = StoreCouponShareLog::setData($share_data);
  266. if ($res == false) throw new Exception(StoreCouponShareLog::getStaticError());
  267. $coupon_user_relate_model->giving_status = StoreEnum::GIVING_STATUS2;
  268. $coupon_user_relate_model->coupon_status = StoreEnum::STATUS_FAILURE;
  269. $res = $coupon_user_relate_model->save();
  270. if ($res == false) throw new Exception('更新赠送者优惠券状态失败');
  271. if ($coupon_model['is_remind']) {
  272. $remind_datas[] = [$params['mall_id'], $params['coupon_id'], $params['user_id'], $coupon_user_relate_model['end_time'], 1, 1, $now, $now];
  273. $remind_fields = ['mall_id', 'coupon_id', 'user_id', 'expire_time', 'status', 'send_status', 'created_at', 'updated_at'];
  274. $remind_table_name = (new StoreCouponRemind())->tableSchema->name;
  275. Yii::$app->db->createCommand()->batchInsert($remind_table_name, $remind_fields, $remind_datas)->execute();
  276. }
  277. $t->commit();
  278. return $coupon_user_model;
  279. } catch (Exception $e) {
  280. $t->rollBack();
  281. self::$static_error = $e->getMessage();
  282. return false;
  283. }
  284. }
  285. /**
  286. * @desc:o2o订单完成后赠送优惠券
  287. * @Author: hua
  288. * @Date: 2022/7/15
  289. * @Time: 17:50
  290. * @Copyright: copyright (c) 2021 广东七件事集团
  291. * @param $mall_id
  292. * @param $store_id
  293. * @param $from_type
  294. * @param $user_id
  295. * @param $give_coupon_arr //$give_coupon_arr=['coupon_id'=>'num'];
  296. * @param int $receive_type
  297. * @param string $receive_platform
  298. * @return array|false
  299. */
  300. public static function giveCouponByOrder($mall_id, $store_id, $user_id, $give_coupon_arr, $from_type, $receive_type = 2, $receive_platform = 'platform')
  301. {
  302. $trans = Yii::$app->db->beginTransaction();
  303. $locks = [];
  304. try {
  305. if ($from_type == 1) {
  306. $class = AddonsCoupon::class;
  307. $is_install = MallAddons::isInstall($mall_id, AddonsEnum::ADDONS_NAME_COUPON);
  308. if (!$is_install) return true;
  309. } else {
  310. $class = StoreCoupon::class;
  311. }
  312. $coupon_id_arr = array_keys($give_coupon_arr);
  313. $coupon_list = $class::findAll(['mall_id' => $mall_id, 'id' => $coupon_id_arr, 'status' => StatusEnum::ENABLED, 'coupon_status' => StoreEnum::STATUS_RECEIVING]);
  314. if (empty($coupon_list)) throw new Exception('暂无可用优惠券');
  315. $user = User::findOne(['id' => $user_id]);
  316. $can_give_coupon_arr = [];
  317. $time = time();
  318. foreach ($coupon_list as $coupon) {
  319. $lock_key = RedisKeyEnum::suffix(RedisKeyEnum::LOCK_COUPON, ':o2o:' . $coupon['id']);
  320. $identification = uniqid();
  321. if (!LockHelper::lock($lock_key, $identification, 30, 60)) throw new Exception('当前访问人数过多');
  322. $locks[$lock_key] = $identification;
  323. if ($coupon['expire_type'] == 2 && $coupon['end_at'] <= $time) continue;
  324. if ($coupon['total_count'] <= 0) continue;
  325. $member_level_arr = array_column($coupon['memberRelate'], 'level');
  326. if (!empty($member_level_arr) && !in_array($user['level'], $member_level_arr)) continue;
  327. $time = time();
  328. switch ($coupon['expire_type']) {
  329. case StoreEnum::EXPIRE_TYPE2:
  330. $start_time = $time;
  331. $end_time = $coupon['end_at'];
  332. break;
  333. case StoreEnum::EXPIRE_TYPE1:
  334. $start_time = $time;
  335. $end_time = $coupon['expire_day'] * 86400 + $time;
  336. break;
  337. case StoreEnum::EXPIRE_TYPE3:
  338. $start_time = $coupon['effect_days'] * 86400 + $time;
  339. $end_time = $coupon['expire_day'] * 86400 + $start_time;
  340. break;
  341. default:
  342. throw new Exception('有效期时间设置不对');
  343. }
  344. $can_give_coupon_arr[$coupon['id']] = [
  345. 'num' => $give_coupon_arr[$coupon['id']],
  346. 'start_time' => $start_time,
  347. 'end_time' => $end_time,
  348. 'total_count' => $coupon['total_count'],
  349. 'name' => $coupon['name'],
  350. 'coupon_status' => $coupon['coupon_status'],
  351. 'store_id'=>$coupon['store_id'],
  352. ];
  353. }
  354. $field = ['mall_id', 'store_id', 'from_type', 'user_id', 'coupon_id', 'coupon_status', 'start_time', 'end_time', 'is_use', 'is_giving', 'giving_status', 'receive_type', 'receive_platform', 'is_force_failure', 'is_send_reward', 'status', 'created_at', 'updated_at'];
  355. $insert = [];
  356. foreach ($can_give_coupon_arr as $coupon_id => $item) {
  357. $num = $item['num'];
  358. if ($item['total_count'] < $item['num']) $num = $item['total_count'];
  359. for ($i = 0; $i < $num; $i++) {
  360. $insert[] = [$mall_id, $store_id, $from_type, $user_id, $coupon_id, $item['coupon_status'], $item['start_time'], $item['end_time'], 0, 0, 0, StoreEnum::getSendFromMap($receive_type), $receive_platform, 0, 0, StatusEnum::ENABLED, $time, $time,];
  361. }
  362. $class = $from_type == 1 ? AddonsCoupon::class : StoreCoupon::class;
  363. $coupon_model = $class::findOne(['id' => $coupon_id]);
  364. $coupon_model->total_count -= $num;
  365. $res = $coupon_model->save();
  366. if ($res == false) throw new \Exception('赠送优惠券失败');
  367. if ($from_type == 1) {
  368. AddonsCouponStatistics::setData(['mall_id' => $mall_id, 'coupon_id' => $coupon_id, 'received_number' => $num,]);
  369. }else{
  370. StoreCouponStatistics::setData(['mall_id'=>$mall_id, 'coupon_id'=>$coupon_id, 'store_id'=>$item['store_id'], 'received_number'=>$num,]);
  371. }
  372. }
  373. if (!empty($insert)) {
  374. $res = Yii::$app->db->createCommand()->batchInsert(StoreCouponUserRelate::tableName(), $field, $insert)->execute();
  375. if ($res == false) {
  376. throw new \Exception('赠送优惠券失败');
  377. }
  378. }
  379. $trans->commit();
  380. LockHelper::batchUlock($locks);
  381. return $can_give_coupon_arr;
  382. } catch (\Exception $e) {
  383. $trans->rollBack();
  384. LockHelper::batchUlock($locks);
  385. self::setStaticError($e->getMessage());
  386. return false;
  387. }
  388. }
  389. /**
  390. * @notes:优惠券状态处理
  391. * @param $params
  392. * @return bool
  393. * @throws \yii\db\Exception
  394. * @author: lun
  395. * @Time: 2022/10/27 9:45
  396. */
  397. public static function handle($params)
  398. {
  399. $trans = Yii::$app->db->beginTransaction();
  400. try {
  401. $where = [['mall_id' => $params['mall_id']], ['id' => $params['id']], ['status' => StatusEnum::ENABLED]];
  402. $where[] = ['or like', 'store_id', [$params['store_id'].'%', '%'.$params['store_id'].'%', '%'.$params['store_id']], false];
  403. $model = self::getOne($where);
  404. if (empty($model)) throw new Exception('查无此数据');
  405. if ($params['status'] == StatusEnum::DELETE) {
  406. $model->status = $params['status'];
  407. $model->is_force_failure = StatusEnum::ENABLED;
  408. }
  409. if (!$model->save()) throw new \yii\db\Exception($model->getErrorMessage());
  410. $trans->commit();
  411. return true;
  412. } catch (Exception $e) {
  413. $trans->rollBack();
  414. return false;
  415. }
  416. }
  417. }