RebateGoods.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /*
  3. * @Descripttion:
  4. * @copyright: Copyright (c) 2021 广东七件事集团
  5. * @Author: lun
  6. * @Date: 2021-12-28 21:23:40
  7. */
  8. namespace addons\Rebate\common\models;
  9. use common\enums\StatusEnum;
  10. use common\models\goods\Goods;
  11. use common\models\order\Order;
  12. use common\models\statistics\UserStatisticsTotal;
  13. use common\models\user\User;
  14. use phpseclib3\Math\BigInteger;
  15. use Yii;
  16. use yii\db\Exception;
  17. use yii\helpers\Json;
  18. /**
  19. * This is the model class for table "{{%addons_rebate_goods}}".
  20. *
  21. * @property int $id
  22. * @property int $mall_idis_enable
  23. * @property int $is_enable
  24. * @property int $initiate_type 发起人权限类型[0:全部;1:新会员;2:等级会员]
  25. * @property string $initiate_level 等级会员json串[initiate_type=2时使用]
  26. * @property int $consume_type 参与人权限类型[0:全部;1:新会员;2:等级会员]
  27. * @property string $consume_level 等级会员json串[consume_type=2时使用]
  28. * @property int $item_id 活动商品id
  29. * @property string $item_type
  30. * @property int $reward_type 奖励类型[1:奖励条件;2:阶梯奖励]
  31. * @property string $reward_info 奖励详情
  32. * @property int $is_repeat 阶梯奖励是否重复奖励[0=否,1=是]
  33. * @property int $status 状态[-1:删除;0:禁用;1启用]
  34. * @property int $created_at 创建时间
  35. * @property int $updated_at 更新时间
  36. * @property int $join_type 参与类型
  37. */
  38. class RebateGoods extends \common\models\base\BaseActiveRecord
  39. {
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function tableName()
  44. {
  45. return '{{%addons_rebate_goods}}';
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function rules()
  51. {
  52. return [
  53. [['mall_id'], 'required'],
  54. [['mall_id','is_enable', 'initiate_type', 'consume_type', 'item_id', 'reward_type', 'is_repeat', 'status', 'created_at', 'updated_at','join_type','repeat_setting','agent_limit_num'], 'integer'],
  55. [['initiate_level','item_type','consume_level','reward_info'], 'string'],
  56. ];
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function attributeLabels()
  62. {
  63. return [
  64. 'id' => 'ID',
  65. 'mall_id' => '商城id',
  66. 'is_enable' => '是否开启[0=否,1=是]',
  67. 'initiate_type' => '发起人权限类型[0:全部;1:新会员;2:等级会员]',
  68. 'initiate_level' => '等级会员json串[initiate_type=2时使用]',
  69. 'consume_type' => '参与人权限类型[0:全部;1:新会员;2:等级会员]',
  70. 'consume_level' => '等级会员json串[consume_type=2时使用]',
  71. 'item_id' => '活动商品id',
  72. 'item_type'=>'项目类型:商品goods',
  73. 'reward_type' => '奖励类型[1:奖励条件;2:阶梯奖励]',
  74. 'reward_info' => '奖励具体信息',
  75. 'is_repeat' => '阶梯奖励是否重复奖励[0=否,1=是]',
  76. 'status' => '状态[-1:删除;0:禁用;1启用]',
  77. 'created_at' => '创建时间',
  78. 'updated_at' => '更新时间',
  79. 'join_type' => '参与类型',
  80. ];
  81. }
  82. /**
  83. * 关联统计表
  84. * @Author: lun
  85. * @DateTime: 2022/1/18 0018 15:17
  86. * @Copyright: copyright (c) 2021 广东七件事集团
  87. * @return \yii\db\ActiveQuery
  88. */
  89. public function getStatistics()
  90. {
  91. return $this->hasOne(RebateStatistics::class, ['r_id' => 'id'])->where(['status' => 1]);
  92. }
  93. /**
  94. * 关联商品信息
  95. * @Author: lun
  96. * @DateTime: 2022/1/18 0018 16:56
  97. * @Copyright: copyright (c) 2021 广东七件事集团
  98. * @return \yii\db\ActiveQuery
  99. */
  100. public function getGoods()
  101. {
  102. return $this->hasOne(Goods::class, ['id' => 'item_id']);
  103. }
  104. /**
  105. * goods表字段过滤
  106. * @param $item
  107. * @return array
  108. */
  109. public static function filterGoods($goods_id)
  110. {
  111. if (!$goods_id) {
  112. return [];
  113. }
  114. $goods = Goods::findOne(['id' =>$goods_id]);
  115. if (!$goods) {
  116. return [];
  117. }
  118. return [
  119. 'id' => $goods->id,
  120. 'name' => $goods->name,
  121. 'cover_pic' => $goods->coverPic,
  122. 'price' => $goods->price,
  123. ];
  124. }
  125. /**
  126. * 保存数据
  127. * @Author: lun
  128. * @DateTime: 2022/1/15 0015 9:26
  129. * @Copyright: copyright (c) 2021 广东七件事集团
  130. * @param $mall_id
  131. * @param array $params
  132. * @return Rebate|bool
  133. */
  134. public static function setData($mallArr, array $params){
  135. try {
  136. $params = $params['commission'];
  137. $model = self::findOne(['item_id' => $mallArr['item_id'], 'mall_id' => $mallArr['mall_id'], 'status' => [StatusEnum::ENABLED]]);
  138. if (empty($model)) {
  139. $model = new self();
  140. $model->mall_id = $mallArr['mall_id'];
  141. $model->item_id = $mallArr['item_id'];
  142. $model->loadDefaultValues();
  143. }
  144. // 将内容转为json字符串
  145. $params['initiate_level'] = !empty($params['initiate_level']) ? Json::encode($params['initiate_level']) : '';
  146. $params['consume_level'] = !empty($params['consume_level']) ? Json::encode($params['consume_level']) : '';
  147. $params['reward_info'] = !empty($params['reward_info']) ? Json::encode($params['reward_info']) : '';
  148. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  149. return $model;
  150. } catch (\Exception $e) {
  151. self::setStaticError($e->getMessage());
  152. return false;
  153. }
  154. }
  155. /**
  156. * 根据商品id获取活动内容
  157. * @Author: lun
  158. * @DateTime: 2022/1/15 0015 9:26
  159. * @Copyright: copyright (c) 2021 广东七件事集团
  160. * @param $mall_id
  161. * @param $goods_id
  162. * @return array|bool|\yii\db\ActiveRecord
  163. */
  164. public static function getRebateByGoodsId($mall_id, $goods_id)
  165. {
  166. $select = "id,initiate_type,initiate_level,consume_type,consume_level,reward_type,reward_info,is_repeat";
  167. $data = self::find()->select($select)->where(['mall_id' => $mall_id, 'item_id' => $goods_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
  168. if(empty($data)) return false;
  169. return $data;
  170. }
  171. /**
  172. * 根据商品id获取活动内容
  173. * @Author: lun
  174. * @DateTime: 2022/1/15 0015 9:26
  175. * @Copyright: copyright (c) 2021 广东七件事集团
  176. * @param $mall_id
  177. * @param $goods_id
  178. * @return array|bool|\yii\db\ActiveRecord
  179. */
  180. public static function getRebateAllGoodsId($mall_id)
  181. {
  182. $time = time();
  183. $select = "id,item_id,initiate_type,initiate_level,consume_type,consume_level,reward_type,reward_info,is_repeat,join_type,repeat_setting,agent_limit_num";
  184. $data = self::find()->select($select)
  185. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
  186. ->asArray()->all();
  187. return $data;
  188. }
  189. /**
  190. * 判断该商品是否已参与其他活动
  191. * @Author: lun
  192. * @DateTime: 2022/1/15 0015 9:24
  193. * @Copyright: copyright (c) 2021 广东七件事集团
  194. * @param $mall_id
  195. * @param $goods_id
  196. * @param int $r_id
  197. * @return bool
  198. */
  199. public static function checkGoods($mall_id, $goods_id, $r_id = 0)
  200. {
  201. $checkGoods = self::find()->select("id")->where(['mall_id' => $mall_id, 'item_id' => $goods_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
  202. if ($checkGoods) {
  203. if(empty($r_id) || (!empty($r_id) && $checkGoods['id'] != $r_id)) {
  204. return false;
  205. }
  206. }
  207. return true;
  208. }
  209. /**
  210. * 判断用户是否支付过
  211. * @Author: lun
  212. * @DateTime: 2022/1/15 0015 9:24
  213. * @Copyright: copyright (c) 2021 广东七件事集团
  214. * @param $user_id
  215. * @return mixed
  216. */
  217. public static function checkUserHasOrder($mall_id, $user_id)
  218. {
  219. // $num = UserStatisticsTotal::find()->select("pay_num")->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->scalar();
  220. $num = Order::find()->select("id")->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'pay_status' => StatusEnum::ENABLED,'status' => StatusEnum::ENABLED])->asArray()->count();
  221. if (empty($num)) return false;
  222. return $num;
  223. }
  224. /**
  225. * 判断用户等级是否符合条件
  226. * @Author: lun
  227. * @DateTime: 2022/1/6 0006 17:54
  228. * @Copyright: copyright (c) 2021 广东七件事集团
  229. * @param $user_id
  230. * @param $level_arr
  231. * @return bool
  232. */
  233. public static function checkUserLevel($user_id, $level_arr)
  234. {
  235. $level = User::find()->select("level")->where(['id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->scalar();
  236. if(in_array($level, Json::decode($level_arr, true))) return true;
  237. return false;
  238. }
  239. public static function checkNewRegisterUser($mall_id, $user_id)
  240. {
  241. $res = User::find()->where([
  242. 'mall_id'=>$mall_id,
  243. 'id'=>$user_id,
  244. 'status'=>StatusEnum::ENABLED
  245. ])->andWhere(['>=','created_at',time()-7*24*3600])->one();
  246. if (empty($res)) return false;
  247. return true;
  248. }
  249. public static function checkRebateUserLevel($user_id, $level_arr)
  250. {
  251. $level = RebateUser::find()->select("level")->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->scalar();
  252. if(in_array($level, Json::decode($level_arr, true))) return true;
  253. return false;
  254. }
  255. public static function listsInit($params)
  256. {
  257. $list= self::lists($params);
  258. if(!empty($list))
  259. foreach ($list as $key => &$item) {
  260. $item['consume_level'] = empty($item['consume_level']) ? [] : Json::decode($item['consume_level'], true);
  261. $item['initiate_level'] = empty($item['initiate_level']) ? [] : Json::decode($item['initiate_level'], true);
  262. $item['reward_info'] = Json::decode($item['reward_info'], true);
  263. }
  264. return $list;
  265. }
  266. }