CustomerFeedback.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Request;
  5. /**
  6. * 客户反馈与投诉记录
  7. */
  8. class CustomerFeedback extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. /**
  13. * 左侧菜单栏(处理中客诉人员 + 年度客诉统计)
  14. * @ApiMethod GET
  15. * @return void
  16. */
  17. public function getTab()
  18. {
  19. if (Request::instance()->isGet() == false) {
  20. $this->error('非法请求');
  21. }
  22. // 处理中客诉 —— 按建档人 Sys_id 分组取不重复人员
  23. // 已在 db_客诉流程 中存在“处理完毕”环节的客诉视为已关闭,不计入处理中
  24. $personList = \db('db_客诉记录')
  25. ->whereRaw("UniqID NOT IN (SELECT RelationID FROM db_客诉流程 WHERE rtrim(处理环节) = '处理完毕')")
  26. ->where('Sys_id', '<>', '')
  27. ->group('Sys_id')
  28. ->field('rtrim(Sys_id) as Sys_id')
  29. ->order('Sys_id asc')
  30. ->select();
  31. $children = [];
  32. foreach ($personList as $item) {
  33. $name = $item['Sys_id'];
  34. // 拆分 "admin/超级管理员" 格式
  35. $parts = explode('/', $name);
  36. $code = isset($parts[0]) ? trim($parts[0]) : '';
  37. $realName = isset($parts[1]) ? trim($parts[1]) : $name;
  38. $children[] = [
  39. 'label' => $name,
  40. 'code' => $code,
  41. 'name' => $realName,
  42. 'type' => 'person', // 人员节点
  43. ];
  44. }
  45. // 处理中客诉
  46. $treeData[] = [
  47. 'label' => '处理中客诉',
  48. 'children' => $children,
  49. 'type' => 'group',
  50. ];
  51. // 年度客诉统计 —— 二级:年度,三级:年月(按客诉日期统计次数)
  52. // 客诉日期存在 "2024.06.05"、"2024-06-05"、"2024/06/05" 等多种写法,统一替换后再截取
  53. $dateExpr = "LEFT(REPLACE(REPLACE(REPLACE(rtrim(`客诉日期`),'.','-'),'/','-'),'年','-'), 7)";
  54. $statRows = \db('db_客诉记录')
  55. ->field($dateExpr . ' as ym,COUNT(*) as cnt')
  56. ->whereRaw("rtrim(`客诉日期`) <> ''")
  57. ->group('ym')
  58. ->order('ym desc')
  59. ->select();
  60. $yearMap = [];
  61. foreach ($statRows as $row) {
  62. $ym = trim($row['ym']);
  63. if (strlen($ym) < 7 || strpos($ym, '-') === false) continue;
  64. $y = substr($ym, 0, 4);
  65. $m = substr($ym, 5, 2);
  66. $cnt = (int)$row['cnt'];
  67. if (!ctype_digit($y) || !ctype_digit($m)) continue;
  68. if (!isset($yearMap[$y])) {
  69. $yearMap[$y] = ['cnt' => 0, 'months' => []];
  70. }
  71. $yearMap[$y]['cnt'] += $cnt;
  72. $yearMap[$y]['months'][$m] = $cnt;
  73. }
  74. krsort($yearMap);
  75. $yearChildren = [];
  76. foreach ($yearMap as $y => $info) {
  77. krsort($info['months']);
  78. $monthChildren = [];
  79. foreach ($info['months'] as $m => $cnt) {
  80. $monthChildren[] = [
  81. 'label' => $y . $m . ' 【客诉 ' . $cnt . ' 次】',
  82. 'type' => 'month_stat',
  83. 'year' => (string)$y,
  84. 'month' => (string)$m,
  85. ];
  86. }
  87. $yearChildren[] = [
  88. 'label' => $y . ' 【客诉 ' . $info['cnt'] . ' 次】',
  89. 'type' => 'year_stat',
  90. 'year' => (string)$y,
  91. 'children' => $monthChildren,
  92. ];
  93. }
  94. // 年度客诉统计
  95. $treeData[] = [
  96. 'label' => '年度客诉统计',
  97. 'type' => 'year_stat',
  98. 'children' => $yearChildren,
  99. ];
  100. $this->success('请求成功', $treeData);
  101. }
  102. /**
  103. * 获取右侧表格数据
  104. * @ApiMethod GET
  105. * @param string code 人员编号(可选)
  106. * @param string type 节点类型 person | year_stat(可选)
  107. * @return void
  108. */
  109. public function getList()
  110. {
  111. if (Request::instance()->isGet() == false) {
  112. $this->error('非法请求');
  113. }
  114. $params = Request::instance()->param();
  115. $field = 'rtrim(客诉单号) as 客诉单号,rtrim(客诉日期) as 客诉日期,rtrim(客诉方式) as 客诉方式,rtrim(订单编号) as 订单编号,rtrim(产品编号) as 产品编号,rtrim(产品名称) as 产品名称,rtrim(客户编号) as 客户编号,rtrim(客户名称) as 客户名称,rtrim(客诉描述) as 客诉描述,rtrim(缺陷关键字) as 缺陷关键字,rtrim(客诉性质) as 客诉性质,扣分,rtrim(主要责任部门) as 主要责任部门,rtrim(次要责任部门) as 次要责任部门,UniqID,rtrim(Sys_id) as Sys_id,Sys_rq,Mod_rq';
  116. $nodeType = isset($params['type']) ? $params['type'] : '';
  117. $code = isset($params['code']) ? trim($params['code']) : '';
  118. $name = isset($params['name']) ? trim($params['name']) : '';
  119. $query = \db('db_客诉记录')->field($field);
  120. // 已在 db_客诉流程 中存在“处理完毕”环节的客诉视为已关闭
  121. $notDoneSql = "UniqID NOT IN (SELECT RelationID FROM db_客诉流程 WHERE rtrim(处理环节) = '处理完毕')";
  122. if ($nodeType === 'person') {
  123. // 处理中客诉 —— 无处理完毕环节,且按建档人 Sys_id 过滤
  124. $query->whereRaw($notDoneSql);
  125. if (!empty($name)) {
  126. $query->whereLike('Sys_id', '%' . $name . '%');
  127. }
  128. } elseif ($nodeType === 'year_stat') {
  129. // 年度客诉统计 —— 全部客诉;传 year 时按年度过滤
  130. $year = isset($params['year']) ? trim($params['year']) : '';
  131. if (!empty($year)) {
  132. $query->whereRaw("LEFT(REPLACE(REPLACE(REPLACE(rtrim(`客诉日期`),'.','-'),'/','-'),'年','-'), 4) = '" . addslashes($year) . "'");
  133. }
  134. } elseif ($nodeType === 'month_stat') {
  135. // 年度客诉统计 —— 年月节点
  136. $year = isset($params['year']) ? trim($params['year']) : '';
  137. $month = isset($params['month']) ? trim($params['month']) : '';
  138. if (!empty($year) && !empty($month)) {
  139. $ym = $year . '-' . $month;
  140. $query->whereRaw("LEFT(REPLACE(REPLACE(REPLACE(rtrim(`客诉日期`),'.','-'),'/','-'),'年','-'), 7) = '" . addslashes($ym) . "'");
  141. }
  142. } else {
  143. // 默认:全部处理中(无处理完毕环节)
  144. $query->whereRaw($notDoneSql);
  145. }
  146. $list = $query->order('客诉日期 desc')->select();
  147. if (empty($list)) {
  148. $this->success('暂无数据', []);
  149. }
  150. $this->success('请求成功', $list);
  151. }
  152. /**
  153. * 新增客诉记录
  154. * @ApiMethod POST
  155. * @return void
  156. */
  157. public function add()
  158. {
  159. if (Request::instance()->isPost() == false) {
  160. $this->error('非法请求');
  161. }
  162. $data = Request::instance()->post();
  163. if (empty($data)) {
  164. $this->error('参数为空');
  165. }
  166. // 自动生成 UniqID(取当前最大+1)
  167. $maxId = \db('db_客诉记录')->max('UniqID');
  168. $data['UniqID'] = $maxId ? $maxId + 1 : 1;
  169. // 若客诉日期为空,填当天
  170. if (empty($data['客诉日期'])) {
  171. $data['客诉日期'] = date('Y-m-d H:i:s');
  172. }
  173. // 系统字段
  174. $data['Sys_rq'] = date('Y-m-d H:i:s');
  175. $data['Mod_rq'] = date('Y-m-d H:i:s');
  176. $sql = \db('db_客诉记录')->fetchSql(true)->insert($data);
  177. $res = \db()->query($sql);
  178. if ($res === false) {
  179. $this->error('新增失败');
  180. }
  181. $this->success('新增成功', ['UniqID' => $data['UniqID']]);
  182. }
  183. /**
  184. * 修改客诉记录
  185. * @ApiMethod POST
  186. * @return void
  187. */
  188. public function edit()
  189. {
  190. if (Request::instance()->isPost() == false) {
  191. $this->error('非法请求');
  192. }
  193. $data = Request::instance()->post();
  194. if (empty($data['UniqID'])) {
  195. $this->error('缺少 UniqID');
  196. }
  197. $data['Mod_rq'] = date('Y-m-d H:i:s');
  198. $uniqId = $data['UniqID'];
  199. unset($data['UniqID']);
  200. $sql = \db('db_客诉记录')->where('UniqID', $uniqId)->fetchSql(true)->update($data);
  201. $res = \db()->query($sql);
  202. if ($res === false) {
  203. $this->error('修改失败');
  204. }
  205. $this->success('修改成功');
  206. }
  207. /**
  208. * 删除客诉记录(支持批量)
  209. * @ApiMethod POST
  210. * @param UniqID 单个 UniqID 或者 UniqID 数组
  211. * @return void
  212. */
  213. public function del()
  214. {
  215. if (Request::instance()->isPost() == false) {
  216. $this->error('非法请求');
  217. }
  218. $params = Request::instance()->post();
  219. if (empty($params['UniqID']) && empty($params['ids'])) {
  220. $this->error('参数错误');
  221. }
  222. if (!empty($params['ids'])) {
  223. // 批量删除
  224. $ids = is_array($params['ids']) ? $params['ids'] : explode(',', $params['ids']);
  225. $res = \db('db_客诉记录')->whereIn('UniqID', $ids)->delete();
  226. } else {
  227. $res = \db('db_客诉记录')->where('UniqID', $params['UniqID'])->delete();
  228. }
  229. if ($res === false) {
  230. $this->error('删除失败');
  231. }
  232. $this->success('删除成功');
  233. }
  234. // =====================================================
  235. // 以下为处理环节记录 (db_客诉流程) 和 相关整改附件 (db_客诉附件) 接口
  236. // =====================================================
  237. /**
  238. * 获取处理环节记录列表
  239. * @ApiMethod GET
  240. * @param int RelationID 关联主表 UniqID
  241. * @return void
  242. */
  243. public function getFlowList()
  244. {
  245. if (Request::instance()->isGet() == false) {
  246. $this->error('非法请求');
  247. }
  248. $params = Request::instance()->param();
  249. if (empty($params['RelationID'])) {
  250. $this->error('缺少 RelationID');
  251. }
  252. $field = 'rtrim(RelationID) as RelationID,rtrim(操作者) as 操作者,rtrim(处理环节) as 处理环节,rtrim(备注说明) as 备注说明,sys_id,sys_rq,mod_rq,UniqId';
  253. $list = \db('db_客诉流程')
  254. ->where('RelationID', intval($params['RelationID']))
  255. ->field($field)
  256. ->order('UniqId asc')
  257. ->select();
  258. $this->success('请求成功', $list ?: []);
  259. }
  260. /**
  261. * 新增处理环节记录
  262. * @ApiMethod POST
  263. * @return void
  264. */
  265. public function addFlow()
  266. {
  267. if (Request::instance()->isPost() == false) {
  268. $this->error('非法请求');
  269. }
  270. $data = Request::instance()->post();
  271. if (empty($data['RelationID'])) {
  272. $this->error('缺少 RelationID');
  273. }
  274. // 自动生成 UniqId
  275. $maxId = \db('db_客诉流程')->max('UniqId');
  276. $data['UniqId'] = $maxId ? $maxId + 1 : 1;
  277. $data['sys_rq'] = date('Y-m-d H:i:s');
  278. $data['mod_rq'] = date('Y-m-d H:i:s');
  279. $sql = \db('db_客诉流程')->fetchSql(true)->insert($data);
  280. $res = \db()->query($sql);
  281. if ($res === false) {
  282. $this->error('新增处理环节失败');
  283. }
  284. $this->success('新增成功', ['UniqId' => $data['UniqId']]);
  285. }
  286. /**
  287. * 删除处理环节记录
  288. * @ApiMethod POST
  289. * @param int UniqId
  290. * @return void
  291. */
  292. public function delFlow()
  293. {
  294. if (Request::instance()->isPost() == false) {
  295. $this->error('非法请求');
  296. }
  297. $params = Request::instance()->post();
  298. if (empty($params['UniqId'])) {
  299. $this->error('缺少 UniqId');
  300. }
  301. $res = \db('db_客诉流程')->where('UniqId', intval($params['UniqId']))->delete();
  302. if ($res === false) {
  303. $this->error('删除失败');
  304. }
  305. $this->success('删除成功');
  306. }
  307. /**
  308. * 获取整改附件列表
  309. * @ApiMethod GET
  310. * @param int relationId 关联主表 UniqID
  311. * @return void
  312. */
  313. public function getAttachmentList()
  314. {
  315. if (Request::instance()->isGet() == false) {
  316. $this->error('非法请求');
  317. }
  318. $params = Request::instance()->param();
  319. if (empty($params['relationId'])) {
  320. $this->error('缺少 relationId');
  321. }
  322. $field = 'relationId,名称及路径,修订日期,附件类型,sys_id,sys_rq,mod_rq,UniqId';
  323. $list = \db('db_客诉附件')
  324. ->where('relationId', intval($params['relationId']))
  325. ->field($field)
  326. ->order('UniqId asc')
  327. ->select();
  328. $this->success('请求成功', $list ?: []);
  329. }
  330. /**
  331. * 新增整改附件
  332. * @ApiMethod POST (FormData)
  333. * @return void
  334. */
  335. public function addAttachment()
  336. {
  337. if (Request::instance()->isPost() == false) {
  338. $this->error('非法请求');
  339. }
  340. $data = Request::instance()->post();
  341. if (empty($data['relationId'])) {
  342. $this->error('缺少 relationId');
  343. }
  344. if (empty($data['名称及路径'])) {
  345. $this->error('缺少 名称及路径');
  346. }
  347. // 自动生成 UniqId
  348. $maxId = \db('db_客诉附件')->max('UniqId');
  349. $data['UniqId'] = $maxId ? $maxId + 1 : 1;
  350. $data['修订日期'] = date('Y-m-d H:i:s');
  351. $data['sys_rq'] = date('Y-m-d H:i:s');
  352. $data['mod_rq'] = date('Y-m-d H:i:s');
  353. // 附件内容是 base64,直接存入 longblob
  354. if (!empty($data['附件内容'])) {
  355. // 去掉 data:xxx;base64, 前缀
  356. if (preg_match('/^data:[^;]+;base64,(.*)$/', $data['附件内容'], $m)) {
  357. $data['附件内容'] = base64_decode($m[1]);
  358. }
  359. }
  360. $sql = \db('db_客诉附件')->fetchSql(true)->insert($data);
  361. $res = \db()->query($sql);
  362. if ($res === false) {
  363. $this->error('新增附件失败');
  364. }
  365. $this->success('新增成功', ['UniqId' => $data['UniqId']]);
  366. }
  367. /**
  368. * 删除整改附件
  369. * @ApiMethod POST
  370. * @param int UniqId
  371. * @return void
  372. */
  373. public function delAttachment()
  374. {
  375. if (Request::instance()->isPost() == false) {
  376. $this->error('非法请求');
  377. }
  378. $params = Request::instance()->post();
  379. if (empty($params['UniqId'])) {
  380. $this->error('缺少 UniqId');
  381. }
  382. $res = \db('db_客诉附件')->where('UniqId', intval($params['UniqId']))->delete();
  383. if ($res === false) {
  384. $this->error('删除失败');
  385. }
  386. $this->success('删除成功');
  387. }
  388. }