123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: OA工作台
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\oa\controller;
  8. use app\admin\controller\ApiCommon;
  9. use think\Hook;
  10. use think\Request;
  11. use think\Db;
  12. class Index extends ApiCommon
  13. {
  14. /**
  15. * 用于判断权限
  16. * @permission 无限制
  17. * @allow 登录用户可访问
  18. * @other 其他根据系统设置
  19. **/
  20. public function _initialize()
  21. {
  22. $action = [
  23. 'permission'=>[''],
  24. 'allow'=>['index','tasklist','eventlist','event']
  25. ];
  26. Hook::listen('check_auth',$action);
  27. $request = Request::instance();
  28. $a = strtolower($request->action());
  29. if (!in_array($a, $action['permission'])) {
  30. parent::_initialize();
  31. }
  32. }
  33. /**
  34. * 工作圈
  35. * @author
  36. * @return
  37. */
  38. public function index()
  39. {
  40. $param = $this->param;
  41. $userInfo = $this->userInfo;
  42. $userModel = new \app\admin\model\User();
  43. $structureModel = new \app\admin\model\Structure();
  44. $fileModel = new \app\admin\model\File();
  45. $commonModel = new \app\admin\model\Comment();
  46. $BusinessModel = new \app\crm\model\Business();
  47. $ContactsModel = new \app\crm\model\Contacts();
  48. $ContractModel = new \app\crm\model\Contract();
  49. $CustomerModel = new \app\crm\model\Customer();
  50. if ($param['type'] == 1) { //日志
  51. $where = ' controller_name = "log" and module_name = "oa" ';
  52. } elseif ($param['type'] == 2) { //日程
  53. $where = ' controller_name = "event" and module_name = "oa" ';
  54. } elseif ($param['type'] == 3) { //公告
  55. $where = ' controller_name = "announcement" and module_name = "oa" ';
  56. } elseif ($param['type'] == 4) { //任务
  57. $where = ' ( controller_name = "task" and module_name = "oa" ) ';
  58. } elseif ($param['type'] == 5) { //审批
  59. $where = ' ( controller_name = "examine" and module_name = "oa" ) ';
  60. } else { //全部
  61. $where = ' ( module_name = "oa" ) ';
  62. }
  63. $limit = $param['limit'] ? : '15' ;
  64. $page = $param['page'] ? : '1' ;
  65. //获取权限范围内的员工
  66. $auth_user_ids = getSubUserId();
  67. $auth_user_ids = implode(',',$auth_user_ids);
  68. $actionList = Db::name('AdminActionLog')
  69. ->where( $where.' and ( action_delete != 1 ) and ( user_id IN ('.$auth_user_ids.') or join_user_ids like "%,'.$userInfo['id'].',%" or structure_ids like "%,'.$userInfo['structure_id'].',%" )')
  70. ->page($page, $limit)
  71. ->order('create_time desc')
  72. ->select();
  73. $actionCount = Db::name('AdminActionLog')
  74. ->where( $where.' and ( action_delete != 1 ) and ( user_id IN ('.$auth_user_ids.') or join_user_ids like "%,'.$userInfo['id'].',%" or structure_ids like "%,'.$userInfo['structure_id'].',%" )')
  75. ->count();
  76. foreach ($actionList as $key=>$value) {
  77. $actionList[$key]['create_user_info'] = $userModel->getUserById($value['user_id']);
  78. $actionList[$key]['action_content'] = $value['content'] ? : '';
  79. if ($value['controller_name'] == 'log') {
  80. $logInfo = [];
  81. $logInfo = Db::name('OaLog')->where('log_id = '.$value['action_id'].'')->find();
  82. if ($logInfo) {
  83. $actionList[$key]['title'] = $logInfo['title'];
  84. $actionList[$key]['today'] = $logInfo['today'];
  85. $actionList[$key]['category_id'] = $logInfo['category_id'];
  86. $actionList[$key]['content'] = $logInfo['content'];
  87. $actionList[$key]['tomorrow'] = $logInfo['tomorrow'];
  88. $actionList[$key]['question'] = $logInfo['question'];
  89. //附件、图片
  90. $fileList = [];
  91. $imgList = [];
  92. $where = [];
  93. $where['module'] = 'oa_log';
  94. $where['module_id'] = $value['action_id'];
  95. $newFileList = [];
  96. $newFileList = $fileModel->getDataList($where);
  97. foreach ($newFileList['list'] as $val) {
  98. if ($val['types'] == 'file') {
  99. $fileList[] = $val;
  100. } else {
  101. $imgList[] = $val;
  102. }
  103. }
  104. $actionList[$key]['fileList'] = $fileList ? : [];
  105. $actionList[$key]['imgList'] = $imgList ? : [];
  106. //参与人
  107. $actionList[$key]['sendUserList'] = $userModel->getDataByStr($logInfo['send_user_ids']) ? : [];
  108. //参与部门
  109. $actionList[$key]['sendStructList'] = $structureModel->getDataByStr($logInfo['send_structure_ids']) ? : [];
  110. //评论
  111. $commonParam = [];
  112. $commonParam['type_id'] = $logInfo['log_id'];
  113. $commonParam['type'] = 'oa_log';
  114. $actionList[$key]['replyList'] = $commonModel->read($commonParam);
  115. $actionList[$key]['type'] = 1;
  116. $actionList[$key]['type_name'] = "日志";
  117. } else {
  118. unset($actionList[$key]);
  119. }
  120. } elseif ($value['controller_name'] == 'event') {
  121. //日程
  122. $eventInfo = Db::name('OaEvent')->field('event_id,title,remark,start_time,end_time,owner_user_ids')->where('event_id = '.$value['action_id'].'')->find();
  123. if ($eventInfo) {
  124. $actionList[$key]['title'] = $eventInfo['title'] ? : '';
  125. $actionList[$key]['remark'] = $eventInfo['remark'] ? : '';
  126. $actionList[$key]['start_time'] = $eventInfo['start_time'];
  127. $actionList[$key]['end_time'] = $eventInfo['end_time'];
  128. $actionList[$key]['ownerList'] = $userModel->getDataByStr($eventInfo['owner_user_ids']);
  129. $actionList[$key]['type'] = 2;
  130. $actionList[$key]['type_name'] = "日程";
  131. $relation = Db::name('OaEventRelation')->where('event_id ='.$value['action_id'])->find();
  132. $actionList[$key]['businessList'] = $relation['business_ids'] ? $BusinessModel->getDataByStr($relation['business_ids']) : []; //商机
  133. $actionList[$key]['contactsList'] = $relation['contacts_ids'] ? $ContactsModel->getDataByStr($relation['contacts_ids']) : []; //联系人
  134. $actionList[$key]['contractList'] = $relation['contract_ids'] ? $ContractModel->getDataByStr($relation['contract_ids']) : []; //合同
  135. $actionList[$key]['customerList'] = $relation['customer_ids'] ? $CustomerModel->getDataByStr($relation['customer_ids']) : []; //客户
  136. } else {
  137. unset($actionList[$key]);
  138. }
  139. } elseif ($value['controller_name'] == 'announcement') {
  140. //公告
  141. $announcementInfo = Db::name('OaAnnouncement')->field('announcement_id,title,content')->where('announcement_id = '.$value['action_id'].'')->find();
  142. if ($announcementInfo) {
  143. $actionList[$key]['title'] = $announcementInfo['title'] ? : '';
  144. $actionList[$key]['ann_content'] = $announcementInfo['content'] ? : '';
  145. $actionList[$key]['type'] = 3;
  146. $actionList[$key]['type_name'] = "公告";
  147. } else {
  148. unset($actionList[$key]);
  149. }
  150. } elseif ($value['controller_name'] == 'task') {
  151. //任务
  152. $taskInfo = Db::name('Task')->field('task_id,name')->where('ishidden =0 and task_id = '.$value['action_id'].'')->find();
  153. if (!$taskInfo || $taskInfo['pid']) {
  154. unset($actionList[$key]);
  155. } else {
  156. $actionList[$key]['pname'] = '';
  157. $actionList[$key]['title'] = $taskInfo['name'] ? : '查看详情';
  158. $actionList[$key]['type'] = 4;
  159. $actionList[$key]['type_name'] = "任务";
  160. }
  161. } elseif ($value['controller_name'] == 'examine') {
  162. $examineInfo = db('oa_examine')->where(['examine_id' => $value['action_id']])->find();
  163. if ($examineInfo) {
  164. $actionList[$key]['title'] = $examineInfo['content'] ? : '查看详情';
  165. $actionList[$key]['type'] = 5;
  166. $actionList[$key]['type_name'] = "审批";
  167. } else {
  168. unset($actionList[$key]);
  169. }
  170. }
  171. }
  172. $actionList = $actionList ? array_merge($actionList) : [];
  173. $data = [];
  174. $data['list'] = $actionList ? : [];
  175. $data['dataCount'] = $actionCount ? : 0;
  176. return resultArray(['data'=>$data]);
  177. }
  178. /**
  179. * 任务展示
  180. * @author
  181. * @return
  182. */
  183. public function taskList()
  184. {
  185. $userInfo = $this->userInfo;
  186. $count = Db::name('Task')->where('( main_user_id = '.$userInfo['id'].' or create_user_id ='.$userInfo['id'].' or owner_user_id like "%,'.$userInfo['id'].',%") and status=1 and ishidden=0')->count();
  187. $list = Db::name('Task')->field('task_id,name,stop_time,priority,create_time,pid')->where(' ( main_user_id = '.$userInfo['id'].' or create_user_id ='.$userInfo['id'].' or owner_user_id like "%,'.$userInfo['id'].',%") and status=1 and ishidden=0')->select();
  188. foreach ($list as $k=>$v) {
  189. if ($v['pid']) {
  190. $ptask = Db::name('Task')->field('task_id,name')->where('ishidden = 0 and task_id ='.$v['pid'].'')->find();
  191. if($ptask){
  192. $list[$k]['pname'] = $ptask['name'];
  193. } else {
  194. $list[$k]['pname'] = '';
  195. }
  196. } else {
  197. $list[$k]['pname'] = '';
  198. }
  199. if ($v['stop_time']) {
  200. if ($v['stop_time'] < time()) {
  201. $list[$k]['task_status'] = '2';
  202. $list[$k]['task_remark'] = '已逾期';
  203. } else {
  204. $list[$k]['task_status'] = '1';
  205. $list[$k]['task_remark'] = '进行中';
  206. }
  207. } else {
  208. $list[$k]['task_status'] = '0';
  209. $list[$k]['task_remark'] = '未设置截至时间';
  210. }
  211. }
  212. $data['count'] = $count;
  213. $data['list'] = $list;
  214. return resultArray(['data'=>$data]);
  215. }
  216. /**
  217. * 日程展示
  218. * @author
  219. * @return
  220. */
  221. public function eventList()
  222. {
  223. $param = $this->param;
  224. $userInfo = $this->userInfo;
  225. if($param['start_time'] && $param['end_time']) {
  226. $start_time = $param['start_time'];
  227. $end_time = $param['end_time'];
  228. } else {
  229. $start_time = mktime(0,0,0,date('m'),1,date('Y'));
  230. $end_time = mktime(23,59,59,date('m'),date('t'),date('Y'));
  231. }
  232. $where = '( ( start_time BETWEEN '.$start_time.' AND '.$end_time.' ) AND ( create_user_id = '.$userInfo['id'].' or owner_user_ids like "%,'.$userInfo['id'].',%" ) ) OR ( ( end_time BETWEEN '.$start_time.' AND '.$end_time.' ) AND ( create_user_id = '.$userInfo['id'].' or owner_user_ids like "%,'.$userInfo['id'].',%" ) ) OR ( start_time < '.$start_time.' AND end_time > '.$end_time.' AND ( create_user_id = '.$userInfo['id'].' or owner_user_ids like "%,'.$userInfo['id'].',%" ) )';
  233. $event_date = Db::name('OaEvent')->where($where)->select();
  234. //生成从开始日期到结束日期的日期数组
  235. $date_array = dateList($start_time,$end_time,2);
  236. $temp = array();
  237. foreach($date_array as $k1=>$v1){
  238. $temp[$k1]['date'] = date('Y-m-d',$v1['sdate']);
  239. $temp[$k1]['status'] = 0;
  240. }
  241. //获取该月日程日期数组
  242. $event_arr = array();
  243. foreach ($event_date as $val) {
  244. $date_arr = array();
  245. $date_arr = dateList($val['start_time'],$val['end_time'],2);
  246. if ($date_arr) {
  247. foreach ($date_arr as $k=>$v) {
  248. foreach($temp as $k2=>$v2){
  249. if( $temp[$k2]['date'] == date('Y-m-d',$v['sdate']) ){
  250. $temp[$k2]['status'] = 1;
  251. }
  252. }
  253. }
  254. } else {
  255. foreach ($temp as $k2=>$v2) {
  256. if (date('Y-m-d',$val['start_time'])) {
  257. if ($temp[$k2]['date'] == date('Y-m-d',$val['start_time'])) {
  258. $temp[$k2]['status'] = 1;
  259. }
  260. } elseif (date('Y-m-d',$val['end_time'] )) {
  261. if ($temp[$k2]['date'] == date('Y-m-d',$val['end_time'])) {
  262. $temp[$k2]['status'] = 1;
  263. }
  264. }
  265. }
  266. }
  267. }
  268. return resultArray(['data'=>$temp]);
  269. }
  270. /**
  271. * 日程详情
  272. * @author
  273. * @return
  274. */
  275. public function event()
  276. {
  277. $param = $this->param;
  278. $userInfo = $this->userInfo;
  279. $userModel = new \app\admin\model\User();
  280. if ($param['start_time']){
  281. $where['start_time'] = ['<=',$param['start_time']+3600*24 ];
  282. $where['end_time'] = ['>=',$param['start_time']];
  283. $eventList = Db::name('OaEvent')
  284. ->where($where)
  285. ->where(function($query) use($userInfo){
  286. $query->where(['owner_user_ids' => ['like','%,'.$userInfo['id'].',%']])
  287. ->whereOr(['create_user_id' => $userInfo['id']]);
  288. })->select();
  289. if (count($eventList)) {
  290. foreach ($eventList as $k=>$v){
  291. $eventList[$k]['ownList']= $userModel->getDataByStr($v['owner_user_ids']);
  292. }
  293. }
  294. return resultArray(['data' => $eventList]);
  295. } else {
  296. return resultArray(['error'=>'参数错误']);
  297. }
  298. }
  299. }