package org.dromara.zhishu.controller;

import java.util.List;
import java.util.Map;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.zhishu.domain.vo.NoticeVo;
import org.dromara.zhishu.domain.bo.NoticeBo;
import org.dromara.zhishu.service.INoticeService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 消息通知
 *
 * @author yxy
 * @date 2025-05-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/zhishu/notice")
public class NoticeController extends BaseController {

    private final INoticeService noticeService;

    /**
     * 查询消息通知列表
     */
    @SaCheckPermission("zhishu:notice:list")
    @GetMapping("/list")
    public TableDataInfo<NoticeVo> list(NoticeBo bo, PageQuery pageQuery) {
        bo.setRecipient(LoginHelper.getUserId().toString());
        return noticeService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出消息通知列表
     */
    @SaCheckPermission("zhishu:notice:export")
    @Log(title = "消息通知", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(NoticeBo bo, HttpServletResponse response) {
        List<NoticeVo> list = noticeService.queryList(bo);
        ExcelUtil.exportExcel(list, "消息通知", NoticeVo.class, response);
    }

    /**
     * 获取消息通知详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("zhishu:notice:query")
    @GetMapping("/{id}")
    public R<NoticeVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(noticeService.queryById(id));
    }

    /**
     * 新增消息通知
     */
    @SaCheckPermission("zhishu:notice:add")
    @Log(title = "消息通知", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody NoticeBo bo) {
        return toAjax(noticeService.insertByBo(bo));
    }

    /**
     * 修改消息通知
     */
    @SaCheckPermission("zhishu:notice:edit")
    @Log(title = "消息通知", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody NoticeBo bo) {
        return toAjax(noticeService.updateByBo(bo));
    }

    /**
     * 删除消息通知
     *
     * @param ids 主键串
     */
    @SaCheckPermission("zhishu:notice:remove")
    @Log(title = "消息通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(noticeService.deleteWithValidByIds(List.of(ids), true));
    }


    @SaIgnore
    @PostMapping("/addNotice")
    public void addNotice(@RequestBody Map map){
//        System.out.println("新增通知："+map);
        String msg  = (String) map.get("msg");
        String userId = (String) map.get("userId");
        String isbn = (String) map.get("isbn");
        String bookName = (String) map.get("bookName");
        String sender = map.get("sender") == null ? "系统" : (String) map.get("sender");
        NoticeBo notice = new NoticeBo();
        notice.setSender(sender);
        notice.setRecipient(userId);
        notice.setContent("自动发布商品失败原因："+msg+"   ISBN:"+isbn+"   商品名称:"+bookName);
        notice.setStatus("0");
        notice.setCreateBy(Long.parseLong(userId));
        notice.setTenantId("000000");
        noticeService.insertByBo(notice);
    }
}
