123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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','listproduct']
  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. $excelLogic = new ExcelLogic();
  92. $data = $excelLogic->productExcel($param, $list['list']);
  93. return $data;
  94. }
  95. /**
  96. * 产品销量列表
  97. * @author alvin guogaobo
  98. * @version 1.0 版本号
  99. * @since 2021/4/20 0020 16:16
  100. */
  101. public function listProduct(){
  102. $param = $this->param;
  103. $productModel = new \app\crm\model\Product();
  104. if (!empty($param['start_time'])) $param['start_time'] = $param['start_time'] . ' 00:00:00';
  105. if (!empty($param['end_time'])) $param['end_time'] = $param['end_time'] . ' 23:59:59';
  106. $list = $productModel->listProduct($param);
  107. return resultArray(['data' => $list]);
  108. }
  109. }