package com.ruoyi.api.controller.clock;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.api.controller.common.AbstractApiController;
import com.ruoyi.clock.domain.Employee;
import com.ruoyi.clock.domain.bo.ActivityAgentBo;
import com.ruoyi.clock.domain.bo.ActivityBo;
import com.ruoyi.clock.domain.vo.ActivityAgentVo;
import com.ruoyi.clock.domain.vo.ActivityVo;
import com.ruoyi.clock.service.IActivityAgentService;
import com.ruoyi.clock.service.IActivityService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.QueryGroup;
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.validation.constraints.NotNull;
import java.util.List;

/**
 * 活动信息Controller
 *
 * @author pengc
 * @date 2026-03-04
 */
@Validated
@Api(value = "活动信息控制器", tags = {"活动信息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/clock/activity")
public class ApiActivityController extends AbstractApiController {

    private final IActivityService iActivityService;
    private final IActivityAgentService iActivityAgentService;

    /**
     * 查询活动信息列表
     */
    @ApiOperation("查询活动信息列表")
    @GetMapping("/list")
    public R<List<ActivityVo>> list(@Validated(QueryGroup.class) ActivityBo bo) {
        bo.setProgressStatus(true);
        Employee loginUser = getLoginUser();
        List<ActivityVo> list = iActivityService.queryList(bo);
        if (CollUtil.isNotEmpty(list)){
            list.forEach(item -> {
                item.loadAgentList();
                item.loadProductList();
                item.loadClockCount(loginUser);
            });
        }
        return R.ok(list);
    }

    /**
     * 查询活动与代理商关联列表
     */
    @ApiOperation("查询活动与代理商关联列表")
    @GetMapping("/agentList")
    public R<List<ActivityAgentVo>> agentList(@Validated(QueryGroup.class) ActivityAgentBo bo) {
        return R.ok(iActivityAgentService.queryList(bo));
    }

    /**
     * 获取活动信息详细信息
     */
    @ApiOperation("获取活动信息详细信息")
    @GetMapping("/info/{activityId}")
    public R<ActivityVo> getInfo(@ApiParam("主键")
                                  @NotNull(message = "主键不能为空")
                                  @PathVariable("activityId") Long activityId) {
        ActivityVo activityVo = iActivityService.queryById(activityId);
        if(ObjectUtil.isNotNull(activityVo)){
            activityVo.loadAgentList();
            activityVo.loadProductList();
        }
        return R.ok(activityVo);
    }

}
