2019-01-17 11:05:47 +08:00
< ? php
namespace app\common\Model ;
2019-07-12 11:15:04 +08:00
use Exception ;
2019-01-17 11:05:47 +08:00
use function GuzzleHttp\Promise\task ;
use service\DateService ;
2019-07-12 11:15:04 +08:00
use service\RandomService ;
2019-01-17 11:05:47 +08:00
use think\Db ;
2019-01-24 17:47:34 +08:00
use think\db\exception\DataNotFoundException ;
use think\db\exception\ModelNotFoundException ;
use think\exception\DbException ;
2019-01-17 11:05:47 +08:00
use think\facade\Hook ;
/**
* 任务
* Class Organization
* @ package app\common\Model
*/
class Task extends CommonModel
{
2019-02-16 12:18:11 +08:00
protected $append = [ 'priText' , 'liked' , 'stared' , 'tags' , 'childCount' , 'hasUnDone' , 'parentDone' , 'hasComment' , 'hasSource' , 'canRead' ];
2019-01-17 11:05:47 +08:00
public function read ( $code )
{
if ( ! $code ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '请选择任务' , 1 );
2019-01-17 11:05:47 +08:00
}
$task = self :: where ([ 'code' => $code ]) -> field ( 'id' , true ) -> find ();
if ( ! $task ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '该任务已失效' , 404 );
2019-01-17 11:05:47 +08:00
}
2019-06-14 18:05:42 +08:00
$project = Project :: where ([ 'code' => $task [ 'project_code' ]]) -> field ( 'name,open_begin_time' ) -> find ();
2019-01-17 11:05:47 +08:00
$stage = TaskStages :: where ([ 'code' => $task [ 'stage_code' ]]) -> field ( 'name' ) -> find ();
$task [ 'executor' ] = null ;
if ( $task [ 'assign_to' ]) {
$task [ 'executor' ] = Member :: where ([ 'code' => $task [ 'assign_to' ]]) -> field ( 'name,code,avatar' ) -> find ();
}
if ( $task [ 'pcode' ]) {
$task [ 'parentTask' ] = self :: where ([ 'code' => $task [ 'pcode' ]]) -> field ( 'id' , true ) -> find ();
2019-01-24 16:07:28 +08:00
$parents = [];
if ( isset ( $task [ 'path' ])) {
$paths = explode ( ',' , $task [ 'path' ]);
if ( $paths ) {
foreach ( $paths as $parentCode ) {
$item = self :: where ([ 'code' => $parentCode ]) -> field ( 'name' ) -> find ();
$parents [] = [ 'code' => $parentCode , 'name' => $item [ 'name' ]];
}
}
}
$task [ 'parentTasks' ] = array_reverse ( $parents );
2019-01-17 11:05:47 +08:00
}
2019-06-14 18:05:42 +08:00
$task [ 'openBeginTime' ] = $project [ 'open_begin_time' ];
2019-01-17 11:05:47 +08:00
$task [ 'projectName' ] = $project [ 'name' ];
$task [ 'stageName' ] = $stage [ 'name' ];
//TODO 查看权限
return $task ;
}
/**
* @ param $projectCode
* @ param $deleted
2019-07-12 11:15:04 +08:00
* @ throws DbException
2019-01-17 11:05:47 +08:00
*/
public function listForProject ( $projectCode , $deleted )
{
$this -> _list ( $where );
}
public function dateTotalForProject ( $projectCode , $beginTime = '' , $endTime = '' )
{
! $beginTime && $beginTime = date ( " Y-m-d " , strtotime ( " -20 day " ));
! $endTime && $endTime = nowTime ();
$dateList = DateService :: getDateFromRange ( $beginTime , $endTime );
$list = [];
if ( $dateList ) {
foreach ( $dateList as $date ) {
$currentDate = " { $date } 00:00:00 " ;
$currentDateEnd = " { $date } 23:59:59 " ;
$total = Task :: where ( " project_code = ' { $projectCode } ' and (create_time between ' { $currentDate } ' and ' { $currentDateEnd } ') " ) -> count ( 'id' );
$list [] = [ 'date' => $date , 'total' => $total ];
}
}
return $list ;
}
public function edit ( $code , $data )
{
2019-06-14 18:05:42 +08:00
2019-01-17 11:05:47 +08:00
if ( ! $code ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '请选择任务' , 1 );
2019-01-17 11:05:47 +08:00
}
$task = self :: where ([ 'code' => $code , 'deleted' => 0 ]) -> field ( 'id' , true ) -> find ();
if ( ! $task ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '该任务在回收站中无法编辑' , 1 );
2019-01-17 11:05:47 +08:00
}
if ( isset ( $data [ 'description' ]) && $data [ 'description' ] == '<p><br></p>' ) {
$data [ 'description' ] = " " ;
}
$result = self :: update ( $data , [ 'code' => $code ]);
$member = getCurrentMember ();
2019-06-27 23:05:27 +08:00
$type = '' ;
2019-01-17 11:05:47 +08:00
if ( isset ( $data [ 'name' ])) {
$type = 'name' ;
}
if ( isset ( $data [ 'description' ])) {
$type = 'content' ;
if ( ! $data [ 'description' ]) {
$type = 'clearContent' ;
}
}
if ( isset ( $data [ 'pri' ])) {
$type = 'pri' ;
}
2019-06-16 06:02:44 +08:00
if ( isset ( $data [ 'begin_time' ])) {
$type = 'setBeginTime' ;
if ( ! $data [ 'begin_time' ]) {
$type = 'clearBeginTime' ;
}
}
2019-01-17 11:05:47 +08:00
if ( isset ( $data [ 'end_time' ])) {
$type = 'setEndTime' ;
if ( ! $data [ 'end_time' ]) {
$type = 'clearEndTime' ;
}
}
2019-07-16 18:00:22 +08:00
if ( isset ( $data [ 'work_time' ])) {
$type = 'setWorkTime' ;
}
2019-06-27 23:05:27 +08:00
$type && self :: taskHook ( $member [ 'code' ], $code , $type );
2019-01-17 11:05:47 +08:00
//TODO 任务动态
return $result ;
}
public function taskSources ( $code )
{
if ( ! $code ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '请选择任务' , 1 );
2019-01-17 11:05:47 +08:00
}
$task = self :: where ([ 'code' => $code ]) -> field ( 'id' , true ) -> find ();
if ( ! $task ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '该任务不存在' , 2 );
2019-01-17 11:05:47 +08:00
}
$sources = SourceLink :: where ([ 'link_code' => $code , 'link_type' => 'task' ]) -> field ( 'id' , true ) -> order ( 'id desc' ) -> select () -> toArray ();
if ( $sources ) {
foreach ( $sources as & $source ) {
$source = SourceLink :: getSourceDetail ( $source [ 'code' ]);
}
}
return $sources ;
}
/**
* @ param $code
* @ param bool $like
* @ return bool
* @ throws \think\Exception
2019-07-12 11:15:04 +08:00
* @ throws DataNotFoundException
* @ throws ModelNotFoundException
* @ throws DbException
2019-01-17 11:05:47 +08:00
*/
public function like ( $code , $like = true )
{
if ( ! $code ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '请选择任务' , 1 );
2019-01-17 11:05:47 +08:00
}
$task = self :: where ([ 'code' => $code , 'deleted' => 0 ]) -> field ( 'id' , true ) -> find ();
if ( ! $task ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '该任务在回收站中不能点赞' , 1 );
2019-01-17 11:05:47 +08:00
}
if ( $like ) {
$result = self :: where ([ 'code' => $code ]) -> setInc ( 'like' );
} else {
$result = self :: where ([ 'code' => $code ]) -> setDec ( 'like' );;
}
$member = getCurrentMember ();
TaskLike :: likeTask ( $code , $member [ 'code' ], $like );
return $result ;
}
/**
* @ param $code
* @ param bool $star
* @ return bool
* @ throws \think\Exception
2019-07-12 11:15:04 +08:00
* @ throws DataNotFoundException
* @ throws ModelNotFoundException
* @ throws DbException
2019-01-17 11:05:47 +08:00
*/
public function star ( $code , $star = true )
{
if ( ! $code ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '请选择任务' , 1 );
2019-01-17 11:05:47 +08:00
}
$task = self :: where ([ 'code' => $code , 'deleted' => 0 ]) -> field ( 'id' , true ) -> find ();
if ( ! $task ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '该任务在回收站中不能收藏' , 1 );
2019-01-17 11:05:47 +08:00
}
if ( $star ) {
$result = self :: where ([ 'code' => $code ]) -> setInc ( 'star' );
} else {
$result = self :: where ([ 'code' => $code ]) -> setDec ( 'star' );;
}
$member = getCurrentMember ();
Collection :: starTask ( $code , $member [ 'code' ], $star );
return $result ;
}
/**
* 创建任务
* @ param $stageCode
* @ param $projectCode
* @ param $name
* @ param $memberCode
* @ param string $assignTo
* @ param string $parentCode
2019-07-12 11:15:04 +08:00
* @ param string $pri
* @ param string $description
* @ param array $tagCodes
2019-01-17 11:05:47 +08:00
* @ return Task
2019-07-12 11:15:04 +08:00
* @ throws DataNotFoundException
* @ throws DbException
* @ throws ModelNotFoundException
2019-01-17 11:05:47 +08:00
*/
2019-07-12 11:15:04 +08:00
public function createTask ( $stageCode , $projectCode , $name , $memberCode , $assignTo = '' , $parentCode = '' , $pri = '' , $description = '' , $tagCodes = [], $beginTime = '' , $endTime = '' )
2019-01-17 11:05:47 +08:00
{
if ( ! $name ) {
2019-07-12 11:15:04 +08:00
return error ( 1 , '请填写任务标题' );
2019-01-17 11:05:47 +08:00
}
$stage = TaskStages :: where ([ 'code' => $stageCode ]) -> field ( 'id' ) -> find ();
if ( ! $stage ) {
2019-07-12 11:15:04 +08:00
return error ( 2 , '该任务列表无效' );
2019-01-17 11:05:47 +08:00
}
2019-06-14 18:05:42 +08:00
$project = Project :: where ([ 'code' => $projectCode , 'deleted' => 0 ]) -> field ( 'id,open_task_private' ) -> find ();
2019-01-17 11:05:47 +08:00
if ( ! $project ) {
2019-07-12 11:15:04 +08:00
return error ( 3 , '该项目已失效' );
2019-01-17 11:05:47 +08:00
}
if ( $parentCode ) {
$parentTask = self :: where ([ 'code' => $parentCode ]) -> find ();
if ( ! $parentTask ) {
2019-07-17 08:15:38 +08:00
return error ( 8 , '父任务无效' );
2019-01-17 11:05:47 +08:00
}
if ( $parentTask [ 'deleted' ]) {
2019-07-12 11:15:04 +08:00
return error ( 6 , '父任务在回收站中无法编辑' );
2019-01-17 11:05:47 +08:00
}
2019-01-24 15:25:41 +08:00
if ( $parentTask [ 'done' ]) {
2019-07-12 11:15:04 +08:00
return error ( 7 , '父任务已完成,无法添加新的子任务' );
2019-01-24 15:25:41 +08:00
}
2019-01-17 11:05:47 +08:00
}
if ( $assignTo ) {
$assignMember = Member :: where ([ 'code' => $assignTo ]) -> field ( 'id' ) -> find ();
if ( ! $assignMember ) {
2019-07-12 11:15:04 +08:00
return error ( 4 , '任务执行人有误' );
2019-01-17 11:05:47 +08:00
}
}
Db :: startTrans ();
try {
$taskTitles = explode ( " \n " , $name );
foreach ( $taskTitles as $taskTitle ) {
if ( ! trim ( $taskTitle )) {
continue ;
}
$maxNum = self :: where ([ 'project_code' => $projectCode ]) -> max ( 'id_num' );
if ( ! $maxNum ) {
$maxNum = 0 ;
}
2019-01-24 16:07:28 +08:00
$path = '' ;
if ( $parentCode ) {
$parentTask [ 'path' ] && $parentTask [ 'path' ] = " , { $parentTask [ 'path' ] } " ;
$path = " { $parentTask [ 'code' ] } { $parentTask [ 'path' ] } " ;
}
2019-01-17 11:05:47 +08:00
$data = [
'create_time' => nowTime (),
'code' => createUniqueCode ( 'task' ),
'create_by' => $memberCode ,
'assign_to' => $assignTo ,
'id_num' => $maxNum + 1 ,
'project_code' => $projectCode ,
'pcode' => $parentCode ,
2019-01-24 16:07:28 +08:00
'path' => $path ,
2019-01-17 11:05:47 +08:00
'stage_code' => $stageCode ,
2019-07-12 11:15:04 +08:00
'pri' => $pri ,
'description' => $description ,
'begin_time' => $beginTime ,
'end_time' => $endTime ,
2019-06-14 18:05:42 +08:00
'private' => $project [ 'open_task_private' ] ? 1 : 0 ,
2019-01-17 11:05:47 +08:00
'name' => trim ( $taskTitle ),
];
$result = self :: create ( $data );
// self::update(['sort' => $result['id']], ['id' => $result['id']]);
self :: taskHook ( $memberCode , $data [ 'code' ], 'create' );
if ( $parentCode ) {
self :: taskHook ( $memberCode , $parentCode , 'createChild' , '' , '' , 0 , '' , '' , [ 'taskName' => trim ( $taskTitle )]);
}
$isExecutor = 0 ;
$logType = 'inviteMember' ;
if ( $assignTo ) {
if ( $memberCode == $assignTo ) {
$isExecutor = 1 ;
$logType = 'claim' ;
}
// Task::taskHook($memberCode, $data['code'], $logType, $assignTo);
TaskMember :: inviteMember ( $assignTo , $data [ 'code' ], 1 , $isExecutor );
}
if ( ! $assignTo || ! $isExecutor ) {
TaskMember :: inviteMember ( $memberCode , $data [ 'code' ], 0 , 1 );
}
2019-07-12 11:15:04 +08:00
if ( $tagCodes ) {
foreach ( $tagCodes as $tagCode ) {
TaskTag :: setTag ( $tagCode , $data [ 'code' ]);
}
}
2019-01-17 11:05:47 +08:00
}
2019-07-12 11:15:04 +08:00
2019-01-17 11:05:47 +08:00
//todo 添加任务动态
Db :: commit ();
2019-07-12 11:15:04 +08:00
} catch ( Exception $e ) {
2019-01-17 11:05:47 +08:00
Db :: rollback ();
2019-07-12 11:15:04 +08:00
return error ( 9 , $e -> getMessage ());
2019-01-17 11:05:47 +08:00
}
2019-03-08 09:49:27 +08:00
return $this -> read ( $result [ 'code' ]);
2019-01-17 11:05:47 +08:00
}
public function taskDone ( $taskCode , $done )
{
if ( ! $taskCode ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '请选择任务' , 1 );
2019-01-17 11:05:47 +08:00
}
$task = self :: where ([ 'code' => $taskCode ]) -> find ();
if ( ! $task ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务已失效' , 2 );
2019-01-17 11:05:47 +08:00
}
if ( $task [ 'deleted' ]) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务在回收站中无法进行编辑' , 3 );
2019-01-17 11:05:47 +08:00
}
2019-01-25 10:44:02 +08:00
if ( $task [ 'pcode' ] && $task [ 'parentDone' ]) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '父任务已完成,无法重做子任务' , 4 );
2019-01-24 15:25:41 +08:00
}
if ( $task [ 'hasUnDone' ]) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '子任务尚未全部完成,无法完成父任务' , 5 );
2019-01-24 15:25:41 +08:00
}
2019-01-17 11:05:47 +08:00
Db :: startTrans ();
try {
$result = self :: update ([ 'done' => $done ], [ 'code' => $taskCode ]);
//todo 添加任务动态,编辑权限检测
Db :: commit ();
2019-09-02 16:07:35 +08:00
$project = Project :: where ([ 'code' => $task [ 'project_code' ]]) -> field ( 'auto_update_schedule,schedule' ) -> find ();
if ( $project [ 'auto_update_schedule' ]) {
$taskCount = \app\common\Model\Task :: where ([ 'project_code' => $task [ 'project_code' ]]) -> count ( 'code' );
if ( $taskCount ) {
$doneTaskCount = \app\common\Model\Task :: where ([ 'project_code' => $task [ 'project_code' ], 'done' => 1 ]) -> count ( 'code' );
$schedule = $doneTaskCount / $taskCount * 100 ;
$project -> schedule = $schedule ;
$project -> save ();
}
}
$projectAutoUpdateSchedule = 1 ;
2019-07-12 11:15:04 +08:00
} catch ( Exception $e ) {
2019-01-17 11:05:47 +08:00
Db :: rollback ();
2019-07-12 11:15:04 +08:00
throw new Exception ( $e -> getMessage ());
2019-01-17 11:05:47 +08:00
}
$member = getCurrentMember ();
$done ? $type = 'done' : $type = 'redo' ;
self :: taskHook ( $member [ 'code' ], $taskCode , $type );
if ( $task [ 'pcode' ]) {
$done ? $type = 'doneChild' : $type = 'redoChild' ;
self :: taskHook ( $member [ 'code' ], $task [ 'pcode' ], $type );
}
return $result ;
}
/**
* 指派任务
* @ param $taskCode
* @ param $executorCode
* @ return TaskMember | bool
2019-07-12 11:15:04 +08:00
* @ throws DataNotFoundException
* @ throws ModelNotFoundException
* @ throws DbException
2019-01-17 11:05:47 +08:00
*/
public function assignTask ( $taskCode , $executorCode )
{
if ( ! $taskCode ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '请选择任务' , 1 );
2019-01-17 11:05:47 +08:00
}
$task = self :: where ([ 'code' => $taskCode ]) -> find ();
if ( ! $task ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务已失效' , 2 );
2019-01-17 11:05:47 +08:00
}
if ( $task [ 'deleted' ]) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务在回收站中无法进行指派' , 3 );
2019-01-17 11:05:47 +08:00
}
Db :: startTrans ();
try {
$result = TaskMember :: inviteMember ( $executorCode , $taskCode , 1 );
//todo 添加任务动态,编辑权限检测
Db :: commit ();
2019-07-12 11:15:04 +08:00
} catch ( Exception $e ) {
2019-01-17 11:05:47 +08:00
Db :: rollback ();
2019-07-12 11:15:04 +08:00
throw new Exception ( $e -> getMessage ());
2019-01-17 11:05:47 +08:00
}
return $result ;
}
2019-01-24 17:47:34 +08:00
public function batchAssignTask ( $taskCodes , $executorCode )
{
if ( $taskCodes ) {
try {
foreach ( $taskCodes as $taskCode ) {
$this -> assignTask ( $taskCode , $executorCode );
}
2019-07-12 11:15:04 +08:00
} catch ( Exception $e ) {
2019-01-24 17:47:34 +08:00
return error ( 201 , $e -> getMessage ());
}
}
return true ;
}
2019-01-17 11:05:47 +08:00
/**
* @ param $taskCode
* @ param $comment
2019-08-17 18:51:40 +08:00
* @ param $mentions
* @ return bool
2019-07-12 11:15:04 +08:00
* @ throws DataNotFoundException
* @ throws DbException
2019-08-17 18:51:40 +08:00
* @ throws ModelNotFoundException
2019-01-17 11:05:47 +08:00
*/
2019-08-17 18:51:40 +08:00
public function createComment ( $taskCode , $comment , $mentions = [])
2019-01-17 11:05:47 +08:00
{
if ( ! $taskCode ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '请选择任务' , 1 );
2019-01-17 11:05:47 +08:00
}
$task = self :: where ([ 'code' => $taskCode ]) -> find ();
if ( ! $task ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务已失效' , 2 );
2019-01-17 11:05:47 +08:00
}
2019-08-17 18:51:40 +08:00
// $data = [
// 'member_code' => getCurrentMember()['code'],
// 'source_code' => $taskCode,
// 'action_type' => 'task',
// 'code' => createUniqueCode('projectLog'),
// 'create_time' => nowTime(),
// 'is_comment' => 1,
// 'content' => $comment,
// 'type' => 'comment'
// ];
self :: taskHook ( getCurrentMember ()[ 'code' ], $taskCode , 'comment' , '' , 1 , '' , $comment , '' , $mentions );
return true ;
// return ProjectLog::create($data);
2019-01-17 11:05:47 +08:00
}
/**
* 任务排序
* @ param $stageCode string 移到的任务列表code
* @ param $codes array 经过排序的任务code列表
* @ return bool
2019-06-23 22:44:32 +08:00
* @ throws DataNotFoundException
* @ throws DbException
* @ throws ModelNotFoundException
2019-01-17 11:05:47 +08:00
*/
public function sort ( $stageCode , $codes )
{
if ( ! $codes ) {
return false ;
}
if ( $codes ) {
2019-06-23 22:44:32 +08:00
$stage = TaskStages :: where ([ 'code' => $stageCode ]) -> find ();
2019-01-17 11:05:47 +08:00
foreach ( $codes as $key => $code ) {
2019-06-23 22:44:32 +08:00
$task = self :: where ([ 'code' => $code ]) -> find ();
2019-01-17 11:05:47 +08:00
self :: update ([ 'sort' => $key , 'stage_code' => $stageCode ], [ 'code' => $code ]);
2019-06-23 22:44:32 +08:00
if ( $task [ 'stage_code' ] != $stageCode ) {
self :: taskHook ( getCurrentMember ()[ 'code' ], $code , 'move' , '' , '' , '' , '' , '' , [ 'stageName' => $stage [ 'name' ]]);
}
2019-01-17 11:05:47 +08:00
}
return true ;
}
return false ;
}
2020-03-03 21:11:53 +08:00
/**
* 成员任务
* @ param string $memberCode
* @ param int $done
* @ param int $taskType 搜索类型 1 - 我执行的 2 - 我参与的 3 - 我创建的
* @ param int $page
* @ param int $pageSize
* @ return array
*/
public function getMemberTasks ( $memberCode = '' , $done = 0 , $taskType = 1 , $page = 1 , $pageSize = 10 )
2019-01-17 11:05:47 +08:00
{
if ( ! $memberCode ) {
$memberCode = getCurrentMember ()[ 'code' ];
}
if ( $page < 1 ) {
$page = 1 ;
}
$offset = ( $page - 1 ) * $page ;
$limit = $pageSize ;
$prefix = config ( 'database.prefix' );
2019-01-24 10:44:49 +08:00
$doneSql = '' ;
if ( $done != - 1 ) {
$doneSql = " and t.done = { $done } " ;
}
2020-03-03 21:11:53 +08:00
//我执行的
if ( $taskType == 1 ) {
$sql = " select *,t.id as id,t.name as name,t.code as code,t.create_time as create_time,t.end_time,t.begin_time from { $prefix } task as t join { $prefix } project as p on t.project_code = p.code where t.deleted = 0 { $doneSql } and t.assign_to = ' { $memberCode } ' and p.deleted = 0 order by t.id desc " ;
}
//我参与的
if ( $taskType == 2 ) {
$sql = " select *,t.id as id,t.name as name,t.code as code,t.create_time as create_time,t.end_time,t.begin_time from { $prefix } task as t join { $prefix } project as p on t.project_code = p.code left join { $prefix } task_member as tm on tm.task_code = t.code where t.deleted = 0 { $doneSql } and tm.member_code = ' { $memberCode } ' and p.deleted = 0 order by t.id desc " ;
}
//我创建的
if ( $taskType == 3 ) {
$sql = " select *,t.id as id,t.name as name,t.code as code,t.create_time as create_time,t.end_time,t.begin_time from { $prefix } task as t join { $prefix } project as p on t.project_code = p.code where t.deleted = 0 { $doneSql } and t.create_by = ' { $memberCode } ' and p.deleted = 0 order by t.id desc " ;
}
2019-01-17 11:05:47 +08:00
$total = Db :: query ( $sql );
$total = count ( $total );
$sql .= " limit { $offset } , { $limit } " ;
$list = Db :: query ( $sql );
return [ 'list' => $list , 'total' => $total ];
}
2019-07-12 11:15:04 +08:00
/**
* 导入成员
* @ param \think\File $file
* @ return bool
* @ throws Exception
*/
public function uploadFile ( \think\File $file , $projectCode , $memberCode )
{
try {
$data = importExcel ( $file -> getInfo ()[ 'tmp_name' ]);
} catch ( Exception $e ) {
return error ( '201' , $e -> getMessage ());
}
$count = 0 ;
if ( $data ) {
foreach ( $data as $key => $item ) {
if ( $key > 2 ) {
$name = trim ( $item [ 'A' ]);
$pTaskName = trim ( $item [ 'B' ]);
$taskStageName = trim ( $item [ 'C' ]);
$executorName = trim ( $item [ 'D' ]);
$beginTime = trim ( $item [ 'E' ]);
$endTime = trim ( $item [ 'F' ]);
$description = trim ( $item [ 'G' ]);
$priName = trim ( $item [ 'H' ]);
$tagNameList = trim ( $item [ 'I' ]);
if ( ! $name || ! $taskStageName ) {
continue ;
}
$taskStage = TaskStages :: where ([ 'name' => $taskStageName , 'project_code' => $projectCode ]) -> field ( 'code' ) -> find ();
if ( ! $taskStage ) {
continue ;
}
$taskStageCode = $taskStage [ 'code' ];
switch ( $priName ) {
case '紧急' :
$pri = 1 ;
break ;
case '非常紧急' :
$pri = 2 ;
break ;
default :
$pri = 0 ;
}
$tagCodes = [];
if ( $tagNameList ) {
$tagNameList = explode ( ';' , $tagNameList );
foreach ( $tagNameList as $tagName ) {
$tag = TaskTag :: where ([ 'name' => $tagName , 'project_code' => $projectCode ]) -> field ( 'code' ) -> find ();
if ( $tag ) {
$tagCodes [] = $tag [ 'code' ];
}
}
}
if ( $pTaskName ) {
if ( ! isset ( $parentCode ) || ! $parentCode ) {
$pTask = self :: where ([ 'name' => $pTaskName , 'project_code' => $projectCode ]) -> field ( 'code' ) -> order ( 'id desc' ) -> find ();
if ( $pTask ) {
$parentCode = $pTask [ 'code' ];
} else {
$parentCode = '' ;
}
}
} else {
$parentCode = '' ;
}
$executorCode = '' ;
if ( $executorName ) {
$prefix = config ( 'database.prefix' );
$sql = " select m.code as code from { $prefix } project_member as pm join { $prefix } member as m on pm.member_code = m.code where m.name = ' { $executorName } ' " ;
$executor = Db :: query ( $sql );
if ( $executor ) {
$executorCode = $executor [ 0 ][ 'code' ];
}
}
$beginTime = DateService :: checkDateIsValid ( $beginTime ) ? $beginTime : '' ;
$endTime = DateService :: checkDateIsValid ( $endTime ) ? $endTime : '' ;
$task = $this -> createTask ( $taskStageCode , $projectCode , $name , $memberCode , $executorCode , $parentCode , $pri , $description , $tagCodes , $beginTime , $endTime );
if ( $task ) {
$count ++ ;
}
}
}
}
return $count ;
}
2019-01-17 11:05:47 +08:00
/**
* 批量放入回收站
* @ param $stageCode
* @ return Task
2019-07-12 11:15:04 +08:00
* @ throws DataNotFoundException
* @ throws ModelNotFoundException
* @ throws DbException
2019-01-17 11:05:47 +08:00
*/
public function recycleBatch ( $stageCode )
{
$stage = TaskStages :: where ([ 'code' => $stageCode ]) -> find ();
if ( ! $stage ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务列表不存在' , 1 );
2019-01-17 11:05:47 +08:00
}
$where = [ 'stage_code' => $stageCode , 'deleted' => 0 ];
$taskCodes = self :: where ( $where ) -> column ( 'code' );
$memberCode = getCurrentMember ()[ 'code' ];
if ( $taskCodes ) {
foreach ( $taskCodes as $taskCode ) {
self :: taskHook ( $memberCode , $taskCode , 'recycle' );
}
}
$result = self :: update ([ 'deleted' => 1 , 'deleted_time' => nowTime ()], $where );
return $result ;
}
/**
* 放入回收站
* @ param $code
* @ return Project
2019-07-12 11:15:04 +08:00
* @ throws DataNotFoundException
* @ throws ModelNotFoundException
* @ throws DbException
2019-01-17 11:05:47 +08:00
*/
public function recycle ( $code )
{
$info = self :: where ([ 'code' => $code ]) -> find ();
if ( ! $info ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务不存在' , 1 );
2019-01-17 11:05:47 +08:00
}
if ( $info [ 'deleted' ]) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务已在回收站' , 2 );
2019-01-17 11:05:47 +08:00
}
$result = self :: update ([ 'deleted' => 1 , 'deleted_time' => nowTime ()], [ 'code' => $code ]);
self :: taskHook ( getCurrentMember ()[ 'code' ], $code , 'recycle' );
return $result ;
}
/**
* 恢复任务
* @ param $code
* @ return Project
2019-07-12 11:15:04 +08:00
* @ throws DataNotFoundException
* @ throws ModelNotFoundException
* @ throws DbException
2019-01-17 11:05:47 +08:00
*/
public function recovery ( $code )
{
$info = self :: where ([ 'code' => $code ]) -> find ();
if ( ! $info ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务不存在' , 1 );
2019-01-17 11:05:47 +08:00
}
if ( ! $info [ 'deleted' ]) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务已恢复' , 2 );
2019-01-17 11:05:47 +08:00
}
$result = self :: update ([ 'deleted' => 0 ], [ 'code' => $code ]);
self :: taskHook ( getCurrentMember ()[ 'code' ], $code , 'recovery' );
return $result ;
}
public function del ( $code )
{
//权限判断
$info = self :: where ([ 'code' => $code ]) -> find ();
if ( ! $info ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务不存在' , 1 );
2019-01-17 11:05:47 +08:00
}
Db :: startTrans ();
try {
self :: where ([ 'code' => $code ]) -> delete ();
self :: where ([ 'pcode' => $code ]) -> delete ();
TaskMember :: where ([ 'task_code' => $code ]) -> delete ();
TaskLike :: where ([ 'task_code' => $code ]) -> delete ();
ProjectLog :: where ([ 'source_code' => $code , 'action_type' => 'task' ]) -> delete ();
Db :: commit ();
2019-07-12 11:15:04 +08:00
} catch ( Exception $e ) {
2019-01-17 11:05:47 +08:00
Db :: rollback ();
2019-07-12 11:15:04 +08:00
throw new Exception ( $e -> getMessage ());
2019-01-17 11:05:47 +08:00
}
return true ;
}
public function getPriTextAttr ( $value , $data )
{
if ( ! isset ( $data [ 'pri' ])) {
$data [ 'pri' ] = 0 ;
}
$status = [ 0 => '普通' , 1 => '紧急' , 2 => '非常紧急' ];
return $status [ $data [ 'pri' ]];
}
2019-02-16 12:18:11 +08:00
/**
* 标签
*/
public function getTagsAttr ( $value , $data )
{
$tags = [];
if ( isset ( $data [ 'code' ])) {
$tags = TaskToTag :: where ([ 'task_code' => $data [ 'code' ]]) -> field ( 'id' , true ) -> order ( 'id asc' ) -> select () -> toArray ();
}
return $tags ;
}
2019-03-07 14:32:30 +08:00
2019-01-24 16:07:28 +08:00
/**
* 子任务数
*/
2019-01-17 11:05:47 +08:00
public function getChildCountAttr ( $value , $data )
{
$childTasks = [];
if ( isset ( $data [ 'code' ])) {
$childTaskCount = self :: where ([ 'pcode' => $data [ 'code' ], 'deleted' => 0 ]) -> count ( 'id' );
$childTasks [] = $childTaskCount ;
$childTaskCount = self :: where ([ 'pcode' => $data [ 'code' ], 'deleted' => 0 , 'done' => 1 ]) -> count ( 'id' );
$childTasks [] = $childTaskCount ;
}
return $childTasks ;
}
2019-01-24 16:07:28 +08:00
/**
* 父任务是否完成
*/
2019-01-24 15:25:41 +08:00
public function getParentDoneAttr ( $value , $data )
{
$done = 1 ;
if ( isset ( $data [ 'code' ]) && isset ( $data [ 'pcode' ]) && $data [ 'pcode' ]) {
2019-03-08 09:49:27 +08:00
$task = self :: where ([ 'code' => $data [ 'pcode' ]]) -> field ( 'done,deleted' ) -> find ();
2019-03-07 14:32:30 +08:00
if ( $task && ! $task [ 'deleted' ] && ! $task [ 'done' ]) {
2019-01-24 15:25:41 +08:00
$done = 0 ;
}
}
return $done ;
}
2019-01-24 16:07:28 +08:00
/**
* 是否有子任务未完成
*/
2019-01-24 15:25:41 +08:00
public function getHasUnDoneAttr ( $value , $data )
{
$hasUnDone = 0 ;
if ( isset ( $data [ 'code' ])) {
2019-03-07 14:32:30 +08:00
$taskCount = self :: where ([ 'pcode' => $data [ 'code' ], 'done' => 0 , 'deleted' => 0 ]) -> count ( 'id' );
2019-01-24 15:25:41 +08:00
if ( $taskCount ) {
$hasUnDone = 1 ;
}
}
return $hasUnDone ;
}
2019-01-17 11:05:47 +08:00
public function getHasCommentAttr ( $value , $data )
{
$comment = 0 ;
if ( isset ( $data [ 'code' ])) {
$comment = ProjectLog :: where ([ 'source_code' => $data [ 'code' ], 'type' => 'task' , 'is_comment' => 1 ]) -> count ( 'id' );
}
return $comment ;
}
public function getHasSourceAttr ( $value , $data )
{
$sources = 0 ;
if ( isset ( $data [ 'code' ])) {
$sources = SourceLink :: where ([ 'link_code' => $data [ 'code' ], 'link_type' => 'task' ]) -> count ( 'id' );
}
return $sources ;
}
2019-01-22 16:04:42 +08:00
/**
* 是否有阅读权限
* @ param $value
* @ param $data
* @ return bool
2019-07-12 11:15:04 +08:00
* @ throws DataNotFoundException
* @ throws ModelNotFoundException
* @ throws DbException
2019-01-22 16:04:42 +08:00
*/
public function getCanReadAttr ( $value , $data )
{
$canRead = 1 ;
if ( isset ( $data [ 'private' ])) {
if ( $data [ 'private' ]) {
$taskMember = TaskMember :: where ([ 'task_code' => $data [ 'code' ], 'member_code' => getCurrentMember ()[ 'code' ]]) -> field ( 'id' ) -> find ();
if ( ! $taskMember ) {
$canRead = 0 ;
}
}
}
return $canRead ;
}
2019-01-17 11:05:47 +08:00
public function getLikedAttr ( $value , $data )
{
$like = 0 ;
if ( isset ( $data [ 'code' ])) {
$member = getCurrentMember ();
$taskLike = TaskLike :: where ([ 'task_code' => $data [ 'code' ], 'member_code' => $member [ 'code' ]]) -> find ();
if ( $taskLike ) {
$like = 1 ;
}
}
return $like ;
}
public function getStaredAttr ( $value , $data )
{
$stared = 0 ;
if ( isset ( $data [ 'code' ])) {
$member = getCurrentMember ();
$taskStar = Collection :: where ([ 'source_code' => $data [ 'code' ], 'type' => 'task' , 'member_code' => $member [ 'code' ]]) -> find ();
if ( $taskStar ) {
$stared = 1 ;
}
}
return $stared ;
}
/** 任务变动钩子
* @ param $memberCode
* @ param $taskCode
* @ param string $type
* @ param string $toMemberCode
* @ param int $isComment
* @ param string $remark
* @ param string $content
* @ param string $fileCode
* @ param array $data
* @ param string $tag
*/
public static function taskHook ( $memberCode , $taskCode , $type = 'create' , $toMemberCode = '' , $isComment = 0 , $remark = '' , $content = '' , $fileCode = '' , $data = [], $tag = 'task' )
{
$data = [ 'memberCode' => $memberCode , 'taskCode' => $taskCode , 'remark' => $remark , 'type' => $type , 'content' => $content , 'isComment' => $isComment , 'toMemberCode' => $toMemberCode , 'fileCode' => $fileCode , 'data' => $data , 'tag' => $tag ];
Hook :: listen ( $tag , $data );
}
}