package com.ylx.point.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.annotation.Log;
import com.ylx.common.core.controller.BaseController;
import com.ylx.common.core.domain.AjaxResult;
import com.ylx.common.core.domain.R;
import com.ylx.common.enums.BusinessType;
import com.ylx.point.domain.PointActivity;
import com.ylx.point.domain.dto.PointActivityDTO;
import com.ylx.point.domain.dto.PointActivityPageDTO;
import com.ylx.point.domain.dto.PointActivityValidityDTO;
import com.ylx.point.domain.dto.SignDTO;
import com.ylx.point.domain.vo.*;
import com.ylx.point.service.IPointActivityService;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 积分活动主Controller
 *
 * @author wzj
 * @date 2026-03-26
 */
@RestController
@RequestMapping("/point/activity")
@Api(tags = {"积分规则配置"})
@Slf4j
public class PointActivityController extends BaseController {

    @Autowired
    private IPointActivityService pointActivityService;

    /**
     * 查询积分活动主列表
     */
    @PreAuthorize("@ss.hasPermi('point:activity:list')")
    @ApiOperation("根据条件分页查询积分活动")
    @GetMapping("/page")
    public R<Page<PointActivityPageVo>> page(Page<PointActivity> page, PointActivityPageDTO dto) {
        Page<PointActivityPageVo> pageData = this.pointActivityService.list(page, dto);
        return R.ok(pageData);
    }

    /**
     * 获取积分活动主详细信息
     */
    @PreAuthorize("@ss.hasPermi('point:activity:query')")
    @ApiOperation("根据id获取积分活动详情")
    @GetMapping(value = "/{id}")
    public R<PointActivityDetailsVo<?>> getDetailsInfo(@PathVariable("id") Long id) {
        PointActivityDetailsVo<?> vo = this.pointActivityService.getDetailsInfo(id);
        return R.ok(vo);
    }

    /**
     * 新增积分活动
     */
    @PreAuthorize("@ss.hasPermi('point:activity:add')")
    @ApiOperation("新增积分活动")
    @Log(title = "积分活动添加", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PointActivityDTO dto) {
        try {
            this.pointActivityService.add(dto);
            return success();
        } catch (Exception e) {
            log.error("新增积分活动失败", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改积分活动主
     */
    @PreAuthorize("@ss.hasPermi('point:activity:edit')")
    @ApiOperation("编辑积分活动")
    @Log(title = "积分活动编辑", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PointActivityDTO dto) {
        this.pointActivityService.edit(dto);
        return success();
    }

    /**
     * 获取积分活动主详细信息
     */
    @PreAuthorize("@ss.hasPermi('point:activity:query')")
    @ApiOperation("根据id获取积分活动统计数据")
    @GetMapping(value = "/stat/{id}")
    public R<PointActivityStatVo> getStatInfo(@PathVariable("id") Long id) {
        PointActivityStatVo vo = this.pointActivityService.getStatInfo(id);
        return R.ok(vo);
    }

    /**
     * 修改积分活动主
     */
    @PreAuthorize("@ss.hasPermi('point:activity:edit')")
    @ApiOperation("编辑积分活动时效")
    @Log(title = "积分活动时效编辑", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/validity")
    public AjaxResult editValidity(@Validated @RequestBody PointActivityValidityDTO dto) {
        this.pointActivityService.editValidity(dto);
        return success();
    }

    /**
     * 修改积分活动主
     */
    @PreAuthorize("@ss.hasPermi('point:activity:query')")
    @ApiOperation("积分活动时效回显")
    @GetMapping(value = "/validity/details")
    public R<List<PointActivityValidityVo>> validityDetails() {
        List<PointActivityValidityVo> list = this.pointActivityService.validityDetails();
        return R.ok(list);
    }


    @ApiOperation("获取签到面板数据")
    @PostMapping(value = "/sign/info")
    public R<List<SignDayVo>> getSignInfo(@Validated @RequestBody SignDTO dto) {
        List<SignDayVo> list = this.pointActivityService.getSignInfo(dto);
        return R.ok(list);
    }

    /**
     * 用户签到接口
     * @param dto
     * @return R
     */
    @ApiOperation("用户签到接口")
    @PostMapping(value = "/sign")
    public R sign(@Validated @RequestBody SignDTO dto) {
        this.pointActivityService.sign(dto);
        return R.ok();
    }

}
