123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: CRM工作台
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\crm\model;
  8. use think\Db;
  9. use app\admin\model\Common;
  10. use think\Request;
  11. use think\Validate;
  12. use think\helper\Time;
  13. class Index extends Common
  14. {
  15. /**
  16. * 销售简报
  17. * @param
  18. * @return
  19. * @author Michael_xu
  20. */
  21. public function getSalesData($param)
  22. {
  23. $where = array();
  24. $start_time = $param['start_time'];
  25. $where['create_time'] = Time::today();
  26. }
  27. public function getQueryRepeat($type, $content)
  28. {
  29. $result = [];
  30. $customerList = [];
  31. $poolList = [];
  32. $leadsList = [];
  33. # 客户列表
  34. $customerList = $this->getCustomerList($type, $content);
  35. # 公海列表
  36. if (count($customerList) < 10) $poolList = $this->getPoolList($type, $content);
  37. # 线索列表
  38. if (count($customerList) + count($poolList) < 10) $leadsList = $this->getLeadsList($type, $content);
  39. # 处理客户列表数据
  40. foreach ($customerList as $key => $value) {
  41. $ownerUserName = !empty($value['owner_user_id']) ? db('admin_user')->where('id', $value['owner_user_id'])->value('realname') : '';
  42. if (!empty($ownerUserName)) {
  43. $result[] = [
  44. 'id' => $value['customer_id'],
  45. 'name' => $value['name'],
  46. 'create_time' => date('Y-m-d H:i:s', $value['create_time']),
  47. 'mobile' => !empty($value['mobile']) ? $value['mobile'] : '',
  48. 'telephone' => !empty($value['telephone']) ? $value['telephone'] : '',
  49. 'last_time' => !empty($value['deal_time']) ? date('Y-m-d H:i:s', $value['deal_time']) : '',
  50. 'owner_user_name' => $ownerUserName,
  51. 'module' => '客户模块',
  52. 'type' => 2
  53. ];
  54. }
  55. }
  56. # 处理公海列表数据
  57. foreach ($poolList as $key => $value) {
  58. $ownerUserName = !empty($value['owner_user_id']) ? db('admin_user')->where('id', $value['owner_user_id'])->value('realname') : '';
  59. $result[] = [
  60. 'id' => $value['customer_id'],
  61. 'name' => $value['name'],
  62. 'create_time' => date('Y-m-d H:i:s', $value['create_time']),
  63. 'mobile' => !empty($value['mobile']) ? $value['mobile'] : '',
  64. 'telephone' => !empty($value['telephone']) ? $value['telephone'] : '',
  65. 'last_time' => !empty($value['deal_time']) ? date('Y-m-d H:i:s', $value['deal_time']) : '',
  66. 'owner_user_name' => $ownerUserName,
  67. 'module' => '公海模块',
  68. 'type' => 9,
  69. // guogaobo 公海数据权限
  70. 'poolAuthList' => [
  71. 'receive' => true,
  72. 'excelexport' => true,
  73. 'poolId' => $value['customer_id'], // guogaobo 多公海使用 公海类型id
  74. 'index' => true,
  75. 'distribute' => true,
  76. 'delete' => true
  77. ]
  78. ];
  79. }
  80. # 处理线索模块数据
  81. foreach ($leadsList as $key => $value) {
  82. $ownerUserName = !empty($value['owner_user_id']) ? db('admin_user')->where('id', $value['owner_user_id'])->value('realname') : '';
  83. $result[] = [
  84. 'id' => $value['leads_id'],
  85. 'name' => $value['name'],
  86. 'create_time' => date('Y-m-d H:i:s', $value['create_time']),
  87. 'mobile' => !empty($value['mobile']) ? $value['mobile'] : '',
  88. 'telephone' => !empty($value['telephone']) ? $value['telephone'] : '',
  89. 'last_time' => '',
  90. 'owner_user_name' => $ownerUserName,
  91. 'module' => '线索模块',
  92. 'type' => 1
  93. ];
  94. }
  95. return $result;
  96. }
  97. /**
  98. * 获取客户列表
  99. *
  100. * @param $type
  101. * @param $content
  102. * @return bool|\PDOStatement|string|\think\Collection
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\ModelNotFoundException
  105. * @throws \think\exception\DbException
  106. */
  107. private function getCustomerList($type, $content)
  108. {
  109. # 默认条件
  110. $customerWhere = $this->getCustomerWhere();
  111. # 查询条件
  112. $searchWhere = $this->getSearchWhere($type, $content);
  113. # 查询字段
  114. $field = ['customer_id', 'name', 'create_time', 'owner_user_id', 'deal_time', 'telephone', 'mobile'];
  115. return db('crm_customer')->alias('customer')->field($field)->where($customerWhere)
  116. ->where($searchWhere)->limit(10)->order('update_time', 'desc')->select();
  117. }
  118. /**
  119. * 获取公海客户列表
  120. *
  121. * @param $type
  122. * @param $content
  123. * @return bool|\PDOStatement|string|\think\Collection
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. * @throws \think\exception\DbException
  127. */
  128. private function getPoolList($type, $content)
  129. {
  130. # 公海条件
  131. $poolWhere = $this->getPoolWhere();
  132. # 查询条件
  133. $searchWhere = $this->getSearchWhere($type, $content);
  134. # 查询字段
  135. $field = ['customer_id', 'name', 'create_time', 'owner_user_id', 'deal_time', 'telephone', 'mobile'];
  136. return db('crm_customer')->alias('customer')->field($field)->where($poolWhere)
  137. ->where($searchWhere)->limit(10)->order('update_time', 'desc')->select();
  138. }
  139. /**
  140. * 获取线索列表
  141. *
  142. * @param $type
  143. * @param $content
  144. * @return bool|\PDOStatement|string|\think\Collection
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\ModelNotFoundException
  147. * @throws \think\exception\DbException
  148. */
  149. private function getLeadsList($type, $content)
  150. {
  151. # 查询条件
  152. $searchWhere = $this->getSearchWhere($type, $content);
  153. # 查询字段
  154. $field = ['leads_id', 'name', 'telephone', 'mobile', 'owner_user_id', 'create_time', 'is_transform'];
  155. return db('crm_leads')->field($field)->where($searchWhere)->where('is_transform', 0)->limit(10)
  156. ->order('update_time', 'desc')->select();
  157. }
  158. /**
  159. * 获取查询条件
  160. *
  161. * @param $type
  162. * @param $content
  163. * @return array|\Closure
  164. */
  165. private function getSearchWhere($type, $content)
  166. {
  167. $searchWhere = [];
  168. # 查询客户名称
  169. if ($type == 'name') {
  170. $searchWhere = function ($query) use ($content) {
  171. $query->where('name', 'like', '%' . $content . '%');
  172. };
  173. }
  174. # 查询手机或电话
  175. if ($type == 'phone') {
  176. $searchWhere = function ($query) use ($content) {
  177. $query->where(function ($query) use ($content) {
  178. $query->whereOr('telephone', $content);
  179. $query->whereOr('mobile', $content);
  180. });
  181. };
  182. }
  183. return $searchWhere;
  184. }
  185. /**
  186. * [客户公海条件]
  187. * @param
  188. * @return
  189. * @author Michael_xu
  190. */
  191. private function getPoolWhere()
  192. {
  193. $configModel = new \app\crm\model\ConfigData();
  194. $configInfo = $configModel->getData();
  195. $config = $configInfo['config'] ?: 0;
  196. $follow_day = $configInfo['follow_day'] ?: 0;
  197. $deal_day = $configInfo['deal_day'] ?: 0;
  198. $whereData = [];
  199. //启用
  200. if ($config == 1) {
  201. //默认公海条件(没有负责人或已经到期)
  202. $data['follow_time'] = time() - $follow_day * 86400;
  203. $data['deal_time'] = time() - $deal_day * 86400;
  204. $data['deal_status'] = '未成交';
  205. if ($follow_day < $deal_day) {
  206. $whereData = function ($query) use ($data) {
  207. $query->where(['customer.owner_user_id' => 0])
  208. ->whereOr(function ($query) use ($data) {
  209. $query->where(function ($query) use ($data) {
  210. $query->where(['customer.update_time' => array('elt', $data['follow_time'])])
  211. ->whereOr(['customer.deal_time' => array('elt', $data['deal_time'])]);
  212. })
  213. ->where(['customer.is_lock' => 0])
  214. ->where(['customer.deal_status' => ['neq', '已成交']]);
  215. });
  216. };
  217. } else {
  218. $whereData = function ($query) use ($data) {
  219. $query->where(['customer.owner_user_id' => 0])
  220. ->whereOr(function ($query) use ($data) {
  221. $query->where(function ($query) use ($data) {
  222. $query->where(['customer.deal_time' => array('elt', $data['deal_time'])]);
  223. })
  224. ->where(['customer.is_lock' => 0])
  225. ->where(['customer.deal_status' => ['neq', '已成交']]);
  226. });
  227. };
  228. }
  229. } else {
  230. $whereData['customer.owner_user_id'] = 0;
  231. }
  232. return $whereData ?: [];
  233. }
  234. /**
  235. * [客户默认条件]
  236. * @param
  237. * @return
  238. * @author Michael_xu
  239. */
  240. private function getCustomerWhere()
  241. {
  242. $configModel = new \app\crm\model\ConfigData();
  243. $configInfo = $configModel->getData();
  244. $config = $configInfo['config'] ?: 0;
  245. $follow_day = $configInfo['follow_day'] ?: 0;
  246. $deal_day = $configInfo['deal_day'] ?: 0;
  247. //默认条件(没有到期或已锁定)
  248. $data['follow_time'] = time() - $follow_day * 86400;
  249. $data['deal_time'] = time() - $deal_day * 86400;
  250. if ($config == 1) {
  251. if ($follow_day < $deal_day) {
  252. $whereData = function ($query) use ($data) {
  253. $query->where(function ($query) use ($data) {
  254. $query->where(['customer.update_time' => array('gt', $data['follow_time']), 'customer.deal_time' => array('gt', $data['deal_time'])]);
  255. })
  256. ->whereOr(['customer.deal_status' => '已成交'])
  257. ->whereOr(['customer.is_lock' => 1]);
  258. };
  259. } else {
  260. $whereData = function ($query) use ($data) {
  261. $query->where(function ($query) use ($data) {
  262. $query->where(['customer.deal_time' => array('gt', $data['deal_time'])]);
  263. })
  264. ->whereOr(['customer.deal_status' => '已成交'])
  265. ->whereOr(['customer.is_lock' => 1]);
  266. };
  267. }
  268. }
  269. return $whereData ?: [];
  270. }
  271. }