package com.ruoyi.web.controller.clock;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.clock.domain.bo.ActivityProductBo;
import com.ruoyi.clock.domain.vo.ActivityProductVo;
import com.ruoyi.clock.service.IActivityProductService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 活动与产品关联Controller
 *
 * @author pengc
 * @date 2026-03-04
 */
@Validated
@Api(value = "活动与产品关联控制器", tags = {"活动与产品关联管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/clock/activityProduct")
public class ActivityProductController extends BaseController {

    private final IActivityProductService iActivityProductService;

    /**
     * 分页查询活动与产品关联列表
     */
    @ApiOperation("查询活动与产品关联列表")
    @SaCheckPermission("clock:activityProduct:list")
    @GetMapping("/page")
    public TableDataInfo<ActivityProductVo> page(@Validated(QueryGroup.class) ActivityProductBo bo, PageQuery pageQuery) {
        return iActivityProductService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取活动与产品关联详细信息
     */
    @ApiOperation("获取活动与产品关联详细信息")
    @SaCheckPermission("clock:activityProduct:query")
    @GetMapping("/info/{productId}")
    public R<ActivityProductVo> getInfo(@ApiParam("主键")
                                                  @NotNull(message = "主键不能为空")
                                                  @PathVariable("productId") Long productId) {
        return R.ok(iActivityProductService.queryById(productId));
    }

    /**
     * 新增活动与产品关联
     */
    @ApiOperation("新增活动与产品关联")
    @SaCheckPermission("clock:activityProduct:add")
    @Log(title = "活动与产品关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ActivityProductBo bo) {
        return toAjax(iActivityProductService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改活动与产品关联
     */
    @ApiOperation("修改活动与产品关联")
    @SaCheckPermission("clock:activityProduct:edit")
    @Log(title = "活动与产品关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ActivityProductBo bo) {
        return toAjax(iActivityProductService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除活动与产品关联
     */
    @ApiOperation("删除活动与产品关联")
    @SaCheckPermission("clock:activityProduct:remove")
    @Log(title = "活动与产品关联", businessType = BusinessType.DELETE)
    @PostMapping("/del/{productIds}")
    public R<Void> remove(@ApiParam("主键串")
                                       @NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] productIds) {
        return toAjax(iActivityProductService.deleteWithValidByIds(Arrays.asList(productIds), true) ? 1 : 0);
    }

    /**
     * 导出活动与产品关联列表
     */
    @ApiOperation("导出活动与产品关联列表")
    @SaCheckPermission("clock:activityProduct:export")
    @Log(title = "活动与产品关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated ActivityProductBo bo, HttpServletResponse response) {
        List<ActivityProductVo> list = iActivityProductService.queryList(bo);
        ExcelUtil.exportExcel(list, "活动与产品关联", ActivityProductVo.class, response);
    }

}
