Period.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. <?php
  2. namespace common\models;
  3. use common\helpers\bonus\Calc\CalcConsole;
  4. use common\helpers\Date;
  5. use common\helpers\LoggerTool;
  6. use common\helpers\Tool;
  7. use Yii;
  8. use yii\db\Exception;
  9. /**
  10. * This is the model class for table "{{%PERIOD}}".
  11. *
  12. * @property string $ID
  13. * @property int $PERIOD_NUM 期数
  14. * @property int $CALC_MONTH 所在结算月
  15. * @property int $CALC_YEAR 所在结算年
  16. * @property int $START_TIME 期数开始时间戳
  17. * @property int $END_TIME 期数结束时间戳
  18. * @property int $IS_MONTH 是否结算月节点
  19. * @property int $IS_YEAR 是否结算年节点
  20. * @property int $IS_CLOSED 是否已封期
  21. * @property int $IS_PERFED 是否已生成业绩单
  22. * @property int $IS_CALCULATED 是否已结算
  23. * @property int $IS_SENT 是否已发放
  24. * @property int $IS_PERFING 是否正在生成业绩单
  25. * @property int $IS_CALCING 是否正在计算状态
  26. * @property int $IS_SENDING 是否正在挂网状态
  27. * @property int $CALC_PERCENT 结算进度
  28. * @property int $SENT_PERCENT 发放进度
  29. * @property string $PERF_ADMIN_ID 生成业绩单管理员
  30. * @property string $CLOSE_ADMIN_ID 手动封期管理员ID
  31. * @property string $CALC_ADMIN_ID 结算管理员ID
  32. * @property string $SENT_ADMIN_ID 发放管理员ID
  33. * @property int $CLOSED_AT 发放管理员ID
  34. * @property int $PERF_STARTED_AT 生成业绩单开始时间
  35. * @property int $PERFED_AT 生成业绩单结束时间
  36. * @property int $CALCULATE_STARTED_AT 结算开始时间
  37. * @property int $CALCULATED_AT 结算完成时间
  38. * @property int $SEND_STARTED_AT 发放开始时间
  39. * @property int $SENT_AT 发放完成时间
  40. * @property int $CREATED_AT 创建时间
  41. */
  42. class Period extends \common\components\ActiveRecord
  43. {
  44. const CALCULATE_NONE = 0;
  45. const CALCULATE_FINISH = 1;
  46. const CALCULATE_FAIL = 2;
  47. const SEND_NONE = 0;
  48. const SEND_FINISH = 1;
  49. const SEND_FAIL = 2;
  50. const PERF_NONE = 0;
  51. const PERF_FINISH = 1;
  52. const PERF_FAIL = 2;
  53. const SYSTEM_START_PERIOD_NUM = 100;
  54. const AUTO_EXEC_CALC = 1;
  55. const MANUAL_EXEC_CALC = 0;
  56. const IS_PROCESSING = 1;
  57. const NOT_PROCESSING = 0;
  58. const IS_PREPARING = 1;
  59. const NOT_PREPARING = 0;
  60. public $nowPeriodArr = null;
  61. public $periodNum = null;
  62. public $periodArr = null;
  63. public function init()
  64. {
  65. parent::init();
  66. }
  67. /**
  68. * @inheritdoc
  69. */
  70. public static function tableName()
  71. {
  72. return '{{%PERIOD}}';
  73. }
  74. /**
  75. * @inheritdoc
  76. */
  77. public function rules()
  78. {
  79. return [
  80. [['PERIOD_NUM', 'CALC_MONTH', 'CALC_YEAR', 'START_TIME', 'END_TIME', 'CREATED_AT'], 'required'],
  81. [['PERIOD_NUM', 'CALC_MONTH', 'CALC_YEAR', 'START_TIME', 'END_TIME', 'IS_MONTH', 'IS_YEAR', 'IS_CLOSED', 'IS_PERFED', 'IS_CALCULATED', 'IS_SENT', 'IS_PERFING', 'IS_CALCING', 'IS_SENDING', 'CALC_PERCENT', 'SENT_PERCENT', 'CLOSED_AT', 'PERF_STARTED_AT', 'PERFED_AT', 'CALCULATE_STARTED_AT', 'CALCULATED_AT', 'SEND_STARTED_AT', 'SENT_AT', 'CREATED_AT'], 'integer'],
  82. [['ID', 'PERF_ADMIN_ID', 'CLOSE_ADMIN_ID', 'CALC_ADMIN_ID', 'SENT_ADMIN_ID'], 'string', 'max' => 32],
  83. [['PERIOD_NUM'], 'unique'],
  84. [['ID'], 'unique'],
  85. ];
  86. }
  87. /**
  88. * @inheritdoc
  89. */
  90. public function attributeLabels()
  91. {
  92. return [
  93. 'ID' => 'ID',
  94. 'PERIOD_NUM' => '期数',
  95. 'CALC_MONTH' => '所在结算月',
  96. 'CALC_YEAR' => '所在结算年',
  97. 'START_TIME' => '期数开始时间戳',
  98. 'END_TIME' => '期数结束时间戳',
  99. 'IS_MONTH' => '是否结算月节点',
  100. 'IS_YEAR' => '是否结算年节点',
  101. 'IS_CLOSED' => '是否已封期',
  102. 'IS_PERFED' => '是否已生成业绩单',
  103. 'IS_CALCULATED' => '是否已结算',
  104. 'IS_SENT' => '是否已发放',
  105. 'IS_PERFING' => '是否正在生成业绩单',
  106. 'IS_CALCING' => '是否正在计算状态',
  107. 'IS_SENDING' => '是否正在挂网状态',
  108. 'CALC_PERCENT' => '结算进度',
  109. 'SENT_PERCENT' => '发放进度',
  110. 'PERF_ADMIN_ID' => '生成业绩单管理员',
  111. 'CLOSE_ADMIN_ID' => '手动封期管理员ID',
  112. 'CALC_ADMIN_ID' => '结算管理员ID',
  113. 'SENT_ADMIN_ID' => '发放管理员ID',
  114. 'CLOSED_AT' => '发放管理员ID',
  115. 'PERF_STARTED_AT' => '生成业绩单开始时间',
  116. 'PERFED_AT' => '生成业绩单结束时间',
  117. 'CALCULATE_STARTED_AT' => '结算开始时间',
  118. 'CALCULATED_AT' => '结算完成时间',
  119. 'SEND_STARTED_AT' => '发放开始时间',
  120. 'SENT_AT' => '发放完成时间',
  121. 'CREATED_AT' => '创建时间',
  122. ];
  123. }
  124. /**
  125. * 期数赋值给属性
  126. * @param int $periodNum
  127. * @return array|null|\yii\db\ActiveRecord
  128. */
  129. public function setPeriodNum($periodNum = null){
  130. if($periodNum === 0 || $periodNum === null || $periodNum === ''){
  131. //if($this->nowPeriodArr === null){
  132. $this->nowPeriodArr = static::find()->where('IS_CLOSED=0')->orderBy('PERIOD_NUM ASC')->asArray()->one();
  133. //}
  134. $this->periodArr = $this->nowPeriodArr;
  135. $periodNum = $this->nowPeriodArr['PERIOD_NUM'];
  136. } else {
  137. LoggerTool::debug(['calc-11', $periodNum, $this->periodNum]);
  138. if($periodNum !== $this->periodNum){
  139. $this->periodArr = static::findOneAsArray(['PERIOD_NUM'=>$periodNum]);
  140. }
  141. }
  142. $this->periodNum = $periodNum;
  143. return $this->periodArr;
  144. }
  145. // 通过期数,获取此期数据
  146. public static function getInfoByPeriodNum($periodNum) {
  147. return static::findOneAsArray(['PERIOD_NUM'=>$periodNum]);
  148. }
  149. /**
  150. * 是否存在所传期数
  151. * @param int $periodNum
  152. * @return bool
  153. */
  154. public static function isExistsPeriodNum(int $periodNum){
  155. return static::find()->where(['PERIOD_NUM'=>$periodNum])->exists();
  156. }
  157. /**
  158. * 获取当前未封期的最小期数
  159. * @return int
  160. */
  161. public function getNowPeriodNum(){
  162. $this->setPeriodNum();
  163. if($this->nowPeriodArr){
  164. return $this->nowPeriodArr['PERIOD_NUM'];
  165. } else {
  166. return self::SYSTEM_START_PERIOD_NUM;
  167. }
  168. }
  169. public function getTeamsPeriodNum(){
  170. $teamsPeriodNum = static::find()->where('IS_CLOSED=1 AND IS_SENT=0')->orderBy('PERIOD_NUM ASC')->asArray()->one();
  171. $this->setPeriodNum($teamsPeriodNum['PERIOD_NUM']);
  172. if($this->nowPeriodArr){
  173. return $this->nowPeriodArr['PERIOD_NUM'];
  174. } else {
  175. return self::SYSTEM_START_PERIOD_NUM;
  176. }
  177. }
  178. /**
  179. * 获取当前期的开始时间
  180. * @return int
  181. */
  182. public function getNowPeriodStart(){
  183. $this->setPeriodNum();
  184. if($this->nowPeriodArr){
  185. return $this->nowPeriodArr['START_TIME'];
  186. } else {
  187. return Date::nowTime();
  188. }
  189. }
  190. /**
  191. * 获取当前期的结束时间
  192. * @return int
  193. */
  194. public function getNowPeriodEnd(){
  195. $this->setPeriodNum();
  196. if($this->nowPeriodArr){
  197. return $this->nowPeriodArr['END_TIME'];
  198. } else {
  199. return Date::nowTime();
  200. }
  201. }
  202. /**
  203. * 获取当前所在的结算月
  204. * @return int
  205. */
  206. public function getNowMonth(){
  207. $this->setPeriodNum();
  208. if($this->nowPeriodArr){
  209. return $this->nowPeriodArr['CALC_MONTH'];
  210. } else {
  211. return 0;
  212. }
  213. }
  214. /**
  215. * 获取当前期数所在结算年
  216. * @return int
  217. */
  218. public function getNowYear(){
  219. $this->setPeriodNum();
  220. if($this->nowPeriodArr){
  221. return $this->nowPeriodArr['CALC_YEAR'];
  222. } else {
  223. return 0;
  224. }
  225. }
  226. /**
  227. * 当前期数所在年月
  228. * @return int|string
  229. */
  230. public function getNowYearMonth(){
  231. $this->setPeriodNum();
  232. if($this->nowPeriodArr){
  233. return $this->nowPeriodArr['CALC_YEAR'].Tool::numFix($this->nowPeriodArr['CALC_MONTH']);
  234. } else {
  235. return 0;
  236. }
  237. }
  238. public function getTeamsYearMonth($periodNum){
  239. LoggerTool::debug(['calc-12', $periodNum]);
  240. $this->setPeriodNum($periodNum);
  241. if($this->nowPeriodArr){
  242. return $this->nowPeriodArr['CALC_YEAR'].Tool::numFix($this->nowPeriodArr['CALC_MONTH']);
  243. } else {
  244. return 0;
  245. }
  246. }
  247. /**
  248. * 获取期数所在的结算月
  249. * @param int $periodNum
  250. * @return mixed
  251. * @throws Exception
  252. */
  253. public function getMonth($periodNum = null){
  254. $period = $this->setPeriodNum($periodNum);
  255. if($period){
  256. return $period['CALC_MONTH'];
  257. } else {
  258. throw new Exception('期数不存在');
  259. }
  260. }
  261. /**
  262. * 获取期数所在的结算年
  263. * @param $periodNum
  264. * @return mixed
  265. * @throws Exception
  266. */
  267. public function getYear($periodNum = null){
  268. $period = $this->setPeriodNum($periodNum);
  269. if($period){
  270. return $period['CALC_YEAR'];
  271. } else {
  272. throw new Exception('期数不存在');
  273. }
  274. }
  275. /**
  276. * 期数所在年月
  277. * @param $periodNum
  278. * @return string
  279. * @throws Exception
  280. */
  281. public function getYearMonth($periodNum = null){
  282. $period = $this->setPeriodNum($periodNum);
  283. if($period){
  284. return $period['CALC_YEAR'].Tool::numFix($period['CALC_MONTH'], 2);
  285. } else {
  286. throw new Exception('期数不存在'.$periodNum);
  287. }
  288. }
  289. /**
  290. * 所传年、月所有期数的数量
  291. * @param $year
  292. * @param $month
  293. * @return int
  294. */
  295. public function getYearMonthAllPeriodNumCount($year, $month){
  296. return Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->count(1);
  297. }
  298. /**
  299. * 是否是结算月节点
  300. * @param $periodNum
  301. * @return mixed
  302. * @throws Exception
  303. */
  304. public function isCalcMonth($periodNum = null){
  305. $period = $this->setPeriodNum($periodNum);
  306. if($period){
  307. return $period['IS_MONTH'];
  308. } else {
  309. throw new Exception('期数不存在');
  310. }
  311. }
  312. /**
  313. * 是否是结算年节点
  314. * @param $periodNum
  315. * @return mixed
  316. * @throws Exception
  317. */
  318. public function isCalcYear($periodNum = null){
  319. $period = $this->setPeriodNum($periodNum);
  320. if($period){
  321. return $period['IS_YEAR'];
  322. } else {
  323. throw new Exception('期数不存在');
  324. }
  325. }
  326. /**
  327. * 获取挂网的最大期数
  328. * @return int
  329. */
  330. public function getMaxIsSentPeriodNum() {
  331. $maxIsSentData = static::find()->where('IS_SENT=1')->orderBy('PERIOD_NUM DESC')->asArray()->one();
  332. $maxIsSentPeriodNum = self::SYSTEM_START_PERIOD_NUM;
  333. if( $maxIsSentData && $maxIsSentData["PERIOD_NUM"] ) {
  334. $maxIsSentPeriodNum = $maxIsSentData["PERIOD_NUM"];
  335. }
  336. return $maxIsSentPeriodNum;
  337. }
  338. /**
  339. * 返回所传结算月包含的所有期数
  340. * @param $year
  341. * @param $month
  342. * @return array|\yii\db\ActiveRecord[]
  343. */
  344. public static function getPeriodNumsFromMonth($year, $month){
  345. return static::find()->select('PERIOD_NUM')->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  346. }
  347. /**
  348. * 获取所传结算月的最小期数和最大期数
  349. * @param $year
  350. * @param $month
  351. * @return array
  352. */
  353. public static function getPeriodNumRangeFromMonth($year, $month){
  354. $allPeriod = self::getPeriodNumsFromMonth($year, $month);
  355. $tempPeriod = [];
  356. foreach($allPeriod as $period){
  357. $tempPeriod[] = $period['PERIOD_NUM'];
  358. }
  359. if(!$tempPeriod){
  360. return null;
  361. }
  362. $minPeriod = min($tempPeriod);
  363. $maxPeriod = max($tempPeriod);
  364. return ['min'=>$minPeriod, 'max'=>$maxPeriod];
  365. }
  366. /**
  367. * 返回结算月的节点期数
  368. * @param $year
  369. * @param $month
  370. * @return mixed|null
  371. */
  372. public static function getPeriodNumPointFromMonth($year, $month) {
  373. $data = static::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_MONTH=1', [':CALC_YEAR'=>$year, ':CALC_MONTH' => $month])->asArray()->one();
  374. if($data) return $data['PERIOD_NUM'];
  375. else return null;
  376. }
  377. /**
  378. * 返回结算月的节点期
  379. * @param $year
  380. * @param $month
  381. * @return mixed|null
  382. */
  383. public static function getPeriodPointFromMonth($year, $month) {
  384. $data = static::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_MONTH=1', [':CALC_YEAR'=>$year, ':CALC_MONTH' => $month])->asArray()->one();
  385. if($data) return $data;
  386. else return null;
  387. }
  388. /**
  389. * 是否已经封期
  390. * @param $periodNum
  391. * @return bool
  392. * @throws Exception
  393. */
  394. public function isClosed($periodNum = null){
  395. $period = $this->setPeriodNum($periodNum);
  396. if($period){
  397. return boolval($period['IS_CLOSED']);
  398. } else {
  399. throw new Exception('期数不存在');
  400. }
  401. }
  402. /**
  403. * 是否已经生成业绩单
  404. * @param $periodNum
  405. * @return bool
  406. * @throws Exception
  407. */
  408. public function isPerfed($periodNum = null){
  409. $period = $this->setPeriodNum($periodNum);
  410. if($period){
  411. return boolval($period['IS_PERFED']);
  412. } else {
  413. throw new Exception('期数不存在');
  414. }
  415. }
  416. /**
  417. * 是否已结算
  418. * @param null $periodNum
  419. * @return bool
  420. * @throws Exception
  421. */
  422. public function isCalculated($periodNum = null){
  423. $period = $this->setPeriodNum($periodNum);
  424. if($period){
  425. return boolval($period['IS_CALCULATED'] == self::CALCULATE_FINISH);
  426. } else {
  427. throw new Exception('期数不存在');
  428. }
  429. }
  430. /**
  431. * 是否可被封期
  432. * @param $periodNum
  433. * @return bool
  434. */
  435. public function isCanClose($periodNum = null){
  436. $period = $this->setPeriodNum($periodNum);
  437. if($period['IS_CLOSED']){
  438. return false;
  439. }
  440. $previousPeriod = static::findOne(['PERIOD_NUM'=>$this->periodNum-1]);
  441. if($previousPeriod){
  442. if(!$previousPeriod['IS_CLOSED']){
  443. return false;
  444. }
  445. // 只要上期封期了,下期就可以封期
  446. // if(!$previousPeriod['IS_CALCULATED']){
  447. // return false;
  448. // }
  449. // if(!$previousPeriod['IS_SENT']){
  450. // return false;
  451. // }
  452. }
  453. return true;
  454. }
  455. /**
  456. * 是否可以生成业绩单
  457. * @param $periodNum
  458. * @return bool
  459. */
  460. public function isCanPerf($periodNum = null){
  461. $period = $this->setPeriodNum($periodNum);
  462. // 查看该期是否已经封期
  463. if(!$period['IS_CLOSED']){
  464. return false;
  465. }
  466. // 已经挂网,不能生成业绩单
  467. if($period['IS_SENT'] == self::SEND_FINISH){
  468. return false;
  469. }
  470. //上1期奖金未发放,限制不能生成业绩单
  471. if(!$this->isLastSent($this->periodNum)){
  472. return false;
  473. }
  474. return true;
  475. }
  476. /**
  477. * 是否可以结算
  478. * @param $periodNum
  479. * @return bool
  480. */
  481. public function isCanCalc($periodNum = null){
  482. $period = $this->setPeriodNum($periodNum);
  483. // 查看该期是否已经封期
  484. if(!$period['IS_CLOSED']){
  485. return false;
  486. }
  487. // 还没有生成业绩单,不能封期
  488. if(!$period['IS_PERFED'] || $period['IS_PERFED'] == self::PERF_FAIL){
  489. return false;
  490. }
  491. if($period['IS_SENT'] == self::SEND_FINISH){
  492. return false;
  493. }
  494. // $previousPeriod = static::findOneAsArray(['PERIOD_NUM'=>$this->periodNum-1]);
  495. // if($previousPeriod){
  496. // // 查看上一期是否已经挂网,上一期挂网了,本期才能结算
  497. // if(!$previousPeriod['IS_SENT']){
  498. // return false;
  499. // }
  500. // }
  501. //上1期奖金未发放,限制不能结算
  502. if(!$this->isLastSent($this->periodNum)){
  503. return false;
  504. }
  505. return true;
  506. }
  507. /**
  508. * 是否可以挂网
  509. * @param $periodNum
  510. * @return bool
  511. */
  512. public function isCanSend($periodNum = null){
  513. $period = $this->setPeriodNum($periodNum);
  514. if(!$period['IS_CLOSED']){
  515. return false;
  516. }
  517. if(!$period['IS_CALCULATED'] || $period['IS_CALCULATED'] == self::CALCULATE_FAIL){
  518. return false;
  519. }
  520. if($period['IS_SENT'] == self::SEND_FINISH){
  521. return false;
  522. }
  523. //上1期奖金未发放,限制不能挂网
  524. if(!$this->isLastSent($this->periodNum)){
  525. return false;
  526. }
  527. return true;
  528. }
  529. /**
  530. * 是否已经挂网
  531. * @param $periodNum
  532. * @return bool
  533. */
  534. public function isSent($periodNum = null){
  535. $period = $this->setPeriodNum($periodNum);
  536. if($period['IS_SENT'] == self::SEND_FINISH) return true;
  537. return false;
  538. }
  539. /**
  540. * 上期是否挂网
  541. * @param null $periodNum
  542. * @return bool
  543. */
  544. public function isLastSent($periodNum){
  545. $period = $this->setPeriodNum($periodNum-1);
  546. if ($period) {
  547. if($period['IS_SENT'] == self::SEND_FINISH) {
  548. return true;
  549. } else {
  550. return false;
  551. }
  552. }
  553. return false;
  554. }
  555. /**
  556. * 获取上月的年月
  557. * @param $periodNum
  558. * @return array
  559. */
  560. public function getLastMonth($periodNum = null){
  561. $period = $this->setPeriodNum($periodNum);
  562. if($period){
  563. $year = $period['CALC_YEAR'];
  564. $month = $period['CALC_MONTH'];
  565. $lastYear = Date::lastMonth($year.'-'.$month, 'Y');
  566. $lastMonth = Date::lastMonth($year.'-'.$month, 'm');
  567. return [
  568. 'year' => $lastYear,
  569. 'month' => intval($lastMonth),
  570. 'yearMonth' => $lastYear.Tool::numFix($lastMonth, 2),
  571. ];
  572. } else {
  573. return [
  574. 'year' => 0,
  575. 'month' => 0,
  576. 'yearMonth' => 0,
  577. ];
  578. }
  579. }
  580. /**
  581. * 上几个月的年月
  582. * @param int $num
  583. * @param null $periodNum
  584. * @return array
  585. */
  586. public function getLastNumMonth($num=1,$periodNum = null){
  587. $period = $this->setPeriodNum($periodNum);
  588. if($period){
  589. $year = $period['CALC_YEAR'];
  590. $month = $period['CALC_MONTH'];
  591. $lastYear = Date::lastNumMonth($num,$year.'-'.$month, 'Y');
  592. $lastMonth = Date::lastNumMonth($num,$year.'-'.$month, 'm');
  593. return [
  594. 'year' => $lastYear,
  595. 'month' => intval($lastMonth),
  596. 'yearMonth' => $lastYear.Tool::numFix($lastMonth, 2),
  597. ];
  598. } else {
  599. return [
  600. 'year' => 0,
  601. 'month' => 0,
  602. 'yearMonth' => 0,
  603. ];
  604. }
  605. }
  606. /**
  607. * 下月的年月
  608. * @param null $periodNum
  609. * @return array
  610. */
  611. public function getNextMonth($periodNum = null){
  612. $period = $this->setPeriodNum($periodNum);
  613. if($period){
  614. $year = $period['CALC_YEAR'];
  615. $month = $period['CALC_MONTH'];
  616. $nextYear = Date::nextMonth($year.'-'.$month, 'Y');
  617. $nextMonth = Date::nextMonth($year.'-'.$month, 'm');
  618. return [
  619. 'year' => $nextYear,
  620. 'month' => intval($nextMonth),
  621. 'yearMonth' => $nextYear.Tool::numFix($nextMonth, 2),
  622. ];
  623. } else {
  624. return [
  625. 'year' => 0,
  626. 'month' => 0,
  627. 'yearMonth' => 0,
  628. ];
  629. }
  630. }
  631. /**
  632. * 通过时间戳获取期
  633. * @param $time
  634. * @return array|null|\yii\db\ActiveRecord
  635. */
  636. public static function getPeriodFromTime($time){
  637. return static::find()->where('START_TIME<=:TIME AND END_TIME>:TIME', [':TIME'=>$time])->asArray()->one();
  638. }
  639. /**
  640. * 通过时间戳范围获取期间内的期
  641. * @param $minTime
  642. * @param $maxTime
  643. * @return array|\yii\db\ActiveRecord[]
  644. */
  645. public static function getPeriodFromTimeRange($minTime, $maxTime){
  646. return static::find()->where('END_TIME>:MIN_TIME AND START_TIME<:MAX_TIME', [':MIN_TIME'=>$minTime, ':MAX_TIME'=>$maxTime])->orderBy('PERIOD_NUM ASC')->asArray()->all();
  647. }
  648. /**
  649. * 获取近几期的期数
  650. * @param $num
  651. * @return array
  652. */
  653. public static function getNearlyPeriodNum($num){
  654. // 获取当前期
  655. $period = self::instance();
  656. $periodNum = $period->getNowPeriodNum();
  657. // 获取前$num期的期数
  658. $periodNums = [
  659. $periodNum
  660. ];
  661. for($i=0;$i<$num;$i++){
  662. if($periodNum - $i > 0){
  663. // 查看该期是否存在
  664. if(self::isExistsPeriodNum($periodNum - $i)){
  665. $periodNums[] = $periodNum - $i;
  666. }
  667. }
  668. }
  669. return array_reverse($periodNums);
  670. }
  671. /**
  672. * 获取近几期的挂网期数
  673. * @param $num
  674. * @return array
  675. */
  676. public static function getNearlySendPeriodNum($num){
  677. // 获取当前期
  678. $period = self::instance();
  679. $periodNum = $period->sentMaxPeriodNum();
  680. // 获取前$num期的期数
  681. $periodNums = [
  682. $periodNum
  683. ];
  684. for($i=0;$i<$num;$i++){
  685. if($periodNum - $i > 0){
  686. // 查看该期是否存在
  687. if(self::isExistsPeriodNum($periodNum - $i)){
  688. $periodNums[] = $periodNum - $i;
  689. }
  690. }
  691. }
  692. return array_reverse($periodNums);
  693. }
  694. /**
  695. * 获取近几期的年月
  696. * @param $num
  697. * @return array
  698. * @throws Exception
  699. */
  700. public static function getNearlyPeriodYearMonth($num){
  701. // 获取当前期
  702. $period = self::instance();
  703. $periodNum = $period->getNowPeriodNum();
  704. $nowYearMonth = $period->getNowYearMonth();
  705. // 获取前$num期的期数
  706. $periodYearMonths = [];
  707. for($i=1;$i<=$num;$i++){
  708. if($periodNum - $i > 0){
  709. // 查看该期是否存在
  710. if(self::isExistsPeriodNum($periodNum - $i)){
  711. $yearMonth = $period->getYearMonth($periodNum - $i);
  712. $periodYearMonths[] = [
  713. 'yearMonth' => $yearMonth,
  714. 'periodNum' => $periodNum - $i,
  715. ];
  716. }
  717. }
  718. }
  719. return array_reverse($periodYearMonths);
  720. }
  721. /**
  722. * 获取本月所有已经挂网的期数
  723. * @param $yearMonth
  724. * @return array
  725. */
  726. public static function monthSentAllPeriodNum($yearMonth){
  727. $year = intval(substr($yearMonth, 0, 4));
  728. $month = intval(substr($yearMonth, 4, 2));
  729. $allPeriod = Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_SENT=1', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->select('PERIOD_NUM')->asArray()->all();
  730. $result = [];
  731. if($allPeriod){
  732. foreach($allPeriod as $period){
  733. $result[] = $period['PERIOD_NUM'];
  734. }
  735. }
  736. return $result;
  737. }
  738. /**
  739. * 所传月份所有已结算的期数
  740. * @param $yearMonth
  741. * @return array
  742. */
  743. public static function monthCalcAllPeriodNum($yearMonth){
  744. $year = intval(substr($yearMonth, 0, 4));
  745. $month = intval(substr($yearMonth, 4, 2));
  746. $allPeriod = Period::find()->where('CALC_YEAR=:CALC_YEAR AND CALC_MONTH=:CALC_MONTH AND IS_CALCING=1', [':CALC_YEAR'=>$year, ':CALC_MONTH'=>$month])->select('PERIOD_NUM')->asArray()->all();
  747. $result = [];
  748. if($allPeriod){
  749. foreach($allPeriod as $period){
  750. $result[] = $period['PERIOD_NUM'];
  751. }
  752. }
  753. return $result;
  754. }
  755. /**
  756. * 获取已结算的最大期数
  757. * @return mixed
  758. */
  759. public static function calculatedMaxPeriodNum(){
  760. return static::find()->where('IS_CALCULATED=1')->max('PERIOD_NUM');
  761. }
  762. /**
  763. * 已挂网的最大期数
  764. * @return mixed
  765. */
  766. public static function sentMaxPeriodNum(){
  767. return static::find()->where('IS_SENT=1')->max('PERIOD_NUM');
  768. }
  769. /**
  770. * 期数结束时间
  771. * @param $periodNum
  772. * @return int|mixed
  773. */
  774. public static function getEndTime($periodNum) {
  775. $endTime = static::find()->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $periodNum])->select('END_TIME')->asArray()->one();
  776. return $endTime['END_TIME'] ?? 0;
  777. }
  778. /**
  779. * 到年底的剩余月份
  780. * @param null $periodNum
  781. * @return int
  782. * @throws Exception
  783. */
  784. public static function getMonthLeft($periodNum = null) {
  785. $period = self::instance();
  786. if ($periodNum) {
  787. $nowMonth = $period->getMonth($periodNum);
  788. } else {
  789. $nowMonth = $period->getNowMonth();
  790. }
  791. return 12 - (int)$nowMonth + 1;
  792. }
  793. /**
  794. * 起始期数和结束期数几个月
  795. * @param $startPeriodNum
  796. * @param $endPeriodNum
  797. * @return int|string
  798. */
  799. public static function getMonthNum($startPeriodNum,$endPeriodNum){
  800. return self::find()->select('CALC_MONTH')->groupBy('CALC_MONTH')->where('PERIOD_NUM>=:START_PERIOD AND PERIOD_NUM<=:END_PERIOD',[':START_PERIOD'=>$startPeriodNum,':END_PERIOD'=>$endPeriodNum])->count();
  801. }
  802. /**
  803. * 从期数获取当前月的所有期
  804. *
  805. */
  806. public static function getCurrentMonthPeriodByPeriodNum($periodNum){
  807. $currentPeriod = self::getInfoByPeriodNum($periodNum);
  808. $currentYear = $currentPeriod['CALC_YEAR'];
  809. $currentMonth = $currentPeriod['CALC_MONTH'];
  810. $periodsArray = self::findAllAsArray(['CALC_YEAR'=>$currentYear,'CALC_MONTH'=>$currentMonth]);
  811. $periods = [];
  812. foreach ($periodsArray as $p){
  813. $periods[] = $p['PERIOD_NUM'];
  814. }
  815. return $periods;
  816. }
  817. public static function getCurrentQuarterPeriodByPeriodNum($periodNum){
  818. $currentPeriod = self::getInfoByPeriodNum($periodNum);
  819. $currentYear = $currentPeriod['CALC_YEAR'];
  820. $currentMonth = $currentPeriod['CALC_MONTH'];
  821. $periodsArray = self::find()->select("*")
  822. ->where(['CALC_YEAR'=>$currentYear])
  823. ->andWhere(['in', 'CALC_MONTH', [$currentMonth-2, $currentMonth-1, $currentMonth]])
  824. ->asArray()->all();
  825. $periods = [];
  826. foreach ($periodsArray as $p){
  827. $periods[] = $p['PERIOD_NUM'];
  828. }
  829. return $periods;
  830. }
  831. public static function updatePeriodIsAutoExec($periodNum, $isAutoExec = 0): int
  832. {
  833. return Period::updateAll(['AUTO_EXEC' => $isAutoExec], ['PERIOD_NUM' => $periodNum]);
  834. }
  835. public static function updatePeriodIsProcessing($periodNum, $isProcessing = 1): int
  836. {
  837. return Period::updateAll(['IS_PROCESSING' => $isProcessing], ['PERIOD_NUM' => $periodNum]);
  838. }
  839. public static function updatePeriodIsPreparing($periodNum, $isPreparing = 1): int
  840. {
  841. return Period::updateAll(['IS_PREPARING' => $isPreparing], ['PERIOD_NUM' => $periodNum]);
  842. }
  843. /**
  844. * 是否计算操作中
  845. * @param $periodNum
  846. * @return bool
  847. */
  848. public static function isProcessing($periodNum = null): bool
  849. {
  850. $db = CalcConsole::CALC_DB_NAME;
  851. $allPeriod = \Yii::$app->$db->createCommand("SELECT * FROM AR_PERIOD order by PERIOD_NUM desc")->queryAll();
  852. $calcPeriod = [];
  853. foreach ($allPeriod as $v) {
  854. if ($v['IS_PREPARE'] > 0) {
  855. $calcPeriod = $v;
  856. break;
  857. }
  858. }
  859. LoggerTool::debug('calc-l');
  860. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  861. // 若计算服务中CALC_ID不为空,则不允许计算
  862. LoggerTool::debug(['calc-2', $calcPeriod]);
  863. if ($calcPeriod) {
  864. if ($calcPeriod['CALC_ID']) return true;
  865. }
  866. LoggerTool::debug('calc-3');
  867. //挂网则拒绝操作,返回true则拒绝操作
  868. if ($period['IS_SENT'] == self::SEND_FINISH) return true;
  869. LoggerTool::debug('calc-4');
  870. if ($period['IS_PROCESSING'] == self::IS_PROCESSING) return true;
  871. LoggerTool::debug('calc-5');
  872. return false;
  873. }
  874. /**
  875. * 是否预计算操作中
  876. * @param $periodNum
  877. * @return bool
  878. */
  879. public static function isPreparing($periodNum = null): bool
  880. {
  881. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  882. //挂网则拒绝操作,返回true则拒绝操作
  883. if ($period['IS_SENT'] == self::SEND_FINISH) return true;
  884. if ($period['IS_PREPARING'] == self::IS_PREPARING) return true;
  885. $nowTs = time();
  886. if ($nowTs-6 > $period['END_TIME']) return true;
  887. return false;
  888. }
  889. public static function checkPerf($periodNum){
  890. $result = static::find()
  891. ->where(
  892. 'PERIOD_NUM=:PERIOD_NUM ', [':PERIOD_NUM' => $periodNum]
  893. )
  894. ->asArray()
  895. ->one();
  896. if (isset($result['IS_PERFED']) && $result['IS_PERFED'] == 1) {
  897. return true;
  898. }
  899. return false;
  900. }
  901. public static function checkClose($periodNum){
  902. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  903. if ($period['IS_CLOSED'] == 1) {
  904. return true;
  905. }
  906. return false;
  907. }
  908. }