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