From 729e47e5aa77d73914de4353e95f56350611f820 Mon Sep 17 00:00:00 2001 From: 闫增涛 <1829501689@qq.com> Date: 星期一, 14 四月 2025 10:50:55 +0800 Subject: [PATCH] Merge branch 'main' of http://182.92.203.7:2001/r/OAManage --- app/user/controller/Attendance.php | 345 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 256 insertions(+), 89 deletions(-) diff --git a/app/user/controller/Attendance.php b/app/user/controller/Attendance.php index 7bf1907..4a0457e 100644 --- a/app/user/controller/Attendance.php +++ b/app/user/controller/Attendance.php @@ -1,30 +1,80 @@ <?php -declare (strict_types = 1); +declare(strict_types=1); namespace app\user\controller; use app\base\BaseController; -// use app\user\validate\PositionCheck; +use app\user\validate\AttendanceCheck; +use app\user\model\Attendance as AttendanceModel; use think\exception\ValidateException; use think\facade\Db; use think\facade\View; class Attendance extends BaseController { + /** + * 鏋勯�犲嚱鏁� + */ + protected $model; + public function __construct() + { + parent::__construct(); // 璋冪敤鐖剁被鏋勯�犲嚱鏁� + $this->model = new AttendanceModel(); + } + public function index() { if (request()->isAjax()) { - $list = Db::name('Position')->where('status', '>=', 0)->order('create_time asc')->select()->toArray(); - foreach ($list as &$val) { - $groupId = Db::name('PositionGroup')->where(['pid' => $val['id']])->column('group_id'); - $groupName = Db::name('AdminGroup')->where('id', 'in', $groupId)->column('title'); - $val['groupName'] = implode(',', $groupName); - } + $list = Db::name('AttendanceGroup')->where('is_del', '=', 0)->order('create_time asc')->select()->each(function ($item, $key) { + $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']); + $item['update_time'] = $item['update_time'] > 0 ? date('Y-m-d H:i:s', $item['update_time']) : ""; + // 鑾峰彇鍏宠仈鏃堕棿 + $mappingTimes = Db::name('AttendanceGroupSpecialDateLink')->where(['group_id' => $item['id']])->column('special_date_id'); + $times = Db::name('AttendanceSpecialDate')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + $item['specialDate'] = $times; + // 鑾峰彇鍏宠仈鐢ㄦ埛 + $mappingUsers = Db::name('AttendanceUserLink')->where(['group_id' => $item['id']])->select()->toArray(); + foreach ($mappingUsers as &$user) { + $userInfo = Db::name('Admin')->where(['id' => $user['user_id']])->find(); + $user['userInfo'] = $userInfo; + } + $item['linkUserDate'] = $mappingUsers; + // 鑾峰彇鍏宠仈鐨勭彮娆� + $map['mon_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $item['mon_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $item['mon_work_plan_id']])->column('work_time_id'); + $map['mon_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $map['tue_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $item['tue_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $item['tue_work_plan_id']])->column('work_time_id'); + $map['tue_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $map['wed_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $item['wed_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $item['wed_work_plan_id']])->column('work_time_id'); + $map['wed_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $map['thur_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $item['thur_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $item['thur_work_plan_id']])->column('work_time_id'); + $map['thur_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $map['fri_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $item['fri_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $item['fri_work_plan_id']])->column('work_time_id'); + $map['fri_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $map['sat_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $item['sat_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $item['sat_work_plan_id']])->column('work_time_id'); + $map['sat_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $map['sun_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $item['sun_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $item['sun_work_plan_id']])->column('work_time_id'); + $map['sun_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + $item['work_plan'] = $map; + return $item; + })->toArray(); $res['data'] = $list; return table_assign(0, '', $res); } else { - return view(); + return view(); } } @@ -33,89 +83,206 @@ { $param = get_params(); if (request()->isAjax()) { - // if (!empty($param['id']) && $param['id'] > 0) { - // if($param['id']==1){ - // return to_assign(1, '瓒呯骇绠$悊鍛樹笉鑳界紪杈�'); - // } - // try { - // validate(PositionCheck::class)->scene('edit')->check($param); - // } catch (ValidateException $e) { - // // 楠岃瘉澶辫触 杈撳嚭閿欒淇℃伅 - // return to_assign(1, $e->getError()); - // } - // // 鍚姩浜嬪姟 - // Db::startTrans(); - // try { - // Db::name('Position')->where(['id' => $param['id']])->strict(false)->field(true)->update($param); - // Db::name('PositionGroup')->where(['pid' => $param['id']])->delete(); - // foreach ($param['group_id'] as $k => $v) { - // $data[$k] = [ - // 'pid' => $param['id'], - // 'group_id' => $v, - // 'create_time' => time(), - // ]; - // } - // Db::name('PositionGroup')->strict(false)->field(true)->insertAll($data); - // add_log('edit', $param['id'], $param); - // //娓呴櫎鑿滃崟\鏉冮檺缂撳瓨 - // clear_cache('adminMenu'); - // clear_cache('adminRules'); - // // 鎻愪氦浜嬪姟 - // Db::commit(); - // } catch (\Exception $e) { - // // 鍥炴粴浜嬪姟 - // Db::rollback(); - // return to_assign(1, '鎻愪氦澶辫触:' . $e->getMessage()); - // } - // } else { - // try { - // validate(PositionCheck::class)->scene('add')->check($param); - // } catch (ValidateException $e) { - // // 楠岃瘉澶辫触 杈撳嚭閿欒淇℃伅 - // return to_assign(1, $e->getError()); - // } - // // 鍚姩浜嬪姟 - // Db::startTrans(); - // try { - // $uid = Db::name('Position')->strict(false)->field(true)->insertGetId($param); - // foreach ($param['group_id'] as $k => $v) { - // $data[$k] = [ - // 'pid' => $uid, - // 'group_id' => $v, - // 'create_time' => time(), - // ]; - // } - // Db::name('PositionGroup')->strict(false)->field(true)->insertAll($data); - // add_log('add', $uid, $param); - // // 鎻愪氦浜嬪姟 - // Db::commit(); - // } catch (\Exception $e) { - // // 鍥炴粴浜嬪姟 - // Db::rollback(); - // return to_assign(1, '鎻愪氦澶辫触:' . $e->getMessage()); - // } - // } + if (!empty($param['id']) && $param['id'] > 0) { + try { + validate(AttendanceCheck::class)->scene('edit')->check($param); + } catch (ValidateException $e) { + // 楠岃瘉澶辫触 杈撳嚭閿欒淇℃伅 + return to_assign(1, $e->getError()); + } + // 鍚姩浜嬪姟 + Db::startTrans(); + try { + $setData = [ + 'name' => $param['name'], + 'type' => $param['type'], + 'is_del' => 0, + 'create_time' => time(), + 'mon_work_plan_id' => isset($param['work1']) && $param['work1'] == "on" && isset($param['mon_work_plan_id']) ? intval($param['mon_work_plan_id']) : 0, + 'tue_work_plan_id' => isset($param['work2']) && $param['work2'] == "on" && isset($param['tue_work_plan_id']) ? intval($param['tue_work_plan_id']) : 0, + 'wed_work_plan_id' => isset($param['work3']) && $param['work3'] == "on" && isset($param['wed_work_plan_id']) ? intval($param['wed_work_plan_id']) : 0, + 'thur_work_plan_id' => isset($param['work4']) && $param['work4'] == "on" && isset($param['thur_work_plan_id']) ? intval($param['thur_work_plan_id']) : 0, + 'fri_work_plan_id' => isset($param['work5']) && $param['work5'] == "on" && isset($param['fri_work_plan_id']) ? intval($param['fri_work_plan_id']) : 0, + 'sat_work_plan_id' => isset($param['work6']) && $param['work6'] == "on" && isset($param['sat_work_plan_id']) ? intval($param['sat_work_plan_id']) : 0, + 'sun_work_plan_id' => isset($param['work7']) && $param['work7'] == "on" && isset($param['sun_work_plan_id']) ? intval($param['sun_work_plan_id']) : 0, + ]; + Db::name('AttendanceGroup')->where(['id' => $param['id']])->strict(false)->field(true)->update($setData); + // 鍒犻櫎鐢ㄦ埛鍏宠仈 + Db::name('AttendanceUserLink')->where(['group_id' => $param['id']])->delete(); + // 鍒犻櫎鏃堕棿 + $specialDateIds = Db::name('AttendanceGroupSpecialDateLink')->where(['group_id' => $param['id']])->column('special_date_id'); + Db::name('AttendanceSpecialDate')->where('id', 'in', $specialDateIds)->delete(); + Db::name('AttendanceGroupSpecialDateLink')->where(['group_id' => $param['id']])->delete(); + // 鎻掑叆鐢ㄦ埛鍏宠仈琛� + $this->model->insertUserLinks(intval($param['id']), $param['attendance_participants_id'], 1); + $this->model->insertUserLinks(intval($param['id']), $param['attendance_not_participants_id'], 2); + + // 鎻掑叆鏃堕棿琛� + if (isset($param['necessaryTime'])) { + $this->model->insertSpecialDates(intval($param['id']), $param['necessaryTime'], 1); + } + if (isset($param['unNecessaryTime'])) { + $this->model->insertSpecialDates(intval($param['id']), $param['unNecessaryTime'], 2); + } + add_log('edit', $param['id'], $param); + // 鎻愪氦浜嬪姟 + Db::commit(); + } catch (\Exception $e) { + // 鍥炴粴浜嬪姟 + Db::rollback(); + return to_assign(1, '鎻愪氦澶辫触:' . $e->getMessage()); + } + } else { + try { + validate(AttendanceCheck::class)->scene('add')->check($param); + } catch (ValidateException $e) { + // 楠岃瘉澶辫触 杈撳嚭閿欒淇℃伅 + return to_assign(1, $e->getError()); + } + // 鍚姩浜嬪姟 + Db::startTrans(); + try { + // 鎻掑叆鑰冨嫟缁勮〃 + $setData = [ + 'name' => $param['name'], + 'type' => $param['type'], + 'is_del' => 0, + 'create_time' => time(), + 'mon_work_plan_id' => isset($param['work1']) && $param['work1'] == "on" && isset($param['mon_work_plan_id']) ? intval($param['mon_work_plan_id']) : 0, + 'tue_work_plan_id' => isset($param['work2']) && $param['work2'] == "on" && isset($param['tue_work_plan_id']) ? intval($param['tue_work_plan_id']) : 0, + 'wed_work_plan_id' => isset($param['work3']) && $param['work3'] == "on" && isset($param['wed_work_plan_id']) ? intval($param['wed_work_plan_id']) : 0, + 'thur_work_plan_id' => isset($param['work4']) && $param['work4'] == "on" && isset($param['thur_work_plan_id']) ? intval($param['thur_work_plan_id']) : 0, + 'fri_work_plan_id' => isset($param['work5']) && $param['work5'] == "on" && isset($param['fri_work_plan_id']) ? intval($param['fri_work_plan_id']) : 0, + 'sat_work_plan_id' => isset($param['work6']) && $param['work6'] == "on" && isset($param['sat_work_plan_id']) ? intval($param['sat_work_plan_id']) : 0, + 'sun_work_plan_id' => isset($param['work7']) && $param['work7'] == "on" && isset($param['sun_work_plan_id']) ? intval($param['sun_work_plan_id']) : 0, + ]; + $uid = Db::name('AttendanceGroup')->strict(false)->field(true)->insertGetId($setData); + + // 鎻掑叆鐢ㄦ埛鍏宠仈琛� + $this->model->insertUserLinks($uid, $param['attendance_participants_id'], 1); + $this->model->insertUserLinks($uid, $param['attendance_not_participants_id'], 2); + + // 鎻掑叆鏃堕棿琛� + if (isset($param['necessaryTime'])) { + $this->model->insertSpecialDates($uid, $param['necessaryTime'], 1); + } + if (isset($param['unNecessaryTime'])) { + $this->model->insertSpecialDates($uid, $param['unNecessaryTime'], 2); + } + + add_log('add', $uid, $param); + // 鎻愪氦浜嬪姟 + Db::commit(); + } catch (\Exception $e) { + // 鍥炴粴浜嬪姟 + Db::rollback(); + return to_assign(1, '鎻愪氦澶辫触:' . $e->getMessage()); + } + } return to_assign(); - } - else{ - // $id = isset($param['id']) ? $param['id'] : 0; - // $group = Db::name('AdminGroup')->order('create_time asc')->select()->toArray(); - // if ($id > 0) { - // $detail = Db::name('Position')->where(['id' => $id])->find(); - // $detail['group_id'] = Db::name('PositionGroup')->where(['pid' => $id])->column('group_id'); - // foreach ($group as &$val) { - // if (in_array($val['id'], $detail['group_id'])) { - // $val['checked'] = 1; - // } else { - // $val['checked'] = 0; - // } - // } - // View::assign('detail', $detail); - // } - // View::assign('group', $group); - // View::assign('id', $id); + } else { + $id = isset($param['id']) ? $param['id'] : 0; + if ($id > 0) { + $detail = Db::name('AttendanceGroup')->where(['id' => $id])->find(); + // 鑾峰彇鍏宠仈鏃堕棿 + $mappingTimes = Db::name('AttendanceGroupSpecialDateLink')->where(['group_id' => $detail['id']])->column('special_date_id'); + $times = Db::name('AttendanceSpecialDate')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + $time1 = array_values(array_filter($times, function ($item) { + return $item['type'] == 1; + })); + + $detail['necessaryTime'] = []; + foreach ($time1 as $key => $value) { + $timeData['time'] = str_split($value['begin_data'], 10)[0]; + $timeData['id'] = $value['id']; + $detail['necessaryTime'][] = $timeData; + } + + $time2 = array_values(array_filter($times, function ($item) { + return $item['type'] == 2; + })); + + $detail['unNecessaryTime'] = []; + foreach ($time2 as $key => $value) { + $timeData['time'] = str_split($value['begin_data'], 10)[0]; + $timeData['id'] = $value['id']; + $detail['unNecessaryTime'][] = $timeData; + } + // 鑾峰彇鍏宠仈鐢ㄦ埛 + $mappingUsers = Db::name('AttendanceUserLink')->where(['group_id' => $detail['id']])->select()->toArray(); + foreach ($mappingUsers as &$user) { + $userInfo = Db::name('Admin')->where(['id' => $user['user_id']])->find(); + $user['userInfo'] = $userInfo; + } + + $user1 = array_values(array_filter($mappingUsers, function ($item) { + return $item['type'] == 1; + })); + + $userName1 = []; + $userId1 = []; + foreach ($user1 as $key => $value) { + $userName1[] = $value['userInfo']['name']; + $userId1[] = $value['userInfo']['id']; + } + $detail['attendance_participants'] = implode(",", $userName1); + $detail['attendance_participants_id'] = implode(",", $userId1); + $user2 = array_values(array_filter($mappingUsers, function ($item) { + return $item['type'] == 2; + })); + + $userName2 = []; + $userId2 = []; + foreach ($user2 as $key => $value) { + $userName2[] = $value['userInfo']['name']; + $userName2[] = $value['userInfo']['id']; + } + $detail['attendance_not_participants'] = implode(",", $userName2); + $detail['attendance_not_participants_id'] = implode(",", $userId2); + // 鑾峰彇鍏宠仈鐨勭彮娆� + $detail['mon_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $detail['mon_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $detail['mon_work_plan_id']])->column('work_time_id'); + $detail['mon_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $detail['tue_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $detail['tue_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $detail['tue_work_plan_id']])->column('work_time_id'); + $detail['tue_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $detail['wed_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $detail['wed_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $detail['wed_work_plan_id']])->column('work_time_id'); + $detail['wed_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $detail['thur_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $detail['thur_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $detail['thur_work_plan_id']])->column('work_time_id'); + $detail['thur_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $detail['fri_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $detail['fri_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $detail['fri_work_plan_id']])->column('work_time_id'); + $detail['fri_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $detail['sat_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $detail['sat_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $detail['sat_work_plan_id']])->column('work_time_id'); + $detail['sat_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + + $detail['sun_work_plan'] = Db::name('AttendanceWorkPlan')->where(['id' => $detail['sun_work_plan_id']])->find(); + $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $detail['sun_work_plan_id']])->column('work_time_id'); + $detail['sun_work_plan']['times'] = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray(); + View::assign('detail', $detail); + } + View::assign('id', $id); return view(); } } + // 鍒犻櫎 + public function delete() + { + $id = get_params("id"); + if (Db::name('AttendanceGroup')->where('id', $id)->update(['is_del' => 1]) !== false) { + add_log('delete', $id, [], '鑰冨嫟缁�'); + return to_assign(0, "鍒犻櫎鎴愬姛"); + } else { + return to_assign(1, "鍒犻櫎澶辫触"); + } + } } -- Gitblit v1.9.1