Stataftersale.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. /**
  3. * 售后统计分析
  4. */
  5. namespace app\admin\controller;
  6. use think\facade\View;
  7. use think\facade\Lang;
  8. /**
  9. * ============================================================================
  10. *
  11. * ============================================================================
  12. *
  13. * ----------------------------------------------------------------------------
  14. *
  15. * ============================================================================
  16. * 控制器
  17. */
  18. class Stataftersale extends AdminControl
  19. {
  20. private $search_arr; //处理后的参数
  21. public function initialize()
  22. {
  23. parent::initialize(); // TODO: Change the autogenerated stub
  24. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/stat.lang.php');
  25. include_once root_path() . 'extend/mall/statistics.php';
  26. include_once root_path() . 'extend/mall/datehelper.php';
  27. $stat_model = model('stat');
  28. //存储参数
  29. $this->search_arr = input('param.');
  30. //处理搜索时间
  31. if (in_array(request()->action(), array('refund'))) {
  32. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  33. //获得系统年份
  34. $year_arr = getSystemYearArr();
  35. //获得系统月份
  36. $month_arr = getSystemMonthArr();
  37. //获得本月的周时间段
  38. $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  39. View::assign('year_arr', $year_arr);
  40. View::assign('month_arr', $month_arr);
  41. View::assign('week_arr', $week_arr);
  42. }
  43. View::assign('search_arr', $this->search_arr);
  44. }
  45. /**
  46. * 退款统计
  47. */
  48. public function refund()
  49. {
  50. $where = array();
  51. if (!isset($this->search_arr['search_type'])) {
  52. $this->search_arr['search_type'] = 'day';
  53. }
  54. $stat_model = model('stat');
  55. //获得搜索的开始时间和结束时间
  56. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  57. $field = ' SUM(refund_amount) as amount ';
  58. if ($this->search_arr['search_type'] == 'day') {
  59. //构造横轴数据
  60. for ($i = 0; $i < 24; $i++) {
  61. $stat_arr['xAxis']['categories'][] = "$i";
  62. $statlist[$i] = 0;
  63. }
  64. $field .= ' ,HOUR(FROM_UNIXTIME(add_time)) as timeval ';
  65. }
  66. if ($this->search_arr['search_type'] == 'week') {
  67. //构造横轴数据
  68. for ($i = 1; $i <= 7; $i++) {
  69. $tmp_weekarr = getSystemWeekArr();
  70. //横轴
  71. $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
  72. unset($tmp_weekarr);
  73. $statlist[$i] = 0;
  74. }
  75. $field .= ' ,WEEKDAY(FROM_UNIXTIME(add_time))+1 as timeval ';
  76. }
  77. if ($this->search_arr['search_type'] == 'month') {
  78. //计算横轴的最大量(由于每个月的天数不同)
  79. $dayofmonth = date('t', $searchtime_arr[0]);
  80. //构造横轴数据
  81. for ($i = 1; $i <= $dayofmonth; $i++) {
  82. //横轴
  83. $stat_arr['xAxis']['categories'][] = $i;
  84. $statlist[$i] = 0;
  85. }
  86. $field .= ' ,day(FROM_UNIXTIME(add_time)) as timeval ';
  87. }
  88. $where = array();
  89. $where[] = array('add_time', 'between', $searchtime_arr);
  90. $statlist_tmp = $stat_model->statByRefundreturn($where, $field, 0, 0, 'timeval asc', 'timeval');
  91. if ($statlist_tmp) {
  92. foreach ((array)$statlist_tmp as $k => $v) {
  93. $statlist[$v['timeval']] = floatval($v['amount']);
  94. }
  95. }
  96. //得到统计图数据
  97. $stat_arr['legend']['enabled'] = false;
  98. $stat_arr['series'][0]['name'] = lang('stattrade_refund_total');
  99. $stat_arr['series'][0]['data'] = array_values($statlist);
  100. $stat_arr['title'] = lang('refund_amount_statis');
  101. $stat_arr['yAxis'] = lang('stattrade_refund_total');
  102. $stat_json = getStatData_LineLabels($stat_arr);
  103. View::assign('stat_json', $stat_json);
  104. View::assign('searchtime', implode('|', $searchtime_arr));
  105. $this->setAdminCurItem('refund');
  106. return View::fetch('aftersale_refund');
  107. }
  108. /**
  109. * 退款统计
  110. */
  111. public function refundlist()
  112. {
  113. $refundreturn_model = model('refundreturn');
  114. $refundstate_arr = $this->getRefundStateArray();
  115. $where = array();
  116. $statlist = array();
  117. $searchtime_arr_tmp = explode('|', $this->search_arr['t']);
  118. foreach ((array)$searchtime_arr_tmp as $k => $v) {
  119. $searchtime_arr[] = intval($v);
  120. }
  121. $where[] = array('add_time', 'between', $searchtime_arr);
  122. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  123. $refundlist_tmp = $refundreturn_model->getRefundreturnList($where, 0);
  124. } else {
  125. $refundlist_tmp = $refundreturn_model->getRefundreturnList($where, 10);
  126. }
  127. $statheader = array();
  128. $statheader[] = array('text' => lang('ds_order_sn'), 'key' => 'order_sn');
  129. $statheader[] = array('text' => lang('ds_refund_sn'), 'key' => 'refund_sn');
  130. $statheader[] = array('text' => lang('ds_store_name'), 'key' => 'store_name', 'class' => 'alignleft');
  131. $statheader[] = array('text' => lang('ds_goods_name'), 'key' => 'goods_name', 'class' => 'alignleft');
  132. $statheader[] = array('text' => lang('ds_member_name'), 'key' => 'buyer_name');
  133. $statheader[] = array('text' => lang('apply_time'), 'key' => 'add_time');
  134. $statheader[] = array('text' => lang('stattrade_refund_total'), 'key' => 'refund_amount');
  135. $statheader[] = array('text' => lang('seller_state'), 'key' => 'seller_state');
  136. $statheader[] = array('text' => lang('admin_state'), 'key' => 'refund_state');
  137. foreach ((array)$refundlist_tmp as $k => $v) {
  138. $tmp = $v;
  139. foreach ((array)$statheader as $h_k => $h_v) {
  140. $tmp[$h_v['key']] = $v[$h_v['key']];
  141. if ($h_v['key'] == 'add_time') {
  142. $tmp[$h_v['key']] = @date('Y-m-d', $v['add_time']);
  143. }
  144. if ($h_v['key'] == 'refund_state') {
  145. $tmp[$h_v['key']] = $v['seller_state'] == 2 ? $refundstate_arr['admin'][$v['refund_state']] : lang('none');
  146. }
  147. if ($h_v['key'] == 'seller_state') {
  148. $tmp[$h_v['key']] = $refundstate_arr['seller'][$v['seller_state']];
  149. }
  150. if ($h_v['key'] == 'goods_name') {
  151. $tmp[$h_v['key']] = '<a href="' . (string)url('Goods/index', array('goods_id' => $v['goods_id'])) . '" target="_blank">' . $v['goods_name'] . '</a>';
  152. }
  153. }
  154. $statlist[] = $tmp;
  155. }
  156. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  157. //导出Excel
  158. $excel_obj = new \excel\Excel();
  159. $excel_data = array();
  160. //设置样式
  161. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  162. //header
  163. foreach ((array)$statheader as $k => $v) {
  164. $excel_data[0][] = array('styleid' => 's_title', 'data' => $v['text']);
  165. }
  166. //data
  167. foreach ((array)$statlist as $k => $v) {
  168. foreach ((array)$statheader as $h_k => $h_v) {
  169. $excel_data[$k + 1][] = array('data' => $v[$h_v['key']]);
  170. }
  171. }
  172. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  173. $excel_obj->addArray($excel_data);
  174. $excel_obj->addWorksheet($excel_obj->charset(lang('refund_log'), CHARSET));
  175. $excel_obj->generateXML($excel_obj->charset(lang('refund_log'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  176. exit();
  177. } else {
  178. View::assign('statheader', $statheader);
  179. View::assign('statlist', $statlist);
  180. View::assign('show_page', $refundreturn_model->page_info->render());
  181. View::assign('searchtime', input('param.t'));
  182. View::assign('actionurl', (string)url('Stataftersale/refundlist', ['t' => $this->search_arr['t']]));
  183. echo View::fetch('stat_listandorder');
  184. }
  185. }
  186. /**
  187. * 店铺动态评分统计
  188. */
  189. public function evalstore()
  190. {
  191. //店铺分类
  192. View::assign('class_list', rkcache('storeclass', true));
  193. $stat_model = model('stat');
  194. $where = array();
  195. $statlist = array();
  196. if (intval(input('param.store_class')) > 0) {
  197. $where[] = array('storeclass_id', '=', intval(input('param.store_class')));
  198. }
  199. if (isset($this->search_arr['storename'])) {
  200. $where[] = array('seval_storename', 'like', "%" . trim($this->search_arr['storename']) . "%");
  201. }
  202. $field = ' seval_storeid, seval_storename';
  203. $field .= ' ,(SUM(seval_desccredit)/COUNT(*)) as avgdesccredit';
  204. $field .= ' ,(SUM(seval_servicecredit)/COUNT(*)) as avgservicecredit';
  205. $field .= ' ,(SUM(seval_deliverycredit)/COUNT(*)) as avgdeliverycredit';
  206. $orderby_arr = array('avgdesccredit asc', 'avgdesccredit desc', 'avgservicecredit asc', 'avgservicecredit desc', 'avgdeliverycredit asc', 'avgdeliverycredit desc');
  207. if (!isset($this->search_arr['orderby']) || !in_array(trim($this->search_arr['orderby']), $orderby_arr)) {
  208. $this->search_arr['orderby'] = 'avgdesccredit desc';
  209. }
  210. $orderby = trim($this->search_arr['orderby']) . ',seval_storeid';
  211. //查询评论的店铺总数
  212. $count_arr = $stat_model->statByStoreAndEvaluatestore($where, 'count(DISTINCT evaluatestore.seval_storeid) as countnum');
  213. $countnum = intval($count_arr[0]['countnum']);
  214. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  215. $statlist_tmp = $stat_model->statByStoreAndEvaluatestore($where, $field, 0, 0, $orderby, 'seval_storeid');
  216. } else {
  217. $statlist_tmp = $stat_model->statByStoreAndEvaluatestore($where, $field, 10, 0, $orderby, 'seval_storeid');
  218. }
  219. foreach ((array)$statlist_tmp as $k => $v) {
  220. $tmp = $v;
  221. $tmp['avgdesccredit'] = round($v['avgdesccredit'], 2);
  222. $tmp['avgservicecredit'] = round($v['avgservicecredit'], 2);
  223. $tmp['avgdeliverycredit'] = round($v['avgdeliverycredit'], 2);
  224. $statlist[] = $tmp;
  225. }
  226. //导出Excel
  227. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  228. //导出Excel
  229. $excel_obj = new \excel\Excel();
  230. $excel_data = array();
  231. //设置样式
  232. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  233. //header
  234. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_store_name'));
  235. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('avgdesccredit'));
  236. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('avgservicecredit'));
  237. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('avgdeliverycredit'));
  238. //data
  239. foreach ((array)$statlist as $k => $v) {
  240. $excel_data[$k + 1][] = array('data' => $v['seval_storename']);
  241. $excel_data[$k + 1][] = array('data' => $v['avgdesccredit']);
  242. $excel_data[$k + 1][] = array('data' => $v['avgservicecredit']);
  243. $excel_data[$k + 1][] = array('data' => $v['avgdeliverycredit']);
  244. }
  245. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  246. $excel_obj->addArray($excel_data);
  247. $excel_obj->addWorksheet($excel_obj->charset(lang('store_dynamic_score_statis'), CHARSET));
  248. $excel_obj->generateXML($excel_obj->charset(lang('store_dynamic_score_statis'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  249. exit();
  250. }
  251. View::assign('statlist', $statlist);
  252. View::assign('orderby', $this->search_arr['orderby']);
  253. View::assign('show_page', $stat_model->page_info->render());
  254. $this->setAdminCurItem('evalstore');
  255. return View::fetch('aftersale_evalstore');
  256. }
  257. function getRefundStateArray($type = 'all')
  258. {
  259. $state_array = array(
  260. '1' => lang('refund_state_confirm'),
  261. '2' => lang('refund_state_yes'),
  262. '3' => lang('refund_state_no')
  263. ); //卖家处理状态:1为待审核,2为同意,3为不同意
  264. View::assign('state_array', $state_array);
  265. $admin_array = array(
  266. '1' => lang('admin_state_1'),
  267. '2' => lang('admin_state_2'),
  268. '3' => lang('admin_state_3')
  269. ); //确认状态:1为买家或卖家处理中,2为待平台管理员处理,3为退款退货已完成
  270. View::assign('admin_array', $admin_array);
  271. $state_data = array(
  272. 'seller' => $state_array,
  273. 'admin' => $admin_array
  274. );
  275. if ($type == 'all') {
  276. return $state_data; //返回所有
  277. }
  278. return $state_data[$type];
  279. }
  280. protected function getAdminItemList()
  281. {
  282. $menu_array = array(
  283. array('name' => 'refund', 'text' => lang('stat_refund'), 'url' => (string)url('Stataftersale/refund')),
  284. array('name' => 'evalstore', 'text' => lang('stat_evalstore'), 'url' => (string)url('Stataftersale/evalstore')),
  285. );
  286. return $menu_array;
  287. }
  288. }