Ranking.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\admin\controller\qingdong\statistic;
  3. use addons\qingdong\model\Staff;
  4. use app\common\controller\Backend;
  5. use addons\qingdong\model\Contacts;
  6. use addons\qingdong\model\Contract;
  7. use addons\qingdong\model\Customer;
  8. use addons\qingdong\model\Field;
  9. use addons\qingdong\model\File;
  10. use addons\qingdong\model\Receivables;
  11. use addons\qingdong\model\Record;
  12. use addons\qingdong\model\Leads;
  13. use addons\qingdong\model\ExamineRecord;
  14. use think\Config;
  15. /**
  16. * 排行分析
  17. */
  18. class Ranking extends Backend
  19. {
  20. /**
  21. * 合同金额分析
  22. */
  23. public function index()
  24. {
  25. $row = input('row/a');
  26. if (isset($row['times']) && $row['times']) {
  27. $times = explode(' - ', $row['times']);
  28. } else {
  29. $times = [date('Y-01-01'), date('Y-m-d')];
  30. }
  31. if (isset($row['type']) && $row['type']) {
  32. $type = $row['type'];
  33. } else {
  34. $type = 0;
  35. }
  36. $staff = Staff::info();
  37. $between = [$times[0].' 00:00:00', $times[1].' 23:59:59'];
  38. if ($type == 1) {//本人
  39. $ids = [$staff->id];
  40. } elseif ($type == 2) {//下属
  41. $ids = Staff::getLowerStaffId();
  42. } else {//全部
  43. $ids = Staff::getMyStaffIds();
  44. }
  45. if (isset($row['staff_id']) && $row['staff_id']) {
  46. $ids = $staff_id = $row['staff_id'];
  47. } else {
  48. $staff_id = '';
  49. }
  50. $datalist = Contract::where([
  51. 'check_status' => 2,
  52. 'order_staff_id' => ['in', $ids],
  53. 'order_date' => ['between', $between]
  54. ])->field("order_staff_id,sum(`money`) as c")->group('order_staff_id')->select();
  55. $datainfo = $this->arraySortByOneField($datalist, 'c');
  56. $typedata = [];
  57. foreach ($datainfo as $k => $v) {
  58. $staff = Staff::where(array('id' => $v['order_staff_id']))->value('name');
  59. if($k<10) {
  60. $typedata['name'][] = $staff;
  61. $typedata['num'][] = $v['c'];
  62. }
  63. $datainfo[$k]['name'] = $staff;
  64. }
  65. $rank = $this->arraySortByOneField($datainfo, 'c', SORT_DESC);
  66. $this->view->assign([
  67. 'customerdata' => $typedata,
  68. 'dataInfo' => $rank,
  69. 'times' => implode(' - ', $times),
  70. 'type' => $type,
  71. 'staff_id' => $staff_id,
  72. ]);
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 回款金额分析
  77. */
  78. public function contract()
  79. {
  80. $row = input('row/a');
  81. if (isset($row['times']) && $row['times']) {
  82. $times = explode(' - ', $row['times']);
  83. } else {
  84. $times = [date('Y-01-01'), date('Y-m-d')];
  85. }
  86. if (isset($row['type']) && $row['type']) {
  87. $type = $row['type'];
  88. } else {
  89. $type = 0;
  90. }
  91. $staff = Staff::info();
  92. $between = [$times[0].' 00:00:00', $times[1].' 23:59:59'];
  93. if ($type == 1) {//本人
  94. $ids = [$staff->id];
  95. } elseif ($type == 2) {//下属
  96. $ids = Staff::getLowerStaffId();
  97. } else {//全部
  98. $ids = Staff::getMyStaffIds();
  99. }
  100. if (isset($row['staff_id']) && $row['staff_id']) {
  101. $ids = $staff_id = $row['staff_id'];
  102. } else {
  103. $staff_id = '';
  104. }
  105. $datalist = Receivables::where([
  106. 'check_status' => 2,
  107. 'owner_staff_id' => ['in', $ids],
  108. 'return_time' => ['between', $between]
  109. ])->field("owner_staff_id,sum(`money`) as c")->group('owner_staff_id')->select();
  110. $datainfo = $this->arraySortByOneField($datalist, 'c');
  111. $typedata = [];
  112. foreach ($datainfo as $k => $v) {
  113. $staff = Staff::where(array('id' => $v['owner_staff_id']))->value('name');
  114. $typedata['name'][] = $staff;
  115. $typedata['num'][] = $v['c'];
  116. $datainfo[$k]['name'] = $staff;
  117. }
  118. $rank = $this->arraySortByOneField($datainfo, 'c', SORT_DESC);
  119. $this->view->assign([
  120. 'customerdata' => $typedata,
  121. 'dataInfo' => $rank,
  122. 'times' => implode(' - ', $times),
  123. 'type' => $type,
  124. 'staff_id' => $staff_id,
  125. ]);
  126. return $this->view->fetch();
  127. }
  128. /**
  129. * 新增客户数分析
  130. */
  131. public function customer()
  132. {
  133. $row = input('row/a');
  134. if (isset($row['times']) && $row['times']) {
  135. $times = explode(' - ', $row['times']);
  136. } else {
  137. $times = [date('Y-01-01'), date('Y-m-d')];
  138. }
  139. if (isset($row['type']) && $row['type']) {
  140. $type = $row['type'];
  141. } else {
  142. $type = 0;
  143. }
  144. $staff = Staff::info();
  145. $between = [strtotime($times[0]), strtotime($times[1]) + 86400 - 1];
  146. if ($type == 1) {//本人
  147. $ids = [$staff->id];
  148. } elseif ($type == 2) {//下属
  149. $ids = Staff::getLowerStaffId();
  150. } else {//全部
  151. $ids = Staff::getMyStaffIds();
  152. }
  153. if (isset($row['staff_id']) && $row['staff_id']) {
  154. $ids = $staff_id = $row['staff_id'];
  155. } else {
  156. $staff_id = '';
  157. }
  158. $datalist = Customer::where(['owner_staff_id' => ['in', $ids], 'createtime' => ['between', $between]])
  159. ->field("owner_staff_id,count(*) as c")
  160. ->group('owner_staff_id')->select();
  161. $datainfo = $this->arraySortByOneField($datalist, 'c');
  162. $typedata = [];
  163. foreach ($datainfo as $k => $v) {
  164. $staff = Staff::where(array('id' => $v['owner_staff_id']))->value('name');
  165. $typedata['name'][] = $staff;
  166. $typedata['num'][] = $v['c'];
  167. $datainfo[$k]['name'] = $staff;
  168. }
  169. $rank = $this->arraySortByOneField($datainfo, 'c', SORT_DESC);
  170. $this->view->assign([
  171. 'customerdata' => $typedata,
  172. 'dataInfo' => $rank,
  173. 'times' => implode(' - ', $times),
  174. 'type' => $type,
  175. 'staff_id' => $staff_id,
  176. ]);
  177. return $this->view->fetch();
  178. }
  179. /**
  180. * 跟进次数分析
  181. */
  182. public function record()
  183. {
  184. $row = input('row/a');
  185. if (isset($row['times']) && $row['times']) {
  186. $times = explode(' - ', $row['times']);
  187. } else {
  188. $times = [date('Y-01-01'), date('Y-m-d')];
  189. }
  190. if (isset($row['type']) && $row['type']) {
  191. $type = $row['type'];
  192. } else {
  193. $type = 0;
  194. }
  195. $staff = Staff::info();
  196. $between = [strtotime($times[0]), strtotime($times[1]) + 86400 - 1];
  197. if ($type == 1) {//本人
  198. $ids = [$staff->id];
  199. } elseif ($type == 2) {//下属
  200. $ids = Staff::getLowerStaffId();
  201. } else {//全部
  202. $ids = Staff::getMyStaffIds();
  203. }
  204. if (isset($row['staff_id']) && $row['staff_id']) {
  205. $ids = $staff_id = $row['staff_id'];
  206. } else {
  207. $staff_id = '';
  208. }
  209. $datalist = Record::where(['create_staff_id' => ['in', $ids], 'createtime' => ['between', $between],'follow_type'=>['neq','其它']])
  210. ->field("create_staff_id,count(*) as c")
  211. ->group('create_staff_id')->select();
  212. $datainfo = $this->arraySortByOneField($datalist, 'c');
  213. $typedata = [];
  214. foreach ($datainfo as $k => $v) {
  215. $staff = Staff::where(array('id' => $v['create_staff_id']))->value('name');
  216. $typedata['name'][] = $staff;
  217. $typedata['num'][] = $v['c'];
  218. $datainfo[$k]['name'] = $staff;
  219. }
  220. $rank = $this->arraySortByOneField($datainfo, 'c', SORT_DESC);
  221. $this->view->assign([
  222. 'customerdata' => $typedata,
  223. 'dataInfo' => $rank,
  224. 'times' => implode(' - ', $times),
  225. 'type' => $type,
  226. 'staff_id' => $staff_id,
  227. ]);
  228. return $this->view->fetch();
  229. }
  230. /**
  231. * 排序
  232. * @param $data
  233. * @param $field
  234. * @param int $sort
  235. * @return mixed
  236. */
  237. public function arraySortByOneField($data, $field, $sort = SORT_ASC)
  238. {
  239. $field = array_column($data, $field);
  240. array_multisort($field, $sort, $data);
  241. return $data;
  242. }
  243. }