WebWorkorder.php 9.3 KB

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