package cn.ezhizao.project.business.message.controller;

import cn.ezhizao.common.utils.poi.ExcelUtil;
import cn.ezhizao.framework.aspectj.lang.annotation.Log;
import cn.ezhizao.framework.aspectj.lang.enums.BusinessType;
import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.message.domain.BizMessage;
import cn.ezhizao.project.business.message.service.IBizMessageService;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/business/message")
public class BizMessageController  extends BaseController {
    @Resource
    IBizMessageService bizMessageService;

    /**
     * 查询消息列表
     */
//    @PreAuthorize("@ss.hasPermi('business:carrier:list')")
    @Log(title = "查询消息列表", businessType = BusinessType.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BizMessage data) throws NoSuchFieldException, IllegalAccessException, JsonProcessingException {
        Integer zero = 0;
//        String tenantId = request.getHeader("tenantId");
//        if (tenantId != null && !zero.toString().equals(tenantId)) {
//            data.setTenantId(Long.valueOf(tenantId));
//        }
        startPage();
        List<BizMessage> list = bizMessageService.getList(data);
        return getDataTable(list);
    }
    /**
     * 导出消息列表
     */
    @Log(title = "报工技术咨询", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizMessage message) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(message);
        List<BizMessage> list = bizMessageService.getList(message);
        ExcelUtil<BizMessage> util = new ExcelUtil<BizMessage>(BizMessage.class);
        util.exportExcel(response, list, "报工技术咨询数据");
    }

    /**
     * 获取消息详情
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bizMessageService.getById(id));
    }

    /**
     * 新增消息
     */
    @Log(title = "新增消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizMessage message) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(bizDayworkItemConsult);
        return toAjax(bizMessageService.save(message));
    }

    /**
     * 修改消息信息
     */
    @Log(title = "修改消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizMessage message) throws NoSuchFieldException, IllegalAccessException
    {
//        setTenantId(bizDayworkItemConsult);
        return toAjax(bizMessageService.updateById(message));
    }

    /**
     * 删除报工技术咨询
     */
    @Log(title = "删除消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids)
    {
        return toAjax(bizMessageService.removeBatchByIds(ids));
    }

    @PostMapping("/read")
    public AjaxResult read(@RequestBody BizMessage message) {
        message.setMsgRead(1);
        message.setReadTime(new Date());
        return toAjax(bizMessageService.updateById(message));
    }
}
