PullPerfDataFromCalc.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. || 3 == $data['IS_PERFED']
  48. ) {
  49. return true;
  50. }
  51. return false;
  52. }
  53. public function clearPerfPeriod()
  54. {
  55. // 周业绩
  56. PerfPeriod::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  57. // 业绩单
  58. // PerfOrder::pageDeleteAll('PERIOD_NUM=' . $this->_periodNum);
  59. // 删除活跃用户
  60. // PerfActiveUser::pageDeleteAll('PERIOD_NUM='.$this->_periodNum.' AND IS_SENT=0 ');
  61. // 月结时要清空的数据
  62. if ($this->_isCalcMonth) {
  63. // 月业绩表
  64. PerfMonth::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  65. //达标业绩表
  66. // PerfStandard::pageDeleteAll("CALC_MONTH='{$this->_calcYearMonth}'");
  67. }
  68. }
  69. public function pullPerfMonth(): bool
  70. {
  71. $db = $this->_calc_db_name;
  72. $_offset = 0;
  73. // $pCalcMonth = date('Y-m-d', strtotime($formatOrderData['CREATED_AT']));
  74. $pCalcMonth = Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  75. periodMonth:
  76. $offset = $_offset * $this->_limit;
  77. //u.EMP_LV as LAST_EMP_LV,
  78. $data = \Yii::$app->$db->createCommand("SELECT p.*,'$pCalcMonth' as P_CALC_MONTH ,u.DEC_LV as LAST_DEC_LV,
  79. 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
  80. where p.CALC_MONTH = $this->_calcYearMonth limit $this->_limit offset $offset;")->queryAll();
  81. if (!empty($data)) {
  82. $fieldArray = array_keys($data[0]);
  83. $_offset += 1;
  84. \Yii::$app->db->createCommand()->batchInsert('AR_PERF_MONTH', $fieldArray, $data)->execute();
  85. $data = null;
  86. goto periodMonth;
  87. }
  88. $data = null;
  89. return true;
  90. }
  91. public function pullPerfPeriod(): bool
  92. {
  93. $db = $this->_calc_db_name;
  94. $_offset = 0;
  95. // $pCalcMonth = date('Y-m-d', strtotime($formatOrderData['CREATED_AT']));
  96. $pCalcMonth = Date::ociToDate($this->_calcYearMonth, Date::OCI_TIME_FORMAT_SHORT_MONTH);
  97. period:
  98. $offset = $_offset * $this->_limit;
  99. //u.EMP_LV as LAST_EMP_LV,
  100. $data = \Yii::$app->$db->createCommand("SELECT p.*,'$pCalcMonth' as P_CALC_MONTH ,u.DEC_LV as LAST_DEC_LV,
  101. 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
  102. where p.PERIOD_NUM = $this->_periodNum limit $this->_limit offset $offset;")->queryAll();
  103. if (!empty($data)) {
  104. $fieldArray = array_keys($data[0]);
  105. $_offset += 1;
  106. \Yii::$app->db->createCommand()->batchInsert('AR_PERF_PERIOD', $fieldArray, $data)->execute();
  107. $data = null;
  108. goto period;
  109. }
  110. $data = null;
  111. return true;
  112. }
  113. }