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