package com.zjrs.zwnw.portal.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.portal.bpo.FbglBPO;
import com.zjrs.zwnw.portal.dto.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

@Api("管理")
@RestController
@RequestMapping("/api/portal/fbgl/")
public class FbglController {
    private static final Logger LOG = LoggerFactory.getLogger(FbglController.class);

    @Resource
    private FbglBPO fbglBPO;

    @ApiOperation(value = "新增发布管理信息", notes = "根据条件查询消息列表1")
    @PostMapping(value = "saveFbglZw")
    public AjaxResponse saveFbglZw(@RequestBody @Valid SaveFbglZwDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("id", fbglBPO.saveFbglZw(dto));
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "上传发布管理附件信息", notes = "根据条件查询消息列表1")
    @PostMapping(value = "saveFbglFj")
    public AjaxResponse saveFbglFj(@RequestBody @Valid SaveFbglFjDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            // 操作表
            fbglBPO.saveFbglFj(dto);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "删除发布管理附件信息", notes = "根据条件查询消息列表1")
    @PostMapping(value = "delFbglFj")
    public AjaxResponse delFbglFj(@RequestBody @Valid CommonFbglDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            fbglBPO.delFbglFj(dto);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "获取发布管理附件列表", notes = "根据条件查询消息列表1")
    @PostMapping(value = "getFbglFjList")
    public AjaxResponse getFbglFjList(@RequestBody @Valid GetFbglFjListDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("data", fbglBPO.getFbglFjList(dto));
            ajaxResponse.setData("pagenum", dto.getPagenum());
            ajaxResponse.setData("pagesize", dto.getPagesize());
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "获取发布管理信息", notes = "根据条件查询消息列表1")
    @PostMapping(value = "getFbglZw")
    public AjaxResponse getFbglZw(@RequestBody @Valid CommonFbglDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("data", fbglBPO.getFbglZw(dto));
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "获取发布管理列表", notes = "根据条件查询消息列表1")
    @PostMapping(value = "getFbglList")
    public AjaxResponse getFbglList(@RequestBody @Valid GetFbglListDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("data", fbglBPO.getFbglList(dto));
            ajaxResponse.setData("pagenum", dto.getPagenum());
            ajaxResponse.setData("pagesize", dto.getPagesize());
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "获取发布管理内容列表", notes = "根据条件查询消息列表1")
    @PostMapping(value = "getfbglNr")
    public AjaxResponse getfbglNr(@RequestBody @Valid GetFbglListDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("data", fbglBPO.getfbglNr(dto));
            ajaxResponse.setData("pagenum", dto.getPagenum());
            ajaxResponse.setData("pagesize", dto.getPagesize());
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "更新发布管理发布状态", notes = "根据条件查询消息列表1")
    @PostMapping(value = "updateFbglFbzt")
    public AjaxResponse updateFbglFbzt(@RequestBody @Valid ChangeFbglDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            fbglBPO.updateFbglFbzt(dto);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "更新发布管理置顶状态", notes = "根据条件查询消息列表1")
    @PostMapping(value = "updateFbglZdzt")
    public AjaxResponse updateFbglZdzt(@RequestBody @Valid ChangeFbglDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            fbglBPO.updateFbglZdzt(dto);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "删除发布管理信息", notes = "根据条件查询消息列表1")
    @PostMapping(value = "delFbgl")
    public AjaxResponse delFbgl(@RequestBody @Valid CommonFbglDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            fbglBPO.delFbgl(dto);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }


    @ApiOperation(value = "获取发布管理列表", notes = "根据条件查询消息列表1")
    @PostMapping(value = "getYjfkList")
    public AjaxResponse getYjfkList(@RequestBody @Valid GetYjfkListDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("data", fbglBPO.getYjfkList(dto));
            ajaxResponse.setData("pagenum", dto.getPagenum());
            ajaxResponse.setData("pagesize", dto.getPagesize());
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "获取发布管理信息", notes = "根据条件查询消息列表1")
    @PostMapping("getYjfk/{id}")
    public AjaxResponse getYjfk(@PathVariable String id) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("data", fbglBPO.getYjfk(id));
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "新增发布管理信息", notes = "根据条件查询消息列表1")
    @PostMapping(value = "saveYjfk")
    public AjaxResponse saveYjfk(@RequestBody @Valid SaveYjfkDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("id", fbglBPO.saveYjfk(dto));
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "知识库查询列表", notes = "知识库查询列表")
    @PostMapping(value = "/getZskList")
    public AjaxResponse getZskList(@RequestBody @Valid ZskQueryDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setData("data", fbglBPO.getZskList(dto));
        return ajaxResponse;
    }

    @ApiOperation(value = "保存知识库", notes = "保存知识库")
    @PostMapping(value = "/saveZsk")
    public AjaxResponse saveZsk(@RequestBody @Valid ZskDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        ajaxResponse.setData("data", fbglBPO.saveZsk(dto));
        return ajaxResponse;
    }

    @ApiOperation(value = "删除知识库", notes = "删除知识库")
    @PostMapping(value = "/deleteZsk")
    public AjaxResponse deleteZsk(@RequestBody @Valid ZskDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        fbglBPO.deleteZsk(dto);
        ajaxResponse.setMsg("删除成功");
        return ajaxResponse;
    }

    @ApiOperation(value = "发布/取消发布知识库", notes = "发布/取消发布知识库")
    @PostMapping(value = "/publishZsk")
    public AjaxResponse publishZsk(@RequestBody @Valid ZskDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        fbglBPO.publishZsk(dto);
        return ajaxResponse;
    }

    @ApiOperation(value = "查看知识库详细信息", notes = "查看知识库详细信息")
    @PostMapping(value = "/getZskXxById")
    public AjaxResponse getZskXxById(@RequestBody @Valid ZskDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
         ajaxResponse.setData("data",fbglBPO.getZskXxById(dto));
        return ajaxResponse;
    }

    @ApiOperation(value = "导出知识库列表", notes = "导出知识库列表")
    @PostMapping(value = "/exportZskList")
    public void exportZsk(@RequestBody @Valid ZskQueryDTO dto, HttpServletResponse res) {
      fbglBPO.exportZsk(dto,res);
    }

    @ApiOperation(value = "获取知识库附件列表", notes = "获取知识库附件列表")
    @PostMapping("/getZskFileList")
    public AjaxResponse getZskFileList(@RequestBody @Valid ZskQueryDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        List<Map<String,Object>> zskFileList = fbglBPO.getZskFileList(dto);
        ajaxResponse.setData("data", zskFileList);
        return ajaxResponse;
    }
}
