123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 产品
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\bi\model;
  8. use think\Db;
  9. use app\admin\model\Common;
  10. use think\Request;
  11. use think\Validate;
  12. class Product extends Common
  13. {
  14. /**
  15. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  16. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  17. */
  18. protected $name = 'crm_product';
  19. protected $createTime = 'create_time';
  20. protected $updateTime = 'update_time';
  21. protected $autoWriteTimestamp = true;
  22. /**
  23. * [产品分类销量分析]
  24. *
  25. * @param $request
  26. * @return mixed
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. * @throws \think\exception\DbException
  30. */
  31. public function getStatistics($request)
  32. {
  33. $where = $this->getWhere($request);
  34. $join = [
  35. ['__CRM_CONTRACT__ contract', 'contract.contract_id = a.contract_id', 'LEFT'],
  36. ['__ADMIN_USER__ user', 'user.id = contract.owner_user_id', 'LEFT'],
  37. ['__CRM_PRODUCT__ product', 'product.product_id = a.product_id', 'LEFT'],
  38. ['__CRM_PRODUCT_CATEGORY__ product_category', 'product_category.category_id = product.category_id', 'LEFT'],
  39. ];
  40. $sql = db('crm_contract_product')
  41. ->alias('a')
  42. ->where($where)
  43. ->join($join)
  44. ->group('a.product_id')
  45. ->field('a.product_id,sum(a.num) as num,product.name as product_name,product_category.name as category_id_info,product_category.category_id')
  46. ->fetchSql()
  47. ->select();
  48. $list = queryCache($sql);
  49. $data = [];
  50. foreach ($list AS $key => $value) {
  51. if (empty($data[$value['category_id']])) {
  52. $data[$value['category_id']] = $value;
  53. } else {
  54. $data[$value['category_id']]['num'] += $value['num'];
  55. }
  56. }
  57. return array_values($data);
  58. }
  59. /**
  60. * 产品成交客户数
  61. * @param
  62. * @return
  63. */
  64. function getDealByProduct($request)
  65. {
  66. $where = $this->getWhere($request);
  67. $where['customer.deal_status'] = '已成交';
  68. $join = [
  69. ['__CRM_CONTRACT__ contract', 'contract.contract_id = a.contract_id', 'LEFT'],
  70. ['__ADMIN_USER__ user', 'user.id = contract.owner_user_id', 'LEFT'],
  71. ['__CRM_PRODUCT__ product', 'product.product_id = a.product_id', 'LEFT'],
  72. ['__CRM_PRODUCT_CATEGORY__ product_category', 'product_category.category_id = product.category_id', 'LEFT'],
  73. ['__CRM_CUSTOMER__ customer', 'customer.customer_id = contract.customer_id', 'LEFT'],
  74. ];
  75. $list = db('crm_contract_product')
  76. ->alias('a')
  77. ->where($where)
  78. ->join($join)
  79. ->group('a.product_id')
  80. ->field('a.product_id,count(customer.customer_id) as num,product.name as product_name,product_category.name as category_id_info,product_category.category_id')
  81. ->select();
  82. return $list;
  83. }
  84. /**
  85. * 产品成交周期
  86. * @param
  87. * @return
  88. */
  89. function getCycleByProduct($request,$product_id)
  90. {
  91. $where = $this->getWhere($request);
  92. $where['customer.deal_status'] = '已成交';
  93. $where['a.product_id'] = $product_id;
  94. $join = [
  95. ['__CRM_CONTRACT__ contract', 'contract.contract_id = a.contract_id', 'LEFT'],
  96. ['__ADMIN_USER__ user', 'user.id = contract.owner_user_id', 'LEFT'],
  97. ['__CRM_PRODUCT__ product', 'product.product_id = a.product_id', 'LEFT'],
  98. ['__CRM_PRODUCT_CATEGORY__ product_category', 'product_category.category_id = product.category_id', 'LEFT'],
  99. ['__CRM_CUSTOMER__ customer', 'customer.customer_id = contract.customer_id', 'LEFT'],
  100. ];
  101. $list = db('crm_contract_product')
  102. ->alias('a')
  103. ->where($where)
  104. ->join($join)
  105. ->order('order_date')
  106. ->group('customer.customer_id')
  107. ->field('customer.customer_id')
  108. ->select();
  109. $customer_ids = array();
  110. foreach ($list as $key => $value) {
  111. $customer_ids[] = $value['customer_id'];
  112. }
  113. return $customer_ids;
  114. }
  115. /**
  116. * 产品销量排行
  117. * @param
  118. * @return
  119. */
  120. function getSortByProduct($request)
  121. {
  122. $where = $this->getWhere($request);
  123. $join = [
  124. ['__CRM_CONTRACT__ contract', 'contract.contract_id = a.contract_id', 'LEFT'],
  125. ['__ADMIN_USER__ user', 'user.id = contract.owner_user_id', 'LEFT'],
  126. ['__CRM_PRODUCT__ product', 'product.product_id = a.product_id', 'LEFT'],
  127. ['__CRM_PRODUCT_CATEGORY__ product_category', 'product_category.category_id = product.category_id', 'LEFT'],
  128. ];
  129. $sql = db('crm_contract_product')
  130. ->alias('a')
  131. ->where($where)
  132. ->join($join)
  133. ->order('num desc')
  134. ->group('contract.owner_user_id')
  135. ->field('sum(a.num) as num,contract.owner_user_id')
  136. ->fetchSql()
  137. ->select();
  138. $list = queryCache($sql);
  139. return $list;
  140. }
  141. /**
  142. * 获取条件
  143. * @param
  144. * @return
  145. */
  146. function getWhere($param)
  147. {
  148. $adminModel = new \app\admin\model\Admin();
  149. $userModel = new \app\admin\model\User();
  150. $perUserIds = $userModel->getUserByPer('bi', 'product', 'read'); //权限范围内userIds
  151. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  152. $userIds = $whereData['userIds'];
  153. $between_time = $whereData['between_time'];
  154. $where = [];
  155. if(!empty($param['category_id'])){
  156. $where['product_category.category_id'] = array('eq',$param['category_id']);
  157. }
  158. $where['contract.check_status'] = array('eq',2);
  159. $where['contract.owner_user_id'] = array('in',$userIds);
  160. $where['contract.create_time'] = array('between',$between_time);
  161. return $where;
  162. }
  163. }