| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace addons\Happiness\common\logic\weight;
- use addons\Happiness\common\enums\HappinessEnum;
- use addons\Happiness\common\models\HappinessWeightPoolLog;
- use addons\Happiness\common\models\HappinessWeightPoolJob;
- use addons\Happiness\common\models\HappinessWeightPoolOrderLog;
- use addons\Happiness\common\service\WeightService;
- use common\enums\StatusEnum;
- use common\models\mall\Mall;
- use common\models\mall\MallAddons;
- use common\traits\ErrorTrait;
- use Exception;
- use Yii;
- class PoolLogLogic
- {
-
- use ErrorTrait;
-
- /**
- * 执行分红操作
- * @return bool
- */
- public function execute()
- {
- $mall_list = Mall::getEnableMallList(true);
- if (!empty($mall_list)) {
- foreach ($mall_list as $mall) {
- $mallId = $mall['id'];
- // 获取基础配置
- if (!MallAddons::isInstall($mallId, HappinessEnum::ADDONS_NAME)) {
- continue;
- }
- $config = Yii::$app->services->setting->addons->get($mallId, HappinessEnum::ADDONS_NAME, 'basic');
- if ($config['compute_period'] == 4) {
- continue;
- }
- // 奖金池
- list(
- 'continue' => $continue,
- 'start_day' => $start_day,
- 'end_day' => $end_day,
- 'compute_period' => $compute_period
- ) = self::getExecutePeriod($config, 'compute_period', $mallId);
- if ($continue) continue;
- $date_limit = $start_day . '-' . $end_day;
- $job = HappinessWeightPoolJob::findOne(['mall_id' => $mallId, 'compute_period' => $compute_period, 'date_limit' => $date_limit, 'status' => StatusEnum::ENABLED]);
- if ($job) continue;
- // 生成执行记录
- $job = new HappinessWeightPoolJob();
- $job->mall_id = $mallId;
- $job->date_limit = $date_limit;
- $job->compute_period = $compute_period;
- $job->total_amount = 0;
- $job->period_total_amount = 0;
- $last_period = HappinessWeightPoolJob::find()->where(['mall_id' => $mallId, 'status' => StatusEnum::ENABLED])
- ->select('period')->orderBy('id DESC')->scalar();
- $period = $last_period ? $last_period + 1 : 1;
- $job->period = $period;
- if (!$job->save()) throw new Exception($job->getErrorMessage());
- $service = new WeightService();
- $service->send($mallId, $config, $start_day, $end_day, $job->id);
- }
- return true;
- }
- }
-
- public static function executeNoAuto($mallId)
- {
- // 获取基础配置
- if (!MallAddons::isInstall($mallId, HappinessEnum::ADDONS_NAME)) {
- throw new Exception('插件未安装');
- }
- $config = Yii::$app->services->setting->addons->get($mallId, HappinessEnum::ADDONS_NAME, 'basic');
- if ($config['compute_period'] != 4) {
- throw new Exception('只能手动执行');
- }
- // 奖金池
- list(
- 'continue' => $continue,
- 'start_day' => $start_day,
- 'end_day' => $end_day,
- 'compute_period' => $compute_period
- ) = self::getExecutePeriod($config, 'compute_period', $mallId);
- $date_limit = $start_day . '-' . $end_day;
- $job = HappinessWeightPoolJob::findOne(['mall_id' => $mallId, 'compute_period' => $compute_period, 'date_limit' => $date_limit, 'status' => StatusEnum::ENABLED]);
- if ($job) throw new Exception('记录已经存在');
- // 生成执行记录
- $job = new HappinessWeightPoolJob();
- $job->mall_id = $mallId;
- $job->date_limit = $date_limit;
- $job->compute_period = $compute_period;
- $job->total_amount = 0;
- $job->period_total_amount = 0;
- $last_period = HappinessWeightPoolJob::find()->where(['mall_id' => $mallId, 'status' => StatusEnum::ENABLED])
- ->select('period')->orderBy('id DESC')->scalar();
- $period = $last_period ? $last_period + 1 : 1;
- $job->period = $period;
- if (!$job->save()) throw new Exception($job->getErrorMessage());
- $service = new WeightService();
- return $service->send($mallId, $config, $start_day, $end_day, $job->id);
- }
-
- public static function getExecutePeriod($config, $key, $mall_id)
- {
- $continue = false;
- $start_day = 0;
- $end_day = 0;
- // 获取结算周期,0=天,1=周,2=月
- $compute_period = $config[$key];
- switch ($config[$key]) {
- case 1:
- $start_day = date("Ymd", strtotime("-1 day"));
- // $start_day = date("Ymd");
- $end_day = $start_day;
- break;
- case 2:
- if (date('w') != 1) $continue = true; // 非周一直接退出
- $start_day = date("Ymd", strtotime("last Monday"));
- $end_day = date('Ymd', strtotime('-1 sunday', time()));;
- break;
- case 3:
- if (date('d') != 1) $continue = true; // 非当月1号直接退出 (测试时注释,否则需要改服务器时间)
- $start_day = date("Ym01", strtotime("-1 month"));
- $end_day = date('Ymd', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400);
- break;
- case 4:
- //自定义结算
- $has = HappinessWeightPoolJob::find()->where(['mall_id' => $mall_id])->orderBy('id desc')->one();//最后结算的
- if (!$has) {
- $has = HappinessWeightPoolOrderLog::find()->where(['mall_id' => $mall_id])->orderBy('id asc')
- ->one();//最先的订单
- }
- $start_day = null;
- if (!$has) {
- $start_day = date("YmdHis", time() - 86400);
- } else {
- $start_day = date("YmdHis", $has->created_at);
- }
- $end_day = date("YmdHis", time());
- break;
- }
- return compact('continue', 'start_day', 'end_day', 'compute_period');
- }
-
- protected function getLogPath($type)
- {
- return Yii::getAlias('@runtime') . "/logs/Happiness/" . $type . '/' . date('Y_m_d') . '.txt';
- }
- }
|