ConfigPeriodForm.php 6.9 KB

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