package com.jwipc.message.controller.rest;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.exception.DaoException;
import com.jwipc.message.dto.AddresseeUserDTO;
import com.jwipc.message.dto.MessageAddresseeDTO;
import com.jwipc.message.dto.MessageCenterParamDTO;
import com.jwipc.message.logic.MessageAddresseeLogic;
import com.jwipc.message.logic.MessageCenterLogic;
import com.jwipc.message.model.query.AddresseeUserQuery;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.util.EasyExcelUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static com.jwipc.message.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 我发起的 前端控制器
 * </p>
 *
 * @author shenyang.ou
 * @since 2024-07-03
 */
@RestController
@RequestMapping(MODULE_ROOT + "/addressee/rest")
@RequiredArgsConstructor
public class MessageAddresseeRestController {
    private final MessageAddresseeLogic messageAddresseeLogic;

    /**
     * 查询对账单或开票申请列表
     * @param query 查询条件
     * @return 结果
     */
    @PostMapping("/list")
    public List<MessageAddresseeDTO> list(Long messageCenterId){
        return messageAddresseeLogic.list(messageCenterId);
    }

    /**
     * 导出
     * @param exportFiled 导出的列信息
     * @param queryStr 查询条件
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, String exportFiled, String queryStr, Long messageCenterId) throws IOException {
        final List<MessageAddresseeDTO> exportData = messageAddresseeLogic.list(messageCenterId);
        //动态导出工具类，只需要修改导出excel名称
        EasyExcelUtils.trendsModelWrite(response,"站内信-" + DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN),"sheet", exportData, exportFiled);
    }

    @PostMapping("/setRead")
    public ResponseEntity setRead(Long id) {
        messageAddresseeLogic.setRead(id);
        return ResponseEntity.ok(true);
    }

    @PostMapping("/confirm")
    public ResponseEntity confirm(Long id) {
        messageAddresseeLogic.confirm(id);
        return ResponseEntity.ok(true);
    }

    @PostMapping("/reply")
    public ResponseEntity reply(Long id,String replyContent) {
        messageAddresseeLogic.reply(id,replyContent);
        return ResponseEntity.ok(true);
    }

    @PostMapping("/replyFile")
    public ResponseEntity replyFile(Long id,String replyFile) {
        messageAddresseeLogic.replyFile(id,replyFile);
        return ResponseEntity.ok(true);
    }
    @PostMapping("/confirmDetails")
    public MessageAddresseeDTO confirmDetails(Long id) {
        MessageAddresseeDTO messageAddresseeDTO = messageAddresseeLogic.getById(id);
        return messageAddresseeDTO;
    }

}
