Stat.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\Db;
  4. use think\App;
  5. class Stat extends \app\BaseController
  6. {
  7. public function __construct(App $app)
  8. {
  9. parent::__construct($app);
  10. }
  11. public function list()
  12. {
  13. $post = request()->post();
  14. $endtime = isset($post['date']) ? $post['date'] : date("Y-m-d");
  15. $sql = "
  16. SELECT
  17. `month` AS m,
  18. ifnull( month_total_fee, 0 ) AS total,
  19. ifnull( `week`, '' ) AS w,
  20. ifnull( week_total_fee, 0 ) AS wtotal,
  21. ifnull( `day`, '' ) AS d,
  22. ifnull( day_total_fee, 0 ) AS dtotal,
  23. ifnull( a.NAME, '' ) AS depart,
  24. a.id AS depar_id,
  25. ifnull( month_thfee, 0 ) AS mthfee,
  26. ifnull( week_thfee, 0 ) AS wthfee,
  27. ifnull( day_thfee, 0 ) AS thfee,
  28. ifnull( a.platform_name, '' ) AS p
  29. FROM
  30. (
  31. SELECT MONTH
  32. ( a.addtime ) AS `month`,
  33. SUM( total_price ) AS month_total_fee,
  34. (
  35. SELECT
  36. ifnull(sum(e.th_fee),0)
  37. FROM
  38. wsm_th_data e
  39. LEFT JOIN wsm_depart_user k ON e.apply_id = k.uid
  40. LEFT JOIN wsm_sale f on e.orderCode=f.orderCode
  41. AND k.is_del = 0
  42. AND k.STATUS = 1
  43. WHERE
  44. k.itemid = c.id
  45. AND f.platform_id = a.platform_id
  46. and f.is_del=0
  47. AND DATE_FORMAT( e.addtime, '%Y-%m' ) = DATE_FORMAT( '$endtime', '%Y-%m' )) AS month_thfee,
  48. a.platform_id,
  49. c.`name`,
  50. c.id,
  51. d.platform_name
  52. FROM
  53. wsm_sale a
  54. LEFT JOIN wsm_depart_user b ON a.apply_id = b.uid
  55. AND b.is_del = 0
  56. AND b.STATUS = 1
  57. LEFT JOIN wsm_platform d ON d.id = a.platform_id
  58. LEFT JOIN wsm_company_item c ON c.id = b.itemid
  59. WHERE
  60. DATE_FORMAT( a.addtime, '%Y-%m' ) = DATE_FORMAT( '$endtime', '%Y-%m' )
  61. and a.is_del=0
  62. GROUP BY
  63. a.platform_id,
  64. platform_name,
  65. c.NAME,
  66. c.id,
  67. `month`
  68. ) AS a
  69. LEFT JOIN (
  70. SELECT WEEK
  71. ( a.addtime, 1 ) AS `week`,
  72. SUM( total_price ) AS week_total_fee,
  73. (SELECT
  74. ifnull(sum(e.th_fee),0)
  75. FROM
  76. wsm_th_data e
  77. LEFT JOIN wsm_depart_user k ON e.apply_id = k.uid
  78. LEFT JOIN wsm_sale f on e.orderCode=f.orderCode
  79. AND k.is_del = 0
  80. AND k.STATUS = 1
  81. WHERE
  82. k.itemid = c.id
  83. and f.is_del=0
  84. AND f.platform_id = a.platform_id
  85. AND WEEK ( f.addtime, 1 ) = WEEK ( '$endtime', 1 )
  86. AND DATE_FORMAT ( f.addtime, '%Y' ) = DATE_FORMAT ( '$endtime', '%Y' )) AS week_thfee,
  87. a.platform_id,
  88. c.`name`,
  89. c.id,
  90. platform_name
  91. FROM
  92. wsm_sale a
  93. LEFT JOIN wsm_depart_user b ON a.apply_id = b.uid
  94. AND b.is_del = 0
  95. AND b.STATUS = 1
  96. LEFT JOIN wsm_platform d ON d.id = a.platform_id
  97. LEFT JOIN wsm_company_item c ON c.id = b.itemid
  98. WHERE
  99. WEEK ( a.addtime, 1 ) = WEEK ( '$endtime', 1 )
  100. and a.is_del=0
  101. GROUP BY
  102. a.platform_id,
  103. platform_name,
  104. c.NAME,
  105. c.`id`,
  106. `week`
  107. ) AS b ON a.NAME = b.NAME
  108. AND a.platform_id = b.platform_id
  109. LEFT JOIN (
  110. SELECT
  111. date_format( a.addtime, '%Y-%m-%d' ) AS `day`,
  112. SUM( total_price ) AS day_total_fee,
  113. (SELECT
  114. ifnull(sum(e.th_fee),0)
  115. FROM
  116. wsm_th_data e
  117. LEFT JOIN wsm_depart_user k ON e.apply_id = k.uid
  118. LEFT JOIN wsm_sale f on e.orderCode=f.orderCode
  119. AND k.is_del = 0
  120. AND k.STATUS = 1
  121. WHERE
  122. k.itemid = c.id
  123. and f.is_del=0
  124. AND f.platform_id = a.platform_id
  125. AND WEEK ( f.addtime, 1 ) = WEEK ( '$endtime', 1 )
  126. AND DATE_FORMAT( f.addtime, '%Y-%m-%d' ) = DATE_FORMAT( '$endtime', '%Y-%m-%d' )) AS day_thfee,
  127. a.platform_id,
  128. c.`name`,
  129. c.id AS depar_id,
  130. platform_name
  131. FROM
  132. wsm_sale a
  133. LEFT JOIN wsm_depart_user b ON a.apply_id = b.uid
  134. AND b.is_del = 0
  135. AND b.STATUS = 1
  136. LEFT JOIN wsm_platform d ON d.id = a.platform_id
  137. LEFT JOIN wsm_company_item c ON c.id = b.itemid
  138. WHERE
  139. date_format( a.addtime, '%Y-%m-%d' ) = date_format ( '$endtime', '%Y-%m-%d' )
  140. and a.is_del=0
  141. GROUP BY
  142. a.platform_id,
  143. platform_name,
  144. c.NAME,
  145. c.id,
  146. `day`
  147. ) AS d ON a.NAME = d.NAME
  148. AND a.platform_id = d.platform_id
  149. ";
  150. $data = Db::query($sql);
  151. $dtae = isset($endtime) && $endtime != "" ? $endtime . " 00:00:00" : date("Y-m-d H:i:s");
  152. $temp = ["p" => "总计", 'item' => "-", "thtotal" => 0, "total" => 0, "mthfee" => 0, "wtotal" => 0, "wthfee" => 0, "dtotal" => 0, "thfee" => 0, "tips" => 0, "rate" => 0, "thrate" => 0];
  153. $tips = Db::name("depart_tips")->field("depart_id ,total_tips")->where("year=YEAR('{$dtae}') and month=month('{$dtae}')")->select();
  154. $kdata = [];
  155. try{
  156. $wlb =$this->innterOrder($endtime);
  157. if(!empty($wlb)) $data=array_merge($data,$wlb);
  158. foreach ($data as $key => $value) {
  159. $value['tips'] = 0;
  160. foreach ($tips as $val) {
  161. if ($value["depar_id"] == $val['depart_id']) {
  162. $value['tips'] = $val['total_tips'];
  163. }
  164. }
  165. $value['thtotal'] = $value['total'] - $value['mthfee'];
  166. $kdata[] = $value;
  167. }
  168. $list = $this->check($kdata, $temp);
  169. array_walk($list, function (&$value) {
  170. if (is_null($value['tips']) || $value['tips'] == 0) {
  171. $value['tips'] = "-";
  172. $value["rate"] = "-";
  173. $value["thrate"] = "-";
  174. } else {
  175. $value["rate"] = number_format($value['total'] / $value['tips'] * 100, 2);
  176. $value["thrate"] = number_format(($value['total'] - $value['mthfee']) / $value['tips'] * 100, 2);
  177. $value['tips'] = sprintf("%.2f", $value['tips']);
  178. }
  179. $value['total'] = sprintf("%.2f", $value['total']);
  180. $value['mthfee'] = sprintf("%.2f", $value['mthfee']);
  181. $value['wtotal'] = sprintf("%.2f", $value['wtotal']);
  182. $value['wthfee'] = sprintf("%.2f", $value['wthfee']);
  183. $value['dtotal'] = sprintf("%.2f", $value['dtotal']);
  184. $value['thfee'] = sprintf("%.2f", $value['thfee']);
  185. $value['thtotal'] = sprintf("%.2f", $value['thtotal']);
  186. });
  187. $list=array_values($list);
  188. if ($temp['tips'] == 0) {
  189. $temp['rate'] = "-";
  190. $temp['thrate'] = "-";
  191. $temp['tips'] = "-";
  192. } else {
  193. $temp['rate'] = number_format($temp['total'] / $temp['tips'] * 100, 2);
  194. $temp['thrate'] = number_format(($temp['total'] - $temp['mthfee']) / $temp['tips'] * 100, 2);;
  195. $temp['tips'] = round($temp['tips'], 2);
  196. }
  197. $temp['total'] = sprintf("%.2f", $temp['total']);
  198. $temp['mthfee'] = sprintf("%.2f", $temp['mthfee']);
  199. $temp['wtotal'] = sprintf("%.2f", $temp['wtotal']);
  200. $temp['wthfee'] = sprintf("%.2f", $temp['wthfee']);
  201. $temp['dtotal'] = sprintf("%.2f", $temp['dtotal']);
  202. $temp['thfee'] = sprintf("%.2f", $temp['thfee']);
  203. $temp['thtotal'] = sprintf("%.2f", $temp['thtotal']);
  204. $sort = array_column($list, "thrate");
  205. array_multisort($sort, SORT_ASC, $list);
  206. array_push($list, $temp);
  207. $list = array_reverse($list);
  208. }catch (\Exception $e){
  209. return error_show(1004, $e->getFile()."|".$e->getLine()."|".$e->getMessage());
  210. }
  211. return app_show(0, "获取成功", $list);
  212. }
  213. public function check($data, &$temp)
  214. {
  215. $list = [];
  216. foreach ($data as $key => $value) {
  217. $temp['total'] += $value['total'];
  218. $temp['mthfee'] += $value['mthfee'];
  219. $temp['wtotal'] += $value['wtotal'];
  220. $temp['wthfee'] += $value['wthfee'];
  221. $temp['thfee'] += $value['thfee'];
  222. $temp['dtotal'] += $value['dtotal'];
  223. isset($list[$value['depar_id']]) ? "" : $list[$value['depar_id']] = ['total' => 0, 'mthfee' => 0, 'tips' => $value['tips'], 'wtotal' => 0, 'wthfee' => 0, 'dtotal' => 0,
  224. 'child' => [], 'depart' => '', 'thfee' => 0, 'thtotal' => 0];
  225. $list[$value['depar_id']]['total'] += $value['total'];
  226. $list[$value['depar_id']]['mthfee'] += $value['mthfee'];
  227. $list[$value['depar_id']]['wtotal'] += $value['wtotal'];
  228. $list[$value['depar_id']]['wthfee'] += $value['wthfee'];
  229. $list[$value['depar_id']]['dtotal'] += $value['dtotal'];
  230. $list[$value['depar_id']]['thfee'] += $value['thfee'];
  231. $list[$value['depar_id']]['thtotal'] += $value['thtotal'];
  232. $value['tips']="-";
  233. $value['total'] = sprintf("%.2f", $value['total']);
  234. $value['mthfee'] = sprintf("%.2f", $value['mthfee']);
  235. $value['wtotal'] = sprintf("%.2f", $value['wtotal']);
  236. $value['wthfee'] = sprintf("%.2f", $value['wthfee']);
  237. $value['dtotal'] = sprintf("%.2f", $value['dtotal']);
  238. $value['thfee'] = sprintf("%.2f", $value['thfee']);
  239. $value['thtotal'] = sprintf("%.2f", $value['thtotal']);
  240. $list[$value['depar_id']]['child'][] = $value;
  241. $list[$value['depar_id']]['depart'] = $value['depart'];
  242. }
  243. $temp['tips'] = array_sum(array_column($list,'tips'));
  244. $temp['thtotal'] = sprintf("%.2f", array_sum(array_column($list,'thtotal')));
  245. return $list;
  246. }
  247. public function innterOrder($endtime)
  248. {
  249. $db =Db::connect("mysql3");
  250. $list = $db->query("SELECT
  251. a.suppitem as p,
  252. a.depart,
  253. 43 as depar_id,
  254. m,
  255. a.sale_total AS total,
  256. `a`.`th_total` AS `mthfee`,
  257. IFNULL(w,WEEK('{$endtime}',1)) as w,
  258. IFNULL(b.sale_total,0) AS wtotal,
  259. ifnull( `b`.`th_total`, 0 ) AS `wthfee`,
  260. IFNULL(c.d,date_format('{$endtime}', '%Y-%m-%d' )) as d,
  261. IFNULL(c.sale_total,0) AS dtotal,
  262. ifnull( `c`.`th_total`, 0 ) AS `thfee`
  263. FROM
  264. (
  265. SELECT MONTH
  266. ( ordertime ) AS m,
  267. sum( sale_total ) AS sale_total,
  268. suppitem,
  269. depart,
  270. (
  271. SELECT
  272. ifnull( sum( t.th_qrd_fee ), 0 )
  273. FROM
  274. cfp_th_source t LEFT JOIN source_all l on l.productNo = t.th_qrdcpNo
  275. WHERE
  276. t.th_qrdcpNo <> '' and l.depart=k.depart and l.suppitem=k.suppitem and t.th_status<>3
  277. AND DATE_FORMAT ( t.createtime,'%Y-%m' ) = DATE_FORMAT ( '{$endtime}' ,'%Y-%m')
  278. ) AS th_total
  279. FROM
  280. source_all k
  281. WHERE
  282. DATE_FORMAT(ordertime,'%Y-%m') = DATE_FORMAT('{$endtime}','%Y-%m')
  283. GROUP BY
  284. suppitem,depart,
  285. m
  286. ) AS a
  287. LEFT JOIN (
  288. SELECT WEEK
  289. ( ordertime, 1 ) AS w,
  290. sum( sale_total ) AS sale_total,
  291. suppitem,
  292. depart,
  293. (
  294. SELECT
  295. ifnull( sum( t.th_qrd_fee ), 0 )
  296. FROM
  297. cfp_th_source t LEFT JOIN source_all l on l.productNo = t.th_qrdcpNo
  298. WHERE
  299. t.th_qrdcpNo <> '' and l.depart=k.depart and l.suppitem=k.suppitem and t.th_status<>3
  300. AND week ( t.createtime,1 ) = WEEK ( '{$endtime}', 1 ) and DATE_FORMAT ( t.createtime,'%Y' ) = DATE_FORMAT ( '{$endtime}' ,'%Y')
  301. ) AS th_total
  302. FROM
  303. source_all k
  304. WHERE
  305. WEEK ( ordertime, 1 ) = WEEK ( '{$endtime}', 1 ) and DATE_FORMAT ( ordertime,'%Y' ) = DATE_FORMAT ( '{$endtime}' ,'%Y')
  306. GROUP BY
  307. suppitem,depart,
  308. w
  309. ) AS b ON a.suppitem = b.suppitem and a.depart=b.depart
  310. LEFT JOIN (
  311. SELECT
  312. date_format( ordertime, '%Y-%m-%d' ) AS d,
  313. sum( sale_total ) AS sale_total,
  314. suppitem,depart,
  315. (
  316. SELECT
  317. ifnull( sum( t.th_qrd_fee ), 0 )
  318. FROM
  319. cfp_th_source t LEFT JOIN source_all l on l.productNo = t.th_qrdcpNo
  320. WHERE
  321. t.th_qrdcpNo <> '' and l.depart=k.depart and l.suppitem=k.suppitem and t.th_status<>3
  322. AND date_format ( t.createtime,'%Y-%m-%d') = date_format ( '{$endtime}', '%Y-%m-%d' )
  323. ) AS th_total FROM
  324. source_all k
  325. WHERE
  326. date_format( ordertime, '%Y-%m-%d' ) = date_format( '{$endtime}', '%Y-%m-%d' )
  327. GROUP BY
  328. suppitem,depart,
  329. d
  330. ) AS c ON a.suppitem = c.suppitem and a.depart=c.depart
  331. ");
  332. return $list;
  333. }
  334. }