package com.usky.cdi.controller.web;

import com.usky.cdi.domain.CdiDeliveryLog;
import com.usky.cdi.mapper.CdiDefenseProjectMapper;
import com.usky.cdi.service.CdiDeliveryLogService;
import com.usky.cdi.service.vo.SyncTaskStatisticsVO;
import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 人防_投递日志表（记录数据的MQTT投递情况） 前端控制器
 * </p>
 *
 * @author fu
 * @since 2026-02-02
 */
@RestController
@RequestMapping("/cdiDeliveryLog")
public class CdiDeliveryLogController {

    @Autowired
    private CdiDeliveryLogService cdiDeliveryLogService;
    @Autowired
    private CdiDefenseProjectMapper cdpMapper;

    /**
     * 同步数据查询
     * @param id 主键id
     * @return 同步数据
     */
    @GetMapping("synchronizeData")
    public List<SyncTaskStatisticsVO> synchronizeData(@RequestParam(value = "id", required = false) Long id) {
        return cdiDeliveryLogService.selectById(id);
    }

    /**
     * 同步数据
     * @param vo 同步数据
     * @return 同步结果
     */
    @PutMapping("synchronizeData")
    public ApiResult<Void> synchronizeData(@RequestBody SyncTaskStatisticsVO vo) {
        cdiDeliveryLogService.synchronizeData(vo);
        return ApiResult.success();
    }

    /**
     * 日志列表
     * @param id 主键id
     * @param pageNum     页码
     * @param pageSize     每页数量
     * @param dataType 数据类型
     * @param logType 日志类型
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 日志列表
     */
    @GetMapping("logList")
    public ApiResult<CommonPage<CdiDeliveryLog>> logList(@RequestParam(value = "id", required = false) Long id,
                                                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                         @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize,
                                                         @RequestParam(value = "dataType", required = false) Integer dataType,
                                                         @RequestParam(value = "logType", required = false) Integer logType,
                                                         @RequestParam(value = "startTime", required = false) String startTime,
                                                         @RequestParam(value = "endTime", required = false) String endTime) {
        return ApiResult.success(cdiDeliveryLogService.logList(id, pageNum, pageSize, dataType, logType, startTime, endTime));
    }

    /**
     * 日志详情
     * @param id 主键id
     * @return 日志详情
     */
    @GetMapping("logId")
    public ApiResult<String> logId(@RequestParam(value = "id") Long id) {
        CdiDeliveryLog log = cdiDeliveryLogService.lambdaQuery()
                .eq(CdiDeliveryLog::getTenantId, SecurityUtils.getTenantId())
                .eq(CdiDeliveryLog::getId, id)
                .one();
        if (log == null) {
            return ApiResult.error("日志不存在");
        }
        return ApiResult.success(log.getInfoContent());
    }
}

