package com.ruoyi.web.controller.jarvis;

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

import com.ruoyi.jarvis.domain.XbMessageItem;
import com.ruoyi.jarvis.service.IXbMessageItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jarvis.domain.XbMessage;
import com.ruoyi.jarvis.service.IXbMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 线报消息Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/jarvis/xbmessage")
public class XbMessageController extends BaseController
{
    @Autowired
    private IXbMessageService xbMessageService;
    @Autowired
    private IXbMessageItemService xbMessageItemService;

    /**
     * 查询线报消息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(XbMessage xbMessage)
    {
        startPage();
        List<XbMessage> list = xbMessageService.selectXbMessageList(xbMessage);
        TableDataInfo dataTable = getDataTable(list);
        if (!dataTable.getRows().isEmpty()) {
            @SuppressWarnings("unchecked")
            List<XbMessage> rows = (List<XbMessage>) dataTable.getRows();
            for (XbMessage xbMessageCache : rows) {
                XbMessageItem item = new XbMessageItem();
                item.setXbMessageId(xbMessageCache.getId().toString());
                List<XbMessageItem> children = xbMessageItemService.selectXbMessageItemList(item);
                xbMessageCache.setChildren(children);
            }
            dataTable.setRows(rows);
        }
        return dataTable;
    }

    /**
     * 导出线报消息列表
     */
    @Log(title = "线报消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XbMessage xbMessage)
    {
        List<XbMessage> list = xbMessageService.selectXbMessageList(xbMessage);
        ExcelUtil<XbMessage> util = new ExcelUtil<XbMessage>(XbMessage.class);
        util.exportExcel(response, list, "线报消息数据");
    }

    /**
     * 获取线报消息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(xbMessageService.selectXbMessageById(id));
    }

    /**
     * 新增线报消息
     */
    @Log(title = "线报消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XbMessage xbMessage)
    {
        return toAjax(xbMessageService.insertXbMessage(xbMessage));
    }

    /**
     * 修改线报消息
     */
    @Log(title = "线报消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XbMessage xbMessage)
    {
        return toAjax(xbMessageService.updateXbMessage(xbMessage));
    }

    /**
     * 删除线报消息
     */
    @Log(title = "线报消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(xbMessageService.deleteXbMessageByIds(ids));
    }
}
