package com.sundot.airport.web.controller.item;

import com.sundot.airport.common.core.controller.BaseController;
import com.sundot.airport.common.core.domain.AjaxResult;
import com.sundot.airport.common.core.domain.model.LoginUser;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.common.utils.SecurityUtils;
import com.sundot.airport.item.domain.push.SeizureMessagePushDTO;
import com.sundot.airport.item.service.ISeizureMessagePushService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 查获消息推送 Controller
 *
 * @author wangxx
 * @date 2026-03-31
 */
@Slf4j
@RestController
@RequestMapping("/item/seizure/push")
@Api(tags = "查获消息推送")
public class SeizureMessagePushController extends BaseController {

    @Autowired
    private ISeizureMessagePushService seizureMessagePushService;

    /**
     * 获取查获消息推送数据
     *
     * @param startDate 开始日期（可选，格式：yyyy-MM-dd）
     * @param endDate   结束日期（可选，格式：yyyy-MM-dd）
     * @return 查获消息推送数据
     */
    @ApiOperation("获取查获消息推送数据")
    @GetMapping("/message")
    public AjaxResult getMessagePushData(@RequestParam(required = false) String startDate,
                                         @RequestParam(required = false) String endDate) {
        try {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            Long userId = loginUser.getUserId();
            Long deptId = loginUser.getDeptId();

            Date startDateTime;
            Date endDateTime;

            if (startDate == null || endDate == null) {
                // 默认近 15 天（不包含今天）
                endDateTime = DateUtils.addDays(new Date(), -1);
                startDateTime = DateUtils.addDays(endDateTime, -14);
            } else {
                startDateTime = DateUtils.parseDate(startDate);
                endDateTime = DateUtils.parseDate(endDate);
            }

            SeizureMessagePushDTO result = seizureMessagePushService.getSeizureMessagePushData(
                    userId, deptId, startDateTime, endDateTime);

            return AjaxResult.success(result);
        } catch (Exception e) {
            log.error("获取查获消息推送数据失败，error: ", e);
            return AjaxResult.error("获取查获消息推送数据失败：" + e.getMessage());
        }
    }
}
