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
{
2020-05-09 11:45:26 +08:00
protected $append = [ 'priText' , 'statusText' , '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
}
2024-01-06 14:11:33 +08:00
$task = self :: where ([ 'code' => $code ]) -> field ( 'id' , true ) -> find () -> toArray ();
2019-01-17 11:05:47 +08:00
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 ();
}
2024-01-06 14:11:33 +08:00
$exchange_list = explode ( ',' , $task [ 'exchange_list' ]);
$copied_list = explode ( ',' , $task [ 'copied_list' ]);
$task [ 'exchange_info' ] = null ;
$task [ 'copied_info' ] = null ;
foreach ( $exchange_list as $k => $v ){
2024-01-09 15:42:21 +08:00
$find = Member :: where ([ 'code' => $v ]) -> field ( 'name,code,avatar' ) -> find ();
if ( $find ){
$task [ 'exchange_info' ][] = $find ;
}
2024-01-06 14:11:33 +08:00
}
foreach ( $copied_list as $k => $v ){
2024-01-09 15:42:21 +08:00
$find = Member :: where ([ 'code' => $v ]) -> field ( 'name,code,avatar' ) -> find ();
if ( $find ){
$task [ 'copied_info' ][] = $find ;
}
2024-01-06 14:11:33 +08:00
}
2024-01-08 15:54:24 +08:00
if ( $task [ 'department_liaison' ]){
2024-01-08 16:29:47 +08:00
$task [ 'liasion_info' ] = MemberAccount :: where ([ 'code' => $task [ 'department_liaison' ]]) -> field ( 'name,member_code as code,avatar' ) -> find ();
2024-01-08 15:54:24 +08:00
} else {
$task [ 'liasion_info' ] = [];
}
if ( $task [ 'department' ]){
2024-01-08 16:02:22 +08:00
$task [ 'department_info' ] = Department :: where ([ 'code' => $task [ 'department' ]]) -> field ( 'name' ) -> find ();
2024-01-08 15:54:24 +08:00
} else {
$task [ 'department_info' ] = [];
}
2019-01-17 11:05:47 +08:00
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' ;
}
2020-03-04 09:52:58 +08:00
if ( isset ( $data [ 'status' ])) {
$type = 'status' ;
}
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 {
2020-05-09 11:45:26 +08:00
$result = self :: where ([ 'code' => $code ]) -> setDec ( 'like' );
2019-01-17 11:05:47 +08:00
}
$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 {
2020-05-09 11:45:26 +08:00
$result = self :: where ([ 'code' => $code ]) -> setDec ( 'star' );
2019-01-17 11:05:47 +08:00
}
$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
*/
2024-01-08 14:56:47 +08:00
public function createTask ( $stageCode , $projectCode , $name , $memberCode , $assignTo = '' , $parentCode = '' , $pri = '' , $description = '' , $tagCodes = [], $beginTime = '' , $endTime = '' , $data = [])
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 ;
}
2020-05-09 11:45:26 +08:00
$maxSort = self :: where ( 'project_code' , $projectCode ) -> where ( 'stage_code' , $stageCode ) -> max ( 'sort' );
$maxSort = $maxSort ? ? 0 ;
2019-01-24 16:07:28 +08:00
$path = '' ;
if ( $parentCode ) {
$parentTask [ 'path' ] && $parentTask [ 'path' ] = " , { $parentTask [ 'path' ] } " ;
$path = " { $parentTask [ 'code' ] } { $parentTask [ 'path' ] } " ;
}
2024-01-09 10:25:47 +08:00
$code = TaskStages :: where ( 'project_code' , $projectCode ) -> where ( 'name' , '进行中' ) -> where ( 'deleted' , 0 ) -> value ( 'code' );
2024-01-10 10:43:39 +08:00
$datas = [
2019-01-17 11:05:47 +08:00
'create_time' => nowTime (),
'code' => createUniqueCode ( 'task' ),
'create_by' => $memberCode ,
'assign_to' => $assignTo ,
'id_num' => $maxNum + 1 ,
2020-05-09 22:04:33 +08:00
'sort' => $maxSort + 65536 ,
2019-01-17 11:05:47 +08:00
'project_code' => $projectCode ,
'pcode' => $parentCode ,
2019-01-24 16:07:28 +08:00
'path' => $path ,
2024-01-09 10:25:47 +08:00
'stage_code' => $code ,
2019-07-12 11:15:04 +08:00
'pri' => $pri ,
'description' => $description ,
'begin_time' => $beginTime ,
2024-01-06 11:07:40 +08:00
'end_time' => date ( 'Y-m-d H:i:s' , bcdiv ( $endTime , 1000 )),
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 ),
2024-01-06 11:10:30 +08:00
'exchange_list' => $data [ 'exchangeList' ],
2024-01-06 13:37:31 +08:00
'copied_list' => $data [ 'copied_list' ],
2024-01-08 15:47:26 +08:00
'department' => $data [ 'department_code' ],
'department_liaison' => $data [ 'liasion_code' ],
2024-01-09 15:47:07 +08:00
'status' => 2 ,
2019-01-17 11:05:47 +08:00
];
2024-01-10 10:43:39 +08:00
$result = self :: create ( $datas );
2019-01-17 11:05:47 +08:00
// self::update(['sort' => $result['id']], ['id' => $result['id']]);
2024-01-10 10:43:39 +08:00
self :: taskHook ( $memberCode , $datas [ 'code' ], 'create' );
2019-01-17 11:05:47 +08:00
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);
2024-01-10 10:43:39 +08:00
TaskMember :: inviteMember ( $assignTo , $datas [ 'code' ], 1 , $isExecutor );
2019-01-17 11:05:47 +08:00
}
if ( ! $assignTo || ! $isExecutor ) {
2024-01-10 10:43:39 +08:00
TaskMember :: inviteMember ( $memberCode , $datas [ 'code' ], 0 , 1 );
2019-01-17 11:05:47 +08:00
}
2019-07-12 11:15:04 +08:00
if ( $tagCodes ) {
foreach ( $tagCodes as $tagCode ) {
2024-01-10 10:43:39 +08:00
TaskTag :: setTag ( $tagCode , $datas [ 'code' ]);
2019-07-12 11:15:04 +08:00
}
}
2024-01-10 09:54:57 +08:00
if ( isset ( $data [ 'file_list' ]) && $data [ 'file_list' ]){
$file_list = explode ( ',' , $data [ 'file_list' ]);
2024-01-10 10:43:39 +08:00
foreach ( $file_list as $v ){
\app\common\Model\SourceLink :: createSource ( 'file' , $v , 'task' , $datas [ 'code' ]);
2024-01-10 09:54:57 +08:00
}
}
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' ]) {
2020-08-30 17:19:14 +08:00
throw new Exception ( '子任务尚未全部完成,无法完成父任务' , 55 );
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-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 );
2024-01-10 11:50:05 +08:00
if ( $task [ 'exchange_list' ]){
$exchange_list = explode ( ',' , $task [ 'exchange_list' ]);
foreach ( $exchange_list as $k => $exchange ){
if ( $executorCode == $exchange ){
unset ( $exchange_list [ $k ]);
}
}
if ( $exchange_list ){
$task -> exchange_list = implode ( ',' , $exchange_list );
$task -> save ();
} else {
$task -> exchange_list = '' ;
$task -> save ();
}
}
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
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
*/
2024-01-08 10:44:39 +08:00
public function createComment ( $taskCode , $comment , $mentions = [], $type = 0 )
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
}
2024-01-08 14:56:47 +08:00
$task = self :: where ([ 'code' => $taskCode ]) -> withAttr ( 'exchange_list' , function ( $value ) {
if ( $value ){
return explode ( ',' , $value );
} else {
return [];
}
}) -> withAttr ( 'transferred' , function ( $value ) {
if ( $value ){
return explode ( ',' , $value );
} else {
return [];
}
})
-> find ();
2019-01-17 11:05:47 +08:00
if ( ! $task ) {
2019-07-12 11:15:04 +08:00
throw new Exception ( '任务已失效' , 2 );
2019-01-17 11:05:47 +08:00
}
2024-01-08 15:04:24 +08:00
$task = $task -> toArray ();
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'
// ];
2024-01-08 13:43:36 +08:00
$comments = 'comment' ;
2024-01-08 10:44:39 +08:00
if ( $type == 1 ){
2024-01-10 11:50:05 +08:00
$data [ 'status' ] = 2 ; //进行中
2024-01-08 10:44:39 +08:00
if ( $task [ 'exchange_list' ]){
2024-01-10 11:53:08 +08:00
if ( $task [ 'transferred' ] != '' ){
$transferred = explode ( ',' , $task [ 'transferred' ]);
} else {
$transferred = [];
}
2024-01-10 11:50:05 +08:00
array_push ( $transferred , $task [ 'exchange_list' ][ 0 ]);
$data [ 'transferred' ] = implode ( ',' , $transferred );
$data [ 'assign_to' ] = $task [ 'exchange_list' ][ 0 ];
self :: where ( 'id' , $task [ 'id' ]) -> update ( $data );
2024-01-08 13:43:36 +08:00
} else {
2024-01-09 10:25:47 +08:00
$code = TaskStages :: where ( 'project_code' , $task [ 'project_code' ]) -> where ( 'name' , '已完成' ) -> where ( 'deleted' , 0 ) -> value ( 'code' );
2024-01-08 13:43:36 +08:00
$data [ 'status' ] = 1 ;
2024-01-09 10:25:47 +08:00
$data [ 'done' ] = 1 ;
$data [ 'execute_status' ] = 'done' ;
2024-01-08 14:01:44 +08:00
$data [ 'stage_code' ] = $code ;
2024-01-10 11:50:05 +08:00
self :: name ( 'task' ) -> where ( 'id' , $task [ 'id' ]) -> update ( $data );
2024-01-08 10:44:39 +08:00
}
2024-01-08 13:43:36 +08:00
$comments = 'pass' ;
2024-01-08 10:44:39 +08:00
} elseif ( $type == 2 ){
2024-01-08 13:43:36 +08:00
$comments = 'reject' ;
2024-01-08 10:44:39 +08:00
$data [ 'status' ] = 2 ;
2024-01-08 15:04:24 +08:00
self :: name ( 'task' ) -> where ( 'id' , $task [ 'id' ]) -> update ( $data );
2024-01-08 11:54:32 +08:00
} elseif ( $type == 3 ){
2024-01-08 13:43:36 +08:00
$comments = 'add' ;
2024-01-08 11:54:32 +08:00
$data [ 'status' ] = 4 ;
2024-01-08 15:04:24 +08:00
self :: name ( 'task' ) -> where ( 'id' , $task [ 'id' ]) -> update ( $data );
2024-01-08 10:44:39 +08:00
}
2024-01-08 13:43:36 +08:00
self :: taskHook ( getCurrentMember ()[ 'code' ], $taskCode , $comments , '' , 1 , '' , $comment , '' , $mentions );
2024-01-08 11:54:32 +08:00
2019-08-17 18:51:40 +08:00
return true ;
// return ProjectLog::create($data);
2019-01-17 11:05:47 +08:00
}
/**
2020-05-09 11:45:26 +08:00
* 任务排序,事件:把 $preCode 移动到 $nextCode 前面
* @ param $preCode string 前一个移动的列表
* @ param $nextCode string 后一个移动的列表
* @ param $toStageCode string 要移动到到的分组
2019-01-17 11:05:47 +08:00
* @ return bool
2019-06-23 22:44:32 +08:00
* @ throws DataNotFoundException
* @ throws DbException
* @ throws ModelNotFoundException
2019-01-17 11:05:47 +08:00
*/
2020-05-09 11:45:26 +08:00
public function sort ( $preCode , $nextCode , $toStageCode )
2019-01-17 11:05:47 +08:00
{
2020-05-09 11:45:26 +08:00
$preTask = self :: where ([ 'code' => $preCode ]) -> field ( 'sort,stage_code,done' ) -> find ();
if ( $preCode == $nextCode ) {
2019-01-17 11:05:47 +08:00
return false ;
}
2020-05-09 11:45:26 +08:00
if ( $preTask ) {
$done = $preTask [ 'done' ];
if ( $nextCode ) {
$nextTask = self :: where ([ 'code' => $nextCode ]) -> field ( 'sort' ) -> find ();
$nextPreTask = self :: where ( 'sort' , '<' , $nextTask [ 'sort' ]) -> where ( 'code' , '<>' , $nextCode ) -> where ( 'stage_code' , '=' , $toStageCode ) -> where ( 'done' , $done ) -> order ( 'sort desc' ) -> find ();
$nextPreTaskSort = $nextPreTask ? $nextPreTask [ 'sort' ] : 0 ;
2020-05-09 21:15:44 +08:00
$newSort = ( int )( $nextTask [ 'sort' ] + $nextPreTaskSort ) / 2 ;
2020-05-09 11:45:26 +08:00
} else {
$maxSort = self :: where ( 'stage_code' , '=' , $toStageCode ) -> where ( 'done' , $done ) -> max ( 'sort' );
2020-05-09 22:04:33 +08:00
$newSort = $maxSort + 65536 ;
2020-05-09 11:45:26 +08:00
}
if ( $newSort and $newSort > 50 ) {
$preTask -> stage_code = $toStageCode ;
$preTask -> sort = $newSort ;
$preTask -> save ();
} else {
// 小于安全值
2020-05-09 13:02:40 +08:00
$this -> resetSort ( $preTask [ 'stage_code' ], $done );
2020-05-09 12:03:48 +08:00
$this -> sort ( $preCode , $nextCode , $toStageCode );
2019-01-17 11:05:47 +08:00
}
return true ;
}
return false ;
}
2020-05-09 11:45:26 +08:00
2020-05-09 13:02:40 +08:00
public function resetSort ( $stageCode , $done )
2020-05-09 11:45:26 +08:00
{
2020-05-09 13:02:40 +08:00
$taskList = self :: where ( 'stage_code' , $stageCode ) -> order ( 'sort asc, id asc' ) -> where ( 'done' , $done ) -> select ();
2020-05-09 11:45:26 +08:00
if ( $taskList ) {
2020-05-09 22:04:33 +08:00
$sort = 65536 ;
2020-05-09 11:45:26 +08:00
foreach ( $taskList as $task ) {
$task -> sort = $sort ;
$task -> save ();
2020-05-09 22:04:33 +08:00
$sort += 65536 ;
2020-05-09 11:45:26 +08:00
}
}
}
/**
* 任务排序
* @ param $stageCode string 移到的任务列表code
* @ param $codes array 经过排序的任务code列表
* @ return bool
* @ throws DataNotFoundException
* @ throws DbException
* @ throws ModelNotFoundException
*/
/* public function sort ( $stageCode , $codes )
{
if ( ! $codes ) {
return false ;
}
if ( $codes ) {
$stage = TaskStages :: where ([ 'code' => $stageCode ]) -> find ();
$sort = 0 ;
foreach ( $codes as $key => $code ) {
$task = self :: where ([ 'code' => $code ]) -> find ();
self :: update ([ 'sort' => $sort , 'stage_code' => $stageCode ], [ 'code' => $code ]);
2020-05-09 22:04:33 +08:00
$sort += 65536 ;
2020-05-09 11:45:26 +08:00
if ( $task [ 'stage_code' ] != $stageCode ) {
self :: taskHook ( getCurrentMember ()[ 'code' ], $code , 'move' , '' , '' , '' , '' , '' , [ 'stageName' => $stage [ 'name' ]]);
}
}
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 ;
}
2020-03-03 22:14:52 +08:00
$offset = ( $page - 1 ) * $pageSize ;
2019-01-17 11:05:47 +08:00
$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' ]];
}
2020-05-09 11:45:26 +08:00
2020-03-04 09:52:58 +08:00
public function getStatusTextAttr ( $value , $data )
{
if ( ! isset ( $data [ 'status' ])) {
$data [ 'status' ] = 0 ;
}
2024-01-08 14:01:44 +08:00
$status = [ 0 => '未开始' , 1 => '已完成' , 2 => '进行中' , 3 => '挂起' , 4 => '验收中' ];
2020-03-04 09:52:58 +08:00
return $status [ $data [ 'status' ]];
}
2019-01-17 11:05:47 +08:00
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 );
}
}