ElectronCouponGoods.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace addons\ElectronCoupon\common\models;
  3. use addons\ElectronCoupon\common\enums\ElectronCouponEnums;
  4. use common\enums\StatusEnum;
  5. use common\models\base\User;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "{{%addons_electron_coupon_goods}}".
  10. *
  11. * @property int $id 主键id
  12. * @property int $created_at 创建时间
  13. * @property int $updated_at 更新时间
  14. * @property int $goods_id 商品id
  15. * @property string|null $e_coupon_id 赠送的电子券ID;
  16. * @property int $is_open 是否开启
  17. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  18. * @property int $mall_id 商城id
  19. * @property int|null $num 发行数量
  20. */
  21. class ElectronCouponGoods extends \common\models\base\BaseActiveRecord
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function tableName()
  27. {
  28. return '{{%addons_electron_coupon_goods}}';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function rules()
  34. {
  35. return [
  36. [['created_at', 'updated_at', 'goods_id', 'is_open', 'status', 'mall_id', 'num','e_coupon_id', 'send_type'], 'integer'],
  37. [['goods_id', 'mall_id'], 'required'],
  38. ];
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'id' => '主键id',
  47. 'created_at' => '创建时间',
  48. 'updated_at' => '更新时间',
  49. 'goods_id' => '商品id',
  50. 'e_coupon_id' => '赠送的电子券ID;',
  51. 'is_open' => '是否开启',
  52. 'status' => '状态[-1:删除;0:禁用;1启用]',
  53. 'mall_id' => '商城id',
  54. 'num' => '发行数量',
  55. 'send_type' => '赠送类型',
  56. ];
  57. }
  58. /**
  59. * 保存数据
  60. * @Author: ltm
  61. * @DateTime: 2022/6/27 0022 17:13
  62. * @Copyright: copyright (c) 2021 广东七件事集团
  63. * @param array $params
  64. * @return string
  65. */
  66. public static function setGoodsData($mall_id,$goods_id,$params){
  67. try{
  68. $model = self::findOne(['goods_id'=>$goods_id,'e_coupon_id'=> $params['e_coupon_id'],'mall_id'=>$mall_id]);
  69. if (empty($model)) {
  70. $model = new self();
  71. $model->loadDefaultValues();
  72. }
  73. $model->mall_id = $mall_id;
  74. $model->goods_id = $goods_id;
  75. //$model->cate_id = $params['cate_id'];
  76. if(!$model->load($params,'') || !$model->save()){
  77. throw new Exception($model->getErrorMessage());
  78. }
  79. return $model;
  80. }catch(Exception $e){
  81. //var_dump($e->getMessage());
  82. self::$static_error = $e->getMessage();
  83. return false;
  84. }
  85. }
  86. /**
  87. * 设置商品无效
  88. * @Author: ltm
  89. * @DateTime: 2022/6/27 0022 17:13
  90. * @Copyright: copyright (c) 2021 广东七件事集团
  91. * @param array $params
  92. * @return string
  93. */
  94. public static function setCanceGoods($params){
  95. $model = self::updateAll(['status' => StatusEnum::DISABLED],['mall_id'=>$params['mall_id'],'goods_id'=>$params['goods_id']]);
  96. return $model;
  97. }
  98. /**
  99. * 关联用户信息
  100. * @Author:ltm
  101. * @DateTime: 2022/6/28 0022 17:13
  102. * @Copyright: copyright (c) 2021 广东七件事集团
  103. * @return \yii\db\ActiveQuery
  104. */
  105. public function getUser()
  106. {
  107. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,username,mobile,avatar_url');
  108. }
  109. /**
  110. * 关联活动信息
  111. * @Author:ltm
  112. * @DateTime: 2022/6/28 0022 17:13
  113. * @Copyright: copyright (c) 2021 广东七件事集团
  114. * @return \yii\db\ActiveQuery
  115. */
  116. public function getCoupon()
  117. {
  118. return $this->hasOne(ElectronCoupon::class, ['id' => 'e_coupon_id']);
  119. }
  120. /**
  121. * 购买商品赠送电子券
  122. * @param $mall_id
  123. * @param $user_id
  124. * @param $order_detail
  125. */
  126. public static function sendUserCoupon($mall_id, $user_id, $order_detail)
  127. {
  128. foreach ($order_detail as $k => $v){
  129. $coupon_goods = self::find()
  130. ->where(['goods_id' => $v['goods_id']])
  131. ->andWhere(['mall_id' => $mall_id])
  132. ->andWhere(['status' => StatusEnum::ENABLED])
  133. ->andWhere(['is_open' => ElectronCouponEnums::OPEN_GOODS])
  134. ->asArray()
  135. ->all();
  136. if (!$coupon_goods) {
  137. continue;
  138. }
  139. foreach ($coupon_goods as $item) {
  140. $receive_data = [
  141. 'e_coupon_id' => $item['e_coupon_id'],
  142. 'mall_id' => $mall_id,
  143. 'user_id' => $user_id,
  144. 'get_type' => ElectronCouponEnums::COUPON_GET_TYPE_GOODS,
  145. 'goods_id' => $v['goods_id'],
  146. 'pieces_amount' => (int) ($v['num'] * $item['num'])
  147. ];
  148. ElectronCouponUserCoupon::userReceiveCoupon($receive_data);
  149. }
  150. }
  151. }
  152. /**
  153. * 通过订单详情赠送电子券
  154. *
  155. * @param integer $mall_id
  156. * @param integer $user_id
  157. * @param array $detail
  158. * @return void
  159. */
  160. public static function sendUserCouponByDetail(int $mall_id, int $user_id, array $detail)
  161. {
  162. $coupon_goods = self::find()
  163. ->where(['goods_id' => $detail['goods_id']])
  164. ->andWhere(['mall_id' => $mall_id])
  165. ->andWhere(['status' => StatusEnum::ENABLED])
  166. ->andWhere(['is_open' => ElectronCouponEnums::OPEN_GOODS])
  167. ->asArray()
  168. ->all();
  169. if (empty($coupon_goods)) return;
  170. foreach ($coupon_goods as $item) {
  171. $receive_data = [
  172. 'e_coupon_id' => $item['e_coupon_id'],
  173. 'mall_id' => $mall_id,
  174. 'user_id' => $user_id,
  175. 'get_type' => ElectronCouponEnums::COUPON_GET_TYPE_GOODS,
  176. 'goods_id' => $detail['goods_id'],
  177. 'pieces_amount' => (int) ($detail['num'] * $item['num'])
  178. ];
  179. ElectronCouponUserCoupon::userReceiveCoupon($receive_data);
  180. }
  181. }
  182. /**
  183. * 获取商品绑定电子券赠送类型
  184. *
  185. * @param integer $mall_id
  186. * @param integer $goods_id
  187. * @return string|int|null|false the value of the first column in the first row of the query result.
  188. */
  189. public static function getGoodsSendType(int $mall_id, int $goods_id)
  190. {
  191. return static::find()
  192. ->where([
  193. 'is_open' => ElectronCouponEnums::OPEN_GOODS,
  194. 'goods_id' => $goods_id,
  195. 'mall_id' => $mall_id,
  196. 'status' => StatusEnum::ENABLED
  197. ])
  198. ->select(['send_type'])
  199. ->scalar();
  200. }
  201. }