package com.ruoyi.project.tool.swagger;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.mapper.TbFormMapper;
import com.ruoyi.project.system.service.ITbFormModuleService;
import com.ruoyi.project.system.service.ITbFormService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;

/**
 * swagger 用户测试方法
 *
 * @author ruoyi
 */
@Api("用户信息管理")
@RestController
@RequestMapping("/test/user")
public class TestController extends BaseController
{
    @Autowired
    private ITbFormService tbFormService;
    @Autowired
    private ITbFormModuleService tbFormModuleService;
    private final static Map<Integer, UserEntity> users = new LinkedHashMap<Integer, UserEntity>();
    {
        users.put(1, new UserEntity(1, "admin", "admin123", "15888888888"));
        users.put(2, new UserEntity(2, "ry", "admin123", "15666666666"));
    }
    /**
     * 根据id查询表信息
     * @param tableName 表名
     * @param id 信息id
     * @return 查询数据
     */
    @ApiOperation("根据id查询表信息")
    @GetMapping(value = "/selAutoTableById/{id}")
    public AjaxResult selAutoTableById(String tableName,@PathVariable("id") Long id) {
        Map resultMap = tbFormService.selAutoTableById(tableName, id);
        return AjaxResult.success(resultMap);
    }
    /**
     * 删除单个文件
     * @param   sPath    被删除文件的文件名
     * @return 单个文件删除成功返回true，否则返回false
     */
    @ApiOperation("删除单个文件")
    @GetMapping(value = "/deleteFile}")
    public boolean deleteFile(String filePath) {
        System.err.println(filePath);
        boolean flag = false;
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }
    /**
     * 动态修改表数据
     * @param tableName 表名
     * @param tableMap 需要修改的数据
     * @param id 数据的主键id
     * @return 受影响行数
     */
    @ApiOperation("动态修改表数据")
    @PostMapping(value = "/updateTableData/{tableName}")
    public AjaxResult updateTableData(@PathVariable("tableName") String tableName, @RequestBody LinkedHashMap tableMap,  String id) {

        int i = tbFormModuleService.updateTableData(tableName, tableMap, id);

        return toAjax(i);
    }
    /**
     * 动态删除表数据
     * @param tableName 表名
     * @param id 信息id
     * @return 状态
     */
    @ApiOperation("动态删除表数据")
    @PostMapping(value = "/deleteTableData/{id}")
    public AjaxResult deleteTableData(String tableName,@PathVariable("id") Long id) {

        return toAjax(tbFormModuleService.deleteTableData(tableName,id));
    }

    /**
     * 根据表名查询对应表结构
     * @param tableName 表名
     * @return 表结构
     */
    @ApiOperation("根据表名查询对应表结构")
    @GetMapping(value = "/selTbDataByName/{tableName}")
    public AjaxResult selTbDataByName(@PathVariable("tableName") String tableName)
    {
        return AjaxResult.success(tbFormModuleService.selTbDataByName(tableName));
    }

    /**
     * 动态查询表数据
     * @param tableName 表名
     * @return 查询的数据
     */
    @ApiOperation("动态查询表数据")
    @GetMapping(value = "/selectTableData/{tableName}")
    public AjaxResult selectTableData(@PathVariable("tableName") String tableName)
    {
        return AjaxResult.success(tbFormModuleService.selectTableData(tableName));
    }


    @ApiOperation("修改表名")
    @PostMapping("/altTableName/{oldtableName}")
    public AjaxResult alterTableName(@PathVariable("oldtableName") String oldtableName,String newtableName){
        int s = tbFormService.alterTableName(oldtableName,newtableName);
        if(s == 0){
            s=1;
            return toAjax(s);
        }
        return toAjax(s);
    }
    @ApiOperation("动态插入表数据")
    @PostMapping(value ="/insertTableData/{tableName}")
    public AjaxResult insertTableData(@PathVariable String tableName, @RequestBody LinkedHashMap tableMap)
    {
        return toAjax(tbFormModuleService.insertTableData(tableName,tableMap));
    }
    /**
     * 查询是否表名已存在
     */
    @ApiOperation("查询是否表名已存在")
    @PreAuthorize("@ss.hasPermi('system:form:selTable')")
    @GetMapping(value = "/selTable/{tableName}")
    public AjaxResult selTable(@PathVariable("tableName") String tableName)
    {

        return AjaxResult.success(tbFormService.selecltTable(tableName));
    }

    @ApiOperation("获取用户列表")
    @GetMapping("/list")
    public AjaxResult userList()
    {
        List<UserEntity> userList = new ArrayList<UserEntity>(users.values());
        return AjaxResult.success(userList);
    }

    @ApiOperation("获取用户详细")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
    @GetMapping("/{userId}")
    public AjaxResult getUser(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            return AjaxResult.success(users.get(userId));
        }
        else
        {
            return AjaxResult.error("用户不存在");
        }
    }
    /**
     * 根据表名查询表信息
     * @return
     */
    @ApiOperation("根据表名查询表信息")
    @GetMapping("/tableList")
    public TableDataInfo list(String tableName)
    {
        startPage();
        List<Map> list = tbFormService.selAutoTable(tableName);
        return getDataTable(list);
    }
    @ApiOperation("新增用户")
    @ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
    @PostMapping("/save")
    public AjaxResult save(UserEntity user)
    {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
        {
            return AjaxResult.error("用户ID不能为空");
        }
        return AjaxResult.success(users.put(user.getUserId(), user));
    }
    @ApiOperation("删除组件信息")
    @PostMapping(value = "/deleteTableZJ/{formId}")
    public AjaxResult deleteTableZJ(String formId) {


        return toAjax(tbFormModuleService.deleteTableZJ(formId));
    }

    @ApiOperation("更新用户")
    @ApiImplicitParam(name = "userEntity", value = "新增用户信息", dataType = "UserEntity")
    @PutMapping("/update")
    public AjaxResult update(UserEntity user)
    {
        if (StringUtils.isNull(user) || StringUtils.isNull(user.getUserId()))
        {
            return AjaxResult.error("用户ID不能为空");
        }
        if (users.isEmpty() || !users.containsKey(user.getUserId()))
        {
            return AjaxResult.error("用户不存在");
        }
        users.remove(user.getUserId());
        return AjaxResult.success(users.put(user.getUserId(), user));
    }

    @ApiOperation("删除用户信息")
    @ApiImplicitParam(name = "userId", value = "用户ID", required = true, dataType = "int", paramType = "path")
    @DeleteMapping("/{userId}")
    public AjaxResult delete(@PathVariable Integer userId)
    {
        if (!users.isEmpty() && users.containsKey(userId))
        {
            users.remove(userId);
            return AjaxResult.success();
        }
        else
        {
            return AjaxResult.error("用户不存在");
        }
    }
}

@ApiModel("用户实体")
class UserEntity
{
    @ApiModelProperty("用户ID")
    private Integer userId;

    @ApiModelProperty("用户名称")
    private String username;

    @ApiModelProperty("用户密码")
    private String password;

    @ApiModelProperty("用户手机")
    private String mobile;

    public UserEntity()
    {

    }

    public UserEntity(Integer userId, String username, String password, String mobile)
    {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.mobile = mobile;
    }

    public Integer getUserId()
    {
        return userId;
    }

    public void setUserId(Integer userId)
    {
        this.userId = userId;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }

    public String getPassword()
    {
        return password;
    }

    public void setPassword(String password)
    {
        this.password = password;
    }

    public String getMobile()
    {
        return mobile;
    }

    public void setMobile(String mobile)
    {
        this.mobile = mobile;
    }
}
