ConfigPeriodForm.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\helpers\Form;
  6. use common\libs\logging\operate\AdminOperate;
  7. use common\models\Config;
  8. use common\models\Period;
  9. use yii\db\Exception;
  10. /**
  11. * Login form
  12. */
  13. class ConfigPeriodForm extends Model
  14. {
  15. public $calcYear;
  16. public $closeWeekDate;
  17. public $closeTime;
  18. public function init() {
  19. parent::init();
  20. $this->adminOperateLogger = new AdminOperate([
  21. 'fetchClass' => Config::class,
  22. ]);
  23. }
  24. /**
  25. * @inheritdoc
  26. */
  27. public function rules()
  28. {
  29. return [
  30. [['calcYear', 'closeWeekDate', 'closeTime'], 'required'],
  31. [['calcYear'], 'integer', 'max'=>2050, 'min'=>2018],
  32. [['closeWeekDate'], 'match', 'pattern' => '/^[0-6]/', 'message' => '请填写有效的星期'],
  33. [['closeTime'], 'time', 'format'=>'HH:mm'],
  34. ];
  35. }
  36. public function attributeLabels()
  37. {
  38. return [
  39. 'calcYear' => '计算至年份',
  40. 'closeWeekDate' => '封期日期',
  41. 'closeTime' => '封期时间',
  42. ];
  43. }
  44. /**
  45. * 更新配置
  46. * @return bool|null
  47. * @throws Exception
  48. */
  49. public function update(){
  50. if(!$this->validate()){
  51. return null;
  52. }
  53. $configs = Config::find()->where("TYPE='period'")->orderBy('SORT ASC')->indexBy('CONFIG_NAME')->asArray()->all();
  54. $configNames = [];
  55. foreach ($configs as $value){
  56. $configNames[] = $value['CONFIG_NAME'];
  57. }
  58. $beforeData = Config::getConfigByType('period');
  59. $this->adminOperateLogger->saveBeforeContent=$beforeData;
  60. $transaction = \Yii::$app->db->beginTransaction();
  61. try{
  62. if(!Config::updateAll(['VALUE'=>$this->calcYear, 'UPDATED_AT'=>Date::nowTime()], "CONFIG_NAME='calcYear'")){
  63. throw new Exception('计算至年份更新失败');
  64. }
  65. if(!Config::updateAll(['VALUE'=>$this->closeWeekDate, 'UPDATED_AT'=>Date::nowTime()], "CONFIG_NAME='closeWeekDate'")){
  66. throw new Exception('封期日期更新失败');
  67. }
  68. if(!Config::updateAll(['VALUE'=>$this->closeTime, 'UPDATED_AT'=>Date::nowTime()], "CONFIG_NAME='closeTime'")){
  69. throw new Exception('封期时间更新失败');
  70. }
  71. // 异步重新生成业绩期先注释掉
  72. // 异步处理添加任务
  73. //$taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle('config/update-period', \Yii::$app->request->post());
  74. $taskKey = true;
  75. if($taskKey === false){
  76. throw new Exception('请求异步服务器失败');
  77. }
  78. $transaction->commit();
  79. } catch (Exception $e){
  80. $transaction->rollBack();
  81. $this->addError('closeWeekDate', $e->getMessage());
  82. return null;
  83. }
  84. $afterData = Config::getConfigByType('period');
  85. $this->adminOperateLogger->saveAfterContent=$afterData;
  86. unset($beforeData,$afterData);
  87. $this->adminOperateLogger->clean()->save([
  88. 'optType' => '更新期数配置',
  89. ]);
  90. return true;
  91. }
  92. /**
  93. * 异步更新期数表
  94. * @return bool
  95. * @throws Exception
  96. */
  97. public function updateAsync(){
  98. if(!$this->validate()){
  99. return false;
  100. }
  101. $period = Period::instance();
  102. // 获取当前期数
  103. $periodNum = $period->getNowPeriodNum();
  104. // 把所有没封期的期数都删掉只保留当前期
  105. Period::deleteAll('IS_CLOSED=0 AND PERIOD_NUM<>:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum]);
  106. //$nowPeriodStart = $period->getNowPeriodStart();
  107. $periodEnd = $period->getNowPeriodEnd();
  108. // 开始时间归到0点
  109. $nowPeriodStart = $periodEnd + 1;
  110. // 循环到2030年的所有日期
  111. $nowPeriodNum = $periodNum + 1;
  112. $startDateTemp = $nowPeriodStart;
  113. $transaction = \Yii::$app->db->beginTransaction();
  114. try{
  115. for($startDate = $nowPeriodStart + 86400; $startDate < strtotime($this->calcYear.'-12-31'); $startDate += 86400 ){
  116. if(Date::dateWeek(\date('Y-m-d',$startDate)) == $this->closeWeekDate){
  117. $periodModel = new Period();
  118. $periodModel->PERIOD_NUM = $nowPeriodNum;
  119. $periodModel->START_TIME = $startDateTemp;
  120. $periodModel->END_TIME = Date::dayMinute($startDate, $this->closeTime) - 1;
  121. $periodModel->CALC_MONTH = date('m', $periodModel->END_TIME);
  122. $periodModel->CALC_YEAR = date('Y', $periodModel->END_TIME);
  123. $periodModel->CREATED_AT = Date::nowTime();
  124. if(!$periodModel->save()){
  125. throw new Exception(Form::formatErrorsForApi($periodModel->getErrors()));
  126. };
  127. $startDateTemp = $periodModel->END_TIME+1;
  128. // 查看上一期的月份是否比本期的月份不等,则把上一期设置为月结点
  129. $lastPeriod = Period::findOne(['PERIOD_NUM'=>$nowPeriodNum-1]);
  130. if($lastPeriod){
  131. if($lastPeriod['CALC_MONTH'] != $periodModel->CALC_MONTH){
  132. $lastPeriod->IS_MONTH = 1;
  133. }
  134. // 查看上一期的年份是否比本期的不等,则把上一期的设置为年节点
  135. if($lastPeriod['CALC_YEAR'] != $periodModel->CALC_YEAR){
  136. $lastPeriod->IS_YEAR = 1;
  137. }
  138. if($lastPeriod->IS_MONTH || $lastPeriod->IS_YEAR){
  139. if(!$lastPeriod->save()){
  140. throw new Exception(Form::formatErrorsForApi($lastPeriod->getErrors()));
  141. }
  142. }
  143. }
  144. $periodModel = null;
  145. $lastPeriod = null;
  146. unset($periodModel, $lastPeriod);
  147. $nowPeriodNum += 1;
  148. //echo date('Y-m-d', $startDate).PHP_EOL;
  149. }
  150. }
  151. $transaction->commit();
  152. } catch (Exception $e){
  153. $transaction->rollBack();
  154. $this->addError('update', $e->getMessage());
  155. return false;
  156. }
  157. return true;
  158. }
  159. }