| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- namespace addons\CloudStock\console\controllers;
- use addons\CloudStock\common\logic\order\WangDianTongLogic;
- use addons\CloudStock\common\logic\reward\PoolReward;
- use addons\CloudStock\common\logic\reward\TeamReward;
- use addons\CloudStock\common\models\CloudStockAgent;
- use addons\CloudStock\common\models\CloudStockFillOrder;
- use addons\CloudStock\common\models\CloudStockLevel;
- use addons\CloudStock\common\models\CloudStockStatistic;
- use addons\CloudStock\common\services\AgentService;
- use addons\Distribution\common\models\Distribution;
- use addons\Distribution\common\models\DistributionLevel;
- use addons\WangDianTong\common\models\AddonsWangDianTongLogisticsCompany;
- use common\enums\AddonsEnum;
- use common\enums\OrderStatusEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\helpers\DateHelper;
- use common\models\common\CapitalLog;
- use common\models\mall\Mall;
- use common\models\mall\MallAddons;
- use common\models\order\Order;
- use common\models\order\OrderDetail;
- use common\models\user\User;
- use common\models\user\UserLevel;
- use services\common\UserWalletService;
- use yii\console\Controller;
- use yii\console\ExitCode;
- class IndexController extends Controller
- {
- public function actionSettlement()
- {
- $mall_list = Mall::getEnableMallList(true);
- foreach ($mall_list as $mall) {
- if (MallAddons::isInstall($mall['id'], AddonsEnum::ADDONS_NAME_CLOUD_STOCK) == 0) continue;
- $params['where'] = [
- ['mall_id' => $mall['id']],
- ['source_table' => OrderDetail::tableName()],
- ['from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK],
- ['capital_type' => [UserWalletEnum::PAYMENT_FOR_GOODS_BUY]],
- ['status' => CapitalLog::STATUS_WAIT_SEND]
- ];
- //货款
- $wallet_type = 'commission';
- CapitalLog::$capitalType = "{$wallet_type}_frozen";
- $commission_list = CapitalLog::lists($params);
- if (empty($commission_list)) continue;
- $source_table_id = array_column($commission_list, 'source_table_id');
- $order_detail_list = OrderDetail::lists(['where' => [['id' => $source_table_id]], 'select' => 'id,order_id', 'index' => 'id']);
- $order_ids = array_column($order_detail_list, 'order_id');
- $order_list = Order::lists(['where' => [['id' => $order_ids], ['order_status' => OrderStatusEnum::ACCOMPLISH]], 'select' => 'id', 'index' => 'id']);
- $user_ids = array_column($commission_list, 'user_id');
- $user_arr = User::lists(['where' => [['id' => $user_ids]], 'select' => 'id,mobile', 'index' => 'id']);
- $obj = new CloudStockAgent();
- foreach ($commission_list as $item) {
- if (empty($order_detail_list[$item['source_table_id']])) continue;
- $order_id = $order_detail_list[$item['source_table_id']]['order_id'];
- if (empty($order_list[$order_id])) continue;
- $res = UserWalletService::unfrozen($item['id'], $wallet_type);
- if ($res) {
- $cloud_stock_agent_model = CloudStockAgent::findOne(['user_id' => $item['user_id'], 'status' => StatusEnum::ENABLED]);
- $cloud_stock_agent_model->frozen_price -= $item['change_num'];
- if ($cloud_stock_agent_model->frozen_price < 0) $cloud_stock_agent_model->frozen_price = 0;
- $cloud_stock_agent_model->save();
- $obj->sendGoodsMoneyMessage($user_arr, $item);
- }
- }
- }
- }
- public function actionPoolReward($time = null, $start = null, $end = null)
- {
- $mall_list = Mall::getEnableMallList(true);
- if (!isset($time)) $time = time();
- foreach ($mall_list as $mall) {
- $mall_id = $mall['id'];
- PoolReward::index($mall_id, $time, $start, $end);
- }
- }
- public function actionTeamReward($time = null, $start = null, $end = null)
- {
- $mall_list = Mall::getEnableMallList(true);
- if (!isset($time)) $time = time();
- foreach ($mall_list as $mall) {
- $mall_id = $mall['id'];
- TeamReward::index($mall_id, $time, $start, $end);
- }
- }
- public function actionFixOrder()
- {
- (new CloudStockFillOrder())->fixOrder();
- }
- public function actionTest()
- {
- // (new WangDianTongSyncListener())->handleSync(['id' => 40]);
- /**
- * @var $logistics AddonsWangDianTongLogisticsCompany
- */
- $logistics = AddonsWangDianTongLogisticsCompany::getOne([
- ['erp_type' => 8]
- ]);
- // 发货信息
- $flag = WangDianTongLogic::syncLogistics([
- 'mall_id' => 1,
- 'express_code' => 'SF',
- 'express_no' => '963852',
- 'id' => 46, // 取出订单ID
- ], $logistics->toArray());
- var_dump($flag);
- }
- /**
- * 修正代理库存
- * @return void
- */
- public function actionFixStock()
- {
- $i = 1;
- $page_size = 1000;
- while ($list = CloudStockAgent::find()
- ->offset(($i - 1) * $page_size)->limit($page_size)->all()) {
- foreach ($list as $item) {
- CloudStockAgent::fixStock($item['mall_id'], $item['user_id']);//扣减库存
- }
- $i++;
- }
- }
- public function actionChangeData()
- {
- //云库存
- $level_list = CloudStockLevel::lists([
- 'where' => [
- ['level' => 2],
- ['status' => StatusEnum::ENABLED]
- ],
- 'order' => 'mall_id asc'
- ]);
- $time = time();
- foreach ($level_list as $item) {
- $res = CloudStockAgent::updateAll(['level' => 2, 'upgrade_status' => 3, 'upgrade_at' => $time, 'before_level' => 1], ['level' => 1, 'mall_id' => $item['mall_id']]);
- var_dump($item['mall_id'] . "更新云库存代理等级:" . $res);
- $res = CloudStockLevel::updateAll(['status' => -1], ['level' => 1, 'mall_id' => $item['mall_id']]);
- var_dump($item['mall_id'] . "更新云库存等级:" . $res);
- }
- //分销商
- $level_list = DistributionLevel::lists([
- 'where' => [
- ['level' => 2],
- ['status' => StatusEnum::ENABLED]
- ],
- 'order' => 'mall_id asc'
- ]);
- foreach ($level_list as $item) {
- $res = Distribution::updateAll(['level' => 2, 'upgrade_status' => 3, 'upgrade_level_at' => $time], ['level' => 1, 'mall_id' => $item['mall_id']]);
- var_dump($item['mall_id'] . "更新分销商代理等级:" . $res);
- $res = DistributionLevel::updateAll(['status' => -1], ['level' => 1, 'mall_id' => $item['mall_id']]);
- var_dump($item['mall_id'] . "更新分销商等级:" . $res);
- }
- //会员等级
- $level_list = UserLevel::lists([
- 'where' => [
- ['level' => 2],
- ['status' => StatusEnum::ENABLED]
- ],
- 'order' => 'mall_id asc']);
- foreach ($level_list as $item) {
- $res = User::updateAll(['level' => 2, 'upgrade_level_at' => $time], ['level' => 1, 'mall_id' => $item['mall_id']]);
- var_dump($item['mall_id'] . "更新会员用户等级:" . $res);
- $res = UserLevel::updateAll(['status' => -1], ['level' => 1, 'mall_id' => $item['mall_id']]);
- var_dump($item['mall_id'] . "更新会员等级:" . $res);
- }
- var_dump(1);
- }
- /**
- * 平级奖-结算定时任务
- * @return bool
- */
- public function actionSettlementEqualReward()
- {
- try {
- $mall_list = Mall::getEnableMallList(true);
- $time = time();
- foreach ($mall_list as $mall) {
- $setting = \Yii::$app->services->setting->addons->get($mall['id'], AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic');
- $is_true = false;
- $start_time = $end_time = 0;
- switch ($setting['settlement_cycle_of_equal_prize']) {
- case 1://周一
- $w = date('w', $time);
- if ($w == 1) {
- $is_true = true;
- }
- $res = DateHelper::lastWeek();
- $start_time = $res['start'];
- $end_time = $res['end'];
- break;
- case 2://按月
- $day = date('d', $time);
- if ($day == '01') {
- $is_true = true;
- }
- $res = DateHelper::lastMonth();
- $start_time = $res['start'];
- $end_time = $res['end'];
- break;
- default:
- }
- if (!$is_true) {
- continue;
- }
- $this->settlementEqualReward($mall['id'], $start_time, $end_time);
- }
- return ExitCode::OK;
- } catch (\Exception $e) {
- var_dump($e->getMessage());
- return false;
- }
- }
- protected function settlementEqualReward($mall_id, $start_time, $end_time)
- {
- CapitalLog::$capitalType = 'commission_frozen';
- $statistics_data = [];
- $statistics_user_data = [];
- while (true) {
- $list = CapitalLog::find()
- ->where(['mall_id' => $mall_id, 'status' => StatusEnum::DISABLED])
- ->andWhere(['from_type' => 'CloudStock', 'capital_type' => ['equal', 'price_difference_equal']])
- ->andWhere(['>=', 'created_at', $start_time])
- ->andWhere(['<=', 'created_at', $end_time])
- ->all();
- if (empty($list)) {
- break;
- }
- foreach ($list as $item) {
- if (isset($statistics_data[$item['capital_type']])) {
- $statistics_data[$item['capital_type']]['change_num'] += $item['change_num'];
- } else {
- $statistics_data[$item['capital_type']]['change_num'] = $item['change_num'];
- }
- if (isset($statistics_user_data[$item['user_id']][$item['capital_type']])) {
- $statistics_user_data[$item['user_id']][$item['capital_type']]['change_num'] += $item['change_num'];
- } else {
- $statistics_user_data[$item['user_id']][$item['capital_type']]['change_num'] = $item['change_num'];
- }
- UserWalletService::unfrozen($item['id'], 'commission');
- }
- }
- foreach ($statistics_data as $capital_type => $item) {
- $add_data = ['mall_id' => $mall_id, 'capital_type' => $capital_type, 'settlement_status' => 0, 'money' => -$item['change_num']];
- CloudStockStatistic::setData($add_data);//扣除待结算统计
- $add_data = ['mall_id' => $mall_id, 'capital_type' => $capital_type, 'settlement_status' => 1, 'money' => $item['change_num']];
- CloudStockStatistic::setData($add_data);//增加已结算统计
- }
- foreach ($statistics_user_data as $user_id => $value) {
- foreach ($value as $val) {
- $add_data = ['settlement_status' => 0, 'money' => -$val['change_num']];
- (new AgentService(['mall_id' => $mall_id, 'user_id' => $user_id]))->setData($add_data);
- $add_data = ['settlement_status' => 1, 'money' => $val['change_num']];
- (new AgentService(['mall_id' => $mall_id, 'user_id' => $user_id]))->setData($add_data);
- }
- }
- }
- }
|