package com.pig4cloud.pig.marketing.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.marketing.api.dto.MktMgmtHandPushQueryDTO;
import com.pig4cloud.pig.marketing.api.dto.MktMgmtPushRecordSaveDTO;
import com.pig4cloud.pig.marketing.api.service.MktMgmtHandPushService;
import com.pig4cloud.pig.marketing.api.vo.rule.push.HandPushVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * 手动推送管理控制器
 * 
 * @author WCL
 * @date 2025-09-03
 * @description 手动推送相关接口
 */
@Slf4j
@RestController
@RequestMapping("/handPush")
@RequiredArgsConstructor
@Tag(name = "手动推送管理", description = "手动推送相关接口")
public class MktMgmtHandPushController {

    private final MktMgmtHandPushService mktMgmtHandPushService;

    /**
     * 分页查询手动推送记录
     * 
     * @param queryDTO 查询参数
     * @return 分页结果
     */
    @PostMapping("/page")
    @Operation(summary = "分页查询手动推送记录", description = "根据条件分页查询手动推送记录")
    public R<Page<HandPushVO>> pageQuery(@RequestBody @Valid MktMgmtHandPushQueryDTO queryDTO) {
        try {
            Page<HandPushVO> result = mktMgmtHandPushService.pageQuery(queryDTO);
            return R.ok(result);
        } catch (Exception e) {
            log.error("分页查询手动推送记录异常", e);
            return R.failed("查询失败：" + e.getMessage());
        }
    }

    /**
     * 手动推送
     * 
     * @param saveDTO 推送参数
     * @return 推送结果
     */
    @PostMapping("/push")
    @Operation(summary = "手动推送", description = "根据规则进行手动推送")
    public R<String> handPush(@Valid @RequestBody MktMgmtPushRecordSaveDTO saveDTO) {
        try {
            String result = mktMgmtHandPushService.handPush(saveDTO);
            if (result.contains("成功")) {
                return R.ok(result);
            } else {
                return R.failed(result);
            }
        } catch (Exception e) {
            log.error("手动推送异常", e);
            return R.failed("推送失败：" + e.getMessage());
        }
    }
}

