WebWorkorder.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace addons\qingdongams\controller;
  3. use addons\qingdongams\model\Comment;
  4. use addons\qingdongams\model\Contacts;
  5. use addons\qingdongams\model\CustomerProduct;
  6. use addons\qingdongams\model\Event;
  7. use addons\qingdongams\model\File;
  8. use addons\qingdongams\model\Message;
  9. use addons\qingdongams\model\WorkorderFile;
  10. use think\Db;
  11. use think\Exception;
  12. use function EasyWeChat\Kernel\Support\get_client_ip;
  13. /**
  14. * 工单
  15. */
  16. class WebWorkorder extends WebIndexApi
  17. {
  18. protected $noNeedLogin = ['wanderorder','inspection'];
  19. protected $noNeedRight = [];
  20. /**
  21. * @var \addons\qingdongams\model\Workorder
  22. */
  23. protected $model;
  24. protected $Equipmentdevice;
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->model = new \addons\qingdongams\model\Workorder();
  29. $this->Equipmentdevice = new \app\admin\model\qingdongams\customer\Equipmentdevice;
  30. }
  31. public function getProductList(){
  32. if ($this->personInfo['customer_id']) {
  33. $list= $this->Equipmentdevice->where(array('customer_id'=>$this->personInfo['customer_id']))->select();
  34. if (count($list) > 0) {
  35. $this->success('请求成功', $list);
  36. }
  37. }
  38. $this->success('暂未查询到您合同中绑定的设备,请联系业务员确定!');
  39. }
  40. //获取购买过的客户产品列表/ 所有客户产品列表
  41. // public function getProductList()
  42. // {
  43. // if ($this->personInfo['customer_id']) {
  44. // $list = CustomerProduct::alias('c')->where(['c.customer_id' => $this->personInfo['customer_id']])
  45. // ->join('qingdongams_product p', 'p.id=c.product_id', 'left')
  46. // ->field('c.id,c.number,p.name,p.img,p.unit,p.price,c.status')->select();
  47. // if (count($list) > 0) {
  48. // $this->success('请求成功', $list);
  49. // }
  50. // }
  51. // $this->success('暂未查询到您购买过公司产品,请联系业务员确定!');
  52. // }
  53. //获取工单编号
  54. public function getWorkorderNumber()
  55. {
  56. $this->success('请求成功', ['number' => $this->model::getWorkorderNumber()]);
  57. }
  58. //定时任务超时订单自动转派
  59. public function inspection(){
  60. $result = $this->model->inspection();
  61. echo $result;
  62. }
  63. //定时任务超时订单自动转派
  64. public function wanderorder(){
  65. $result = $this->model->wanderorder();
  66. echo $result;
  67. }
  68. //添加工单
  69. public function addWorkorder()
  70. {
  71. $params = $this->request->post();
  72. if ($this->model::where([
  73. 'workorder_number' => $params['workorder_number'],
  74. ])->find()) {
  75. $this->error('工单编号已存在');
  76. }
  77. if ($this->personInfo['customer_id']) {
  78. $productIds = CustomerProduct::alias('p')->where(['p.id' => $params['customer_product_id']])
  79. ->join('qingdongams_customer c', 'c.id=p.customer_id', 'left')->field('p.contract_id,c.owner_staff_id')->find();
  80. $contacts = Contacts::where(['customer_id' => $this->personInfo['customer_id']])->find();
  81. $params['customer_id'] = $this->personInfo['customer_id'];//客户Id
  82. // $params['contract_id'] = $productIds['contract_id']?$productIds['contract_id']:0;// 合同id
  83. $params['contacts_id'] = $contacts['id'];// 联系人id
  84. }
  85. // else {
  86. // $this->error('暂未查询到您购买过公司产品,抱歉暂不能提交工单!');
  87. // }
  88. // if (strtotime($params['appointment_time']) <= time()) {
  89. // $this->error('时间不得早于当前');
  90. // }
  91. $params['device_ids']=$params['customer_product_id'];
  92. $params['appointment_time'] = date("Y-m-d H:i:s", strtotime($params['appointment_time']));
  93. if (!$params['linkman'] || !$params['linkfun']) {
  94. $this->error('联系人不能为空');
  95. }
  96. // 表单验证
  97. if (($result = $this->validate($params, 'addons\qingdongams\validate\Workorder.create')) !== true) {
  98. $this->error($result);
  99. }
  100. Db::startTrans();
  101. try {
  102. $params['create_type'] = 2;// 1是员工提出,2联系人提出
  103. $params['person_id'] = $this->personId;
  104. $params['workorder_type'] = '维修工单';
  105. $result = $this->model::createCustomerWorkorder($params);
  106. Db::commit();
  107. } catch (Exception $e) {
  108. Db::rollback();
  109. $this->error($e->getMessage());
  110. }
  111. if ($result) {
  112. $this->success('添加工单成功');
  113. }
  114. }
  115. //获取工单列表
  116. public function getOrderList()
  117. {
  118. $limit = input("limit/d", 10);
  119. $params = $this->request->post();
  120. $where = [];
  121. $type = input('type', 0);//状态 0全部 1 处理中 2 已完成
  122. $where1['customer_id'] = $this->personInfo['customer_id'];
  123. // $where1['workorder_type'] = '上门维修';
  124. $where2['person_id'] = $this->personId;
  125. if ($type == 1) {
  126. $where['status'] = ['in', '0,1,2'];
  127. } elseif ($type == 2) {
  128. $where['status'] = ['in','3,9'];
  129. } else if ($type != 0) {
  130. $this->error('状态错误');
  131. }
  132. if (isset($params['name']) && $params['name']) {//查询名称
  133. $where['title|workorder_number'] = ['like', "%{$params['name']}%"];
  134. }
  135. $list = $this->model::where($where)->where(function ($query) use ($where1, $where2) {
  136. return $query->where($where1)->whereor($where2);
  137. })->with([
  138. 'comments',
  139. 'ownerStaff',
  140. ])->order('id desc')->field('deletetime,updatetime', true)->paginate($limit);
  141. $this->success('请求成功', $list);
  142. }
  143. // 工单详情
  144. public function detail()
  145. {
  146. $id = input('id');
  147. if (!$id) {
  148. $this->error('参数不能为空!');
  149. }
  150. $detail = $this->model::where('id', $id)->with([
  151. 'comments',
  152. 'person',
  153. 'ownerStaff',
  154. 'contacts'
  155. ])->field('deletetime,updatetime', true)->find();
  156. $files = WorkorderFile::where(['workorder_id' => $id])->column('file_id');
  157. $detail['linkman'] = $detail['linkman'] ?? $detail['contacts']['name'];
  158. $detail['linkfun'] = $detail['linkfun'] ?? $detail['contacts']['mobile'];
  159. $detail['file'] = [];
  160. if ($files) {
  161. $detail['file'] = File::where('id', 'in', $files)->select();
  162. }
  163. $this->success('', $detail);
  164. }
  165. // 评论的列表
  166. public function commentList()
  167. {
  168. $id = input('id');
  169. if (!$id) {
  170. $this->error('参数不能为空');
  171. }
  172. $commentModel = new Comment();
  173. $where = [
  174. 'relation_id' => $id,
  175. 'relation_type' => 'workorder',
  176. ];
  177. $list = $commentModel->where($where)->select();
  178. $this->success('', $list);
  179. }
  180. //添加评论
  181. public function addComment()
  182. {
  183. $content = input('content');
  184. $record_id = input('record_id');// 工单Id
  185. $relation_type = input('relation_type', 'workorder');
  186. if (empty($content)) {
  187. $this->error('评论内容不能为空');
  188. }
  189. $data = [
  190. 'relation_type' => $relation_type,
  191. 'relation_id' => $record_id,
  192. 'staff_id' => $this->personId,
  193. 'content' => $content,
  194. 'status' => 1,
  195. 'ip' => get_client_ip(),
  196. ];
  197. $commentModel = new Comment();
  198. Db::startTrans();
  199. try {
  200. $commentModel->save($data);
  201. if ($relation_type == $commentModel::WORKORDER_TYPE) {
  202. $record = $this->model->where(['id' => $record_id])->find();
  203. }
  204. if (empty($record)) {
  205. $this->error('记录不存在');
  206. }
  207. Db::commit();
  208. } catch (Exception $e) {
  209. Db::rollback();
  210. $this->error($e->getMessage());
  211. }
  212. $this->success('评论成功');
  213. }
  214. // 获取评价工单列表
  215. public function get_finish_list()
  216. {
  217. $limit = input("limit/d", 10);
  218. $params = $this->request->post();
  219. $type = $this->request->post('type', 1);// 1 已评价 2 未评价
  220. $where = [
  221. 'customer_id' => $this->personInfo['customer_id'],
  222. // 'workorder_type' => '维修工单',
  223. 'status' => 3,
  224. ];
  225. if (isset($params['name']) && $params['name']) {//查询名称
  226. $where['title|workorder_number'] = ['like', "%{$params['name']}%"];
  227. }
  228. // if ($type == 1) {
  229. // $where['is_resolved'] = ['not null', ['neq', ''], 'and'];
  230. // } else {
  231. // $where['is_resolved'] = ['null', '', 'and'];
  232. // }
  233. // dump($where);
  234. $list = $this->model::where($where)->with([
  235. 'comments',
  236. 'ownerStaff'
  237. ])->order('id desc')->field('deletetime,updatetime', true)->paginate($limit);
  238. $this->success('请求成功', $list);
  239. }
  240. // 评价工单
  241. public function addEvaluate()
  242. {
  243. $id = input('id');
  244. if (!$id) {
  245. $this->error('参数错误!');
  246. }
  247. $info = $this->model->where('id', $id)->find();
  248. if ($info['customer_id'] != $this->personInfo['customer_id']) {
  249. // $this->error('您不是客户,无权评价!');
  250. }
  251. $save = [];
  252. $save['is_resolved'] = $this->request->post('is_resolved', 1);
  253. $save['comment_score'] = $this->request->post('comment_score', 5);
  254. $save['comment_content'] = $this->request->post('comment_content', '');
  255. $save['comment_files'] = $this->request->post('comment_files', '');
  256. if (!isset($save['comment_content']) || empty($save['comment_content'])) {
  257. $this->error('请输入评价内容');
  258. }
  259. $save['status'] = 3;
  260. if ($info->save($save)) {
  261. //日程完成
  262. Event::where(['relation_type' => 'workorder', 'relation_id' => $id])->update(['status' => 2]);
  263. $this->success('评论成功');
  264. }
  265. $this->error('评论失败');
  266. }
  267. }