Period.php 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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['IS_CLOSED']){
  511. // return false;
  512. // }
  513. // // 还没有生成业绩单,不能封期
  514. // if(!$period['IS_PERFED'] || $period['IS_PERFED'] == self::PERF_FAIL){
  515. // return false;
  516. // }
  517. // if($period['IS_SENT'] == self::SEND_FINISH){
  518. // return false;
  519. // }
  520. //// $previousPeriod = static::findOneAsArray(['PERIOD_NUM'=>$this->periodNum-1]);
  521. //// if($previousPeriod){
  522. //// // 查看上一期是否已经挂网,上一期挂网了,本期才能结算
  523. //// if(!$previousPeriod['IS_SENT']){
  524. //// return false;
  525. //// }
  526. //// }
  527. // //上1期奖金未发放,限制不能结算
  528. // if(!$this->isLastSent($this->periodNum)){
  529. // return false;
  530. // }
  531. return true;
  532. }
  533. /**
  534. * 是否可以挂网
  535. * @param $periodNum
  536. * @return bool
  537. */
  538. public function isCanSend($periodNum = null){
  539. $period = $this->setPeriodNum($periodNum);
  540. // if(!$period['IS_CLOSED']){
  541. // return false;
  542. // }
  543. if(!$period['IS_CALCULATED'] || $period['IS_CALCULATED'] == self::CALCULATE_FAIL){
  544. return false;
  545. }
  546. if($period['IS_SENT'] == self::SEND_FINISH){
  547. return false;
  548. }
  549. //上1期奖金未发放,限制不能挂网
  550. if(!$this->isLastSent($this->periodNum)){
  551. return false;
  552. }
  553. return true;
  554. }
  555. /**
  556. * 是否已经挂网
  557. * @param $periodNum
  558. * @return bool
  559. */
  560. public function isSent($periodNum = null){
  561. $period = $this->setPeriodNum($periodNum);
  562. if($period['IS_SENT'] == self::SEND_FINISH) return true;
  563. return false;
  564. }
  565. /**
  566. * 上期是否挂网
  567. * @param null $periodNum
  568. * @return bool
  569. */
  570. public function isLastSent($periodNum){
  571. $period = $this->setPeriodNum($periodNum-1);
  572. if ($period) {
  573. if($period['IS_SENT'] == self::SEND_FINISH) {
  574. return true;
  575. } else {
  576. return false;
  577. }
  578. }
  579. return false;
  580. }
  581. /**
  582. * 获取上月的年月
  583. * @param $periodNum
  584. * @return array
  585. */
  586. public function getLastMonth($periodNum = null){
  587. $period = $this->setPeriodNum($periodNum);
  588. if($period){
  589. $year = $period['CALC_YEAR'];
  590. $month = $period['CALC_MONTH'];
  591. $lastYear = Date::lastMonth($year.'-'.$month, 'Y');
  592. $lastMonth = Date::lastMonth($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 int $num
  609. * @param null $periodNum
  610. * @return array
  611. */
  612. public function getLastNumMonth($num=1,$periodNum = null){
  613. $period = $this->setPeriodNum($periodNum);
  614. if($period){
  615. $year = $period['CALC_YEAR'];
  616. $month = $period['CALC_MONTH'];
  617. $lastYear = Date::lastNumMonth($num,$year.'-'.$month, 'Y');
  618. $lastMonth = Date::lastNumMonth($num,$year.'-'.$month, 'm');
  619. return [
  620. 'year' => $lastYear,
  621. 'month' => intval($lastMonth),
  622. 'yearMonth' => $lastYear.Tool::numFix($lastMonth, 2),
  623. ];
  624. } else {
  625. return [
  626. 'year' => 0,
  627. 'month' => 0,
  628. 'yearMonth' => 0,
  629. ];
  630. }
  631. }
  632. /**
  633. * 下月的年月
  634. * @param null $periodNum
  635. * @return array
  636. */
  637. public function getNextMonth($periodNum = null){
  638. $period = $this->setPeriodNum($periodNum);
  639. if($period){
  640. $year = $period['CALC_YEAR'];
  641. $month = $period['CALC_MONTH'];
  642. $nextYear = Date::nextMonth($year.'-'.$month, 'Y');
  643. $nextMonth = Date::nextMonth($year.'-'.$month, 'm');
  644. return [
  645. 'year' => $nextYear,
  646. 'month' => intval($nextMonth),
  647. 'yearMonth' => $nextYear.Tool::numFix($nextMonth, 2),
  648. ];
  649. } else {
  650. return [
  651. 'year' => 0,
  652. 'month' => 0,
  653. 'yearMonth' => 0,
  654. ];
  655. }
  656. }
  657. /**
  658. * 通过时间戳获取期
  659. * @param $time
  660. * @return array|null|\yii\db\ActiveRecord
  661. */
  662. public static function getPeriodFromTime($time){
  663. return static::find()->where('START_TIME<=:TIME AND END_TIME>:TIME', [':TIME'=>$time])->asArray()->one();
  664. }
  665. /**
  666. * 通过时间戳范围获取期间内的期
  667. * @param $minTime
  668. * @param $maxTime
  669. * @return array|\yii\db\ActiveRecord[]
  670. */
  671. public static function getPeriodFromTimeRange($minTime, $maxTime){
  672. 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();
  673. }
  674. /**
  675. * 获取近几期的期数
  676. * @param $num
  677. * @return array
  678. */
  679. public static function getNearlyPeriodNum($num){
  680. // 获取当前期
  681. $period = self::instance();
  682. $periodNum = $period->getNowPeriodNum();
  683. // 获取前$num期的期数
  684. $periodNums = [
  685. $periodNum
  686. ];
  687. for($i=0;$i<$num;$i++){
  688. if($periodNum - $i > 0){
  689. // 查看该期是否存在
  690. if(self::isExistsPeriodNum($periodNum - $i)){
  691. $periodNums[] = $periodNum - $i;
  692. }
  693. }
  694. }
  695. return array_reverse($periodNums);
  696. }
  697. /**
  698. * 获取近几期的挂网期数
  699. * @param $num
  700. * @return array
  701. */
  702. public static function getNearlySendPeriodNum($num){
  703. // 获取当前期
  704. $period = self::instance();
  705. $periodNum = $period->sentMaxPeriodNum();
  706. // 获取前$num期的期数
  707. $periodNums = [
  708. $periodNum
  709. ];
  710. for($i=0;$i<$num;$i++){
  711. if($periodNum - $i > 0){
  712. // 查看该期是否存在
  713. if(self::isExistsPeriodNum($periodNum - $i)){
  714. $periodNums[] = $periodNum - $i;
  715. }
  716. }
  717. }
  718. return array_reverse($periodNums);
  719. }
  720. /**
  721. * 获取近几期的年月
  722. * @param $num
  723. * @return array
  724. * @throws Exception
  725. */
  726. public static function getNearlyPeriodYearMonth($num){
  727. // 获取当前期
  728. $period = self::instance();
  729. $periodNum = $period->getNowPeriodNum();
  730. $nowYearMonth = $period->getNowYearMonth();
  731. // 获取前$num期的期数
  732. $periodYearMonths = [];
  733. for($i=1;$i<=$num;$i++){
  734. if($periodNum - $i > 0){
  735. // 查看该期是否存在
  736. if(self::isExistsPeriodNum($periodNum - $i)){
  737. $yearMonth = $period->getYearMonth($periodNum - $i);
  738. $periodYearMonths[] = [
  739. 'yearMonth' => $yearMonth,
  740. 'periodNum' => $periodNum - $i,
  741. ];
  742. }
  743. }
  744. }
  745. return array_reverse($periodYearMonths);
  746. }
  747. /**
  748. * 获取本月所有已经挂网的期数
  749. * @param $yearMonth
  750. * @return array
  751. */
  752. public static function monthSentAllPeriodNum($yearMonth){
  753. $year = intval(substr($yearMonth, 0, 4));
  754. $month = intval(substr($yearMonth, 4, 2));
  755. $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();
  756. $result = [];
  757. if($allPeriod){
  758. foreach($allPeriod as $period){
  759. $result[] = $period['PERIOD_NUM'];
  760. }
  761. }
  762. return $result;
  763. }
  764. /**
  765. * 所传月份所有已结算的期数
  766. * @param $yearMonth
  767. * @return array
  768. */
  769. public static function monthCalcAllPeriodNum($yearMonth){
  770. $year = intval(substr($yearMonth, 0, 4));
  771. $month = intval(substr($yearMonth, 4, 2));
  772. $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();
  773. $result = [];
  774. if($allPeriod){
  775. foreach($allPeriod as $period){
  776. $result[] = $period['PERIOD_NUM'];
  777. }
  778. }
  779. return $result;
  780. }
  781. /**
  782. * 获取已结算的最大期数
  783. * @return mixed
  784. */
  785. public static function calculatedMaxPeriodNum(){
  786. return static::find()->where('IS_CALCULATED=1')->max('PERIOD_NUM');
  787. }
  788. /**
  789. * 已挂网的最大期数
  790. * @return mixed
  791. */
  792. public static function sentMaxPeriodNum(){
  793. return static::find()->where('IS_SENT=1')->max('PERIOD_NUM');
  794. }
  795. /**
  796. * 期数结束时间
  797. * @param $periodNum
  798. * @return int|mixed
  799. */
  800. public static function getEndTime($periodNum) {
  801. $endTime = static::find()->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM' => $periodNum])->select('END_TIME')->asArray()->one();
  802. return $endTime['END_TIME'] ?? 0;
  803. }
  804. /**
  805. * 到年底的剩余月份
  806. * @param null $periodNum
  807. * @return int
  808. * @throws Exception
  809. */
  810. public static function getMonthLeft($periodNum = null) {
  811. $period = self::instance();
  812. if ($periodNum) {
  813. $nowMonth = $period->getMonth($periodNum);
  814. } else {
  815. $nowMonth = $period->getNowMonth();
  816. }
  817. return 12 - (int)$nowMonth + 1;
  818. }
  819. /**
  820. * 起始期数和结束期数几个月
  821. * @param $startPeriodNum
  822. * @param $endPeriodNum
  823. * @return int|string
  824. */
  825. public static function getMonthNum($startPeriodNum,$endPeriodNum){
  826. 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();
  827. }
  828. /**
  829. * 从期数获取当前月的所有期
  830. *
  831. */
  832. public static function getCurrentMonthPeriodByPeriodNum($periodNum){
  833. $currentPeriod = self::getInfoByPeriodNum($periodNum);
  834. $currentYear = $currentPeriod['CALC_YEAR'];
  835. $currentMonth = $currentPeriod['CALC_MONTH'];
  836. $periodsArray = self::findAllAsArray(['CALC_YEAR'=>$currentYear,'CALC_MONTH'=>$currentMonth]);
  837. $periods = [];
  838. foreach ($periodsArray as $p){
  839. $periods[] = $p['PERIOD_NUM'];
  840. }
  841. return $periods;
  842. }
  843. public static function getCurrentQuarterPeriodByPeriodNum($periodNum){
  844. $currentPeriod = self::getInfoByPeriodNum($periodNum);
  845. $currentYear = $currentPeriod['CALC_YEAR'];
  846. $currentMonth = $currentPeriod['CALC_MONTH'];
  847. $periodsArray = self::find()->select("*")
  848. ->where(['CALC_YEAR'=>$currentYear])
  849. ->andWhere(['in', 'CALC_MONTH', [$currentMonth-2, $currentMonth-1, $currentMonth]])
  850. ->asArray()->all();
  851. $periods = [];
  852. foreach ($periodsArray as $p){
  853. $periods[] = $p['PERIOD_NUM'];
  854. }
  855. return $periods;
  856. }
  857. public static function updatePeriodIsAutoExec($periodNum, $isAutoExec = 0): int
  858. {
  859. return Period::updateAll(['AUTO_EXEC' => $isAutoExec], ['PERIOD_NUM' => $periodNum]);
  860. }
  861. public static function updatePeriodIsProcessing($periodNum, $isProcessing = 1): int
  862. {
  863. return Period::updateAll(['IS_PROCESSING' => $isProcessing], ['PERIOD_NUM' => $periodNum]);
  864. }
  865. public static function updatePeriodIsPreparing($periodNum, $isPreparing = 1): int
  866. {
  867. return Period::updateAll(['IS_PREPARING' => $isPreparing], ['PERIOD_NUM' => $periodNum]);
  868. }
  869. /**
  870. * 是否计算操作中
  871. * @param $periodNum
  872. * @return bool
  873. */
  874. public static function isProcessing($periodNum = null): bool
  875. {
  876. $db = CalcConsole::CALC_DB_NAME;
  877. $allPeriod = \Yii::$app->$db->createCommand("SELECT * FROM AR_PERIOD order by PERIOD_NUM desc")->queryAll();
  878. $calcPeriod = [];
  879. foreach ($allPeriod as $v) {
  880. if ($v['IS_PREPARE'] > 0) {
  881. $calcPeriod = $v;
  882. break;
  883. }
  884. }
  885. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  886. // 若计算服务中CALC_ID不为空,则不允许计算
  887. if ($calcPeriod) {
  888. if ($calcPeriod['CALC_ID']) return true;
  889. }
  890. //挂网则拒绝操作,返回true则拒绝操作
  891. if ($period['IS_SENT'] == self::SEND_FINISH) return true;
  892. if ($period['IS_PROCESSING'] == self::IS_PROCESSING) return true;
  893. return false;
  894. }
  895. /**
  896. * 是否预计算操作中
  897. * @param $periodNum
  898. * @return bool
  899. */
  900. public static function isPreparing($periodNum = null): bool
  901. {
  902. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  903. //挂网则拒绝操作,返回true则拒绝操作
  904. if ($period['IS_SENT'] == self::SEND_FINISH) return true;
  905. if ($period['IS_PREPARING'] == self::IS_PREPARING) return true;
  906. $nowTs = time();
  907. if ($nowTs-6 > $period['END_TIME']) return true;
  908. return false;
  909. }
  910. public static function checkPerf($periodNum){
  911. $result = static::find()
  912. ->where(
  913. 'PERIOD_NUM=:PERIOD_NUM ', [':PERIOD_NUM' => $periodNum]
  914. )
  915. ->asArray()
  916. ->one();
  917. if (isset($result['IS_PERFED']) && $result['IS_PERFED'] == 1) {
  918. return true;
  919. }
  920. return false;
  921. }
  922. public static function checkClose($periodNum){
  923. $period = static::findOneAsArray(['PERIOD_NUM' => $periodNum]);
  924. if ($period['IS_CLOSED'] == 1) {
  925. return true;
  926. }
  927. return false;
  928. }
  929. /**
  930. * 获取期数列表
  931. * @param null $periodNum
  932. * @param int $limit
  933. * @return array|\yii\db\ActiveRecord[]
  934. */
  935. public static function getPeriodList($periodNum = null, $limit = 10) {
  936. if ($periodNum) {
  937. $periodList = static::find()->where('PERIOD_NUM >= :PERIOD_NUM', [':PERIOD_NUM' => $periodNum])->select('PERIOD_NUM,IS_SENT')->limit($limit)->asArray()->all();
  938. } else {
  939. $periodList = static::find()->select('PERIOD_NUM,IS_SENT,IS_CLOSED')->limit($limit)->column();
  940. }
  941. return $periodList;
  942. }
  943. }