Rebate.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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\goods\GoodsCateRelate;
  12. use common\models\order\Order;
  13. use common\models\statistics\UserStatisticsTotal;
  14. use common\models\user\User;
  15. use phpseclib3\Math\BigInteger;
  16. use Yii;
  17. use yii\db\Exception;
  18. use yii\helpers\Json;
  19. /**
  20. * This is the model class for table "{{%addons_rebate}}".
  21. *
  22. * @property int $id
  23. * @property int $mall_id
  24. * @property string $title
  25. * @property int $start_at 开始时间,单位:秒
  26. * @property bigInteger $end_at 结束时间,单位:秒
  27. * @property int $initiate_type 发起人权限类型[0:全部;1:新会员;2:等级会员]
  28. * @property string $initiate_level 等级会员json串[initiate_type=2时使用]
  29. * @property int $consume_type 参与人权限类型[0:全部;1:新会员;2:等级会员]
  30. * @property string $consume_level 等级会员json串[consume_type=2时使用]
  31. * @property int $item_id 活动商品id
  32. * @property string $item_info 活动商品详情
  33. * @property int $reward_type 奖励类型[1:奖励条件;2:阶梯奖励]
  34. * @property string $reward_info 奖励详情
  35. * @property int $is_repeat 阶梯奖励是否重复奖励[0=否,1=是]
  36. * @property int $status 状态[-1:删除;0:禁用;1启用]
  37. * @property int $created_at 创建时间
  38. * @property int $updated_at 更新时间
  39. * @property int $join_type 参与类型
  40. * @property int $is_grant 参与类型
  41. * @property int $is_reward_after_n 是否开启第N人后开始才可以获得奖励 0否 1是
  42. * @property int $reward_threshold 具体配置的第N人之后
  43. * @property int $is_freeze_first_reward 是否冻结第一项奖励
  44. * @property int $item_type 活动商品类型 0商品ID 1商品分类ID
  45. * @property int $freeze_first_reward_type 冻结第一项奖励比例 0全部冻结 1按设置百分比冻结
  46. * @property float $freeze_first_reward_percentage 冻结第一项奖励百分比
  47. */
  48. class Rebate extends \common\models\base\BaseActiveRecord
  49. {
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public static function tableName()
  54. {
  55. return '{{%addons_rebate}}';
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function rules()
  61. {
  62. return [
  63. [['mall_id', 'title'], 'required'],
  64. [['mall_id', 'start_at', 'end_at', 'initiate_type', 'consume_type', 'item_id', 'reward_type', 'is_repeat', 'status', 'created_at', 'updated_at','join_type','repeat_setting','agent_limit_num','is_grant', 'is_reward_after_n', 'reward_threshold', 'is_freeze_first_reward', 'item_type', 'freeze_first_reward_type'], 'integer'],
  65. [['freeze_first_reward_percentage'], 'number'],
  66. [['end_at'], 'safe'],
  67. [['title', 'initiate_level','consume_level','item_info','reward_info'], 'string'],
  68. ];
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function attributeLabels()
  74. {
  75. return [
  76. 'id' => 'ID',
  77. 'mall_id' => '商城id',
  78. 'title' => '活动名称',
  79. 'start_at' => '开始时间,单位:秒',
  80. 'end_at' => '结束时间,单位:秒',
  81. 'initiate_type' => '发起人权限类型[0:全部;1:新会员;2:等级会员]',
  82. 'initiate_level' => '等级会员json串[initiate_type=2时使用]',
  83. 'consume_type' => '参与人权限类型[0:全部;1:新会员;2:等级会员]',
  84. 'consume_level' => '等级会员json串[consume_type=2时使用]',
  85. 'item_id' => '活动商品id',
  86. 'item_info' => '活动商品详情',
  87. 'reward_type' => '奖励类型[1:奖励条件;2:阶梯奖励]',
  88. 'reward_info' => '奖励具体信息',
  89. 'is_repeat' => '阶梯奖励是否重复奖励[0=否,1=是]',
  90. 'status' => '状态[-1:删除;0:禁用;1启用]',
  91. 'created_at' => '创建时间',
  92. 'updated_at' => '更新时间',
  93. 'join_type' => '参与类型',
  94. 'is_grant' => '优惠/抵扣不发奖开关:1开启 0关闭',
  95. 'is_reward_after_n' => '是否开启第N人后开始才可以获得奖励 0否 1是',
  96. 'reward_threshold' => '具体配置的第N人之后',
  97. 'is_freeze_first_reward' => '冻结第一项奖励,',
  98. 'freeze_first_reward_type' => '冻结第一项奖励比例 0全部冻结 1按设置百分比冻结,',
  99. 'freeze_first_reward_percentage' => '冻结第一项奖励百分比,',
  100. ];
  101. }
  102. /**
  103. * 关联统计表
  104. * @Author: lun
  105. * @DateTime: 2022/1/18 0018 15:17
  106. * @Copyright: copyright (c) 2021 广东七件事集团
  107. * @return \yii\db\ActiveQuery
  108. */
  109. public function getStatistics()
  110. {
  111. return $this->hasOne(RebateStatistics::class, ['r_id' => 'id'])->where(['status' => 1]);
  112. }
  113. /**
  114. * 关联商品信息
  115. * @Author: lun
  116. * @DateTime: 2022/1/18 0018 16:56
  117. * @Copyright: copyright (c) 2021 广东七件事集团
  118. * @return \yii\db\ActiveQuery
  119. */
  120. public function getGoods()
  121. {
  122. return $this->hasOne(Goods::class, ['id' => 'item_id']);
  123. }
  124. /**
  125. * goods表字段过滤
  126. * @param $item
  127. * @return array
  128. */
  129. public static function filterGoods($goods_id)
  130. {
  131. if (!$goods_id) {
  132. return [];
  133. }
  134. $goods = Goods::findOne(['id' =>$goods_id]);
  135. if (!$goods) {
  136. return [];
  137. }
  138. return [
  139. 'id' => $goods->id,
  140. 'name' => $goods->name,
  141. 'cover_pic' => $goods->coverPic,
  142. 'price' => $goods->price,
  143. ];
  144. }
  145. /**
  146. * 保存数据
  147. * @Author: lun
  148. * @DateTime: 2022/1/15 0015 9:26
  149. * @Copyright: copyright (c) 2021 广东七件事集团
  150. * @param $mall_id
  151. * @param array $params
  152. * @return Rebate|bool
  153. */
  154. public static function setData($mall_id, array $params){
  155. try{
  156. if(!empty($params['id'])){
  157. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $mall_id],['<>','status', -1]]);
  158. if(empty($model)) throw new \Exception('活动不存在或已删除');
  159. }else{
  160. $model = new self();
  161. $model->loadDefaultValues();
  162. $model->mall_id = $mall_id;
  163. }
  164. if (!empty($params['is_reward_after_n'])) {
  165. if (empty($params['reward_threshold']) || !is_numeric($params['reward_threshold'])) {
  166. throw new \RuntimeException('请设置第N人后开始才可以获得奖励');
  167. }
  168. if (!in_array($params['reward_threshold'], array_column($params['reward_info'], 'num'))) {
  169. throw new \RuntimeException('第N人后开始才可以获得奖励设置错误');
  170. }
  171. } else {
  172. $params['is_reward_after_n'] = 0;
  173. $params['reward_threshold'] = 0;
  174. }
  175. // 将内容转为json字符串
  176. !empty($params['initiate_level']) && $params['initiate_level'] = Json::encode($params['initiate_level']);
  177. !empty($params['consume_level']) && $params['consume_level'] = Json::encode($params['consume_level']);
  178. !empty($params['item_info']) && $params['item_info'] = Json::encode($params['item_info']);
  179. !empty($params['reward_info']) && $params['reward_info'] = Json::encode($params['reward_info']);
  180. if(!$model->load($params,'') || !$model->save()) throw new Exception($model->getErrorMessage());
  181. return $model;
  182. }catch(\Exception $e){
  183. self::setStaticError($e->getMessage());
  184. return false;
  185. }
  186. }
  187. /**
  188. * 根据商品id获取活动内容
  189. * @Author: lun
  190. * @DateTime: 2022/1/15 0015 9:26
  191. * @Copyright: copyright (c) 2021 广东七件事集团
  192. * @param $mall_id
  193. * @param $goods_id
  194. * @return array|bool|\yii\db\ActiveRecord
  195. */
  196. public static function getRebateByGoodsId($mall_id, $goods_id)
  197. {
  198. $select = "id,title,initiate_type,initiate_level,start_at,end_at,consume_type,consume_level,reward_type,reward_info,is_repeat";
  199. $data = self::find()->select($select)->where(['mall_id' => $mall_id, 'item_id' => $goods_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
  200. if(empty($data)) return false;
  201. // 判断活动是否过期
  202. $time = time();
  203. if($data['start_at'] > $time || $time > $data['end_at']) return false;
  204. return $data;
  205. }
  206. /**
  207. * 根据商品id获取活动内容
  208. * @Author: lun
  209. * @DateTime: 2022/1/15 0015 9:26
  210. * @Copyright: copyright (c) 2021 广东七件事集团
  211. * @param $mall_id
  212. * @param $goods_id
  213. * @return array|bool|\yii\db\ActiveRecord
  214. */
  215. public static function getRebateAllGoodsId($mall_id)
  216. {
  217. // 判断活动是否过期
  218. $time = time();
  219. $select = "id,title,item_id,initiate_type,initiate_level,start_at,end_at,consume_type,consume_level,reward_type," .
  220. "reward_info,is_repeat,join_type,is_grant,repeat_setting,agent_limit_num,is_reward_after_n,reward_threshold," .
  221. "is_freeze_first_reward,item_type, freeze_first_reward_type, freeze_first_reward_percentage";
  222. $data = self::find()->select($select)
  223. ->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED])
  224. ->andWhere(["<", "start_at", $time])
  225. ->andWhere([">", "end_at", $time])
  226. ->asArray()->all();
  227. foreach($data as &$item) {
  228. if($item['item_type'] == 1) {
  229. // 分类ID查询对应商品
  230. $item['goods'] = GoodsCateRelate::find()->where(['cate_id' => $item['item_id']])
  231. ->select("id,cate_id,goods_id")
  232. ->with(['goods' => function($query){
  233. $query->select("id,goods_name,price,cover_pic");
  234. }])
  235. ->asArray()
  236. ->all();
  237. }
  238. }
  239. // 初始化结果数组
  240. $result = [];
  241. // 遍历原始数据数组
  242. foreach ($data as $originalItem) {
  243. if ($originalItem['item_type'] == 1) {
  244. //商品分类的重新组装数据
  245. foreach ($originalItem['goods'] as $good) {
  246. if (!empty($good['goods'])) {
  247. $newItem = array_diff_key($originalItem, array_flip(['goods']));
  248. $newItem['item_id'] = $good['goods_id'];
  249. $result[] = $newItem;
  250. }
  251. }
  252. } else {
  253. //商品ID的直接返回
  254. $result[] = $originalItem;
  255. }
  256. }
  257. return $result;
  258. }
  259. /**
  260. * 判断该商品是否已参与其他活动
  261. * @Author: lun
  262. * @DateTime: 2022/1/15 0015 9:24
  263. * @Copyright: copyright (c) 2021 广东七件事集团
  264. * @param $mall_id
  265. * @param $goods_id
  266. * @param int $r_id
  267. * @return bool
  268. */
  269. public static function checkGoods($mall_id, $goods_id, $r_id = 0)
  270. {
  271. $checkGoods = self::find()->select("id")->where(['mall_id' => $mall_id, 'item_id' => $goods_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
  272. if ($checkGoods) {
  273. if(empty($r_id) || (!empty($r_id) && $checkGoods['id'] != $r_id)) {
  274. return false;
  275. }
  276. }
  277. return true;
  278. }
  279. /**
  280. * 判断用户是否支付过
  281. * @Author: lun
  282. * @DateTime: 2022/1/15 0015 9:24
  283. * @Copyright: copyright (c) 2021 广东七件事集团
  284. * @param $user_id
  285. * @return mixed
  286. */
  287. public static function checkUserHasOrder($mall_id, $user_id)
  288. {
  289. // $num = UserStatisticsTotal::find()->select("pay_num")->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->scalar();
  290. $num = Order::find()->select("id")->where(['mall_id' => $mall_id, 'user_id' => $user_id, 'pay_status' => StatusEnum::ENABLED,'status' => StatusEnum::ENABLED])->asArray()->count();
  291. if (empty($num)) return false;
  292. return $num;
  293. }
  294. /**
  295. * 判断用户等级是否符合条件
  296. * @Author: lun
  297. * @DateTime: 2022/1/6 0006 17:54
  298. * @Copyright: copyright (c) 2021 广东七件事集团
  299. * @param $user_id
  300. * @param $level_arr
  301. * @return bool
  302. */
  303. public static function checkUserLevel($user_id, $level_arr)
  304. {
  305. $level = User::find()->select("level")->where(['id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->scalar();
  306. if(in_array($level, Json::decode($level_arr, true))) return true;
  307. return false;
  308. }
  309. public static function checkNewRegisterUser($mall_id, $user_id)
  310. {
  311. $res = User::find()->where([
  312. 'mall_id'=>$mall_id,
  313. 'id'=>$user_id,
  314. 'status'=>StatusEnum::ENABLED
  315. ])->andWhere(['>=','created_at',time()-7*24*3600])->one();
  316. if (empty($res)) return false;
  317. return true;
  318. }
  319. public static function checkRebateUserLevel($user_id, $level_arr)
  320. {
  321. $level = RebateUser::find()->select("level")->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->scalar();
  322. if(in_array($level, Json::decode($level_arr, true))) return true;
  323. return false;
  324. }
  325. }