CustomerFeedback.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. $treeData[] = [
  53. 'label' => '年度客诉统计',
  54. 'type' => 'year_stat',
  55. ];
  56. $this->success('请求成功', $treeData);
  57. }
  58. /**
  59. * 获取右侧表格数据
  60. * @ApiMethod GET
  61. * @param string code 人员编号(可选)
  62. * @param string type 节点类型 person | year_stat(可选)
  63. * @return void
  64. */
  65. public function getList()
  66. {
  67. if (Request::instance()->isGet() == false) {
  68. $this->error('非法请求');
  69. }
  70. $params = Request::instance()->param();
  71. $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';
  72. $nodeType = isset($params['type']) ? $params['type'] : '';
  73. $code = isset($params['code']) ? trim($params['code']) : '';
  74. $name = isset($params['name']) ? trim($params['name']) : '';
  75. $query = \db('db_客诉记录')->field($field);
  76. // 已在 db_客诉流程 中存在“处理完毕”环节的客诉视为已关闭
  77. $notDoneSql = "UniqID NOT IN (SELECT RelationID FROM db_客诉流程 WHERE rtrim(处理环节) = '处理完毕')";
  78. if ($nodeType === 'person') {
  79. // 处理中客诉 —— 无处理完毕环节,且按建档人 Sys_id 过滤
  80. $query->whereRaw($notDoneSql);
  81. if (!empty($name)) {
  82. $query->whereLike('Sys_id', '%' . $name . '%');
  83. }
  84. } elseif ($nodeType === 'year_stat') {
  85. // 年度客诉统计 —— 查全部
  86. } else {
  87. // 默认:全部处理中(无处理完毕环节)
  88. $query->whereRaw($notDoneSql);
  89. }
  90. $list = $query->order('客诉日期 desc')->select();
  91. if (empty($list)) {
  92. $this->success('暂无数据', []);
  93. }
  94. $this->success('请求成功', $list);
  95. }
  96. /**
  97. * 新增客诉记录
  98. * @ApiMethod POST
  99. * @return void
  100. */
  101. public function add()
  102. {
  103. if (Request::instance()->isPost() == false) {
  104. $this->error('非法请求');
  105. }
  106. $data = Request::instance()->post();
  107. if (empty($data)) {
  108. $this->error('参数为空');
  109. }
  110. // 自动生成 UniqID(取当前最大+1)
  111. $maxId = \db('db_客诉记录')->max('UniqID');
  112. $data['UniqID'] = $maxId ? $maxId + 1 : 1;
  113. // 若客诉日期为空,填当天
  114. if (empty($data['客诉日期'])) {
  115. $data['客诉日期'] = date('Y-m-d H:i:s');
  116. }
  117. // 系统字段
  118. $data['Sys_rq'] = date('Y-m-d H:i:s');
  119. $data['Mod_rq'] = date('Y-m-d H:i:s');
  120. $sql = \db('db_客诉记录')->fetchSql(true)->insert($data);
  121. $res = \db()->query($sql);
  122. if ($res === false) {
  123. $this->error('新增失败');
  124. }
  125. $this->success('新增成功', ['UniqID' => $data['UniqID']]);
  126. }
  127. /**
  128. * 修改客诉记录
  129. * @ApiMethod POST
  130. * @return void
  131. */
  132. public function edit()
  133. {
  134. if (Request::instance()->isPost() == false) {
  135. $this->error('非法请求');
  136. }
  137. $data = Request::instance()->post();
  138. if (empty($data['UniqID'])) {
  139. $this->error('缺少 UniqID');
  140. }
  141. $data['Mod_rq'] = date('Y-m-d H:i:s');
  142. $uniqId = $data['UniqID'];
  143. unset($data['UniqID']);
  144. $sql = \db('db_客诉记录')->where('UniqID', $uniqId)->fetchSql(true)->update($data);
  145. $res = \db()->query($sql);
  146. if ($res === false) {
  147. $this->error('修改失败');
  148. }
  149. $this->success('修改成功');
  150. }
  151. /**
  152. * 删除客诉记录(支持批量)
  153. * @ApiMethod POST
  154. * @param UniqID 单个 UniqID 或者 UniqID 数组
  155. * @return void
  156. */
  157. public function del()
  158. {
  159. if (Request::instance()->isPost() == false) {
  160. $this->error('非法请求');
  161. }
  162. $params = Request::instance()->post();
  163. if (empty($params['UniqID']) && empty($params['ids'])) {
  164. $this->error('参数错误');
  165. }
  166. if (!empty($params['ids'])) {
  167. // 批量删除
  168. $ids = is_array($params['ids']) ? $params['ids'] : explode(',', $params['ids']);
  169. $res = \db('db_客诉记录')->whereIn('UniqID', $ids)->delete();
  170. } else {
  171. $res = \db('db_客诉记录')->where('UniqID', $params['UniqID'])->delete();
  172. }
  173. if ($res === false) {
  174. $this->error('删除失败');
  175. }
  176. $this->success('删除成功');
  177. }
  178. // =====================================================
  179. // 以下为处理环节记录 (db_客诉流程) 和 相关整改附件 (db_客诉附件) 接口
  180. // =====================================================
  181. /**
  182. * 获取处理环节记录列表
  183. * @ApiMethod GET
  184. * @param int RelationID 关联主表 UniqID
  185. * @return void
  186. */
  187. public function getFlowList()
  188. {
  189. if (Request::instance()->isGet() == false) {
  190. $this->error('非法请求');
  191. }
  192. $params = Request::instance()->param();
  193. if (empty($params['RelationID'])) {
  194. $this->error('缺少 RelationID');
  195. }
  196. $field = 'rtrim(RelationID) as RelationID,rtrim(操作者) as 操作者,rtrim(处理环节) as 处理环节,rtrim(备注说明) as 备注说明,sys_id,sys_rq,mod_rq,UniqId';
  197. $list = \db('db_客诉流程')
  198. ->where('RelationID', intval($params['RelationID']))
  199. ->field($field)
  200. ->order('UniqId asc')
  201. ->select();
  202. $this->success('请求成功', $list ?: []);
  203. }
  204. /**
  205. * 新增处理环节记录
  206. * @ApiMethod POST
  207. * @return void
  208. */
  209. public function addFlow()
  210. {
  211. if (Request::instance()->isPost() == false) {
  212. $this->error('非法请求');
  213. }
  214. $data = Request::instance()->post();
  215. if (empty($data['RelationID'])) {
  216. $this->error('缺少 RelationID');
  217. }
  218. // 自动生成 UniqId
  219. $maxId = \db('db_客诉流程')->max('UniqId');
  220. $data['UniqId'] = $maxId ? $maxId + 1 : 1;
  221. $data['sys_rq'] = date('Y-m-d H:i:s');
  222. $data['mod_rq'] = date('Y-m-d H:i:s');
  223. $sql = \db('db_客诉流程')->fetchSql(true)->insert($data);
  224. $res = \db()->query($sql);
  225. if ($res === false) {
  226. $this->error('新增处理环节失败');
  227. }
  228. $this->success('新增成功', ['UniqId' => $data['UniqId']]);
  229. }
  230. /**
  231. * 删除处理环节记录
  232. * @ApiMethod POST
  233. * @param int UniqId
  234. * @return void
  235. */
  236. public function delFlow()
  237. {
  238. if (Request::instance()->isPost() == false) {
  239. $this->error('非法请求');
  240. }
  241. $params = Request::instance()->post();
  242. if (empty($params['UniqId'])) {
  243. $this->error('缺少 UniqId');
  244. }
  245. $res = \db('db_客诉流程')->where('UniqId', intval($params['UniqId']))->delete();
  246. if ($res === false) {
  247. $this->error('删除失败');
  248. }
  249. $this->success('删除成功');
  250. }
  251. /**
  252. * 获取整改附件列表
  253. * @ApiMethod GET
  254. * @param int relationId 关联主表 UniqID
  255. * @return void
  256. */
  257. public function getAttachmentList()
  258. {
  259. if (Request::instance()->isGet() == false) {
  260. $this->error('非法请求');
  261. }
  262. $params = Request::instance()->param();
  263. if (empty($params['relationId'])) {
  264. $this->error('缺少 relationId');
  265. }
  266. $field = 'relationId,名称及路径,修订日期,附件类型,sys_id,sys_rq,mod_rq,UniqId';
  267. $list = \db('db_客诉附件')
  268. ->where('relationId', intval($params['relationId']))
  269. ->field($field)
  270. ->order('UniqId asc')
  271. ->select();
  272. $this->success('请求成功', $list ?: []);
  273. }
  274. /**
  275. * 新增整改附件
  276. * @ApiMethod POST (FormData)
  277. * @return void
  278. */
  279. public function addAttachment()
  280. {
  281. if (Request::instance()->isPost() == false) {
  282. $this->error('非法请求');
  283. }
  284. $data = Request::instance()->post();
  285. if (empty($data['relationId'])) {
  286. $this->error('缺少 relationId');
  287. }
  288. if (empty($data['名称及路径'])) {
  289. $this->error('缺少 名称及路径');
  290. }
  291. // 自动生成 UniqId
  292. $maxId = \db('db_客诉附件')->max('UniqId');
  293. $data['UniqId'] = $maxId ? $maxId + 1 : 1;
  294. $data['修订日期'] = date('Y-m-d H:i:s');
  295. $data['sys_rq'] = date('Y-m-d H:i:s');
  296. $data['mod_rq'] = date('Y-m-d H:i:s');
  297. // 附件内容是 base64,直接存入 longblob
  298. if (!empty($data['附件内容'])) {
  299. // 去掉 data:xxx;base64, 前缀
  300. if (preg_match('/^data:[^;]+;base64,(.*)$/', $data['附件内容'], $m)) {
  301. $data['附件内容'] = base64_decode($m[1]);
  302. }
  303. }
  304. $sql = \db('db_客诉附件')->fetchSql(true)->insert($data);
  305. $res = \db()->query($sql);
  306. if ($res === false) {
  307. $this->error('新增附件失败');
  308. }
  309. $this->success('新增成功', ['UniqId' => $data['UniqId']]);
  310. }
  311. /**
  312. * 删除整改附件
  313. * @ApiMethod POST
  314. * @param int UniqId
  315. * @return void
  316. */
  317. public function delAttachment()
  318. {
  319. if (Request::instance()->isPost() == false) {
  320. $this->error('非法请求');
  321. }
  322. $params = Request::instance()->post();
  323. if (empty($params['UniqId'])) {
  324. $this->error('缺少 UniqId');
  325. }
  326. $res = \db('db_客诉附件')->where('UniqId', intval($params['UniqId']))->delete();
  327. if ($res === false) {
  328. $this->error('删除失败');
  329. }
  330. $this->success('删除成功');
  331. }
  332. }