PoolLogLogic.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace addons\Happiness\common\logic\weight;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use addons\Happiness\common\models\HappinessWeightPoolLog;
  5. use addons\Happiness\common\models\HappinessWeightPoolJob;
  6. use addons\Happiness\common\models\HappinessWeightPoolOrderLog;
  7. use addons\Happiness\common\service\WeightService;
  8. use common\enums\StatusEnum;
  9. use common\models\mall\Mall;
  10. use common\models\mall\MallAddons;
  11. use common\traits\ErrorTrait;
  12. use Exception;
  13. use Yii;
  14. class PoolLogLogic
  15. {
  16. use ErrorTrait;
  17. /**
  18. * 执行分红操作
  19. * @return bool
  20. */
  21. public function execute()
  22. {
  23. $mall_list = Mall::getEnableMallList(true);
  24. if (!empty($mall_list)) {
  25. foreach ($mall_list as $mall) {
  26. $mallId = $mall['id'];
  27. // 获取基础配置
  28. if (!MallAddons::isInstall($mallId, HappinessEnum::ADDONS_NAME)) {
  29. continue;
  30. }
  31. $config = Yii::$app->services->setting->addons->get($mallId, HappinessEnum::ADDONS_NAME, 'basic');
  32. if ($config['compute_period'] == 4) {
  33. continue;
  34. }
  35. // 奖金池
  36. list(
  37. 'continue' => $continue,
  38. 'start_day' => $start_day,
  39. 'end_day' => $end_day,
  40. 'compute_period' => $compute_period
  41. ) = self::getExecutePeriod($config, 'compute_period', $mallId);
  42. if ($continue) continue;
  43. $date_limit = $start_day . '-' . $end_day;
  44. $job = HappinessWeightPoolJob::findOne(['mall_id' => $mallId, 'compute_period' => $compute_period, 'date_limit' => $date_limit, 'status' => StatusEnum::ENABLED]);
  45. if ($job) continue;
  46. // 生成执行记录
  47. $job = new HappinessWeightPoolJob();
  48. $job->mall_id = $mallId;
  49. $job->date_limit = $date_limit;
  50. $job->compute_period = $compute_period;
  51. $job->total_amount = 0;
  52. $job->period_total_amount = 0;
  53. $last_period = HappinessWeightPoolJob::find()->where(['mall_id' => $mallId, 'status' => StatusEnum::ENABLED])
  54. ->select('period')->orderBy('id DESC')->scalar();
  55. $period = $last_period ? $last_period + 1 : 1;
  56. $job->period = $period;
  57. if (!$job->save()) throw new Exception($job->getErrorMessage());
  58. $service = new WeightService();
  59. $service->send($mallId, $config, $start_day, $end_day, $job->id);
  60. }
  61. return true;
  62. }
  63. }
  64. public static function executeNoAuto($mallId)
  65. {
  66. // 获取基础配置
  67. if (!MallAddons::isInstall($mallId, HappinessEnum::ADDONS_NAME)) {
  68. throw new Exception('插件未安装');
  69. }
  70. $config = Yii::$app->services->setting->addons->get($mallId, HappinessEnum::ADDONS_NAME, 'basic');
  71. if ($config['compute_period'] != 4) {
  72. throw new Exception('只能手动执行');
  73. }
  74. // 奖金池
  75. list(
  76. 'continue' => $continue,
  77. 'start_day' => $start_day,
  78. 'end_day' => $end_day,
  79. 'compute_period' => $compute_period
  80. ) = self::getExecutePeriod($config, 'compute_period', $mallId);
  81. $date_limit = $start_day . '-' . $end_day;
  82. $job = HappinessWeightPoolJob::findOne(['mall_id' => $mallId, 'compute_period' => $compute_period, 'date_limit' => $date_limit, 'status' => StatusEnum::ENABLED]);
  83. if ($job) throw new Exception('记录已经存在');
  84. // 生成执行记录
  85. $job = new HappinessWeightPoolJob();
  86. $job->mall_id = $mallId;
  87. $job->date_limit = $date_limit;
  88. $job->compute_period = $compute_period;
  89. $job->total_amount = 0;
  90. $job->period_total_amount = 0;
  91. $last_period = HappinessWeightPoolJob::find()->where(['mall_id' => $mallId, 'status' => StatusEnum::ENABLED])
  92. ->select('period')->orderBy('id DESC')->scalar();
  93. $period = $last_period ? $last_period + 1 : 1;
  94. $job->period = $period;
  95. if (!$job->save()) throw new Exception($job->getErrorMessage());
  96. $service = new WeightService();
  97. return $service->send($mallId, $config, $start_day, $end_day, $job->id);
  98. }
  99. public static function getExecutePeriod($config, $key, $mall_id)
  100. {
  101. $continue = false;
  102. $start_day = 0;
  103. $end_day = 0;
  104. // 获取结算周期,0=天,1=周,2=月
  105. $compute_period = $config[$key];
  106. switch ($config[$key]) {
  107. case 1:
  108. $start_day = date("Ymd", strtotime("-1 day"));
  109. // $start_day = date("Ymd");
  110. $end_day = $start_day;
  111. break;
  112. case 2:
  113. if (date('w') != 1) $continue = true; // 非周一直接退出
  114. $start_day = date("Ymd", strtotime("last Monday"));
  115. $end_day = date('Ymd', strtotime('-1 sunday', time()));;
  116. break;
  117. case 3:
  118. if (date('d') != 1) $continue = true; // 非当月1号直接退出 (测试时注释,否则需要改服务器时间)
  119. $start_day = date("Ym01", strtotime("-1 month"));
  120. $end_day = date('Ymd', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400);
  121. break;
  122. case 4:
  123. //自定义结算
  124. $has = HappinessWeightPoolJob::find()->where(['mall_id' => $mall_id])->orderBy('id desc')->one();//最后结算的
  125. if (!$has) {
  126. $has = HappinessWeightPoolOrderLog::find()->where(['mall_id' => $mall_id])->orderBy('id asc')
  127. ->one();//最先的订单
  128. }
  129. $start_day = null;
  130. if (!$has) {
  131. $start_day = date("YmdHis", time() - 86400);
  132. } else {
  133. $start_day = date("YmdHis", $has->created_at);
  134. }
  135. $end_day = date("YmdHis", time());
  136. break;
  137. }
  138. return compact('continue', 'start_day', 'end_day', 'compute_period');
  139. }
  140. protected function getLogPath($type)
  141. {
  142. return Yii::getAlias('@runtime') . "/logs/Happiness/" . $type . '/' . date('Y_m_d') . '.txt';
  143. }
  144. }