package com.zjrs.zwnw.portal.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.util.CurrentUser;
import org.mohrss.leaf.core.framework.util.SecurityUtils;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.portal.bpo.InformationBPO;
import com.zjrs.zwnw.portal.dto.ChangeInformationDTO;
import com.zjrs.zwnw.portal.dto.DelInformationDTO;
import com.zjrs.zwnw.portal.dto.GetInformationDTO;
import com.zjrs.zwnw.portal.dto.SendInformationDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;

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

    @Resource
    private InformationBPO informationBPO;

    /**
     * 获取消息
     *
     * @param dto ydzt 阅读状态 1未阅读，2已阅读|ts_yxbz 有效标志 0无效，1有效
     * @return
     */
    @ApiOperation(value = "根据条件查询消息列表1", notes = "根据条件查询消息列表1")
    @PostMapping(value = "getAllInfo")
    public AjaxResponse getAllInfo(@RequestBody @Valid GetInformationDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer page = dto.getPagenum();
        Integer rows = dto.getPagesize();
        CurrentUser cUser = SecurityUtils.getCurrentUser();
        String userid = cUser.getUserid();
        try {
            ajaxResponse.setData("data", informationBPO.queryinformationList(dto, page, rows, userid));
            ajaxResponse.setData("pagenum", page);
            ajaxResponse.setData("pagesize", rows);
        } 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 = "changeInfo")
    public AjaxResponse changeInfo(@RequestBody @Valid ChangeInformationDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            informationBPO.changeInfo(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("delInfo")
    public AjaxResponse delInfo(@RequestBody @Valid DelInformationDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            informationBPO.delInfo(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")
    @GetMapping("clearInfo")
    public AjaxResponse clearInfo() {
        CurrentUser cUser = SecurityUtils.getCurrentUser();
        String userid = cUser.getUserid();
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            informationBPO.clearInfo(userid);
        } 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("sendInfo")
    public AjaxResponse sendInfo(@RequestBody @Valid SendInformationDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            informationBPO.sendInfo(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;
    }
}
