ServeLog.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace common\models;
  3. use common\helpers\snowflake\SnowFake;
  4. use Exception;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%SERVE_LOG}}".
  8. *
  9. * @property string $ID
  10. * @property string $PERIOD_NUM 业绩期
  11. * @property string $LOG_DESCRIBE 日志信息
  12. * @property string $CREATE_AT 创建时间
  13. * @property string $CALC_MONTH 结算年月
  14. */
  15. class ServeLog extends \common\components\ActiveRecord
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public static function tableName()
  21. {
  22. return '{{%SERVE_LOG}}';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function rules()
  28. {
  29. return [
  30. [['PERIOD_NUM', 'CALC_MONTH', 'CREATE_AT'], 'required'],
  31. [['ID'], 'unique'],
  32. ];
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function attributeLabels()
  38. {
  39. return [
  40. 'ID' => 'ID',
  41. 'PERIOD_NUM' => '业绩期',
  42. 'LOG_DESCRIBE' => '描述信息',
  43. 'CALC_MONTH' => '总人数',
  44. 'CREATE_AT' => '总业绩',
  45. ];
  46. }
  47. // =============================业绩相关日志==========================================================================
  48. /**
  49. * 无逻辑日志,直接传文案
  50. * @param int $periodNum
  51. * @param int $calcMonth 业绩期
  52. */
  53. public static function noParamsLog($periodNum, $calcMonth, $desc) {
  54. $logData[] = [
  55. 'PERIOD_NUM' => $periodNum,
  56. 'CALC_MONTH' => $calcMonth,
  57. 'LOG_DESCRIBE' => $desc,
  58. 'CREATE_AT' => time()
  59. ];
  60. ServeLog::batchInsert($logData);
  61. return true;
  62. }
  63. // 第一步,记录期数结算年月
  64. /**
  65. * 记录业绩期,结算年月
  66. * @param int $periodNum
  67. * @param int $calcMonth 业绩期
  68. */
  69. public static function periodMonth($periodNum, $calcMonth) {
  70. $logData[] = [
  71. 'PERIOD_NUM' => $periodNum,
  72. 'CALC_MONTH' => $calcMonth,
  73. 'LOG_DESCRIBE' => "期数:$periodNum,结算年月:$calcMonth,开始计算业绩",
  74. 'CREATE_AT' => time()
  75. ];
  76. ServeLog::batchInsert($logData);
  77. return true;
  78. }
  79. /**
  80. * 记录推送过来的基础数据
  81. * @param int $periodNum
  82. * @param int $calcMonth 业绩期
  83. */
  84. public static function basicData($periodNum, $calcMonth) {
  85. $userCount = User::find()->where('1=1')->count('ID');
  86. $infoCount = UserInfo::find()->where('1=1')->count('ID');
  87. $networkConut = UserNetwork::find()->where('1=1')->count('ID');
  88. $userPerfConut = UserPerf::find()->where('1=1')->count('ID');
  89. $calcBonusQyConut = CalcBonusQY::find()->where('1=1')->count('ID');
  90. $perfPeriodCount = PerfPeriod::find()->where('1=1')->count('ID');
  91. $perfOrderCount = PerfOrder::find()->where('1=1')->count('ID');
  92. $relationCount = UserRelation::find()->where('1=1')->count('ID');
  93. $periodCount = Period::find()->where('1=1')->count('ID');
  94. $perfMonthCount = PerfMonth::find()->where('1=1')->count('ID');
  95. $desc = "记录推送基础数据信息:AR_USER-推送用户总条数:$userCount;AR_USER_INFO-推送用户信息总条数:$infoCount;
  96. AR_USER_NETWORK_NEW-推送安置网络总条数:$networkConut;AR_USER_PERF-推送用户结余业绩总条数:$userPerfConut;
  97. AR_CALC_BONUS_QY-推送团队奖总条数:$calcBonusQyConut;AR_PERF_PERIOD-推送期业绩总条数:$perfPeriodCount;
  98. AR_PERIOD-推送业绩期总条数:$periodCount;AR_USER_RELATION_NEW-推送开拓网络总条数:$relationCount;
  99. AR_PERF_MONTH-推送月业绩表总条数:$perfMonthCount;AR_PERF_ORDER-推送业绩单总条数:$perfOrderCount";
  100. $logData[] = [
  101. 'PERIOD_NUM' => $periodNum,
  102. 'CALC_MONTH' => $calcMonth,
  103. 'LOG_DESCRIBE' => $desc,
  104. 'CREATE_AT' => time()
  105. ];
  106. ServeLog::batchInsert($logData);
  107. return true;
  108. }
  109. /**
  110. * 记录清除的期业绩和月业绩
  111. * @param int $periodNum
  112. * @param int $calcMonth 业绩期
  113. */
  114. public static function clearPerf($periodNum, $calcMonth) {
  115. $perfPeriodCount = PerfPeriod::find()
  116. ->where('PERIOD_NUM=:PERIOD_NUM', [':PERIOD_NUM'=>$periodNum])
  117. ->count('ID');
  118. $perfMonthCount = PerfMonth::find()
  119. ->where('CALC_MONTH=:CALC_MONTH', [':CALC_MONTH'=>$calcMonth])
  120. ->count('ID');
  121. $desc = "清除业绩数据:AR_PERF_PERIOD-清除期业绩表总条数:$perfPeriodCount;AR_PERF_MONTH-清除月业绩表总条数:$perfMonthCount";
  122. $logData[] = [
  123. 'PERIOD_NUM' => $periodNum,
  124. 'CALC_MONTH' => $calcMonth,
  125. 'LOG_DESCRIBE' => $desc,
  126. 'CREATE_AT' => time()
  127. ];
  128. ServeLog::batchInsert($logData);
  129. return true;
  130. }
  131. /**
  132. * 记录缓存用户数据
  133. * @param int $periodNum
  134. * @param int $calcMonth 业绩期
  135. */
  136. public static function cacheUserTotal($periodNum, $calcMonth) {
  137. $allUserCount = UserInfo::findUseDbCalc()
  138. ->from(UserInfo::tableName(). 'AS UI')
  139. ->select('UI.USER_ID,U.STATUS')
  140. ->leftJoin(User::tableName() . 'AS U', 'U.ID=UI.USER_ID')
  141. ->where('1=1')
  142. ->count('UI.USER_ID');
  143. $activeUserCount = UserInfo::findUseDbCalc()
  144. ->from(UserInfo::tableName(). 'AS UI')
  145. ->select('UI.USER_ID,U.STATUS')
  146. ->leftJoin(User::tableName() . 'AS U', 'U.ID=UI.USER_ID')
  147. ->where('U.STATUS=1')
  148. ->count('UI.USER_ID');
  149. $total = $allUserCount+$activeUserCount;
  150. $desc = "缓存用户信息-结束,缓存数量:$total";
  151. $logData[] = [
  152. 'PERIOD_NUM' => $periodNum,
  153. 'CALC_MONTH' => $calcMonth,
  154. 'LOG_DESCRIBE' => $desc,
  155. 'CREATE_AT' => time()
  156. ];
  157. ServeLog::batchInsert($logData);
  158. return true;
  159. }
  160. /**
  161. * 记录期业绩数据
  162. * @param int $periodNum
  163. * @param int $calcMonth 业绩期
  164. */
  165. public static function perfPeriodInfo($periodNum, $calcMonth) {
  166. $data = PerfPeriod::findUseSlaves()
  167. ->select('SUM(PV_PCS) AS TOTAL_PV_PCS, COUNT(ID) AS TOTAL_NUM')
  168. ->where('PERIOD_NUM=:PERIOD_NUM', ['PERIOD_NUM'=>$periodNum])
  169. ->asArray()
  170. ->one();
  171. $num = $data['TOTAL_NUM'];
  172. $total_pvpcs = $data['TOTAL_NUM'];
  173. $desc = "期业绩计算完成:AR_PERF_PERIOD-期业绩总条数:$num,期业绩个人消费PV总数:$total_pvpcs";
  174. $logData[] = [
  175. 'PERIOD_NUM' => $periodNum,
  176. 'CALC_MONTH' => $calcMonth,
  177. 'LOG_DESCRIBE' => $desc,
  178. 'CREATE_AT' => time()
  179. ];
  180. ServeLog::batchInsert($logData);
  181. return true;
  182. }
  183. /**
  184. * 记录月业绩数据
  185. * @param int $periodNum
  186. * @param int $calcMonth 业绩期
  187. */
  188. public static function perfMonthInfo($periodNum, $calcMonth) {
  189. $data = PerfMonth::findUseSlaves()
  190. ->select('SUM(PV_PCS) AS TOTAL_PV_PCS, COUNT(ID) AS TOTAL_NUM')
  191. ->where('CALC_MONTH=:CALC_MONTH', ['CALC_MONTH'=>$calcMonth])
  192. ->asArray()
  193. ->one();
  194. $num = $data['TOTAL_NUM'];
  195. $total_pvpcs = $data['TOTAL_NUM'];
  196. $desc = "月业绩计算完成:AR_PERF_MONTH-月业绩表总条数:$num,月业绩个人消费总数:$total_pvpcs";
  197. $logData[] = [
  198. 'PERIOD_NUM' => $periodNum,
  199. 'CALC_MONTH' => $calcMonth,
  200. 'LOG_DESCRIBE' => $desc,
  201. 'CREATE_AT' => time()
  202. ];
  203. ServeLog::batchInsert($logData);
  204. return true;
  205. }
  206. /**
  207. * 记录期业绩活跃数据
  208. * @param int $periodNum
  209. * @param int $calcMonth 业绩期
  210. */
  211. public static function perfPeriodActive($periodNum, $calcMonth) {
  212. $data = PerfPeriod::findUseSlaves()
  213. ->select('COUNT(ID) AS TOTAL_NUM')
  214. ->where('PERIOD_NUM=:PERIOD_NUM AND IS_ACTIVE=1', ['PERIOD_NUM'=>$periodNum])
  215. ->asArray()
  216. ->one();
  217. $num = $data['TOTAL_NUM'];
  218. $desc = "期业绩更新活跃状态,活跃人数:$num";
  219. $logData[] = [
  220. 'PERIOD_NUM' => $periodNum,
  221. 'CALC_MONTH' => $calcMonth,
  222. 'LOG_DESCRIBE' => $desc,
  223. 'CREATE_AT' => time()
  224. ];
  225. ServeLog::batchInsert($logData);
  226. return true;
  227. }
  228. // =============================奖金相关日志=====================================================================================
  229. // ------以下为记录操作流程
  230. // 记录请求时间
  231. public static function recordRequest($time, $periodNum, $desc = '', $needCheck = false) {
  232. // 查询此期是否存在计算中,如果在计算中则不用记录
  233. if ($needCheck) {
  234. $hasPerpare = Period::findUseSlaves()
  235. ->select('ID')
  236. ->where('PERIOD_NUM=:PERIOD_NUM AND IS_PREPARE=1', [':PERIOD_NUM'=>$periodNum])
  237. ->asArray()
  238. ->one();
  239. if (empty($hasPerpare)) {
  240. return true;
  241. }
  242. }
  243. $logData[] = [
  244. 'ID' => SnowFake::instance()->generateId(),
  245. 'PERIOD_NUM' => $periodNum ?: 0,
  246. 'TASK_TYPE' => 'serve',
  247. 'TASK_DESCRIBE' => $desc ?: '请求服务开始',
  248. 'START_AT' => $time
  249. ];
  250. ServeProcess::batchInsert($logData);
  251. return true;
  252. }
  253. // 记录计算基础数据
  254. public static function recordDataInfo($time, $periodNum) {
  255. $userCount = User::find()->where('1=1')->count('ID'); // user表总数
  256. $perfSum = PerfOrder::find()->where('1=1')->sum('PV');// 业绩单总业绩
  257. $infoCount = UserInfo::find()->where('1=1')->count('ID'); // userinfo表总数
  258. $networkConut = UserNetwork::find()->where('1=1')->count('ID'); // UserNetwork总数
  259. $relationCount = UserRelation::find()->where('1=1')->count('ID'); // UserRelation
  260. $logData[] = [
  261. 'ID' => SnowFake::instance()->generateId(),
  262. 'PERIOD_NUM' => $periodNum ?: 0,
  263. 'TASK_TYPE' => 'serve',
  264. 'TASK_DESCRIBE' => '统计业务系统数据信息',
  265. 'START_AT' => $time,
  266. 'CALC_USER_TOTAL' => $userCount > 0 ? $userCount : 0,
  267. 'CALC_PERF_TOTAL' => $perfSum > 0 ? $perfSum : 0,
  268. 'CALC_USERINFO_TOTAL' => $infoCount > 0 ? $infoCount : 0,
  269. 'CALC_NETWORK_TOTAL' => $networkConut > 0 ? $networkConut : 0,
  270. 'CALC_RELATION_TOTAL' => $relationCount > 0 ? $relationCount :0
  271. ];
  272. ServeProcess::batchInsert($logData);
  273. }
  274. // 记录流程
  275. public static function recordProcess($start, $end, $periodNum, $desc, $type='perf') {
  276. try {
  277. $logData[] = [
  278. 'ID' => SnowFake::instance()->generateId(),
  279. 'PERIOD_NUM' => $periodNum ?: 0,
  280. 'TASK_TYPE' => $type,
  281. 'START_AT' => $start,
  282. 'END_AT' => $end,
  283. 'TASK_DESCRIBE' => $desc,
  284. ];
  285. ServeProcess::batchInsert($logData);
  286. } catch (Exception $e) {
  287. var_dump($e->getMessage());
  288. }
  289. }
  290. }