package com.sqx.modules.activity.controller;

import cn.hutool.core.util.ObjectUtil;
import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.activity.dto.ActivityDTO;
import com.sqx.modules.activity.dto.ActivityQueryDTO;
import com.sqx.modules.activity.service.ActivityService;
import com.sqx.modules.activity.vo.ActivityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.util.Arrays;

/**
 * 活动
 * @author : codingliang
 * @date : 2024-6-17
 */
@Api(tags = "活动-活动管理")
@RestController
@RequestMapping("admin/activity")
@RequiredArgsConstructor
public class ActivityController{

    private final ActivityService activityService;

    @GetMapping("page")
    @ApiOperation(value = "获取活动信息分页", response = PageUtils.class)
    public Result page(@Valid ActivityQueryDTO queryDTO) {
        PageUtils page = activityService.pageActivity(queryDTO);
        return Result.success().put("data", page);
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id获取活动信息", response = ActivityVO.class)
    public Result getById(@PathVariable Long id) {
        ActivityVO ActivityVO = activityService.getActivityById(id);
        return Result.success().put("data", ActivityVO);
    }

    @PostMapping
    @ApiOperation("添加活动")
    public Result add(@RequestBody @Valid ActivityDTO activityDTO) {
        activityService.addActivity(activityDTO);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改活动")
    public Result update(@RequestBody @Valid ActivityDTO activityDTO) {
        if (ObjectUtil.isNull(activityDTO.getId())) {
            throw new SqxException("活动id不能为空");
        }

        activityService.updateActivity(activityDTO);
        return Result.success();
    }

    @PutMapping("status/{id}/{flag}")
    @ApiOperation("修改活动状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动id"),
            @ApiImplicitParam(name = "flag", value = "启用标识,0关闭 1启用")
    })
    public Result updateStatus(@PathVariable Long id, @PathVariable @Pattern(regexp = "(0|1)", message = "是否启用只能为0或1") String flag) {
        activityService.updateActivityStatus(id, flag);
        return Result.success();
    }

    @PutMapping("join/{id}/{flag}")
    @ApiOperation("修改是否允许商家主动加入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "活动id"),
            @ApiImplicitParam(name = "flag", value = "允许标识,0不允许 1允许")
    })
    public Result updateJoinMethod(@PathVariable Long id, @PathVariable @Pattern(regexp = "(0|1)", message = "是否启用只能为0或1") String flag) {
        activityService.updateActivityJoinMethod(id, flag);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除活动")
    public Result deleteActivity(@RequestBody @ApiParam("id的集合") Long[] ids) {
        activityService.deleteActivity(Arrays.asList(ids));
        return Result.success();
    }
}
