| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\localPublicRanking;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\User;
- use app\models\SaasUser;
- use app\models\Order;
- use app\models\Goods;
- use app\models\LocalPublicRankingUser;
- use app\models\LocalPublicRankingTask;
- use app\models\LocalPublicRankingGoods;
- use app\models\Coupon;
- class LocalPublicRankingForm extends Model {
- public $store_id;
- public $id;
- public $ids;
- public $goods_name;
- public $supplier_name;
- public $goods_id;
- public $cat_id;
- public $supplier_id;
- public $warehouse_id;
- public $warehouse_zone_id;
- public $is_delete;
- public $warning_num;
- public $eiId;
- public $eipId;
- public $nickname;
- public $phone;
- public $order_no;
- public $type;
- public $purchase_in_price;
-
- public function init() {
- parent::init();
- if (empty($this->store_id)) {
- $this->store_id = get_store_id();
- }
- }
- public static function afterOrderSave($order, $insert = 0, $changedAttributes = []) {
- try{
- $store_id = $order->store_id;
- if(!self::isopen($store_id)){
- return;
- }
- if(!$order->user_id){
- return;
- }
- $goods_id = self::isOrderInAct($order);
- if($order->is_pay && $goods_id){
- $doTask = LocalPublicRankingTask::doTask($store_id, $order->user_id, $order, $goods_id);
- debug_log([__LINE__, $store_id, $order->id, $insert, $doTask['data'] ? $doTask['data']->id : $doTask, $changedAttributes], __CLASS__ . '.log');
- }
- } catch (\Exception $ex) {
- \Yii::error($ex);
- debug_log([__LINE__, $order->id, $insert, $changedAttributes, $ex->getMessage()], __CLASS__ . '.log');
- return [
- 'code' => 1,
- 'msg' => $ex->getMessage(),
- ];
- }
- }
- public static function afterTaskSave($task, $insert = 0, $changedAttributes = []) {
- try{
- if ($insert) {
- //复购
- $price2Fugou = self::price2Fugou($task);
- debug_log([__LINE__, '复购', $task->id, $task->order_id, $price2Fugou], __CLASS__ . '.log');
- }
- } catch (\Exception $ex) {
- \Yii::error($ex);
- debug_log([__LINE__, $task->id, $ex->getMessage(), $changedAttributes], __CLASS__ . '.log');
- return [
- 'code' => 1,
- 'msg' => $ex->getMessage(),
- ];
- }
- }
- public static function afterPRUSave($pru, $insert = 0, $changedAttributes = []) {
- try{
- if ($insert) {
- //优惠券
- $sendCoupon = self::sendCoupon($pru);
- debug_log([__LINE__, '优惠券', $pru->id, $pru->user_id, $sendCoupon], __CLASS__ . '.log');
- }
- } catch (\Exception $ex) {
- \Yii::error($ex);
- debug_log([__LINE__, $pru->id, $ex->getMessage(), $changedAttributes], __CLASS__ . '.log');
- return [
- 'code' => 1,
- 'msg' => $ex->getMessage(),
- ];
- }
- }
- public static function checkMchList($store_id, $mch_list = []) {
- try{
- if(self::isopen($store_id)){
- $num = 0;
- $actGoods = null;
- foreach($mch_list as $mch){
- foreach($mch['goods_list'] as $goods){
- $num += $goods['num'];
- if(!$actGoods){
- $actGoods = self::actGoods($goods['id']);
- if($actGoods){
- $actGoods = $goods;
- }
- }
- }
- }
- if($actGoods && $num > 1){
- throw new \Exception('商城公排活动商品只能单独购买,商品名称:' . $actGoods['goods_name']);
- }
- }
- return [
- 'code' => 0,
- ];
- } catch (\Exception $ex) {
- \Yii::error($ex);
- debug_log([__LINE__, $mch_list, $ex->getMessage()], __CLASS__ . '.log');
- return [
- 'code' => 1,
- 'msg' => $ex->getMessage(),
- ];
- }
- }
- public static $confs = [];
- public static function conf($store_id = 0, $refresh = 0) {
- if (isset(self::$confs[$store_id]) && !$refresh) {
- return self::$confs[$store_id];
- }
- $confDef = [
- 'is_open' => 0,
- //规则
- 'PublicRankingRow' => 0, //3人公排(33公排)或2人公排(22公排) ○若已有公排点位,不可修改此项
- 'firstPriceParent' => 0, //首单奖励 1直推人 0公排上级
- //复购
- 'price2Fugou' => '', //强制复购金额:佣金达到强制复购金额后,需重新完成任务,保完成后才能获得对应层级奖金
- 'price2FugouAmount' => '', //复购金额:备注:当用户达到强制复购金额后必须完成规定的复购金额后才能重新开始获得佣金
- //躺赚
- 'auto_price_child_cnt' => '', //躺赚直推人数
- 'auto_price_wait_days' => '', //躺赚直推等待天数
- //推荐奖励活动
- 'coupon_child_user_count' => '', //直推人数
- 'coupon_count' => '',
- 'coupon_id' => '',
- ];
- $conf = Option::get(OptionSetting::LOCAL_PUBLIC_RANKING_SETTING, $store_id, 'store')['value'];
- if ($conf) {
- $conf = array_merge($confDef, json_decode($conf, true));
- } else {
- $conf = $confDef;
- }
- self::$confs[$store_id] = $conf;
- return $conf;
- }
- public static function confSave($store_id = 0, $config = []) {
- $oldConf = self::conf($store_id, 1);
- $conf = array_merge($oldConf, $config);
- $set = Option::set(OptionSetting::LOCAL_PUBLIC_RANKING_SETTING, json_encode($conf, JSON_UNESCAPED_UNICODE), $store_id, 'store');
- self::conf($store_id, 1);
- return [
- 'code' => 0,
- 'msg' => 'ok',
- '$save' => $set,
- ];
- }
- public static function isopen($store_id = 0) {
- $conf = self::conf($store_id);
- if(!$conf['PublicRankingRow']){
- return 0;
- }
- //规定:默认开启,不能关闭
- return 1;
- $isopen = $conf['is_open'];
- return empty($isopen) ? 0 : 1;
- }
- //躺赚
- public static function isAutoPriceSuccess($store_id, $user_id) {
- $publicRankingUser = LocalPublicRankingUser::findOne(['user_id' => $user_id]);
- $conf = self::conf($store_id);
- $conf_auto_price_child_cnt = $conf['auto_price_child_cnt'];
- $conf_auto_price_wait_days = $conf['auto_price_wait_days'];
- if($conf_auto_price_child_cnt <= 0){
- return true;
- }
- if($conf_auto_price_wait_days > 0 && ($publicRankingUser['created_at'] > (time() - 86400 * $conf_auto_price_wait_days))){
- return true;
- }
- $childCnt = LocalPublicRankingUser::find()->where(['user_id' => User::find()->select('id')->where(['old_parent_id' => $user_id])])->count();
- if($childCnt >= $conf_auto_price_child_cnt){
- return true;
- }
- return false;
- }
-
- //复购
- public static function price2Fugou(LocalPublicRankingTask $task) {
- $store_id = $task->store_id;
- debug_log([__LINE__, '检测复购', $store_id, $task['id'], $task['order_id']], __CLASS__ . '.log');
- $parent_user_id = $task->parent_user_id;
- if(!$parent_user_id){
- return null;
- }
- $conf = self::conf($store_id);
- $conf_price2Fugou = $conf['price2Fugou'];
- if($conf_price2Fugou <= 0){
- debug_log([__LINE__, '强制复购金额未配置', $store_id, $parent_user_id], __CLASS__ . '.log');
- return null;
- }
- $priceTotal = LocalPublicRankingTask::priceTotal($store_id, $parent_user_id, 0);
- if($priceTotal >= $conf_price2Fugou){
- $t = \Yii::$app->db->beginTransaction();
- try {
- $parent_pru = LocalPublicRankingUser::findOne(['user_id' => $parent_user_id]);
- $updateAll = LocalPublicRankingTask::updateAll(['has_fugou' => 1], ['store_id' => $store_id, 'parent_user_id' => $parent_user_id, 'has_fugou' => 0, 'status' => 1]);
- $parent_pru->is_fugou = 1;
- if(!$parent_pru->save()){
- throw new \Exception(array_shift($parent_pru->getFirstErrors()));
- }
- $t->commit();
- debug_log([__LINE__, '检测复购 加状态', $task->id, $updateAll, '$pru->is_fugou = 1', $parent_user_id], __CLASS__ . '.log');
- return true;
- } catch (\Exception $e) {
- $t->rollBack();
- \Yii::error($e);
- debug_log([__LINE__, '检测复购', $store_id, $task->id, $e->getMessage()], __CLASS__ . '.log');
- }
- }
- return false;
- }
-
- //复购完成
- public static function isFugouFinish($store_id, $user_id) {
- $conf = self::conf($store_id);
- $price2FugouAmount = $conf['price2FugouAmount'];
- $payTotal = LocalPublicRankingTask::payTotal($store_id, $user_id, 0);
- if($payTotal >= $price2FugouAmount){
- return true;
- }
- return false;
- }
-
- //优惠券
- public static function sendCoupon(LocalPublicRankingUser $pru) {
- $store_id = $pru->store_id;
- $user = User::findOne($pru['user_id']);
- $parent_user_id = $user['old_parent_id'];
- debug_log([__LINE__, '检测优惠券', $store_id, $pru['id'], $pru['user_id'], $parent_user_id], __CLASS__ . '.log');
- if(!$parent_user_id){
- return null;
- }
- $pruParent = LocalPublicRankingUser::findOne(['user_id' => $parent_user_id]);
- if(!$pruParent){
- return null;
- }
- $conf = self::conf($store_id);
- $coupon_child_user_count = $conf['coupon_child_user_count'];
- $coupon_count = $conf['coupon_count'];
- $coupon_id = $conf['coupon_id'];
- if(!$coupon_child_user_count || !$coupon_count || !$coupon_id){
- return null;
- }
- $child_user_cnt = (int)LocalPublicRankingUser::find()->where(['user_id' => User::find()->select('id')->where(['old_parent_id' => $parent_user_id])])->count();
- if($child_user_cnt != $coupon_child_user_count){
- return null;
- }
- for ($i = 0; $i < $coupon_count; $i++) {
- $use_coupon = Coupon::userAddCoupon($parent_user_id, $coupon_id);
- debug_log([__LINE__, '发优惠券', $store_id, $parent_user_id, $coupon_id, $use_coupon], __CLASS__ . '.log');
- }
- return $use_coupon;
- }
- //订单是否是活动商品
- public static function isOrderInAct($order) {
- $detail = $order->detail;
- if(count($detail) == 1){
- $det = $detail[0];
- $goods_id = $det['goods_id'];
- $actGoods = LocalPublicRankingGoods::findOne(['goods_id' => $goods_id, 'status' => 1]);
- if($actGoods){
- return $goods_id;
- }
- }
- return false;
- }
- //活动商品
- public static function actGoods($goods_id = null) {
- return LocalPublicRankingGoods::findOne(['goods_id' => $goods_id, 'status' => 1]);
- }
- public static function publicRankingGoodsList($store_id, $params = []) {
- $query = LocalPublicRankingGoods::find()->where(['store_id' => $store_id]);
- if(isset($params['status']) && $params['status'] >= 0){
- $query->andWhere(['status' => $params['status']]);
- }
- if($params['name']){
- $query->andWhere(['goods_id' => Goods::find()->select('id')->where(['like', 'name', $params['name']])]);
- }
- $query->orderBy('id DESC');
- $data = pagination_make($query);
- foreach($data['list'] as &$item){
- $item['goods'] = Goods::findOne($item['goods_id']);
- }
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => $data,
- ];
- }
- /**
- * 商品保存
- */
- public static function publicRankingGoodsSave($store_id, $goods_id, $commission_amount = 0) {
- try {
- $goodsExt = LocalPublicRankingGoods::findOne(['goods_id' => $goods_id, 'store_id' => $store_id]);
- if (!$goodsExt) {
- $goodsExt = new LocalPublicRankingGoods();
- $goodsExt->store_id = $store_id;
- $goodsExt->goods_id = $goods_id;
- $goodsExt->status = 1;
- }
- $goodsExt->commission_amount = $commission_amount;
- if (!$goodsExt->save()) {
- \Yii::error([__METHOD__, $goodsExt->attributes]);
- throw new \Exception('保存失败。' . array_shift($goodsExt->getFirstErrors()));
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功!'
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /**
- * 商品分红状态
- */
- public static function publicRankingGoodsStatus($id, $status) {
- if (!is_array($id)) {
- $id = explode(',', $id);
- }
- LocalPublicRankingGoods::updateAll(['status' => $status], ['id' => $id]);
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- }
- /**
- * 商品删除
- */
- public static function publicRankingGoodsDel($id) {
- if (!is_array($id)) {
- $id = explode(',', $id);
- }
- LocalPublicRankingGoods::deleteAll(['id' => $id]);
- return [
- 'code' => 0,
- 'msg' => 'success',
- ];
- }
- public static function publicRankingTaskList($store_id, $params = []) {
- $query = LocalPublicRankingTask::find()->where(['store_id' => $store_id]);
- if(isset($params['status']) && $params['status'] >= 0){
- $query->andWhere(['status' => $params['status']]);
- }
- if($params['user_id']){
- $query->andWhere(['user_id' => $params['user_id']]);
- }
- if($params['parent_user_id']){
- $query->andWhere(['parent_user_id' => $params['parent_user_id']]);
- }
- $query->orderBy('id DESC');
- $data = pagination_make($query);
- foreach($data['list'] as &$item){
- $item['user'] = User::findOne($item['user_id']);
- $item['order'] = Order::findOne($item['order_id']);
- $item['order_detail'] = $item['order'] ? $item['order']->detail : null;
- }
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => $data,
- ];
- }
- public static function publicRankingUserList($store_id, $params = []) {
- $conf = self::conf($store_id);
- $query = LocalPublicRankingUser::find()->where(['store_id' => $store_id]);
- if($params['user_name']){
- $query->andWhere(['user_id' => User::find()->select('id')->where(['like', 'nickname', $params['user_name']])]);
- }
- if($params['mobile']){
- $query->andWhere(['user_id' => User::find()->select('id')->where(['like', 'binding', $params['mobile']])]);
- }
- if($params['time_begin']){
- $query->andWhere(['>=', 'created_at', strtotime($params['time_begin'])]);
- }
- if($params['time_end']){
- $query->andWhere(['<=', 'created_at', strtotime($params['time_end'])]);
- }
- if(isset($params['sort']) && $params['sort'] > 0){
- if(strpos($params['sort'], '-')){
- $params['sort'] = LocalPublicRankingUser::posToSort($params['sort'], $conf['PublicRankingRow']);
- }
- $query->andWhere(['sort' => trim($params['sort'])]);
- }
- $query->orderBy('sort');
- $data = pagination_make($query);
- foreach($data['list'] as &$item){
- $item['user'] = User::findOne($item['user_id']);
- $item['saas_user'] = $item['user'] ? SaasUser::findOne(['mobile' => $item['user']['binding']]) : null;
- $buyOrderCnt = (int)LocalPublicRankingTask::find()->where(['user_id' => $item['user_id']])->count();
- $item['buy_order_cnt'] = $buyOrderCnt;
- $buyOrderSum = (float)Order::find()->where(['id' => LocalPublicRankingTask::find()->select('order_id')->where(['user_id' => $item['user_id']])])->sum('pay_price');
- $item['buy_order_sum'] = $buyOrderSum;
- $childOrderCnt = (int)LocalPublicRankingTask::find()->where(['parent_user_id' => $item['user_id']])->count();
- $item['child_order_cnt'] = $childOrderCnt;
- $commission_amount = (float)LocalPublicRankingTask::find()->where(['parent_user_id' => $item['user_id']])->sum('commission_amount');
- $item['commission_amount'] = $commission_amount;
- $child_user_cnt = (int)LocalPublicRankingUser::find()->where(['user_id' => User::find()->select('id')->where(['old_parent_id' => $item['user_id']])])->count();
- $item['child_user_cnt'] = $child_user_cnt;
- $item['getPos'] = LocalPublicRankingUser::getPos($item['sort'], $conf['PublicRankingRow']);
- }
- return [
- 'code' => 0,
- 'msg' => 'ok',
- 'data' => $data,
- ];
- }
- public static function publicRankingUserIndex($store_id, $user_id) {
- try{
- if(!self::isopen($store_id)){
- throw new \Exception('商城功能未开启');
- }
- $conf = self::conf($store_id);
- $taskFinshCount = LocalPublicRankingTask::find()->where(['store_id' => $store_id, 'user_id' => $user_id, 'status' => 1])->count();
- $user = User::findOne($user_id);
- $publicRankingUser = LocalPublicRankingUser::findOne(['user_id' => $user_id]);
- if($publicRankingUser && $publicRankingUser->sort > 1){
- $psort = LocalPublicRankingUser::getParentSorts($publicRankingUser->sort, $conf['PublicRankingRow'], 1);
- $publicRankingUserParent = LocalPublicRankingUser::findOne(['store_id' => $store_id, 'sort' => $psort]);
- $publicRankingUserParentUser = $publicRankingUserParent->user_id ? User::findOne($publicRankingUserParent->user_id) : null;
- }
- $parentUser = $user->old_parent_id ? User::findOne($user->old_parent_id) : null;
- $isAutoPriceSuccess = self::isAutoPriceSuccess($store_id, $user_id);
- $isPriceSuccess = !$publicRankingUser->is_fugou && $isAutoPriceSuccess;
- $child_user_cnt = (int)LocalPublicRankingUser::find()->where(['user_id' => User::find()->select('id')->where(['old_parent_id' => $user_id])])->count();
- return [
- 'code' => 0,
- 'data' => [
- 'conf' => $conf,
- 'publicRankingUserParentUser' => $publicRankingUserParentUser,
- 'parentUser' => $parentUser,
- 'publicRankingUser' => $publicRankingUser,
- 'user' => $user,
- 'saas_user' => get_saas_user(),
- 'taskFinshCount' => $taskFinshCount,
- 'isAutoPriceSuccess' => $isAutoPriceSuccess,
- 'isPriceSuccess' => $isPriceSuccess,
- 'child_user_cnt' => $child_user_cnt,
- 'getPos' => LocalPublicRankingUser::getPos($publicRankingUser['sort'], $conf['PublicRankingRow']),
- ],
- ];
- } catch (\Exception $e) {
- \Yii::error($e);
- debug_log([__LINE__, $e->getMessage()], __CLASS__ . '.log');
- return [
- 'code' => 1,
- 'msg' => '操作失败,' . $e->getMessage()
- ];
- }
- }
- }
|