Task.php 34KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 任务及基础
  4. // +----------------------------------------------------------------------
  5. // | Author: yykun
  6. // +----------------------------------------------------------------------
  7. namespace app\work\controller;
  8. use app\oa\logic\TaskLogic as TasksLogic;
  9. use app\work\logic\TaskLogic;
  10. use app\work\traits\WorkAuthTrait;
  11. use think\Request;
  12. use think\Hook;
  13. use app\admin\controller\ApiCommon;
  14. use app\admin\model\Message;
  15. use think\helper\Time;
  16. use think\Db;
  17. class Task extends ApiCommon
  18. {
  19. use WorkAuthTrait;
  20. /**
  21. * 用于判断权限
  22. * @permission 无限制
  23. * @allow 登录用户可访问
  24. * @other 其他根据系统设置
  25. **/
  26. public function _initialize()
  27. {
  28. $action = [
  29. 'permission'=>[''],
  30. 'allow'=>[
  31. 'index', 'mytask', 'updatetop', 'updateorder', 'read', 'update', 'readloglist', 'updatepriority',
  32. 'updateowner', 'delownerbyid', 'delstruceurebyid', 'updatestoptime', 'updatelable', 'updatename',
  33. 'taskover', 'datelist','save','delmainuserid','rename','delete','archive','recover','archlist',
  34. 'archivetask','setover','updateclassorder','excelimport','excelexport','taskusers','ownertasklist']
  35. ];
  36. Hook::listen('check_auth',$action);
  37. $request = Request::instance();
  38. $a = strtolower($request->action());
  39. if (!in_array($a, $action['permission'])) {
  40. parent::_initialize();
  41. }
  42. //权限判断
  43. $param = $this->param;
  44. if ($param['task_id']) {
  45. $userInfo = $this->userInfo;
  46. $taskModel = model('Task');
  47. if (!$taskModel->checkTask($param['task_id'], $userInfo)) {
  48. header('Content-Type:application/json; charset=utf-8');
  49. exit(json_encode(['code'=>102,'error'=>'没有权限']));
  50. }
  51. }
  52. }
  53. /**
  54. * 项目下任务列表
  55. * @author yykun
  56. * @return
  57. */
  58. public function index()
  59. {
  60. $param = $this->param;
  61. $userInfo = $this->userInfo;
  62. $taskModel = model('Task');
  63. if (!$param['work_id']) {
  64. return resultArray(['error' => '参数错误']);
  65. }
  66. $list = $taskModel->getDataList($param, $userInfo['id']);
  67. return resultArray(['data' => $list]);
  68. }
  69. public function ownerTaskList()
  70. {
  71. $param = $this->param;
  72. $userInfo = $this->userInfo;
  73. $taskModel = model('Task');
  74. if (!$param['work_id']) {
  75. return resultArray(['error' => '参数错误']);
  76. }
  77. $list = $taskModel->getOwnerTaskList($param, $userInfo['id']);
  78. return resultArray(['data' => $list]);
  79. }
  80. /**
  81. * 任务列表导出
  82. *
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. * @throws \think\exception\DbException
  86. */
  87. public function excelExport(){
  88. $param = $this->param;
  89. $userInfo = $this->userInfo;
  90. $param['user_id'] = $userInfo['id'];
  91. # 权限判断
  92. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('excelExport', $param['work_id'], $userInfo['id'])) {
  93. header('Content-Type:application/json; charset=utf-8');
  94. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  95. }
  96. $TaskLogic = new TasksLogic();
  97. $data = $TaskLogic->excelExport($param);
  98. return $data;
  99. }
  100. /**
  101. * 客户导入模板下载
  102. * @author Michael_xu
  103. * @param string $save_path 本地保存路径 用于错误数据导出,在 Admin\Model\Excel::batchImportData()调用
  104. * @return
  105. */
  106. public function excelDownload($save_path = '')
  107. {
  108. $param = $this->param;
  109. $userInfo = $this->userInfo;
  110. $excelModel = new \app\admin\model\Excel();
  111. $field_list = [
  112. '0' => ['name' => '任务名称', 'field' => 'name'],
  113. '1' => ['name' => '任务描述', 'field' => 'description'],
  114. '2' => ['name' => '开始时间', 'field' => 'start_time'],
  115. '3' => ['name' => '结束时间', 'field' => 'stop_time'],
  116. '4' => ['name' => '创建人', 'field' => 'create_user_id'],
  117. '5' => ['name' => '参与人', 'field' => 'owner_user_id'],
  118. '6' => ['name' => '所在任务列表', 'field' => 'class_id'],
  119. ];
  120. // 导入的字段列表
  121. $excelModel->excelImportDownload($field_list, 'task', $save_path);
  122. }
  123. /**
  124. * 客户数据导入
  125. *
  126. * @return \think\response\Json
  127. * @throws \think\db\exception\DataNotFoundException
  128. * @throws \think\db\exception\ModelNotFoundException
  129. * @throws \think\exception\DbException
  130. */
  131. public function excelImport()
  132. {
  133. $param = $this->param;
  134. $userInfo = $this->userInfo;
  135. # 权限判断
  136. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('excelImport', $param['work_id'], $userInfo['id'])) {
  137. header('Content-Type:application/json; charset=utf-8');
  138. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  139. }
  140. $excelModel = new \app\admin\model\Excel();
  141. $param['create_user_id'] = $userInfo['id'];
  142. $param['owner_user_id'] = $param['owner_user_id'] ? : 0;
  143. $file = request()->file('file');
  144. $param['types'] = 'task';
  145. // $res = $excelModel->importExcel($file, $param, $this);
  146. $res = $excelModel->batchTaskImportData($file, $param, $this);
  147. if (!$res) {
  148. return resultArray(['error' => $excelModel->getError()]);
  149. }
  150. return resultArray(['data' => $excelModel->getError()]);
  151. }
  152. /**
  153. * 任务搜索
  154. *
  155. * @param TaskLogic $taskLogic
  156. * @return \think\response\Json
  157. */
  158. public function search(TaskLogic $taskLogic)
  159. {
  160. $data = $taskLogic->getSearchData($this->param);
  161. return resultArray(['data' => $data]);
  162. }
  163. /**
  164. * 我的任务
  165. * @author yykun
  166. * @return
  167. */
  168. public function myTask()
  169. {
  170. $taskModel = model('Task');
  171. $userId = $this->userInfo['id'];
  172. $data = [];
  173. $data[0]['title'] = '收件箱';
  174. $data[1]['title'] = '今天要做';
  175. $data[2]['title'] = '下一步要做';
  176. $data[3]['title'] = '以后要做';
  177. for ($k = 0; $k < 4; $k++) {
  178. $where = [];
  179. $where['ishidden'] = 0;
  180. $where['is_top'] = $k;
  181. $where['pid'] = 0;
  182. $where['whereStr'] = ' ( task.create_user_id ='.$userId.' or ( task.owner_user_id like "%,'.$userId.',%") or ( task.main_user_id = '.$userId.' ) )';
  183. if (!empty($this->param['search'])) $where['taskSearch'] = '(task.name like "%' . $this->param['search'] . '%" OR task.description like "%' . $this->param['search'] . '%")';
  184. $resData = $taskModel->getProjectTaskList($where, $this->param);
  185. $data[$k]['is_top'] = $k;
  186. $data[$k]['list'] = $resData['list'] ? : [];
  187. $data[$k]['count'] = $resData['count'] ? : 0;
  188. }
  189. return resultArray(['data' => $data]);
  190. }
  191. /**
  192. * 我的任务 拖拽改变分类
  193. *
  194. * @return \think\response\Json
  195. * @throws \think\Exception
  196. * @throws \think\exception\PDOException
  197. */
  198. public function updateTop()
  199. {
  200. $param = $this->param;
  201. $tolist = $param['tolist'];
  202. $fromlist = $param['fromlist'];
  203. # 权限判断
  204. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('setTaskOrder', $param['work_id'], $this->userInfo['id'])) {
  205. header('Content-Type:application/json; charset=utf-8');
  206. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  207. }
  208. if ($param['to_top_id'] || $param['to_top_id'] == 0) {
  209. if ($tolist) {
  210. foreach ($tolist as $k1 => $v1) {
  211. $toData = [];
  212. $toData['is_top'] = $param['to_top_id'];
  213. $toData['top_order_id'] = $k1+1;
  214. Db::name('Task')->where(['task_id' => $v1])->update($toData);
  215. }
  216. }
  217. }
  218. if ($param['from_top_id'] || $param['from_top_id'] == 0) {
  219. if ($fromlist) {
  220. foreach ($fromlist as $k2 => $v2) {
  221. $fromData = [];
  222. $fromData['is_top'] = $param['from_top_id'];
  223. $fromData['top_order_id'] = $k2+1;
  224. Db::name('Task')->where(['task_id' => $v2])->update($fromData);
  225. }
  226. }
  227. } else {
  228. return resultArray(['error' => '参数错误' ]);
  229. }
  230. return resultArray(['data' => true ]);
  231. }
  232. /**
  233. * 项目 拖拽改变分类并排序
  234. * @author yykun
  235. * @return
  236. */
  237. public function updateOrder()
  238. {
  239. $param = $this->param;
  240. # 权限判断
  241. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('setTaskOrder', $param['work_id'], $this->userInfo['id'])) {
  242. header('Content-Type:application/json; charset=utf-8');
  243. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  244. }
  245. if ($param['tolist']) {
  246. $tolist = $param['tolist'];
  247. foreach ($tolist as $k1 => $v1) {
  248. $toData = [];
  249. $toData['class_id'] = $param['toid'];
  250. $toData['order_id'] = $k1+1;
  251. Db::name('Task')->where(['task_id' => $v1])->update($toData);
  252. }
  253. }
  254. if ($param['fromlist']) {
  255. $fromlist = $param['fromlist'];
  256. foreach ($fromlist as $k2 => $v2) {
  257. $fromData = [];
  258. $fromData['class_id'] = $param['fromid'];
  259. $fromData['order_id'] = $k2+1;
  260. Db::name('Task')->where(['task_id' => $v2])->update($fromData);
  261. }
  262. }
  263. return resultArray(['data' => true ]);
  264. }
  265. /**
  266. * 项目下 拖拽整个分类排序
  267. *
  268. * @return \think\response\Json
  269. * @throws \think\Exception
  270. * @throws \think\db\exception\DataNotFoundException
  271. * @throws \think\db\exception\ModelNotFoundException
  272. * @throws \think\exception\DbException
  273. * @throws \think\exception\PDOException
  274. */
  275. public function updateClassOrder()
  276. {
  277. $param = $this->param;
  278. $classlist = $param['class_ids'];
  279. if (!$param['work_id'] || !$param['class_ids']) {
  280. return resultArray(['error'=>'参数错误']);
  281. }
  282. # 权限判断
  283. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('updateClassOrder', $param['work_id'], $this->userInfo['id'])) {
  284. header('Content-Type:application/json; charset=utf-8');
  285. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  286. }
  287. foreach ($classlist as $k => $v) {
  288. $temp = [];
  289. $temp['order_id'] = $k+1;
  290. Db::name('WorkTaskClass')->where(['work_id' => $param['work_id'],'class_id' => $v])->update($temp);
  291. }
  292. return resultArray(['data' => '操作成功!']);
  293. }
  294. /**
  295. * 任务详情
  296. * @author yykun
  297. * @return
  298. */
  299. public function read()
  300. {
  301. $param = $this->param;
  302. $userInfo = $this->userInfo;
  303. if (!$param['task_id']) {
  304. return resultArray(['error'=>'参数错误']);
  305. }
  306. $taskModel = model('Task');
  307. $taskData = $taskModel->getDataById($param['task_id'], $userInfo);
  308. # 获取任务的项目信息
  309. $workInfo = Db::name('work')->field(['work_id', 'group_id', 'is_open'])->where('work_id', $taskData['work_id'])->find();
  310. # 是否是公开项目
  311. $userId = $userInfo['id'];
  312. $groupId = !empty($workInfo['is_open']) ? $workInfo['group_id'] : 0;
  313. # 获取项目下的权限
  314. $taskData['auth'] = !empty($taskData['work_id']) ? $this->getRuleList($workInfo['work_id'], $userId, $groupId) : [];
  315. if ($taskData) {
  316. return resultArray(['data'=>$taskData]);
  317. } else {
  318. return resultArray(['error'=>$taskModel->getError()]);
  319. }
  320. }
  321. /**
  322. * 任务编辑
  323. *
  324. * @return \think\response\Json
  325. * @throws \think\db\exception\DataNotFoundException
  326. * @throws \think\db\exception\ModelNotFoundException
  327. * @throws \think\exception\DbException
  328. */
  329. public function update()
  330. {
  331. $taskModel = model('Task');
  332. $param = $this->param;
  333. $userInfo = $this->userInfo;
  334. $param['create_user_id'] = $userInfo['id'];
  335. # 权限判断
  336. $action = 'updateChildTask'; # 修改子任务
  337. if (!empty($param['customer_ids']) || !empty($param['customer_ids']) || !empty($param['customer_ids']) || !empty($param['customer_ids'])) {
  338. $action = 'saveTaskRelation'; # 关联业务
  339. } elseif (!empty($param['description'])) {
  340. $action = 'setTaskDescription'; # 任务描述
  341. }
  342. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth($action, $param['work_id'], $this->userInfo['id'])) {
  343. header('Content-Type:application/json; charset=utf-8');
  344. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  345. }
  346. $ary = array('owner_userid_del','owner_userid_add','stop_time','lable_id_add','lable_id_del','name','structure_id_del','structure_id_add');
  347. if ((in_array($param['type'],$ary))) {
  348. return resultArray(['error'=>'参数错误']);
  349. }
  350. if ($taskModel->updateDetTask($param)) {
  351. return resultArray(['data'=>'操作成功']);
  352. } else {
  353. return resultArray(['error'=>$taskModel->getError()]);
  354. }
  355. }
  356. /**
  357. * 任务操作记录
  358. * @author yykun
  359. * @return
  360. */
  361. public function readLoglist()
  362. {
  363. $param = $this->param;
  364. $taskModel = model('Task');
  365. if (!$param['task_id']) return resultArray(['error'=>'参数错误']);
  366. $list = $taskModel->getTaskLogList($param);
  367. return resultArray(['data'=>$list]);
  368. }
  369. /**
  370. * 优先级设置
  371. * @author yykun
  372. * @return
  373. */
  374. public function updatePriority()
  375. {
  376. $param = $this->param;
  377. $userInfo = $this->userInfo;
  378. $param['create_user_id'] = $userInfo['id'];
  379. if (!isset($param['priority_id']) || !$param['task_id']) {
  380. return resultArray(['error'=>'参数错误']);
  381. }
  382. # 权限判断
  383. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('setTaskPriority', $param['work_id'], $this->userInfo['id'])) {
  384. header('Content-Type:application/json; charset=utf-8');
  385. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  386. }
  387. $flag = Db::name('Task')->where(['task_id' => $param['task_id']])->setField('priority',$param['priority_id']);
  388. if ($flag) {
  389. return resultArray(['data'=>'操作成功']);
  390. } else {
  391. return resultArray(['error'=>'操作失败']);
  392. }
  393. }
  394. /**
  395. * 参与人/参与部门编辑
  396. * @author yykun
  397. * @return
  398. */
  399. public function updateOwner()
  400. {
  401. $param = $this->param;
  402. $userInfo = $this->userInfo;
  403. $task_id = $param['task_id'] ? : '';
  404. $param['create_user_id'] = $userInfo['id'];
  405. $taskInfo = db('task')->where(['task_id' => $param['task_id']])->find();
  406. if (!$taskInfo) {
  407. return resultArray(['error'=>'参数错误']);
  408. }
  409. # 权限判断
  410. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('setTaskOwnerUser', $param['work_id'], $this->userInfo['id'])) {
  411. header('Content-Type:application/json; charset=utf-8');
  412. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  413. }
  414. $data = [];
  415. //部门编辑
  416. $structure_ids = '';
  417. if ($param['structure_ids']) {
  418. $structure_ids = arrayToString($param['structure_ids']);
  419. }
  420. $owner_user_id = '';
  421. $sendUserArr = [];
  422. if ($param['owner_userids']) {
  423. $owner_user_id = arrayToString($param['owner_userids']);
  424. foreach ($param['owner_userids'] as $k=>$v) {
  425. if (!in_array($v,stringToArray($taskInfo['owner_user_id']))) {
  426. $sendUserArr[] = $v;
  427. }
  428. }
  429. // $content = $userInfo['realname'].'邀请您参与《'.$taskInfo['name'].'》项目,请及时查看';
  430. // if ($sendUserArr) sendMessage($sendUserArr,$content,1);
  431. actionLog($param['task_id'],$param['owner_user_id'],$param['structure_ids'],'修改了参与人');
  432. }
  433. $data['structure_ids'] = $structure_ids;
  434. $data['owner_user_id'] = $owner_user_id;
  435. $resUpdate = db('task')->where(['task_id' => $param['task_id']])->update($data);
  436. if ($resUpdate) {
  437. //站内信
  438. if ($sendUserArr) {
  439. (new Message())->send(
  440. Message::TASK_INVITE,
  441. [
  442. 'title' => $taskInfo['name'],
  443. 'action_id' => $taskInfo['task_id']
  444. ],
  445. $sendUserArr
  446. );
  447. }
  448. return resultArray(['data'=>'修改成功']);
  449. }
  450. return resultArray(['error'=>'修改失败或数据无变化']);
  451. }
  452. /**
  453. * 单独删除参与人
  454. * @author yykun
  455. * @return
  456. */
  457. public function delOwnerById()
  458. {
  459. $taskModel = model('Task');
  460. $userInfo = $this->userInfo;
  461. $param = $this->param;
  462. $param['create_user_id'] = $userInfo['id'];
  463. $ary = array('owner_userid_del','owner_userid_add');
  464. if (!in_array($param['type'], $ary)) {
  465. return resultArray(['error'=>'参数错误']);
  466. }
  467. # 权限判断
  468. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('setTaskOwnerUser', $param['work_id'], $this->userInfo['id'])) {
  469. header('Content-Type:application/json; charset=utf-8');
  470. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  471. }
  472. $ret = $taskModel->updateDetTask($param);
  473. if ($ret) {
  474. return resultArray(['data'=>'操作成功']);
  475. } else {
  476. return resultArray(['error'=>$taskModel->getError()]);
  477. }
  478. }
  479. /**
  480. * 单独删除参与部门
  481. * @author yykun
  482. * @return
  483. */
  484. public function delStruceureById()
  485. {
  486. $taskModel = model('Task');
  487. $param = $this->param;
  488. $userInfo = $this->userInfo;
  489. $param['create_user_id'] = $userInfo['id'];
  490. $ary = array('structure_id_del','structure_id_add');
  491. if (!in_array($param['type'], $ary)) {
  492. return resultArray(['error'=>'参数错误']);
  493. }
  494. $res = $taskModel->updateDetTask($param);
  495. if ($res) {
  496. return resultArray(['data'=>'操作成功']);
  497. } else {
  498. return resultArray(['error'=>$taskModel->getError()]);
  499. }
  500. }
  501. /**
  502. * 设置任务截止时间
  503. * @author yykun
  504. * @return
  505. */
  506. public function updateStoptime()
  507. {
  508. $taskModel = model('Task');
  509. $param = $this->param;
  510. $userInfo = $this->userInfo;
  511. $param['create_user_id'] = $userInfo['id'];
  512. // if (!$param['stop_time']) {
  513. // return resultArray(['error'=>'参数错误']);
  514. // }
  515. # 权限判断
  516. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('setTaskTime', $param['work_id'], $this->userInfo['id'])) {
  517. header('Content-Type:application/json; charset=utf-8');
  518. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  519. }
  520. if ($taskModel->updateDetTask($param)) {
  521. return resultArray(['data'=>'操作成功']);
  522. } else {
  523. return resultArray(['error'=>$taskModel->getError()]);
  524. }
  525. }
  526. /**
  527. * 修改任务标签
  528. * @author yykun
  529. * @return
  530. */
  531. public function updateLable()
  532. {
  533. $taskModel = model('Task');
  534. $param = $this->param;
  535. $userInfo = $this->userInfo;
  536. $param['create_user_id'] = $userInfo['id'];
  537. $ary = array('lable_id_add','lable_id_del');
  538. if (!in_array($param['type'], $ary)) {
  539. return resultArray(['error'=>'参数错误']);
  540. }
  541. # 权限判断
  542. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('setTaskLabel', $param['work_id'], $userInfo['id'])) {
  543. header('Content-Type:application/json; charset=utf-8');
  544. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  545. }
  546. if (isset($param['lable_id_add']) && !is_array($param['lable_id_add'])) {
  547. $label_id_arr[] = $param['lable_id_add'];
  548. $param['lable_id_add'] = $label_id_arr;
  549. }
  550. if (isset($param['lable_id_del']) && !is_array($param['lable_id_del'])) {
  551. $label_id_arr[] = $param['lable_id_del'];
  552. $param['lable_id_del'] = $label_id_arr;
  553. }
  554. if ($taskModel->updateDetTask($param)) {
  555. return resultArray(['data'=>'操作成功']);
  556. } else {
  557. return resultArray(['error'=>$taskModel->getError()]);
  558. }
  559. }
  560. /**
  561. * 修改任务名称
  562. * @author yykun
  563. * @return
  564. */
  565. public function updateName()
  566. {
  567. $taskModel = model('Task');
  568. $param = $this->param;
  569. $userInfo = $this->userInfo;
  570. $param['create_user_id'] = $userInfo['id'];
  571. if ($param['type'] !== 'name') {
  572. return resultArray(['error'=>'参数错误']);
  573. }
  574. # 权限判断
  575. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('setTaskTitle', $param['work_id'], $userInfo['id'])) {
  576. header('Content-Type:application/json; charset=utf-8');
  577. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  578. }
  579. $res = $taskModel->updateDetTask($param);
  580. if ($res) {
  581. return resultArray(['data'=>'操作成功']);
  582. } else {
  583. return resultArray(['error'=>$taskModel->getError()]);
  584. }
  585. }
  586. /**
  587. * 任务标记结束
  588. *
  589. * @return \think\response\Json
  590. * @throws \think\db\exception\DataNotFoundException
  591. * @throws \think\db\exception\ModelNotFoundException
  592. * @throws \think\exception\DbException
  593. */
  594. public function taskOver()
  595. {
  596. $taskModel = model('Task');
  597. $param = $this->param;
  598. $userInfo = $this->userInfo;
  599. $param['create_user_id'] = $userInfo['id'];
  600. if (!$param['task_id'] || !$param['status'] ){
  601. return resultArray(['error'=>'参数错误']);
  602. }
  603. # 权限判断
  604. $pid = Db::name('task')->where('task_id', $param['task_id'])->value('pid');
  605. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth(empty($pid) ? 'setTaskStatus' : 'setChildTaskStatus', $param['work_id'], $userInfo['id'])) {
  606. header('Content-Type:application/json; charset=utf-8');
  607. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  608. }
  609. $taskInfo = Db::name('Task')->where(['task_id' => $param['task_id']])->find();
  610. if ($param['status'] == '5') {
  611. $flag = Db::name('Task')->where(['task_id' => $param['task_id']])->setField('status',5);
  612. if ($flag && !$taskInfo['pid']) {
  613. $temp['user_id'] = $userInfo['id'];
  614. $temp['content'] = '任务标记结束';
  615. $temp['create_time'] = time();
  616. $temp['task_id'] = $param['task_id'];
  617. Db::name('WorkTaskLog')->insert($temp);
  618. actionLog($taskInfo['task_id'],$taskInfo['owner_user_id'],$taskInfo['structure_ids'],'任务标记结束');
  619. //抄送站内信
  620. $sendUserArr = [];
  621. $sendUserArr[] = $taskInfo['create_user_id'];
  622. if ($taskInfo['main_user_id']) {
  623. $sendUserArr[] = $taskInfo['main_user_id'];
  624. }
  625. if ($taskInfo['owner_user_id']) {
  626. $sendUserArr = $sendUserArr ? array_merge($sendUserArr,stringToArray($taskInfo['owner_user_id'])) : stringToArray($taskInfo['owner_user_id']);
  627. }
  628. if ($sendUserArr) {
  629. (new Message())->send(
  630. Message::TASK_OVER,
  631. [
  632. 'title' => $taskInfo['name'],
  633. 'action_id' => $param['task_id']
  634. ],
  635. $sendUserArr
  636. );
  637. }
  638. }
  639. } else {
  640. $flag = Db::name('Task')->where('task_id ='.$param['task_id'])->setField('status',1);
  641. if ($flag && !$taskInfo['pid']) {
  642. $temp['user_id'] = $userInfo['id'];
  643. $temp['content'] = '任务标记开始';
  644. $temp['create_time'] = time();
  645. $temp['task_id'] = $param['task_id'];
  646. Db::name('WorkTaskLog')->insert($temp);
  647. actionLog($taskInfo['task_id'],$taskInfo['owner_user_id'],$taskInfo['structure_ids'],'任务标记开始');
  648. }
  649. }
  650. if ($flag) {
  651. return resultArray(['data' => true ]);
  652. } else {
  653. return resultArray(['error' => '标记失败' ]);
  654. }
  655. }
  656. /**
  657. * 日历任务展示/月份
  658. * @author yykun
  659. * @return
  660. */
  661. public function dateList()
  662. {
  663. $param = $this->param;
  664. $taskModel = model('Task');
  665. $userInfo = $this->userInfo;
  666. $param['user_id'] = $userInfo['id'];
  667. $data = $taskModel->getDateList($param);
  668. return resultArray(['data'=>$data]);
  669. }
  670. /**
  671. * 添加任务
  672. * @author Michael_xu
  673. * @return
  674. */
  675. public function save()
  676. {
  677. $param = $this->param;
  678. $taskModel = model('Task');
  679. $workModel = model('Work');
  680. if (!$param['name']) {
  681. return resultArray(['error'=>'参数错误']);
  682. }
  683. $userInfo = $this->userInfo;
  684. $param['create_user_id'] = $userInfo['id'];
  685. $param['create_user_name'] = $userInfo['realname'];
  686. # 任务权限判断
  687. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth(empty($param['pid']) ? 'addChildTask' : 'saveTask', $param['work_id'], $userInfo['id'])) {
  688. header('Content-Type:application/json; charset=utf-8');
  689. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  690. }
  691. // if ($param['work_id'] && !$workModel->isCheck('work','task','save',$param['work_id'],$userInfo['id'])) {
  692. // header('Content-Type:application/json; charset=utf-8');
  693. // exit(json_encode(['code'=>102,'error'=>'无权操作']));
  694. // }
  695. $res = $taskModel->createTask($param);
  696. if ($res) {
  697. return resultArray(['data'=>$res]);
  698. } else {
  699. return resultArray(['error'=>$taskModel->getError()]);
  700. }
  701. }
  702. /**
  703. * 删除主负责人
  704. * @author yykun
  705. * @return
  706. */
  707. public function delMainUserId()
  708. {
  709. $param = $this->param;
  710. $workModel = model('Task');
  711. if ($param['task_id']) {
  712. $userInfo = $this->userInfo;
  713. $param['create_user_id'] = $userInfo['id'];
  714. $taskInfo = Db::name('Task')->where(['task_id' => $param['task_id']])->find();
  715. $data = [];
  716. $data['main_user_id'] = '';
  717. $data['status'] = 1;
  718. $flag = Db::name('Task')->where(['task_id' => $param['task_id']])->update($data);
  719. if ($flag && !$taskInfo['pid']) {
  720. actionLog($taskInfo['task_id'],$taskInfo['owner_user_id'],$taskInfo['structure_ids'],'删除负责人');
  721. return resultArray(['data'=>'操作成功']);
  722. }
  723. return resultArray(['error'=>'操作失败']);
  724. } else {
  725. return resultArray(['error'=>'参数错误']);
  726. }
  727. }
  728. /**
  729. * 重命名任务
  730. * @author yykun
  731. * @return
  732. */
  733. public function rename()
  734. {
  735. $param = $this->param;
  736. $workModel = model('Work');
  737. if (!$param['rename'] || !$param['work_id']) {
  738. return resultArray(['error'=>'参数错误']);
  739. }
  740. $userInfo = $this->userInfo;
  741. $param['create_user_id'] = $userInfo['id'];
  742. $flag = $workModel->rename($param);
  743. if ($flag) {
  744. return resultArray(['data'=>'编辑成功']);
  745. } else {
  746. return resultArray(['error'=>$workModel->getError()]);
  747. }
  748. }
  749. /**
  750. * 删除任务
  751. * @author yykun
  752. * @return
  753. */
  754. public function delete()
  755. {
  756. $param = $this->param;
  757. $taskModel = model('Task');
  758. if (!$param['task_id']) {
  759. return resultArray(['error'=>'参数错误']);
  760. }
  761. # 权限判断
  762. $pid = Db::name('task')->where('task_id', $param['task_id'])->value('pid');
  763. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth(empty($pid) ? 'deleteTask' : 'deleteChildTask', $param['work_id'], $this->userInfo['id'])) {
  764. header('Content-Type:application/json; charset=utf-8');
  765. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  766. }
  767. $userInfo = $this->userInfo;
  768. $param['create_user_id'] = $userInfo['id'];
  769. $flag = $taskModel->delTaskById($param);
  770. if ($flag) {
  771. return resultArray(['data'=>'删除成功']);
  772. } else {
  773. return resultArray(['error'=>$taskModel->getError()]);
  774. }
  775. }
  776. /**
  777. * 归档任务
  778. * @author yykun
  779. * @return
  780. */
  781. public function archive()
  782. {
  783. $param = $this->param;
  784. $taskModel = model('Task');
  785. if (!$param['task_id']) {
  786. return resultArray(['error'=>'参数错误']);
  787. }
  788. # 权限判断
  789. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('archiveTask', $param['work_id'], $this->userInfo['id'])) {
  790. header('Content-Type:application/json; charset=utf-8');
  791. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  792. }
  793. $userInfo = $this->userInfo;
  794. $param['create_user_id'] = $userInfo['id'];
  795. $flag = $taskModel->archiveData($param);
  796. if ($flag) {
  797. $temp['user_id'] = $userInfo['id'];
  798. $temp['content'] = '归档任务';
  799. $temp['create_time'] = time();
  800. $temp['task_id'] = $param['task_id'];
  801. Db::name('WorkTaskLog')->insert($temp);
  802. return resultArray(['data'=>'归档成功']);
  803. } else {
  804. return resultArray(['error'=>$taskModel->getError()]);
  805. }
  806. }
  807. /**
  808. * 恢复归档任务
  809. * @author yykun
  810. * @return
  811. */
  812. public function recover()
  813. {
  814. $param = $this->param;
  815. $taskModel = model('Task');
  816. if (!$param['task_id']) {
  817. return resultArray(['error'=>'参数错误']);
  818. }
  819. # 权限判断
  820. if (!empty($param['work_id']) && !$this->checkWorkOperationAuth('archiveTask', $param['work_id'], $this->userInfo['id'])) {
  821. header('Content-Type:application/json; charset=utf-8');
  822. exit(json_encode(['code' => 102, 'error' => '无权操作!']));
  823. }
  824. $userInfo = $this->userInfo;
  825. $param['create_user_id'] = $userInfo['id'];
  826. $flag = $taskModel->recover($param);
  827. if ($flag) {
  828. $temp['user_id'] = $userInfo['id'];
  829. $temp['content'] = '恢复归档任务';
  830. $temp['create_time'] = time();
  831. $temp['task_id'] = $param['task_id'];
  832. Db::name('WorkTaskLog')->insert($temp);
  833. return resultArray(['data'=>'操作成功']);
  834. } else {
  835. return resultArray(['error'=>$taskModel->getError()]);
  836. }
  837. }
  838. /**
  839. * 归档任务列表
  840. * @author yykun
  841. * @return
  842. */
  843. public function archList()
  844. {
  845. $param = $this->param;
  846. $userInfo = $this->userInfo;
  847. $taskModel = model('Task');
  848. if (!$param['work_id']) return resultArray(['error'=>'参数错误']);
  849. $request = [];
  850. $request['work_id'] = $param['work_id'];
  851. $request['is_archive'] = 1;
  852. $list = $taskModel->getTaskList($request);
  853. return resultArray(['data'=>$list]);
  854. }
  855. /**
  856. * 归档某一类已完成任务
  857. * @author yykun
  858. * @return
  859. */
  860. public function archiveTask()
  861. {
  862. $param = $this->param;
  863. if (!$param['class_id']) return resultArray(['error'=>'参数错误']);
  864. $data = array();
  865. $data['is_archive'] = 1;
  866. $data['archive_time'] = time();
  867. $res = db('task')->where(['class_id' => $param['class_id'],'status' => '5'])->update($data);
  868. if ($res) {
  869. return resultArray(['data' => '操作成功']);
  870. } else {
  871. return resultArray(['error' => '暂无已完成任务,归档失败!']);
  872. }
  873. }
  874. /**
  875. * 任务成员列表
  876. *
  877. * @return \think\response\Json
  878. * @throws \think\db\exception\DataNotFoundException
  879. * @throws \think\db\exception\ModelNotFoundException
  880. * @throws \think\exception\DbException
  881. */
  882. public function taskUsers()
  883. {
  884. $userId = $this->userInfo['id'];
  885. # 查询条件
  886. $where['create_user_id'] = $userId;
  887. $where['main_user_id'] = $userId;
  888. $where['owner_user_id'] = ['like', '%,'.$userId.',%'];
  889. # 查询数据
  890. $data = Db::name('task')->field(['create_user_id', 'main_user_id', 'owner_user_id'])->whereOr($where)->select();
  891. # 整理数据
  892. $userIds = [];
  893. foreach ($data AS $key => $value) {
  894. if (!empty($value['create_user_id'])) $userIds[] = $value['create_user_id'];
  895. if (!empty($value['main_user_id'])) $userIds[] = $value['main_user_id'];
  896. $ownerUserIds = explode(',', $value['owner_user_id']);
  897. foreach ($ownerUserIds AS $k => $v) {
  898. if (!empty($v)) $userIds[] = $v;
  899. }
  900. }
  901. $userIds = array_unique($userIds);
  902. # 查询参与人
  903. $userList = Db::name('admin_user')->field(['id', 'realname'])->whereIn('id', $userIds)->select();
  904. return resultArray(['data' => $userList]);
  905. }
  906. }