package com.zbf.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbf.system.mapper.TUserConfigMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zbf.common.annotation.Log;
import com.zbf.common.core.controller.BaseController;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.enums.BusinessType;
import com.zbf.system.domain.TUserConfig;
import com.zbf.system.service.ITUserConfigService;
import com.zbf.common.utils.poi.ExcelUtil;
import com.zbf.common.core.page.TableDataInfo;

/**
 * 用户配置文件Controller
 *
 * @author tony
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/system/UserConfiguration")
public class TUserConfigController extends BaseController {
    @Autowired
    private ITUserConfigService tUserConfigService;
    @Autowired
    private TUserConfigMapper tUserConfigMapper;


    /**
     * 查询用户配置文件列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TUserConfig tUserConfig) {
        startPage();
        List<TUserConfig> list = tUserConfigService.list(new LambdaQueryWrapper<>(tUserConfig));
        return getDataTable(list);
    }

    /**
     * 导出用户配置文件列表
     */
    @Log(title = "用户配置文件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TUserConfig tUserConfig) {
        List<TUserConfig> list = tUserConfigService.list(new LambdaQueryWrapper<>(tUserConfig));
        ExcelUtil<TUserConfig> util = new ExcelUtil<TUserConfig>(TUserConfig.class);
        util.exportExcel(response, list, "用户配置文件数据");
    }

    /**
     * 获取用户配置文件详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tUserConfigService.getById(id));
    }

    /**
     * 新增用户配置文件
     */
    @Log(title = "用户配置文件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TUserConfig tUserConfig) {
        return toAjax(tUserConfigMapper.insert(tUserConfig));
    }

    /**
     * 修改用户配置文件
     */
    @Log(title = "用户配置文件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TUserConfig tUserConfig) {
        LambdaQueryWrapper<TUserConfig> query = new LambdaQueryWrapper<>();
        query.eq(TUserConfig::getUserId, tUserConfig.getUserId());
        query.eq(TUserConfig::getRouterPath, tUserConfig.getRouterPath());
        query.eq(TUserConfig::getCompName, tUserConfig.getCompName());

        List<TUserConfig> tUserConfigs = tUserConfigMapper.selectList(query);
        if (tUserConfigs.size() > 0) {
            TUserConfig userConfig = tUserConfigs.get(0);
            userConfig.setCnfJson(tUserConfig.getCnfJson());
            return toAjax(tUserConfigService.updateById(userConfig));
        } else {
            return toAjax(tUserConfigMapper.insert(tUserConfig));
        }
    }

    /**
     * 删除用户配置文件
     */
    @Log(title = "用户配置文件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tUserConfigService.removeById(ids));
    }

    /**
     * 修改状态
     */
    @Log(title = "用户配置文件", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TUserConfig tUserConfig) {
        return toAjax(tUserConfigService.updateById(tUserConfig));
    }

    /**
     * 根据id 和菜单查询
     *
     * @param config
     * @return
     */
    @PostMapping(value = "/information")
    public AjaxResult getInfo(@RequestBody TUserConfig config) {
        LambdaQueryWrapper<TUserConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TUserConfig::getUserId, config.getUserId());  // 确保这里传入的是有效的userId
        queryWrapper.eq(TUserConfig::getRouterPath, config.getRouterPath());  // 确保这里传入的是有效的routerPath
        TUserConfig object = tUserConfigService.getOne(queryWrapper);
        return AjaxResult.success(object);
    }

}

