Data.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use think\App;
  5. use think\facade\Db;
  6. //数据统计类(数据看板,获取相关统计数据)
  7. class Data extends BaseController
  8. {
  9. //1.今日销量
  10. public function todaySalesVolume()
  11. {
  12. $rs = Db::name('sale')
  13. ->field('count(id) orders_number,sum(total_price) money')
  14. ->where('is_del', 0)
  15. ->whereDay('addtime', 'today')
  16. ->find();
  17. //跨库查询网络部
  18. $internet = Db::connect('mysql3')
  19. ->table('source_all')
  20. ->field('SUM(sale_total) money,COUNT(id) orders_number')
  21. ->whereDay('ordertime')
  22. ->where('depart', '网络部')
  23. ->find();
  24. $temp = [
  25. 'orders_number' => isset($rs['orders_number']) ? (isset($internet['orders_number']) ? $rs['orders_number'] + $internet['orders_number'] : $rs['orders_number']) : 0,
  26. 'money' => isset($rs['money']) ? (isset($internet['money']) ? $rs['money'] + $internet['money'] : $rs['money']) : 0,
  27. ];
  28. return app_show(0, '请求成功', $temp);
  29. }
  30. //2.今日销冠
  31. public function todaySalesChampion()
  32. {
  33. $temp = Db::name('sale')
  34. ->field('count(id) orders,sum(total_price) money,apply_id')
  35. ->where('is_del', 0)
  36. ->whereDay('addtime', 'today')
  37. ->group('apply_id')
  38. ->order('money', 'desc')
  39. ->limit(1)
  40. ->buildSql();
  41. $rs = Db::table($temp)
  42. ->alias('t')
  43. ->field('t.apply_id,t.orders,t.money,u.itemid company_id, c.`name` company, u.nickname')
  44. ->leftJoin('depart_user u', 'u.uid=t.apply_id AND u.is_del=0')
  45. ->leftJoin('company_item c', 'c.id=u.itemid AND c.is_del=0')
  46. ->select()
  47. ->toArray();
  48. return app_show(0, '请求成功', empty($rs[0]) ? [] : $rs[0]);
  49. }
  50. //3.今日采购
  51. public function todayPurchase()
  52. {
  53. $rs = Db::name('purchease_order')
  54. ->field('count(id) orders_number,sum(good_num) good_num')
  55. ->where('is_del', 0)
  56. ->whereDay('addtime', 'today')
  57. ->find();
  58. $consult_info_total = Db::name('consult_info')
  59. ->where('is_del', 0)
  60. ->whereDay('addtime', 'today')
  61. ->count('id');
  62. $consult_bids_total = Db::name('consult_bids')
  63. ->where('is_del', 0)
  64. ->whereDay('addtime', 'today')
  65. ->count('id');
  66. $data = [
  67. 'orders_number' => isset($rs['orders_number']) ? $rs['orders_number'] : 0,
  68. 'good_num' => isset($rs['good_num']) ? $rs['good_num'] : 0,
  69. 'consult_info_total' => $consult_info_total,
  70. 'consult_bids_total' => $consult_bids_total,
  71. ];
  72. return app_show(0, '请求成功', $data);
  73. }
  74. //4.票(相关,暂不做)
  75. //5.竞价单和采购单
  76. public function totalZixunPurchease()
  77. {
  78. //招标单数(咨询单数)
  79. $zixun_total = Db::name('consult_info')
  80. ->where(['is_del' => 0])
  81. ->count('id');
  82. //已反馈单数,
  83. $sql = Db::name('consult_info')
  84. ->field('zxNo')
  85. ->where(['is_del' => 0, 'status' => 1])
  86. ->buildSql();
  87. //正在进行中的咨询单(招标工作台上的数据)
  88. $zixun_num_ing = Db::name('consult_info')
  89. ->field('zxNo')
  90. ->where(['is_del' => 0, 'status' => 1])
  91. ->count('id');
  92. //已反馈的单数
  93. $temp_sql = Db::name('consult_bids')
  94. ->field('zxNo')
  95. ->where('zxNo IN ' . $sql)
  96. ->group('zxNo')
  97. ->buildSql();
  98. $not_feedback = Db::table($temp_sql)
  99. ->alias('t')
  100. ->count('t.zxNo');
  101. //未竞价单数(未反馈单数) = 进行中的单数-已反馈单数
  102. $not_feedback = $zixun_num_ing - $not_feedback;
  103. //采购单数
  104. $purchease_total = Db::name('purchease_order')
  105. ->where('is_del', 0)
  106. ->count('id');
  107. //采购单未下单数(状态为待与供应商确认)
  108. $purchease_wait_confirm = Db::name('purchease_order')
  109. ->where(['is_del' => 0, 'status' => 0])
  110. ->count('id');
  111. //采购单 采购下单数???????
  112. return app_show(0, '请求成功', [
  113. 'zixun_total' => $zixun_total,
  114. 'not_feedback' => $not_feedback,
  115. 'purchease_total' => $purchease_total,
  116. 'purchease_wait_confirm' => $purchease_wait_confirm,
  117. ]);
  118. }
  119. //6.未发货
  120. public function waitSendTotal()
  121. {
  122. $rs = Db::name('sale')
  123. ->field('count(id) order_num,sum(wsend_num) wsend_num')
  124. ->where(['is_del' => 0, 'status' => 1])//status==1 待发货
  125. ->find();
  126. return app_show(0, '请求成功', $rs);
  127. }
  128. //7.今日订单
  129. public function todaySale()
  130. {
  131. $temp = Db::name('sale')
  132. ->alias('s')
  133. ->field('s.id,s.apply_id,s.total_price,u.nickname,u.itemid')
  134. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  135. ->where('s.is_del', 0)
  136. ->whereDay('s.addtime', 'today')
  137. ->buildSql();
  138. $rs = Db::table($temp)
  139. ->alias('t')
  140. ->field('itemid companyId,c.`name` companyName,SUM(t.total_price) total_price,COUNT(t.id) total_order')
  141. ->leftJoin('company_item c', 'c.id=t.itemid')
  142. ->group('t.itemid')
  143. ->order('total_price', 'desc')
  144. ->column('itemid companyId,c.`name` companyName,SUM(t.total_price) total_price,COUNT(t.id) total_order', 'name');
  145. //跨库添加网络部数据
  146. $internet = Db::connect('mysql3')
  147. ->table('source_all')
  148. ->field('SUM(sale_total) total_price,COUNT(id) total_order ')
  149. ->whereDay('ordertime')
  150. ->where('depart', '网络部')
  151. ->find();
  152. if (!empty($internet)) {
  153. if (isset($rs['网络部'])) {
  154. $rs['网络部']['total_price'] += $internet['total_price'];
  155. $rs['网络部']['total_order'] += $internet['total_order'];
  156. } else {
  157. $rs[] = [
  158. 'companyId' => '',
  159. 'companyName' => '网络部',
  160. 'total_price' => $internet['total_price'],
  161. 'total_order' => $internet['total_order'],
  162. ];
  163. }
  164. //重新按照总金额排序
  165. usort($rs, function ($left, $right) {
  166. return ($left['total_price'] > $right['total_price']) ? -1 : 1;
  167. });
  168. }
  169. return app_show(0, '请求成功', $rs);
  170. }
  171. //8.本月完成率(数据在结算库里,部门完成率=部门净销售额 / 部门销售指标)
  172. //没有销售指标的部门,不纳入统计范围
  173. public function monthFinishRate()
  174. {
  175. //部门净销售额
  176. $sales_volume = Db::name('sale')
  177. ->alias('s')
  178. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  179. ->leftJoin('company_item c', 'c.id=u.itemid AND c.is_del=0')
  180. ->group('u.itemid')
  181. ->whereMonth('s.addtime')
  182. ->column('(sum(s.total_price) - sum(s.th_fee)) sales_volume,u.itemid,c.name', 'c.name');
  183. //额外把网络部的销售数据跨数据库查询出来
  184. $other_network = Db::connect('mysql3')
  185. ->table('source_all')
  186. ->whereMonth('ordertime')
  187. ->where('depart', '网络部')
  188. ->sum('sale_total');
  189. if (isset($sales_volume['网络部'])) $sales_volume['网络部']['sales_volume'] += $other_network;
  190. else {
  191. $sales_volume['网络部'] = [
  192. 'sales_volume' => $other_network,
  193. 'itemid' => -1,
  194. 'name' => '网络部'
  195. ];
  196. }
  197. //部门销售指标
  198. $sale_indicators = Db::name('depart_tips')
  199. ->field('id,total_tips,depart_item department')
  200. ->where(['year' => date('Y'), 'month' => date('n')])
  201. ->select()
  202. ->toArray();
  203. $da = [];
  204. //计算完成率
  205. foreach ($sale_indicators as $value) {
  206. if (isset($sales_volume[$value['department']]['sales_volume'])) {
  207. $value['finish'] = $sales_volume[$value['department']]['sales_volume'];
  208. $value['finish_rate'] = round(($value['finish'] / $value['total_tips']) * 100, 5);
  209. $da[] = $value;
  210. } else continue;
  211. }
  212. //按照完成率排序
  213. usort($da, function ($left, $right) {
  214. return ($left['finish_rate'] > $right['finish_rate']) ? -1 : 1;
  215. });
  216. //计算汇总完成率
  217. $total_finish_rate = round((array_sum(array_column($sales_volume, 'sales_volume')) / array_sum(array_column($da, 'total_tips'))) * 100, 2);
  218. return app_show(0, '请求成功', ['list' => $da, 'total_finish_rate' => $total_finish_rate]);
  219. }
  220. //9.转单率-今日
  221. public function orderTransferRateToday()
  222. {
  223. $consulting = Db::name('sale')
  224. ->alias('s')
  225. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  226. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  227. ->whereDay('s.addtime')
  228. ->group('u.itemid')
  229. ->column('count(s.id) consulting', 'u.itemid');
  230. $rs = Db::name('consult_order')
  231. ->alias('c')
  232. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  233. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  234. ->where(['c.is_del' => 0])
  235. ->whereDay('c.addtime')
  236. ->group('c.depart')
  237. ->append(['transfer_rate'])
  238. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  239. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  240. return round(($consult / $data['total']) * 100, 2);
  241. })
  242. ->select()
  243. ->toArray();
  244. return app_show(0, '请求成功', $rs);
  245. }
  246. //9.转单率-本月
  247. public function orderTransferRateMonth()
  248. {
  249. $consulting = Db::name('sale')
  250. ->alias('s')
  251. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  252. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  253. ->whereMonth('s.addtime')
  254. ->group('u.itemid')
  255. ->column('count(s.id) consulting', 'u.itemid');
  256. $rs = Db::name('consult_order')
  257. ->alias('c')
  258. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  259. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  260. ->where(['c.is_del' => 0])
  261. ->whereMonth('c.addtime')
  262. ->group('c.depart')
  263. ->append(['transfer_rate'])
  264. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  265. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  266. return round(($consult / $data['total']) * 100, 2);
  267. })
  268. ->select()
  269. ->toArray();
  270. return app_show(0, '请求成功', $rs);
  271. }
  272. //9.转单率-今年
  273. public function orderTransferRateYear()
  274. {
  275. $consulting = Db::name('sale')
  276. ->alias('s')
  277. ->leftJoin('depart_user u', 'u.uid=s.apply_id AND u.is_del=0')
  278. ->where(['s.order_type' => 3, 's.is_del' => 0])//order_type==3 咨询采反
  279. ->whereYear('s.addtime', date('Y'))
  280. ->group('u.itemid')
  281. ->column('count(s.id) consulting', 'u.itemid');
  282. $rs = Db::name('consult_order')
  283. ->alias('c')
  284. ->field('count(c.id) total,c.depart companyId,i.name companyName')
  285. ->leftJoin('company_item i', 'i.id=c.depart AND i.is_del=0')
  286. ->where(['c.is_del' => 0])
  287. ->whereYear('c.addtime', date('Y'))
  288. ->group('c.depart')
  289. ->append(['transfer_rate'])
  290. ->withAttr('transfer_rate', function ($val, $data) use ($consulting) {
  291. $consult = isset($consulting[$data['companyId']]) ? $consulting[$data['companyId']] : 0;
  292. return round(($consult / $data['total']) * 100, 2);
  293. })
  294. ->select()
  295. ->toArray();
  296. return app_show(0, '请求成功', $rs);
  297. }
  298. //******* 以下是新版数据大屏 的内容***********************************
  299. public function dnTodaySale()
  300. {
  301. $rs = Db::name('sale')
  302. ->field('id,good_num,total_price')
  303. ->where(['is_del' => 0])
  304. ->where('status', '<>', 3)
  305. ->whereDay('addtime', 'today')
  306. // ->fetchSql()->select();
  307. ->cursor();
  308. $data = ['total_sale' => 0, 'total_num' => 0, 'total_price' => 0];
  309. foreach ($rs as $value) {
  310. $data['total_sale']++;
  311. $data['total_num'] += $value['good_num'];
  312. $data['total_price'] += $value['total_price'];
  313. }
  314. return app_show(0, '请求成功', $data);
  315. }
  316. }