package com.ruoyi.project.system.controller;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.*;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.TbFormModule;
import com.ruoyi.project.system.service.ITbFormModuleService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 组件类型Controller
 *
 * @author ruoyi
 * @date 2020-06-17
 */
@RestController
@RequestMapping("/system/module")
public class TbFormModuleController extends BaseController
{
    @Autowired
    private ITbFormModuleService tbFormModuleService;

    /**
     * 动态修改表数据
     * @param tableName 表名
     * @param tableMap 需要修改的数据
     * @param id 数据的主键id
     * @return 受影响行数
     */
    @PostMapping(value = "/updateTableData/{tableName}/{id}")
    public AjaxResult updateTableData(@PathVariable("tableName") String tableName, @RequestBody Map tableMap, @PathVariable("id") String id) {

        System.err.println(tableName);
        int i = tbFormModuleService.updateTableData("form_"+tableName, tableMap, id);

        return toAjax(i);
    }


    /**
     * 动态插入表数据
     * @param tableName 表名
     * @param
     * @return 状态
     */
    @PostMapping(value ="/insertTableData/{tableName}")
    public AjaxResult insertTableData(@PathVariable String tableName, @RequestBody LinkedHashMap tableMap )   {
        int i=0;
       // tableMap.put("field101","");
       // tableMap.put("field102","");
        i = tbFormModuleService.insertTableData("form_" + tableName, tableMap);
        System.err.println(i);
       System.err.println("tableMap"+ tableMap);

        return toAjax(1);


    }
    @PostMapping(value ="/insertTable/{tableName}")
    public AjaxResult insertTable(@PathVariable String tableName, @RequestBody  List<Object> tableMap )   {
        System.err.println("?asdasdasd:"+"form_"+tableName);
     //   System.err.println("size:===:"+tableMap.size());
        int i1=0;
        LinkedHashMap linMap = new LinkedHashMap();
//        linMap.put("tableName","form_"+tableName);
        for (int i=0; i<tableMap.size();i++){
            System.err.println(tableMap.size());
            Object o = tableMap.get(i);
            JSONObject jsonObject = (JSONObject) JSON.toJSON(o);
            String vModel = jsonObject.getString("vModel");
            String defaultValue = jsonObject.getString("defaultValue");
            //System.err.println("?vModel:"+vModel);
           // System.err.println("?defaultValue:"+defaultValue);
            linMap.put(vModel,defaultValue);
            System.err.println(linMap.size());

           // i1 = tbFormModuleService.insertTable("form_" + tableName, vModel,defaultValue);
        }
        int i = tbFormModuleService.insertTableData("form_"+tableName,linMap);
        return toAjax(i);
    }

    /**
     * 动态删除表数据
     * @param tableName 表名
     * @param id 信息id
     * @return 状态{"label":"标题","tag":"el-input","tagIcon":"input","placeholder":"请输入标题","span":10,"labelWidth":null,"style":{"width":"100%"},"clearable":true,"prepend":"","append":"","prefix-icon":"","suffix-icon":"","maxlength":null,"show-word-limit":false,"readonly":false,"disabled":false,"required":true,"regList":[],"changeTag":true,"document":"https://element.eleme.cn/#/zh-CN/component/input","formId":101,"renderKey":1594020089805,"layout":"colFormItem","vModel":"field101","defaultValue":"哦开辟后"},{"label":"密码","tag":"el-input","tagIcon":"password","placeholder":"请输入密码","span":10,"show-password":true,"labelWidth":null,"style":{"width":"100%"},"clearable":true,"prepend":"","append":"","prefix-icon":"","suffix-icon":"","maxlength":null,"show-word-limit":false,"readonly":false,"disabled":false,"required":true,"regList":[],"changeTag":true,"document":"https://element.eleme.cn/#/zh-CN/component/input","formId":102,"renderKey":1594020110284,"layout":"colFormItem","vModel":"field102","defaultValue":"dsfghjkl;'"}
     */
    @PostMapping(value = "/deleteTableData/{tableName}/{id}")
    public AjaxResult deleteTableData(@PathVariable("tableName")String tableName,@PathVariable("id") Long id) {
        return toAjax(tbFormModuleService.deleteTableData("form_"+tableName,id));
    }
    /**
     * 根据表名查询对应表结构
     * @param tableName 表名
     * @return 表结构
     */
    @GetMapping(value = "/selTbDataByName/{tableName}")
    public AjaxResult selTbDataByName(@PathVariable("tableName") String tableName)
    {
        return AjaxResult.success(tbFormModuleService.selTbDataByName("form_"+tableName));
    }

    /**
     * 动态查询表数据
     * @param tableName 表名
     * @return 查询的数据
     */
    @PostMapping(value = "/selectTableData")
    public TableDataInfo selectTableData(@RequestParam("tableName") String tableName)
    {
        List<TbFormModule> modules=tbFormModuleService.selTbDataByName("form_"+tableName);
        startPage();
        List<Map> list=tbFormModuleService.selectTableData("form_"+tableName);
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        String m= JSONArray.toJSON(modules).toString();
        rspData.setMsg(m);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }


    /**
     * 查询组件类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:module:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbFormModule tbFormModule)
    {
        startPage();
        List<TbFormModule> list = tbFormModuleService.selectTbFormModuleList(tbFormModule);
        return getDataTable(list);
    }

    /**
     * 导出组件类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:module:export')")
    @Log(title = "组件类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbFormModule tbFormModule)
    {
        List<TbFormModule> list = tbFormModuleService.selectTbFormModuleList(tbFormModule);
        ExcelUtil<TbFormModule> util = new ExcelUtil<TbFormModule>(TbFormModule.class);
        return util.exportExcel(list, "module");
    }

    /**
     * 获取组件类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:module:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbFormModuleService.selectTbFormModuleById(id));
    }

    /**
     * 新增组件类型
     */
    @PreAuthorize("@ss.hasPermi('system:module:add')")
    @Log(title = "组件类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbFormModule tbFormModule)
    {
        return toAjax(tbFormModuleService.insertTbFormModule(tbFormModule));
    }

    /**
     * 修改组件类型
     */
    @PreAuthorize("@ss.hasPermi('system:module:edit')")
    @Log(title = "组件类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbFormModule tbFormModule)
    {
        return toAjax(tbFormModuleService.updateTbFormModule(tbFormModule));
    }

    /**
     * 删除组件类型
     */
    @PreAuthorize("@ss.hasPermi('system:module:remove')")
    @Log(title = "组件类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] action)
    {
        return toAjax(tbFormModuleService.deleteTbFormModuleByIds(action));
    }

    /**
     * 根据表名删除组件信息
     * @param formId 表名
     * @return 受影响行数
     */
    @PreAuthorize("@ss.hasPermi('system:form:deleteTableZJ')")
    @PostMapping(value = "/deleteTableZJ/{formId}")
    public AjaxResult deleteTableZJ(String formId) {
        return toAjax(tbFormModuleService.deleteTableZJ(formId));
    }
}
