闫增涛
2025-04-08 b41630280b49408824feb333849d51d83fa06fca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
 
declare(strict_types=1);
 
namespace app\user\controller;
 
use app\base\BaseController;
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('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();
        }
    }
 
    //添加&编辑
    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            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 {
                    Db::name('AttendanceGroup')->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(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;
            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();
        }
    }
}