PullPerfDataFromCalc.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace common\helpers\bonus\Calc;
  3. use common\helpers\Date;
  4. use common\models\PerfMonth;
  5. use common\models\PerfPeriod;
  6. use common\models\PerfStandard;
  7. /**
  8. * 从计算服务拉取生成的业绩单
  9. */
  10. class PullPerfDataFromCalc extends BasePerfBusiness
  11. {
  12. public function __construct($periodNum)
  13. {
  14. parent::__construct($periodNum);
  15. }
  16. public function start(): array
  17. {
  18. try {
  19. //验证状态值
  20. if ($this->verify()) {
  21. //清空业绩单相关数据
  22. $this->clearPerfPeriod();
  23. if ($this->_isCalcMonth) {
  24. //拉取月业绩
  25. $this->pullPerfMonth();
  26. }
  27. //拉取期业绩数据
  28. $this->pullPerfPeriod();
  29. //同步周期表的值到业务系统
  30. self::pullPeriodForUpdate($this->_periodNum);
  31. return $this->success();
  32. } else {
  33. return $this->fail('业绩单还未生成');
  34. }
  35. } catch (\Exception $e) {
  36. return $this->fail('msg:' . $e->getMessage() . 'line:' . $e->getLine());
  37. }
  38. }
  39. public function verify(): bool
  40. {
  41. $db = $this->_calc_db_name;
  42. $data = \Yii::$app->$db->createCommand("SELECT IS_PREPARE,IS_PERFED FROM AR_PERIOD where PERIOD_NUM = $this->_periodNum")->queryOne();;
  43. if (
  44. // 2 == $data['IS_PREPARE'] //计算业绩阶段
  45. // &&
  46. 1 == $data['IS_PERFED'] //1表示累计业绩已完成
  47. ) {
  48. return true;
  49. }
  50. return false;
  51. }
  52. public function clearPerfPeriod()
  53. {
  54. // 周业绩
  55. PerfPeriod::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  56. // 业绩单
  57. // PerfOrder::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  58. // 删除活跃用户
  59. // PerfActiveUser::pageDeleteAll('PERIOD_NUM='.$this->_periodNum.' AND IS_SENT=0 ');
  60. // 月结时要清空的数据
  61. if ($this->_isCalcMonth) {
  62. // 月业绩表
  63. PerfMonth::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  64. //达标业绩表
  65. // PerfStandard::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  66. }
  67. }
  68. public function pullPerfMonth(): bool
  69. {
  70. $db = $this->_calc_db_name;
  71. $_offset = 0;
  72. // $pCalcMonth = date('Y-m-d', strtotime($formatOrderData['CREATED_AT']));
  73. $pCalcMonth = Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  74. periodMonth:
  75. $offset = $_offset * $this->_limit;
  76. //u.EMP_LV as LAST_EMP_LV,
  77. $data = \Yii::$app->$db->createCommand("SELECT p.*,'$pCalcMonth' as P_CALC_MONTH ,u.DEC_LV as LAST_DEC_LV,
  78. ifnull(u.STATUS,0) as LAST_STATUS FROM AR_PERF_MONTH as p LEFT JOIN AR_USER AS u ON p.user_id = u.id
  79. where p.CALC_MONTH = $this->_calcYearMonth limit $this->_limit offset $offset;")->queryAll();
  80. if (!empty($data)) {
  81. $fieldArray = array_keys($data[0]);
  82. $_offset += 1;
  83. \Yii::$app->db->createCommand()->batchInsert('AR_PERF_MONTH', $fieldArray, $data)->execute();
  84. $data = null;
  85. goto periodMonth;
  86. }
  87. $data = null;
  88. return true;
  89. }
  90. public function pullPerfPeriod(): bool
  91. {
  92. $db = $this->_calc_db_name;
  93. $_offset = 0;
  94. // $pCalcMonth = date('Y-m-d', strtotime($formatOrderData['CREATED_AT']));
  95. $pCalcMonth = Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  96. period:
  97. $offset = $_offset * $this->_limit;
  98. //u.EMP_LV as LAST_EMP_LV,
  99. $data = \Yii::$app->$db->createCommand("SELECT p.*,'$pCalcMonth' as P_CALC_MONTH ,u.DEC_LV as LAST_DEC_LV,
  100. ifnull(u.STATUS,0) as LAST_STATUS FROM AR_PERF_PERIOD as p LEFT JOIN AR_USER AS u ON p.user_id = u.id
  101. where p.PERIOD_NUM = $this->_periodNum limit $this->_limit offset $offset;")->queryAll();
  102. if (!empty($data)) {
  103. $fieldArray = array_keys($data[0]);
  104. $_offset += 1;
  105. \Yii::$app->db->createCommand()->batchInsert('AR_PERF_PERIOD', $fieldArray, $data)->execute();
  106. $data = null;
  107. goto period;
  108. }
  109. $data = null;
  110. return true;
  111. }
  112. }