Leadspool.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. namespace app\admin\controller\qingdongams\customer;
  3. use addons\qingdongams\model\File;
  4. use addons\qingdongams\model\Staff;
  5. use app\admin\controller\qingdongams\Base;
  6. use addons\qingdongams\model\Form;
  7. use addons\qingdongams\model\LeadsOther;
  8. use addons\qingdongams\model\OperationLog;
  9. use addons\qingdongams\model\Record;
  10. use addons\qingdongams\model\Customer;
  11. use addons\qingdongams\model\Contacts;
  12. use addons\qingdongams\model\LeadsFile;
  13. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  14. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  15. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  16. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  17. use think\Db;
  18. use think\Exception;
  19. /**
  20. * 线索池
  21. * @desc 操作文档:https://doc.fastadmin.net/qingdongams
  22. * @desc 软件介绍:https://www.fastadmin.net/store/qingdongams.html
  23. * @desc 售后微信:qingdong_crm
  24. */
  25. class Leadspool extends Base {
  26. protected $relationSearch = true;
  27. protected $searchFields = 'id,name';
  28. /**
  29. * @var \addons\qingdongams\model\Leads
  30. */
  31. protected $model = null;
  32. public function _initialize() {
  33. parent::_initialize();
  34. $this->model = new \addons\qingdongams\model\Leads;
  35. }
  36. /**
  37. * 查看
  38. */
  39. public function index() {
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  43. $list = $this->model->where($where)->where('owner_staff_id is null or owner_staff_id = 0')->order($sort, $order)->with(['createStaff'])->paginate($limit);
  44. $result = array("total" => $list->total(), "rows" => $list->items());
  45. return json($result);
  46. }
  47. return $this->view->fetch();
  48. }
  49. /**
  50. * 添加
  51. */
  52. public function add() {
  53. if ($this->request->isPost()) {
  54. $params = $this->request->post("row/a");
  55. if ($params) {
  56. $params = $this->preExcludeFields($params);
  57. $result = false;
  58. Db::startTrans();
  59. try {
  60. $form = Form::getDataValue('leads');
  61. foreach($form as $k=>$v){
  62. if($v['component'] == 'uploadImage' || $v['component'] == 'uploadFile'){
  63. if(key_exists($v['id'],$params)){
  64. if($params[$v['id']]){
  65. $filearr = explode(',',$params[$v['id']]);
  66. $files ='';
  67. if($filearr){
  68. foreach($filearr as $ks=>$vs){
  69. $files = File::where(array('file_path'=>$vs))->order('id desc')->value('id').','.$files;
  70. }
  71. $params[$v['id']] = rtrim($files,',');
  72. }
  73. }
  74. }
  75. }elseif($v['component'] == 'select'){
  76. if(isset($v['config']['multiple']) && $v['config']['multiple'] == true){
  77. if(key_exists($v['id'],$params)){
  78. if($params[$v['id']]){
  79. $params[$v['id']] = implode(',',$params[$v['id']]);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. $result =$this->model::createLeads($params,1);
  86. Db::commit();
  87. } catch (Exception $e) {
  88. Db::rollback();
  89. $this->error($e->getMessage());
  90. }
  91. if ($result !== false) {
  92. $this->success();
  93. } else {
  94. $this->error(__('No rows were inserted'));
  95. }
  96. }
  97. $this->error(__('Parameter %s can not be empty', ''));
  98. }
  99. $staffs = Staff::getList($this->_staff->id);
  100. $this->assign('staffs', $staffs);
  101. $this->view->assign('form_data', Form::getDataValue('leads'));
  102. return $this->view->fetch();
  103. }
  104. /**
  105. * 修改
  106. */
  107. public function edit($ids = null) {
  108. $row = $this->model->get($ids);
  109. if ($this->request->isPost()) {
  110. $params = $this->request->post("row/a");
  111. if ($params) {
  112. $form = Form::getDataValue('leads');
  113. foreach($form as $k=>$v){
  114. if($v['component'] == 'uploadImage' || $v['component'] == 'uploadFile'){
  115. if(key_exists($v['id'],$params)){
  116. if($params[$v['id']]){
  117. $filearr = explode(',',$params[$v['id']]);
  118. $files ='';
  119. if($filearr){
  120. foreach($filearr as $ks=>$vs){
  121. $files = File::where(array('file_path'=>$vs))->order('id desc')->value('id').','.$files;
  122. }
  123. $params[$v['id']] = rtrim($files,',');
  124. }
  125. }
  126. }
  127. }elseif($v['component'] == 'select'){
  128. if(isset($v['config']['multiple']) && $v['config']['multiple'] == true){
  129. if(key_exists($v['id'],$params)){
  130. if($params[$v['id']]){
  131. $params[$v['id']] = implode(',',$params[$v['id']]);
  132. }
  133. }
  134. }
  135. }
  136. }
  137. $params = $this->preExcludeFields($params);
  138. $result = false;
  139. Db::startTrans();
  140. try {
  141. $params['id']=$ids;
  142. $result=$this->model::updateLeads($params);
  143. Db::commit();
  144. } catch (Exception $e) {
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. }
  148. if ($result !== false) {
  149. $this->success();
  150. } else {
  151. $this->error(__('No rows were inserted'));
  152. }
  153. }
  154. $this->error(__('Parameter %s can not be empty', ''));
  155. }
  156. $row=$row->toArray();
  157. $row=LeadsOther::getOther($row);
  158. $this->assign('row', $row);
  159. $this->view->assign('form_data', Form::getDataValue('leads',$row));
  160. return $this->view->fetch();
  161. }
  162. /**
  163. * 线索详情
  164. */
  165. public function detail($ids = null) {
  166. $row = $this->model->with(['create_staff', 'owner_staff'])->where(['id' => $ids])->find();
  167. //跟进记录
  168. $this->assign('records', Record::getList(Record::LEADS_TYPE, $ids));
  169. //操作记录
  170. $this->assign('operation_log', OperationLog::getList(OperationLog::LEADS_TYPE, $ids));
  171. $row=$row->toArray();
  172. $row=LeadsOther::getOther($row);
  173. $this->view->assign('form_data', Form::getDataValue('leads',$row));
  174. $this->assign('row', $row);
  175. $this->assign('ids', $ids);
  176. $this->assignconfig("idinfo", ['id' => $ids]);
  177. return $this->view->fetch();
  178. }
  179. /**
  180. * 获取附件记录
  181. */
  182. public function get_file($ids = null) {
  183. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  184. $list = LeadsFile::where(['leads_id' => $ids])->with(['file'])->field('file_id')->paginate($limit);
  185. $result = array("total" => $list->total(), "rows" => $list->items());
  186. return json($result);
  187. }
  188. /**
  189. * 分配线索
  190. */
  191. public function transfer($ids=null){
  192. if ($this->request->isPost()) {
  193. $leadId = input('ids');
  194. $staff_id = input('staff_id');
  195. if (!$staff_id || !$leadId) {
  196. $this->error('参数错误');
  197. }
  198. $staff = Staff::get($staff_id);
  199. if (empty($staff)) {
  200. $this->error('接收对象不存在');
  201. }
  202. try {
  203. $ids = explode(',',$ids);
  204. $this->model::transfer($ids, $staff_id);
  205. } catch (Exception $e) {
  206. $this->error($e->getMessage());
  207. }
  208. $this->success('分配成功');
  209. }
  210. $ids = json_decode($ids, true);
  211. $row = $this->model::where(['id' => ['in',$ids]])->find();
  212. if (empty($row)) {
  213. $this->error('线索不存在');
  214. }
  215. $staffs = Staff::getList();
  216. $this->assign('staffs', $staffs);
  217. $this->assign('row', $row);
  218. $this->assign('ids', implode(',',$ids));
  219. return $this->view->fetch();
  220. }
  221. /**
  222. * 删除
  223. */
  224. public function del($ids = "") {
  225. if (!$this->request->isPost()) {
  226. $this->error(__("Invalid parameters"));
  227. }
  228. $ids = $ids ? $ids : $this->request->post("ids");
  229. $row = $this->model->get($ids);
  230. $this->modelValidate = true;
  231. if (!$row) {
  232. $this->error(__('No Results were found'));
  233. }
  234. $row->delete();
  235. $this->success();
  236. }
  237. /**
  238. * 导入
  239. */
  240. public function import()
  241. {
  242. set_time_limit(0);
  243. if ($this->request->isPost()) {
  244. $file = $this->request->request('file');
  245. if (!$file) {
  246. $this->error(__('Parameter %s can not be empty', 'file'));
  247. }
  248. $filePath = ROOT_PATH . 'public' . $file;
  249. if (!is_file($filePath)) {
  250. $this->error(__('No results were found'));
  251. }
  252. //实例化reader
  253. $ext = pathinfo($filePath, PATHINFO_EXTENSION);
  254. if (!in_array($ext, ['csv', 'xls', 'xlsx'])) {
  255. $this->error(__('Unknown data format'));
  256. }
  257. $staff = Staff::info();
  258. if ($ext === 'csv') {
  259. $file = fopen($filePath, 'r');
  260. $filePath = tempnam(sys_get_temp_dir(), 'import_csv');
  261. $fp = fopen($filePath, "w");
  262. $n = 0;
  263. while ($line = fgets($file)) {
  264. $line = rtrim($line, "\n\r\0");
  265. $encoding = mb_detect_encoding($line, ['utf-8', 'gbk', 'latin1', 'big5']);
  266. if ($encoding != 'utf-8') {
  267. $line = mb_convert_encoding($line, 'utf-8', $encoding);
  268. }
  269. if ($n == 0 || preg_match('/^".*"$/', $line)) {
  270. fwrite($fp, $line . "\n");
  271. } else {
  272. fwrite($fp, '"' . str_replace(['"', ','], ['""', '","'], $line) . "\"\n");
  273. }
  274. $n++;
  275. }
  276. fclose($file) || fclose($fp);
  277. $reader = new Csv();
  278. } elseif ($ext === 'xls') {
  279. $reader = new Xls();
  280. } else {
  281. $reader = new Xlsx();
  282. }
  283. if (!$PHPExcel = $reader->load($filePath)) {
  284. $this->error(__('Unknown data format'));
  285. }
  286. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  287. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  288. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  289. $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
  290. //开始读取数据
  291. $fields = [];
  292. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  293. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  294. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  295. $fields[$currentRow][] = $val;
  296. }
  297. }
  298. if (!isset($fields[1])) {
  299. $this->error('导入文件第一行没有数据');
  300. }
  301. $lastid = $this->model->order('id desc')->value('id');
  302. $lastid = $lastid + 5;//防止重复
  303. $customerRow = [];
  304. $errorInfo = [];
  305. $names = $this->model->where([])->column('name');
  306. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  307. $values = [];
  308. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  309. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  310. $values[] = is_null($val) ? '' : $val;
  311. }
  312. $lastid++;
  313. $addCustomers = ['id' => $lastid, 'owner_staff_id' => 0, 'create_staff_id' => $staff->id];
  314. $addCustomers['name'] = $values[0];
  315. $addCustomers['source'] = $values[1];
  316. $addCustomers['mobile'] = $values[2];
  317. $addCustomers['address_detail'] = $values[3];
  318. $addCustomers['telephone'] = $values[4];
  319. $addCustomers['sex'] = $values[5];
  320. $addCustomers['industry'] = $values[6];
  321. $addCustomers['remarks'] = $values[7];
  322. if (empty($addCustomers['name'])) {
  323. $errorInfo[] = "第{$currentRow}行,线索名称不能为空;";
  324. continue;
  325. }
  326. if (in_array($addCustomers['name'], $names)) {
  327. $errorInfo[] = "第{$currentRow}行,线索名称`{$addCustomers['name']}`已存在;";
  328. continue;
  329. }
  330. $customerRow[] = $addCustomers;
  331. }
  332. if (!empty($errorInfo)) {
  333. $this->error(implode(',', $errorInfo));
  334. }
  335. Db::startTrans();
  336. try {
  337. $this->model::importleads($customerRow);
  338. Db::commit();
  339. } catch (Exception $e) {
  340. Db::rollback();
  341. $this->error($e->getMessage());
  342. }
  343. $this->success('导入成功');
  344. }
  345. $this->assign('staffs', Staff::getList());
  346. return $this->view->fetch();
  347. }
  348. }