闫增涛
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
<?php
 
declare(strict_types=1);
 
namespace app\user\controller;
 
use app\base\BaseController;
use app\user\validate\WorkPlanCheck;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;
 
class WorkPlan extends BaseController
{
    public function index()
    {
        if (request()->isAjax()) {
            $list = Db::name('AttendanceWorkPlan')->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']) : "";
                return $item;
            })->toArray();
            foreach ($list as &$val) {
                $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $val['id']])->column('work_time_id');
                $times = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray();
                $val['times'] = $times;
            }
            $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(WorkPlanCheck::class)->scene('edit')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                // 启动事务
                Db::startTrans();
                try {
                    // 处理时间
                    foreach ($param['begin_time'] as $key => $value) {
                        if (!$value) {
                            continue;
                        }
                        $timeData = [
                            'id' => intval($param['time_id'][$key]),
                            'begin_time' => $param['begin_time'][$key],
                            'need_begin_check' => intval($param['need_begin_check'][$key]),
                            'end_time' => $param['end_time'][$key],
                            'need_end_check' => intval($param['need_end_check'][$key]),
                            'check_begin_start_time' => $param['check_begin_start_time'][$key],
                            'check_begin_end_time' => $param['check_begin_end_time'][$key],
                            'check_end_start_time' => $param['check_end_start_time'][$key],
                            'check_end_end_time' => $param['check_end_end_time'][$key],
                        ];
                        if ($timeData['id'] > 0) {
                            // 更新
                            $resa = Db::name('AttendanceWorkTime')->strict(false)->field(true)->update($timeData);
                        } else {
                            // 新增
                            unset($timeData['id']);
                            $timeId = Db::name('AttendanceWorkTime')->strict(false)->field(true)->insertGetId($timeData);
                            $mappingData = [
                                'work_plan_id' => $param['id'],
                                'work_time_id' => $timeId,
                            ];
                            $mappingId = Db::name('AttendanceWorkPlanWorkTimeLink')->strict(false)->field(true)->insertGetId($mappingData);
                        }
                    }
                    // 处理班次
                    $param['update_time'] = time();
                    Db::name('AttendanceWorkPlan')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
                    add_log('edit', $param['id'], $param);
                    // 提交事务
                    Db::commit();
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return to_assign(1, '提交失败:' . $e->getMessage());
                }
            } else {
                try {
                    validate(WorkPlanCheck::class)->scene('add')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                // 启动事务
                Db::startTrans();
                try {
                    // 插入班次表
                    $param['is_del'] = 0;
                    $param['create_time'] = time();
                    unset($param['id']);
                    $uid = Db::name('AttendanceWorkPlan')->strict(false)->field(true)->insertGetId($param);
                    // 插入时间表
                    for ($i = 0; $i < count($param['begin_time']); $i++) {
                        // 获取时间数据
                        $timeData = [
                            'begin_time' => $param['begin_time'][$i],
                            'need_begin_check' => intval($param['need_begin_check'][$i]),
                            'end_time' => $param['end_time'][$i],
                            'need_end_check' => intval($param['need_end_check'][$i]),
                            'check_begin_start_time' => $param['check_begin_start_time'][$i],
                            'check_begin_end_time' => $param['check_begin_end_time'][$i],
                            'check_end_start_time' => $param['check_end_start_time'][$i],
                            'check_end_end_time' => $param['check_end_end_time'][$i],
                        ];
                        // 插入时间数据
                        $timeIds[$i] = Db::name('AttendanceWorkTime')->strict(false)->field(true)->insertGetId($timeData);
                    }
                    // mapping数据
                    foreach ($timeIds as $key => $value) {
                        $mappingData[$key] = [
                            'work_plan_id' => $uid,
                            'work_time_id' => $value,
                        ];
                    }
                    Db::name('AttendanceWorkPlanWorkTimeLink')->strict(false)->field(true)->insertAll($mappingData);
                    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('AttendanceWorkPlan')->where(['id' => $id])->find();
                $mappingTimes = Db::name('AttendanceWorkPlanWorkTimeLink')->where(['work_plan_id' => $id])->column('work_time_id');
                $times = Db::name('AttendanceWorkTime')->where('id', 'in', $mappingTimes)->where('is_del', '=', 0)->select()->toArray();
                $detail['times'] = $times;
                View::assign('detail', $detail);
            }
            View::assign('id', $id);
            return view();
        }
    }
 
    // 删除
    public function delete()
    {
        $id = get_params("id");
        if (Db::name('AttendanceWorkPlan')->where('id', $id)->update(['is_del'=>1]) !== false) {
            add_log('delete',  $id,[],'班次');
            return to_assign(0, "删除成功");
        } else {
            return to_assign(1, "删除失败");
        }
    }
 
}