Todo.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. ->find();
  203. if (!empty($todoInfo)) {
  204. $list[] = $todoInfo;
  205. }
  206. } elseif($value['type'] == 2) {//巡更巡检
  207. $patrolInfo = Db::name('patrol_task')
  208. ->where('id', $value['bus_id'])
  209. ->find();
  210. if (!empty($patrolInfo)) {
  211. $list[] = $patrolInfo;
  212. }
  213. }elseif ($value['type'] == 3){ // 日常工作
  214. $dailyInfo = Db::name('daily_task')
  215. ->where('id', $value['bus_id'])
  216. ->find();
  217. if (!empty($dailyInfo)) {
  218. $list[] = $dailyInfo;
  219. }
  220. }elseif ($value['type'] == 4){ // 设备台账
  221. $deviceInfo = Db::name('device_task')
  222. ->where('id', $value['bus_id'])
  223. ->find();
  224. if (!empty($deviceInfo)) {
  225. $list[] = $deviceInfo;
  226. }
  227. }
  228. }
  229. $dcl = count($list);
  230. $map[] = ['to_user_id','=',$this->userId];
  231. $map[] = ['org_id','=',$this->orgId];
  232. $map[] = ['del','=',0];
  233. $a1 = Db::name('todo')
  234. ->where($map)
  235. ->where('todo_mode',3)
  236. ->count();
  237. $ywc = $a1?$a1:0;
  238. $ypj = Db::name('todo')
  239. ->alias('a')
  240. ->join('orders b','b.id=a.order_id','left')
  241. ->where('a.to_user_id',$this->userId)
  242. ->where('a.del',0)
  243. ->where('b.del',0)
  244. ->where('a.org_id',$this->orgId)
  245. ->where('b.order_mode',6)
  246. ->count();
  247. $data = [
  248. 'dcl'=>$dcl,
  249. 'ywc'=>$ywc,
  250. 'ypj'=>$ypj,
  251. 'pfdd'=>model('Orders')->orderCount($this->userId,$this->orgId,0),
  252. 'org_grab_order'=>(int)getOrgGrabOrder($this->userId,$this->orgId),
  253. ];
  254. HelpHander::success($data);
  255. }
  256. //工单挂起结束
  257. public function pauseEnd(){
  258. $id = input('id/d',0);
  259. if($id <= 0){
  260. HelpHander::error('参数错误');
  261. }
  262. $todo = Db::name('todo')->where('id',$id)->where('del',0)->find();
  263. if(!$todo||$todo['to_user_id'] != $this->userId){
  264. HelpHander::error('工单不存在');
  265. }
  266. if($todo['todo_mode'] != 2){
  267. HelpHander::error('该状态不能挂起');
  268. }
  269. if($todo['work_type_mode'] != 1){
  270. HelpHander::error('该订单不能挂起');
  271. }
  272. if($todo['pause'] != 1){
  273. HelpHander::error('该订单未挂起不能结束');
  274. }
  275. $ret = Db::name('todo')->where('id',$id)->update([
  276. 'pause' => 2,
  277. 'pause_end' => date('Y-m-d H:i:s'),
  278. ]);
  279. Db::name('todo_puase')
  280. ->insert([
  281. 'todo_id'=>$id,
  282. 'reason'=>'挂起结束',
  283. 'create_time'=>getTime(),
  284. ]);
  285. if(!$ret){
  286. HelpHander::error('操作失败');
  287. }
  288. HelpHander::success([],'操作成功');
  289. }
  290. //报修协同
  291. public function addInviteUser(){
  292. $toUserIds = input('toUserIds','','trim');
  293. $todoId = input('todoId','');
  294. $todo = Db::name('todo')->where('del',0)->where('id',$todoId)->find();
  295. if(!$todo){
  296. HelpHander::error('订单不存在');
  297. }
  298. if($todo['work_type_mode'] != 1){
  299. HelpHander::error('其他订单不能协同');
  300. }
  301. if(!$toUserIds){
  302. HelpHander::error('协同人不能为空');
  303. }
  304. $order = Db::name('orders')->where('id',$todo['order_id'])->where('del',0)->find();
  305. if(!$order){
  306. HelpHander::error('订单不存在');
  307. }
  308. $toUserIds = explode(',',$toUserIds);
  309. Db::startTrans();
  310. try {
  311. $xtUserIds = Db::name('todo')
  312. ->where('order_id',$todo['order_id'])
  313. ->where('del',0)
  314. ->column('to_user_id');
  315. foreach ($toUserIds as $k=>$v){
  316. if(!$v){
  317. continue;
  318. }
  319. if(in_array($v,$xtUserIds)){
  320. HelpHander::error('无法邀请人员,他已在此任务中');
  321. }
  322. $data = [
  323. 'order_id'=>$todo['order_id'],
  324. 'to_user_id'=>$v,
  325. 'todo_content'=>'',
  326. 'org_id'=>$this->orgId,
  327. 'create_user_id'=>$this->userId,
  328. 'create_time'=>date('Y-m-d H:i:s'),
  329. 'confirm_time'=>date('Y-m-d H:i:s'),
  330. 'todo_mode'=>2,
  331. 'work_type_mode'=>1,
  332. 'create_yyyy'=>date('Y'),
  333. 'create_yyyymm'=>date('Ym'),
  334. 'create_yyyymmdd'=>date('Ymd'),
  335. ];
  336. $todoId = Db::name('todo')->insertGetId($data);
  337. if(!$todoId){
  338. exception('操作失败');
  339. }
  340. $taskData = [
  341. 'org_id'=>$this->orgId,
  342. 'user_id'=>$v,
  343. 'type'=>1,
  344. 'start_time'=>date('Y-m-d H:i:s'),
  345. 'create_time'=>date('Y-m-d H:i:s'),
  346. 'bus_id'=>$todoId,
  347. ];
  348. $res = Db::name('task')
  349. ->insert($taskData);
  350. if(!$res){
  351. exception('操作失败1');
  352. }
  353. send_jpush([$v],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$todoId]);
  354. }
  355. Db::commit();
  356. HelpHander::success([],'操作成功');
  357. } catch (Exception $e) {
  358. Db::rollback();
  359. HelpHander::error($e->getMessage());
  360. }
  361. }
  362. }