package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.ActivitySignInConfig;
import shop.alien.entity.store.UserPoint;
import shop.alien.entity.store.vo.ActivityConfigVo;
import shop.alien.entity.store.vo.ActivityPeriodVo;
import shop.alien.store.service.ActivityConfigService;
import shop.alien.store.service.UserPointService;
import java.util.List;
@Api(tags = {"二期-签到活动"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/activity")
@RequiredArgsConstructor
public class ActivityConfigController {
    private final ActivityConfigService activityConfigService;
    private final UserPointService userPointService;

    /**
     * 创建签到活动
     */
    @ApiOperation("创建签到活动")
    @ApiOperationSupport(order = 1)
    @PostMapping("createOrUpdateActivity")
    public R<ActivityConfigVo> createOrUpdateActivity(@RequestBody ActivityConfigVo configVO) {
        ActivityConfigVo activity = activityConfigService.createOrUpdateActivity(configVO);
        if(activity == null) {
            return R.fail("插入/更新失败");
        }
        if(StringUtils.isNotBlank(activity.getErrorMessage())){
            return R.fail(activity.getErrorMessage());
        }
        return R.data(activity);
    }

    /**
     * web-分页查询店铺信息
     */
    @ApiOperation("分页查询活动列表")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "activityName", value = "活动名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query")

    })
    @GetMapping("/getActivityConfigList")
    public R<IPage<ActivitySignInConfig>> getActivityList(@RequestParam(defaultValue = "1") int pageNum,
                                                            @RequestParam(defaultValue = "10") int pageSize,
                                                            @RequestParam(required = false) String activityName,
                                                          @RequestParam(required = false) Integer status){
        IPage<ActivitySignInConfig> activitySignInConfigList = activityConfigService.getActivityList(pageNum, pageSize, activityName, status);
        return R.data(activitySignInConfigList);
    }

    @ApiOperation("获取签到活动详情")
    @ApiOperationSupport(order = 3)
    @GetMapping("/getActivitySignInConfigById")
    public R<ActivityConfigVo> getActivitySignInConfigById(@RequestParam(required = true) Long activityId) {
        log.info("ActivityConfigController.getActivitySignInConfigById={}", activityId);
        ActivityConfigVo activityConfigVO = activityConfigService.getActivitySignInConfigById(activityId);
        return R.data(activityConfigVO);
    }

    @ApiOperation("获取默认方案签到活动详情")
    @ApiOperationSupport(order = 4)
    @GetMapping("/getDefaultActivitySignInConfigById")
    public R<ActivityConfigVo> getDefaultActivitySignInConfigById() {
        log.info("ActivityConfigController.getDefaultActivitySignInConfigById");
        ActivityConfigVo activityConfigVO = activityConfigService.getDefaultActivitySignInConfigById();
        return R.data(activityConfigVO);
    }

    /**
     * 修改签到活动状态
     *
     * @param id     主键
     * @param status 状态
     * @return boolean
     */
    @ApiOperation("更新签到活动状态")
    @ApiOperationSupport(order = 5)
    @GetMapping("/updateActivityStatus")
    public R<Boolean> updateActivityStatus(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        log.info("ActivityConfigController.updateActivityStatus?id={}&status={}", id, status);
        if (activityConfigService.updateActivityStatus(id, status)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("删除活动")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "活动ID", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/deleteActivity")
    public R<Boolean> deleteActivity(Integer id) {
        return R.data(activityConfigService.deleteActivity(id));
    }

    @ApiOperation("校验是否时间重叠")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query")
    })
    @GetMapping("/checkOverlapTime")
    public R<List<ActivityPeriodVo>> checkOverlapTime(@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime) {
        log.info("ActivityConfigController.checkOverlapTime");
        List<ActivityPeriodVo> activityPeriodVoList = activityConfigService.checkOverlapTime(startTime, endTime);
        return R.data(activityPeriodVoList);
    }

    @ApiOperation("获取用户积分")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getUserPoint")
    public R<UserPoint> getUserPoint(@RequestParam(required = true) Integer userId) {
        log.info("ActivityConfigController.getUserPoint");
        UserPoint userPoint = userPointService.getPoint(userId);
        return R.data(userPoint);
    }

    @ApiOperation("获取用户积分详情")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getUserPointDetails")
    public R<UserPoint> getUserPointDetails(@RequestParam(required = true) Integer userId) {
        log.info("ActivityConfigController.getUserPointDetails");
        UserPoint userPoint = userPointService.getUserPointDetails(userId);
        return R.data(userPoint);
    }
}
