Business.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. class Business extends Common
  12. {
  13. /**
  14. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  15. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  16. */
  17. protected $name = 'crm_business';
  18. /**
  19. * [getDataCount 商机count]
  20. * @author Michael_xu
  21. * @param
  22. * @return
  23. */
  24. function getDataCount($whereArr)
  25. {
  26. $where = [];
  27. $dataCount = $this->where($whereArr)->where($where)->count('business_id');
  28. $count = $dataCount ? : 0;
  29. return $count;
  30. }
  31. /**
  32. * [getDataMoney 商机金额]
  33. * @author Michael_xu
  34. * @param
  35. * @return
  36. */
  37. function getDataMoney($whereArr)
  38. {
  39. $where = [];
  40. $money = $this->where($whereArr)->where($where)->sum('money');
  41. return $money;
  42. }
  43. /**
  44. * 获取商机list
  45. *
  46. * @param $param
  47. * @return mixed
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws \think\exception\DbException
  51. */
  52. function getDataList($param)
  53. {
  54. $page = !empty($param['page']) ? $param['page'] : 1;
  55. $limit = !empty($param['limit']) ? $param['limit'] : 15;
  56. unset($param['page']);
  57. unset($param['limit']);
  58. $userModel = new \app\admin\model\User();
  59. $adminModel = new \app\admin\model\Admin();
  60. $perUserIds = $userModel->getUserByPer('bi', 'business', 'read'); //权限范围内userIds
  61. $whereData = $adminModel->getWhere($param, '', $perUserIds); //统计条件
  62. $userIds = $whereData['userIds'];
  63. if (!empty($whereData['between_time']['last_time'])) unset($whereData['between_time']['last_time']);
  64. $between_time = $whereData['between_time'];
  65. $where['business.owner_user_id'] = array('in',$userIds);
  66. $where['business.create_time'] = ['between', $between_time];
  67. $where['check_status'] = 2;
  68. if (!empty($param['is_end']) && $param['is_end'] == 1) $where['is_end'] = 1;
  69. $count = db('crm_business')->alias('business')
  70. ->join('__CRM_CONTRACT__ contract', 'contract.business_id = business.business_id', 'left')
  71. ->where($where)->group('business.business_id')->count();
  72. $sql = db('crm_business')->alias('business')
  73. ->field('business.business_id,business.customer_id,business.money,business.type_id,business.status_id,business.deal_date,business.create_user_id,business.owner_user_id')
  74. ->join('__CRM_CONTRACT__ contract', 'contract.business_id = business.business_id', 'left')
  75. ->where($where)
  76. ->fetchSql()
  77. ->limit(($page - 1) * $limit, $limit)
  78. ->order(['money' => 'DESC'])
  79. ->group('business.business_id')
  80. ->select();
  81. return ['dataCount' => $count, 'list' => queryCache($sql)];
  82. }
  83. }