package com.ruoyi.web.controller.bgt;

import com.ruoyi.bgt.bo.BgtMessageQueryBo;
import com.ruoyi.bgt.domain.BgtMessage;
import com.ruoyi.bgt.domain.dto.BgtMessageMyListDTO;
import com.ruoyi.bgt.domain.dto.BgtMessageOtherDTO;
import com.ruoyi.bgt.domain.vo.BgtMessageCountVO;
import com.ruoyi.bgt.domain.vo.BgtMessageOtherVO;
import com.ruoyi.bgt.domain.vo.BgtMessageVO;
import com.ruoyi.bgt.service.IBgtMessageService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 消息Controller
 *
 * @author ruoyi
 * @date 2025-02-25
 */
@Api(value = "App包工头消息控制器", tags = {"App包工头消息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/app/bgt/message")
public class AppBgtMessageController extends BaseController {

    private final IBgtMessageService iBgtMessageService;

	@ApiOperation("未读消息统计")
	@GetMapping("/countUnread")
	public AjaxResult<BgtMessageCountVO> count() {
		return AjaxResult.success(iBgtMessageService.countUnread());
	}

	@ApiOperation("我的消息列表")
	@GetMapping("/myList")
	public TableDataInfo<BgtMessageVO> myList(BgtMessageMyListDTO dto) {
		return iBgtMessageService.queryMyPageList(dto);
	}

	@ApiOperation("其他消息列表")
	@GetMapping("/otherList")
	public TableDataInfo<BgtMessageOtherVO> otherList(BgtMessageOtherDTO dto) {
		return iBgtMessageService.queryOtherPageList(dto);
	}


    /**
     * 查询消息列表
     */
    @ApiOperation("查询消息列表")
    @GetMapping("/list")
    public TableDataInfo<BgtMessage> list(@Validated BgtMessageQueryBo bo) {
        return iBgtMessageService.queryPageList(bo);
    }

    /**
     * 导出消息列表
     */
    @ApiOperation("导出消息列表")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<BgtMessage> export(@Validated BgtMessageQueryBo bo) {
        List<BgtMessage> list = iBgtMessageService.queryList(bo);
        ExcelUtil<BgtMessage> util = new ExcelUtil<BgtMessage>(BgtMessage.class);
        return util.exportExcel(list, "消息");
    }

    /**
     * 获取消息详细信息
     */
    @ApiOperation("获取消息详细信息")
    @PreAuthorize("@ss.hasPermi('bgt:message:query')")
    @GetMapping("/{id}")
    public AjaxResult<BgtMessage> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iBgtMessageService.queryById(id));
    }

    /**
     * 新增消息
     */
    @ApiOperation("新增消息")
    @PreAuthorize("@ss.hasPermi('bgt:message:add')")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody BgtMessage bo) {
        return toAjax(iBgtMessageService.insert(bo) ? 1 : 0);
    }

    /**
     * 修改消息
     */
    @ApiOperation("修改消息")
    @PreAuthorize("@ss.hasPermi('bgt:message:edit')")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody BgtMessage bo) {
        return toAjax(iBgtMessageService.update(bo) ? 1 : 0);
    }

    /**
     * 删除消息
     */
    @ApiOperation("删除消息")
    @PreAuthorize("@ss.hasPermi('bgt:message:remove')")
    @Log(title = "消息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iBgtMessageService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
