YM
2025-03-09 e786382e19c697c72c2b519d0acaa4085599e4cc
app/user/controller/Attendance.php
@@ -15,12 +15,13 @@
    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()->toArray();
            // var_dump($list);
            // 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);
            // }
            $res['data'] = $list;
            return table_assign(0, '', $res);
        } else {
@@ -33,75 +34,72 @@
    {
        $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(PositionCheck::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(PositionCheck::class)->scene('add')->check($param);
                } catch (ValidateException $e) {
                    // 验证失败 输出错误信息
                    return to_assign(1, $e->getError());
                }
                // 启动事务
                Db::startTrans();
                try {
                    $uid = Db::name('AttendanceGroup')->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());
                }
            }
            return to_assign();
        }
        else{
            // $id = isset($param['id']) ? $param['id'] : 0;
            $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 = Db::name('AttendanceGroup')->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'])) {
@@ -110,10 +108,10 @@
            //             $val['checked'] = 0;
            //         }
            //     }
            //     View::assign('detail', $detail);
                View::assign('detail', $detail);
            // }
            // View::assign('group', $group);
            // View::assign('id', $id);
            View::assign('id', $id);
            return view();
        }
    }