ChartData.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/6/12
  6. * Time: 下午2:11
  7. */
  8. namespace common\helpers;
  9. use common\models\CalcBonus;
  10. use common\models\PerfCompany;
  11. use common\models\Period;
  12. use common\models\UserInfo;
  13. use common\models\Withdraw;
  14. use yii\base\BaseObject;
  15. use yii\base\StaticInstanceTrait;
  16. class ChartData extends BaseObject
  17. {
  18. use StaticInstanceTrait;
  19. /**
  20. * 年度累计奖金
  21. * @return int|mixed
  22. */
  23. public function totalBonus(){
  24. return 0;
  25. $nowYear = Date::nowYear();
  26. $bonus = CalcBonus::findUseSlaves()->select('SUM(BONUS_QY) AS BONUS_QY_SUM, SUM(BONUS_YC) AS BONUS_YC_SUM, SUM(BONUS_FX) AS BONUS_FX_SUM, SUM(BONUS_LS) AS BONUS_LS_SUM, SUM(BONUS_CF) AS BONUS_CF_SUM, SUM(BONUS_LX) AS BONUS_LX_SUM, SUM(BONUS_FL) AS BONUS_FL_SUM, SUM(BONUS_BT) AS BONUS_BT_SUM, SUM(BONUS_FW) AS BONUS_FW_SUM')->where('CALC_YEAR=:CALC_YEAR AND IS_SENT=:IS_SENT', [':CALC_YEAR'=>$nowYear, ':IS_SENT'=>1])->asArray()->one();
  27. if($bonus){
  28. return $bonus['BONUS_QY_SUM'] + $bonus['BONUS_YC_SUM'] + $bonus['BONUS_FX_SUM'] + $bonus['BONUS_LS_SUM'] + $bonus['BONUS_CF_SUM'] + $bonus['BONUS_LX_SUM'] + $bonus['BONUS_FL_SUM'] + $bonus['BONUS_BT_SUM'] + $bonus['BONUS_FW_SUM'];
  29. } else {
  30. return 0;
  31. }
  32. }
  33. /**
  34. * 年度总业绩
  35. * @return int|mixed
  36. */
  37. public function totalPerf(){
  38. return 0;
  39. $nowYear = Date::nowYear();
  40. $perf = PerfCompany::findUseSlaves()->select('SUM(ZC_PV) AS ZC_PV_SUM, SUM(YH_PV) AS YH_PV_SUM, SUM(ZG_PV) AS ZG_PV_SUM, SUM(LS_PV) AS LS_PV_SUM, SUM(FX_PV) AS FX_PV_SUM')->where('CALC_YEAR=:CALC_YEAR', [':CALC_YEAR'=>$nowYear])->asArray()->one();
  41. if($perf){
  42. return $perf['ZC_PV_SUM'] + $perf['YH_PV_SUM'] + $perf['ZG_PV_SUM'] + $perf['LS_PV_SUM'] + $perf['FX_PV_SUM'];
  43. } else {
  44. return 0;
  45. }
  46. }
  47. /**
  48. * 总提现金额
  49. * @return int|mixed
  50. */
  51. public function totalWithdraw(){
  52. $withdraw = Withdraw::findUseSlaves()->select('SUM(AMOUNT) AS AMOUNT_SUM')->where('AUDIT_STATUS=:AUDIT_STATUS AND CREATED_AT > :START_TIME AND CREATED_AT < :END_TIME', [':AUDIT_STATUS' => Withdraw::STATUS_PAID, ':START_TIME' => Date::yearStart(), ':END_TIME' => Date::yearEnd()])->asArray()->one();
  53. if($withdraw){
  54. return $withdraw['AMOUNT_SUM'];
  55. } else {
  56. return 0;
  57. }
  58. }
  59. /**
  60. * 会员总数量
  61. * @return int|string
  62. */
  63. public function totalUser(){
  64. return UserInfo::findUseSlaves()->where('1=1')->count();
  65. }
  66. /**
  67. * 获取会员注册量
  68. * @return array
  69. */
  70. public function getUserData(){
  71. $monthAgo = [
  72. [
  73. 'start' => Date::monthStart(Date::nowTime()),
  74. 'end' => Date::monthEnd(Date::nowTime()),
  75. ],
  76. [
  77. 'start' => Date::monthStart(Date::lastNumMonth(1)),
  78. 'end' => Date::monthEnd(Date::lastNumMonth(1)),
  79. ],
  80. [
  81. 'start' => Date::monthStart(Date::lastNumMonth(2)),
  82. 'end' => Date::monthEnd(Date::lastNumMonth(2)),
  83. ],
  84. [
  85. 'start' => Date::monthStart(Date::lastNumMonth(3)),
  86. 'end' => Date::monthEnd(Date::lastNumMonth(3)),
  87. ],
  88. [
  89. 'start' => Date::monthStart(Date::lastNumMonth(4)),
  90. 'end' => Date::monthEnd(Date::lastNumMonth(4)),
  91. ],
  92. ];
  93. $result = [];
  94. foreach($monthAgo as $key => $value){
  95. $result[$key]['month'] = date('n月', Date::lastNumMonth($key));
  96. $result[$key]['num'] = $this->_monthUserRegNum($monthAgo[$key]['start'], $monthAgo[$key]['end']);
  97. }
  98. return array_reverse($result);
  99. }
  100. /**
  101. * 获取奖金统计数据
  102. * @return array
  103. */
  104. public function getBonusData(){
  105. $period = Period::instance();
  106. $calcMonth = $period->getNowYearMonth();
  107. $formatCalcMonth = substr($calcMonth, 0, 4).'-'.substr($calcMonth, 4, 2).'-01';
  108. $calcMonths = [
  109. [
  110. 'month' => $calcMonth,
  111. ],
  112. [
  113. 'month' => Date::lastNumMonth(1, strtotime($formatCalcMonth), 'Ym'),
  114. ],
  115. [
  116. 'month' => Date::lastNumMonth(2, strtotime($formatCalcMonth), 'Ym'),
  117. ],
  118. [
  119. 'month' => Date::lastNumMonth(3, strtotime($formatCalcMonth), 'Ym'),
  120. ],
  121. [
  122. 'month' => Date::lastNumMonth(4, strtotime($formatCalcMonth), 'Ym'),
  123. ],
  124. ];
  125. foreach($calcMonths as $key => $value){
  126. $calcMonths[$key]['month'] = date('n月', Date::lastNumMonth($key));
  127. $calcMonths[$key]['num'] = $this->_monthBonus($value['month']);
  128. }
  129. return array_reverse($calcMonths);
  130. }
  131. /**
  132. * 获取提现量
  133. * @return array
  134. */
  135. public function getWithdrawData(){
  136. $monthAgo = [
  137. [
  138. 'start' => Date::monthStart(Date::nowTime()),
  139. 'end' => Date::monthEnd(Date::nowTime()),
  140. ],
  141. [
  142. 'start' => Date::monthStart(Date::lastNumMonth(1)),
  143. 'end' => Date::monthEnd(Date::lastNumMonth(1)),
  144. ],
  145. [
  146. 'start' => Date::monthStart(Date::lastNumMonth(2)),
  147. 'end' => Date::monthEnd(Date::lastNumMonth(2)),
  148. ],
  149. [
  150. 'start' => Date::monthStart(Date::lastNumMonth(3)),
  151. 'end' => Date::monthEnd(Date::lastNumMonth(3)),
  152. ],
  153. [
  154. 'start' => Date::monthStart(Date::lastNumMonth(4)),
  155. 'end' => Date::monthEnd(Date::lastNumMonth(4)),
  156. ],
  157. ];
  158. $result = [];
  159. foreach($monthAgo as $key => $value){
  160. $result[$key]['month'] = date('n月', Date::lastNumMonth($key));
  161. $result[$key]['num'] = $this->_monthWithdrawNum($monthAgo[$key]['start'], $monthAgo[$key]['end']);
  162. }
  163. return array_reverse($result);
  164. }
  165. public function userBonusData($userId){
  166. $period = Period::instance();
  167. $calcMonth = $period->getNowYearMonth();
  168. $formatCalcMonth = substr($calcMonth, 0, 4).'-'.substr($calcMonth, 4, 2).'-01';
  169. $calcMonths = [
  170. [
  171. 'month' => $calcMonth,
  172. ],
  173. [
  174. 'month' => Date::lastNumMonth(1, strtotime($formatCalcMonth), 'Ym'),
  175. ],
  176. [
  177. 'month' => Date::lastNumMonth(2, strtotime($formatCalcMonth), 'Ym'),
  178. ],
  179. [
  180. 'month' => Date::lastNumMonth(3, strtotime($formatCalcMonth), 'Ym'),
  181. ],
  182. [
  183. 'month' => Date::lastNumMonth(4, strtotime($formatCalcMonth), 'Ym'),
  184. ],
  185. ];
  186. foreach($calcMonths as $key => $value){
  187. $calcMonths[$key] = array_merge($calcMonths[$key], $this->_userMonthBonus($value['month'], $userId));
  188. }
  189. return array_reverse($calcMonths);
  190. }
  191. /**
  192. * 某月会员注册量
  193. * @param $monthStart
  194. * @param $monthEnd
  195. * @return int|string
  196. */
  197. private function _monthUserRegNum($monthStart, $monthEnd){
  198. return UserInfo::findUseSlaves()->where('CREATED_AT>:START_TIME AND CREATED_AT<:END_TIME', [':START_TIME'=>$monthStart, ':END_TIME'=>$monthEnd])->count();
  199. }
  200. /**
  201. * 月奖金和
  202. * @param $calcMonth
  203. * @return int|mixed
  204. */
  205. private function _monthBonus($calcMonth){
  206. if($calcMonth){
  207. $bonus = CalcBonus::findUseSlaves()->yearMonth($calcMonth)->select('SUM(BONUS_QY) AS BONUS_QY_SUM, SUM(BONUS_YC) AS BONUS_YC_SUM, SUM(BONUS_FX) AS BONUS_FX_SUM, SUM(BONUS_LS) AS BONUS_LS_SUM, SUM(BONUS_CF) AS BONUS_CF_SUM, SUM(BONUS_LX) AS BONUS_LX_SUM, SUM(BONUS_FL) AS BONUS_FL_SUM, SUM(BONUS_BT) AS BONUS_BT_SUM, SUM(BONUS_FW) AS BONUS_FW_SUM')->where('CALC_MONTH=:CALC_MONTH AND IS_SENT=1', [':CALC_MONTH'=>$calcMonth])->asArray()->one();
  208. if($bonus){
  209. return $bonus['BONUS_QY_SUM'] + $bonus['BONUS_YC_SUM'] + $bonus['BONUS_FX_SUM'] + $bonus['BONUS_LS_SUM'] + $bonus['BONUS_CF_SUM'] + $bonus['BONUS_LX_SUM'] + $bonus['BONUS_FL_SUM'] + $bonus['BONUS_BT_SUM'] + $bonus['BONUS_FW_SUM'];
  210. }
  211. }
  212. return 0;
  213. }
  214. /**
  215. * 月提现
  216. * @param $monthStart
  217. * @param $monthEnd
  218. * @return string
  219. */
  220. private function _monthWithdrawNum($monthStart, $monthEnd){
  221. return Tool::formatPrice(Withdraw::findUseSlaves()->where('CREATED_AT>:START_TIME AND CREATED_AT<:END_TIME AND AUDIT_STATUS=:AUDIT_STATUS', [':START_TIME'=>$monthStart, ':END_TIME'=>$monthEnd, ':AUDIT_STATUS'=>Withdraw::STATUS_PAID])->sum('AMOUNT'));
  222. }
  223. private function _userMonthBonus($calcMonth, $userId){
  224. $bonus = CalcBonus::findUseSlaves()->yearMonth($calcMonth)->select('BONUS_QY, BONUS_YC, BONUS_FX, BONUS_LS, BONUS_CF, BONUS_LX, BONUS_FL, BONUS_BT, BONUS_FW')->where('CALC_MONTH=:CALC_MONTH AND IS_SENT=1 AND USER_ID=:USER_ID', [':CALC_MONTH'=>$calcMonth, ':USER_ID'=>$userId])->asArray()->one();
  225. if($bonus){
  226. return [
  227. 'BONUS_QY' => $bonus['BONUS_QY'],
  228. 'BONUS_YC' => $bonus['BONUS_YC'],
  229. 'BONUS_FX' => $bonus['BONUS_FX'],
  230. 'BONUS_LS' => $bonus['BONUS_LS'],
  231. 'BONUS_CF' => $bonus['BONUS_CF'],
  232. 'BONUS_LX' => $bonus['BONUS_LX'],
  233. 'BONUS_FL' => $bonus['BONUS_FL'],
  234. 'BONUS_BT' => $bonus['BONUS_BT'],
  235. 'BONUS_FW' => $bonus['BONUS_FW'],
  236. ];
  237. } else {
  238. return [
  239. 'BONUS_QY' => 0,
  240. 'BONUS_YC' => 0,
  241. 'BONUS_FX' => 0,
  242. 'BONUS_LS' => 0,
  243. 'BONUS_CF' => 0,
  244. 'BONUS_LX' => 0,
  245. 'BONUS_FL' => 0,
  246. 'BONUS_BT' => 0,
  247. 'BONUS_FW' => 0,
  248. ];
  249. }
  250. }
  251. }