Period.php 31 KB

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