Todo.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\Base;
  4. use app\common\model\Config;
  5. use app\common\util\AppMsg;
  6. use app\hander\HelpHander;
  7. use think\App;
  8. use think\Db;
  9. use think\Exception;
  10. use think\exception\Handle;
  11. class Todo extends Base
  12. {
  13. public function __construct(App $app = null) {
  14. parent::__construct($app);
  15. $this->model = new \app\common\model\Todo();
  16. }
  17. //工单列表
  18. public function lists(){
  19. $page =input('page',1);
  20. $size =input('size',10);
  21. $type =input('type',0);
  22. $list = $this->model->lists($page,$size,$this->userId,$this->orgId,$type);
  23. HelpHander::success($list);
  24. }
  25. //工单详情
  26. public function detail(){
  27. $todoId =input('todoId',0);
  28. $info = $this->model->apiDetail($todoId);
  29. HelpHander::success($info);
  30. }
  31. // 处理驳回工单
  32. public function createNtbo(){
  33. $todoId = input('todoId');
  34. $toUserIdArray =input('toUserIdArray');
  35. $todoContent = input('todoContent');
  36. $typeId = input('typeId/d',0);
  37. $addressId = input('addressId/d',0);
  38. if(!$toUserIdArray){
  39. HelpHander::error('未选择执行人');
  40. }
  41. $ret = $this->model->createNtbo($todoId,$toUserIdArray,$todoContent,$this->userId,$typeId,$addressId);
  42. if(!$ret){
  43. HelpHander::error($this->model->getError());
  44. }
  45. HelpHander::success([],'操作成功');
  46. }
  47. // 修改状态 2=员工已经接单 4=被驳回 6=调度取消
  48. public function updateState(){
  49. $todoId = input('todoId/d',0);
  50. if($todoId <= 0){
  51. HelpHander::error('参数错误');
  52. }
  53. $todoMode =input('todoMode',0);
  54. if(!in_array($todoMode,[2,4,6])){
  55. HelpHander::error('参数错误');
  56. }
  57. $nodoReason = input('nodoReason','');
  58. if($todoMode == 6 && !$nodoReason){
  59. HelpHander::error('取消原因必需填写');
  60. }
  61. $rejectVoice = input('rejectVoice','');
  62. $ret = $this->model->updateState($todoId,$todoMode,$nodoReason,$this->userId,$rejectVoice);
  63. if(!$ret){
  64. HelpHander::error($this->model->getError());
  65. }
  66. HelpHander::success([],'操作成功');
  67. }
  68. //工单完成
  69. public function finishTodo(){
  70. $todoId =input('todoId/d');
  71. if($todoId <= 0){
  72. HelpHander::error('参数错误');
  73. }
  74. $todoMode = input('todoMode');
  75. if(!in_array($todoMode,[3,5])){
  76. HelpHander::error('参数错误');
  77. }
  78. $content = input('content');
  79. $consItems = input('consItems');
  80. $sign = input('sign');
  81. $images = input('img');
  82. if(!$content){
  83. HelpHander::error('未填写备注');
  84. }
  85. $ret = $this->model->finishTodo($todoId,$todoMode,$content,$this->userId,$images,$consItems,$sign);
  86. if(!$ret){
  87. HelpHander::error($this->model->getError());
  88. }
  89. HelpHander::success([],'操作成功');
  90. }
  91. //获取物品列表
  92. public function getMateGoods(){
  93. $page =input('page',1);
  94. $size =input('size',10);
  95. $title =input('title','','trim');
  96. $list = $this->model->getMateGoods($this->orgId,$this->userId,$title,$page,$size);
  97. HelpHander::success($list,'操作成功');
  98. }
  99. //收藏/取消收藏物品
  100. public function favGoods(){
  101. $type = input('type/d',0);
  102. $goodsId = input('goodsId/d',0);
  103. if(empty($goodsId)){
  104. HelpHander::error('物品不能为空');
  105. }
  106. $res = $this->model->favGoods($this->userId,$type,$goodsId);
  107. $res?HelpHander::success([],'操作成功'):HelpHander::error('操作失败');
  108. }
  109. //收藏列表
  110. public function favList(){
  111. $title =input('title','','trim');
  112. $list = $this->model->favList($this->userId,$title);
  113. HelpHander::success($list,'操作成功');
  114. }
  115. // 工单挂起
  116. public function pause(){
  117. $id = input('id/d',0);
  118. $reason = input('reason','','trim');
  119. if($id <= 0){
  120. HelpHander::error('参数错误');
  121. }
  122. if(!$reason){
  123. HelpHander::error('未填写挂起原因');
  124. }
  125. $todo = Db::name('todo')->where('id',$id)->where('del',0)->find();
  126. if(!$todo||$todo['to_user_id'] != $this->userId){
  127. HelpHander::error('工单不存在');
  128. }
  129. if($todo['todo_mode'] != 2){
  130. HelpHander::error('该状态不能挂起');
  131. }
  132. if($todo['work_type_mode'] != 1){
  133. HelpHander::error('该订单不能挂起');
  134. }
  135. if($todo['pause'] == 1){
  136. HelpHander::error('该订单已挂起');
  137. }
  138. $data = [
  139. 'pause' => 1,
  140. 'pause_time' => date('Y-m-d H:i:s'),
  141. 'pause_reason' => $reason
  142. ];
  143. if($todo['pause_time']){
  144. unset($data['pause_time']);
  145. }
  146. $ret = Db::name('todo')->where('id',$id)->update($data);
  147. // Db::name('todo_puase')
  148. // ->insert([
  149. // 'todo_id'=>$id,
  150. // 'reason'=>$reason,
  151. // 'create_time'=>getTime(),
  152. // ]);
  153. if(!$ret){
  154. HelpHander::error('操作失败');
  155. }
  156. HelpHander::success([],'操作成功');
  157. }
  158. //添加挂起进度
  159. public function addTodoPuase(){
  160. $id = input('id/d',0);
  161. $reason = input('reason','','trim');
  162. if($id <= 0){
  163. HelpHander::error('参数错误');
  164. }
  165. if(!$reason){
  166. HelpHander::error('未填写挂起原因');
  167. }
  168. $todo = Db::name('todo')->where('id',$id)->where('del',0)->find();
  169. if(!$todo||$todo['to_user_id'] != $this->userId){
  170. HelpHander::error('工单不存在');
  171. }
  172. if($todo['work_type_mode'] != 1){
  173. HelpHander::error('该订单不能挂起');
  174. }
  175. if($todo['pause'] != 1){
  176. HelpHander::error('该订单未挂起');
  177. }
  178. $ret = Db::name('todo_puase')
  179. ->insert([
  180. 'todo_id'=>$id,
  181. 'reason'=>$reason,
  182. 'create_time'=>getTime(),
  183. ]);
  184. if(!$ret){
  185. HelpHander::error('操作失败');
  186. }
  187. HelpHander::success([],'操作成功');
  188. }
  189. //获取待处理已完成已评价工单数量
  190. public function getOrdersCount(){
  191. $task = Db::name('task')
  192. ->where('user_id', $this->userId)
  193. ->where('org_id', $this->orgId)
  194. ->where('type',1)
  195. ->order('id','desc')
  196. ->select();
  197. $list = [];
  198. foreach ($task as $k => $value) {
  199. if ($value['type'] == 1) {//工单
  200. $todoInfo = Db::name('todo')
  201. ->where('id', $value['bus_id'])
  202. ->where('del',0)
  203. ->find();
  204. if (!empty($todoInfo)) {
  205. $list[] = $todoInfo;
  206. }
  207. } elseif($value['type'] == 2) {//巡更巡检
  208. $patrolInfo = Db::name('patrol_task')
  209. ->where('id', $value['bus_id'])
  210. ->where('del',0)
  211. ->find();
  212. if (!empty($patrolInfo)) {
  213. $list[] = $patrolInfo;
  214. }
  215. }elseif ($value['type'] == 3){ // 日常工作
  216. $dailyInfo = Db::name('daily_task')
  217. ->where('id', $value['bus_id'])
  218. ->where('del',0)
  219. ->find();
  220. if (!empty($dailyInfo)) {
  221. $list[] = $dailyInfo;
  222. }
  223. }elseif ($value['type'] == 4){ // 设备台账
  224. $deviceInfo = Db::name('device_task')
  225. ->where('id', $value['bus_id'])
  226. ->where('del',0)
  227. ->find();
  228. if (!empty($deviceInfo)) {
  229. $list[] = $deviceInfo;
  230. }
  231. }
  232. }
  233. $dcl = count($list);
  234. $map[] = ['to_user_id','=',$this->userId];
  235. $map[] = ['org_id','=',$this->orgId];
  236. $map[] = ['del','=',0];
  237. $a1 = Db::name('todo')
  238. ->where($map)
  239. ->where('todo_mode',3)
  240. ->count();
  241. $ywc = $a1?$a1:0;
  242. $ypj = Db::name('todo')
  243. ->alias('a')
  244. ->join('orders b','b.id=a.order_id','left')
  245. ->where('a.to_user_id',$this->userId)
  246. ->where('a.del',0)
  247. ->where('b.del',0)
  248. ->where('a.org_id',$this->orgId)
  249. ->where('b.order_mode',6)
  250. ->count();
  251. $data = [
  252. 'dcl'=>$dcl,
  253. 'ywc'=>$ywc,
  254. 'ypj'=>$ypj,
  255. 'pfdd'=>model('Orders')->orderCount($this->userId,$this->orgId,0),
  256. 'org_grab_order'=>(int)getOrgGrabOrder($this->userId,$this->orgId),
  257. ];
  258. HelpHander::success($data);
  259. }
  260. //工单挂起结束
  261. public function pauseEnd(){
  262. $id = input('id/d',0);
  263. if($id <= 0){
  264. HelpHander::error('参数错误');
  265. }
  266. $todo = Db::name('todo')->where('id',$id)->where('del',0)->find();
  267. if(!$todo||$todo['to_user_id'] != $this->userId){
  268. HelpHander::error('工单不存在');
  269. }
  270. if($todo['todo_mode'] != 2){
  271. HelpHander::error('该状态不能挂起');
  272. }
  273. if($todo['work_type_mode'] != 1){
  274. HelpHander::error('该订单不能挂起');
  275. }
  276. if($todo['pause'] != 1){
  277. HelpHander::error('该订单未挂起不能结束');
  278. }
  279. $ret = Db::name('todo')->where('id',$id)->update([
  280. 'pause' => 2,
  281. 'pause_end' => date('Y-m-d H:i:s'),
  282. ]);
  283. Db::name('todo_puase')
  284. ->insert([
  285. 'todo_id'=>$id,
  286. 'reason'=>'挂起结束',
  287. 'create_time'=>getTime(),
  288. ]);
  289. if(!$ret){
  290. HelpHander::error('操作失败');
  291. }
  292. HelpHander::success([],'操作成功');
  293. }
  294. //报修协同
  295. public function addInviteUser(){
  296. $toUserIds = input('toUserIds','','trim');
  297. $todoId = input('todoId','');
  298. $todo = Db::name('todo')->where('del',0)->where('id',$todoId)->find();
  299. if(!$todo){
  300. HelpHander::error('订单不存在');
  301. }
  302. if($todo['work_type_mode'] != 1){
  303. HelpHander::error('其他订单不能协同');
  304. }
  305. if(!$toUserIds){
  306. HelpHander::error('协同人不能为空');
  307. }
  308. $order = Db::name('orders')->where('id',$todo['order_id'])->where('del',0)->find();
  309. if(!$order){
  310. HelpHander::error('订单不存在');
  311. }
  312. $toUserIds = explode(',',$toUserIds);
  313. Db::startTrans();
  314. try {
  315. $xtUserIds = Db::name('todo')
  316. ->where('order_id',$todo['order_id'])
  317. ->where('del',0)
  318. ->column('to_user_id');
  319. foreach ($toUserIds as $k=>$v){
  320. if(!$v){
  321. continue;
  322. }
  323. if(in_array($v,$xtUserIds)){
  324. HelpHander::error('无法邀请人员,他已在此任务中');
  325. }
  326. $data = [
  327. 'order_id'=>$todo['order_id'],
  328. 'to_user_id'=>$v,
  329. 'todo_content'=>'',
  330. 'org_id'=>$this->orgId,
  331. 'create_user_id'=>$this->userId,
  332. 'create_time'=>date('Y-m-d H:i:s'),
  333. 'confirm_time'=>date('Y-m-d H:i:s'),
  334. 'todo_mode'=>2,
  335. 'work_type_mode'=>1,
  336. 'create_yyyy'=>date('Y'),
  337. 'create_yyyymm'=>date('Ym'),
  338. 'create_yyyymmdd'=>date('Ymd'),
  339. ];
  340. $todoId = Db::name('todo')->insertGetId($data);
  341. if(!$todoId){
  342. exception('操作失败');
  343. }
  344. $taskData = [
  345. 'org_id'=>$this->orgId,
  346. 'user_id'=>$v,
  347. 'type'=>1,
  348. 'start_time'=>date('Y-m-d H:i:s'),
  349. 'create_time'=>date('Y-m-d H:i:s'),
  350. 'bus_id'=>$todoId,
  351. ];
  352. $res = Db::name('task')
  353. ->insert($taskData);
  354. if(!$res){
  355. exception('操作失败1');
  356. }
  357. send_jpush([$v],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$todoId]);
  358. }
  359. Db::commit();
  360. HelpHander::success([],'操作成功');
  361. } catch (Exception $e) {
  362. Db::rollback();
  363. HelpHander::error($e->getMessage());
  364. }
  365. }
  366. }