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