package com.zkqy.web.controller.dragForm;

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

import com.zkqy.system.entity.DragTable;
import com.zkqy.system.entity.vo.DragTableVo;
import com.zkqy.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zkqy.common.annotation.Log;
import com.zkqy.common.core.controller.BaseController;
import com.zkqy.common.core.domain.AjaxResult;
import com.zkqy.common.enums.BusinessType;
import com.zkqy.common.utils.poi.ExcelUtil;
import com.zkqy.common.core.page.TableDataInfo;

/**
 * 动态表格Controller
 *
 * @author xzz
 * @date 2023-07-31
 */
@RestController
@RequestMapping("/system/table")
public class DragTableController extends BaseController {

    @Autowired // 动态表格
    private IDragTableService dragTableService;

    @Autowired // 统计图标
    private IDragTableStatisticService dragTableStatisticService;

    @Autowired // 表格样式
    private IDragTableStyleService dragTableStyleService;

    @Autowired  // 按钮表格关联
    private IDragTableBtnRelevanceService dragTableBtnRelevanceService;
    
    /**
     * 查询动态表格列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DragTable dragTable) {
        startPage();
        List<DragTable> list = dragTableService.selectDragTableList(dragTable);
        return getDataTable(list);
    }

    /**
     * 导出动态表格列表
     */
    @Log(title = "动态表格", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DragTable dragTable) {
        List<DragTable> list = dragTableService.selectDragTableList(dragTable);
        ExcelUtil<DragTable> util = new ExcelUtil<DragTable>(DragTable.class);
        util.exportExcel(response, list, "动态表格数据");
    }

    /**
     * 获取动态表格详细信息
     */
//    @GetMapping(value = "/{tId}")
//    public AjaxResult getInfo(@PathVariable("tId") Long tId) {
//        return success(dragTableService.selectDragTableByTId(tId));
//    }

    /**
     * 新增动态表格
     */
    @Log(title = "动态表格", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DragTable dragTable) {
        return toAjax(dragTableService.insertDragTable(dragTable));
    }

//    /**
//     * 修改动态表格
//     */
//    @Log(title = "动态表格", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody DragTable dragTable) {
//        return toAjax(dragTableService.updateDragTable(dragTable));
//    }

//    /**
//     * 删除动态表格
//     */
//    @Log(title = "动态表格", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{tIds}")
//    public AjaxResult remove(@PathVariable Long[] tIds) {
//        return toAjax(dragTableService.deleteDragTableByTIds(tIds));
//    }

    /**
     * 新增动态表格模版数据
     */
    @PostMapping("/addDragTable")
    public AjaxResult addDragTable(@RequestBody DragTableVo dragTableVo){
        //新增动态表格信息
        dragTableService.addDragTable(dragTableVo);
        //新增动态表格样式信息
        if(dragTableVo.getDragTableStyleList().size() > 0){
            dragTableStyleService.batchInsertDragTableStyle(dragTableVo);
        }
        //新增动态表格数据统计信息
        //if(dragTableVo.getDragTableStatisticList().size() > 0){
        //    dragTableStatisticService.batchInsertDragTableStatistic(dragTableVo);
        //}
        //新增表格按钮关联表
        if(dragTableVo.getDragTableBtnRelevanceList().size() > 0){
            dragTableBtnRelevanceService.batchInsertDragTableBtnRelevance(dragTableVo.getDragTableBtnRelevanceList());
        }
         return AjaxResult.success();
    }

    /**
     * 获取动态表格详细信息
     */
    @GetMapping("/getInfo/{tId}")
    public AjaxResult getInfo(@PathVariable("tId") Long tId) {
        return success(dragTableService.selectDragTableVoByTId(tId));
    }



    /**
     * 修改动态表格
     */
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody DragTableVo dragTableVo) {
        //修改动态表格信息
        dragTableService.updateDragTable(dragTableVo);
        //修改动态表格样式信息
        dragTableStyleService.updateDragTableStyle(dragTableVo);
        //修改动态表格数据统计信息
//        dragTableStatisticService.updateDragTableStatistic(dragTableVo);
        //修改动态表格和按钮关联表
//        dragTableBtnRelevanceService.updateDragTableBtnRelevance(dragTableVo);
        return AjaxResult.success();
    }

    /**
     * 删除动态表格
     */
    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestBody Map<String, Object> map) {
        List<Long> tIds = (List<Long>) map.get("tIds");
        List<String> sqlKeys = (List<String>) map.get("sqlKeys");
        List<String> tableKeys = (List<String>) map.get("tableKeys");
        // 删除动态表格
        dragTableService.deleteDragTable(tIds, sqlKeys);
        // 删除动态表格统计
        dragTableStatisticService.deleteDragTableStatisticByTableKeys(tableKeys);
        // 删除动态表格样式
        dragTableStyleService.deleteDragTableStyleByTableKeys(tableKeys);
        // 删除动态表格和按钮关联表
        dragTableBtnRelevanceService.deleteDragTableBtnRelevanceByTableKeys(tableKeys);
        return AjaxResult.success();
    }

    /**
     * 查询动态表格列表
     */
    @GetMapping("/dragTableList")
    public AjaxResult dragTableList() {
        return AjaxResult.success(dragTableService.selectDragTableList(new DragTable()));
    }

}
