Product.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 商业智能-产品分析
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\bi\controller;
  8. use app\admin\controller\ApiCommon;
  9. use think\Db;
  10. use think\Hook;
  11. use think\Request;
  12. use app\bi\logic\ExcelLogic;
  13. class Product extends ApiCommon
  14. {
  15. /**
  16. * 用于判断权限
  17. * @permission 无限制
  18. * @allow 登录用户可访问
  19. * @other 其他根据系统设置
  20. **/
  21. public function _initialize()
  22. {
  23. $action = [
  24. 'permission' => [''],
  25. 'allow' => ['statistics', 'productcategory', 'excelexport']
  26. ];
  27. Hook::listen('check_auth', $action);
  28. $request = Request::instance();
  29. $a = strtolower($request->action());
  30. if (!in_array($a, $action['permission'])) {
  31. parent::_initialize();
  32. }
  33. if (!checkPerByAction('bi', 'product', 'read')) {
  34. header('Content-Type:application/json; charset=utf-8');
  35. exit(json_encode(['code' => 102, 'error' => '无权操作']));
  36. }
  37. }
  38. /**
  39. * 产品销量统计
  40. *
  41. * @param string $param
  42. * @return mixed|\think\response\Json
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\ModelNotFoundException
  45. * @throws \think\exception\DbException
  46. */
  47. public function statistics($param = '')
  48. {
  49. $productModel = new \app\crm\model\Product();
  50. if($param['excel_type']!=1){
  51. $param = $this->param;
  52. }
  53. if (!empty($param['start_time'])) $param['start_time'] = $param['start_time'] . ' 00:00:00';
  54. if (!empty($param['end_time'])) $param['end_time'] = $param['end_time'] . ' 23:59:59';
  55. $list = $productModel->getStatistics($param);
  56. //导出使用
  57. if (!empty($param['excel_type'])) {
  58. return $list;
  59. }
  60. return resultArray(['data' => $list]);
  61. }
  62. /**
  63. * 产品分类销量分析
  64. *
  65. * @return \think\response\Json
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. */
  70. public function productCategory()
  71. {
  72. $param = $this->param;
  73. $productModel = new \app\bi\model\Product();
  74. if (!empty($param['start_time'])) $param['start_time'] = $param['start_time'] . ' 00:00:00';
  75. if (!empty($param['end_time'])) $param['end_time'] =$param['end_time'] . ' 23:59:59';
  76. $list = $productModel->getStatistics($param);
  77. return resultArray(['data' => $list]);
  78. }
  79. /**
  80. * 导出
  81. *
  82. * @return mixed
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. * @throws \think\exception\DbException
  86. */
  87. public function excelExport()
  88. {
  89. $param = $this->param;
  90. $list = $this->statistics($param);
  91. $data = [];
  92. $subtotalCount = [];
  93. $sumCount = [];
  94. $item = [];
  95. $res = [];
  96. foreach ($list as &$val) {
  97. $res[] = $val['product_id'];
  98. $data[$val['product_id']][] = $val;
  99. }
  100. $res = array_unique($res);
  101. foreach ($res as $e) {
  102. $unm = 0;
  103. $subtotal = 0;
  104. foreach ($list as $v) {
  105. if ($e == $v['product_id']) {
  106. $unm += $v['num'];
  107. $subtotal += $v['subtotal'];
  108. $sumCount[$e] = $unm;
  109. $subtotalCount[$e] = (float)$subtotal;
  110. }
  111. }
  112. $item[$e][] = [
  113. 'type' => '',
  114. 'category_id_info' => '',
  115. 'product_name' => '',
  116. 'contract_num' => '',
  117. 'realname' => '',
  118. 'name' => '',
  119. 'price' => '合计',
  120. 'num' => $sumCount[$e],
  121. 'subtotal' => $subtotalCount[$e],
  122. ];
  123. }
  124. $items = [];
  125. foreach ($data as $key => $value) {
  126. $items[] = array_merge($data[$key], $item[$key]);
  127. }
  128. foreach ($items as $u) {
  129. foreach ($u as $d) {
  130. $field[] = $d;
  131. }
  132. }
  133. $excelLogic = new ExcelLogic();
  134. $data = $excelLogic->productExcel($param, $field);
  135. return $data;
  136. }
  137. }