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.ActivityInviteConfig;
import shop.alien.entity.store.vo.*;
import shop.alien.store.service.ActivityInviteConfigService;

@Api(tags = {"二期-邀请活动"})
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/activityInvite")
@RequiredArgsConstructor
public class ActivityInviteConfigController {

    private final ActivityInviteConfigService activityInviteConfigService;

    /**
     * 创建签到活动
     */
    @ApiOperation("创建/更新邀请活动")
    @ApiOperationSupport(order = 1)
    @PostMapping("createOrUpdateInviteActivity")
    public R<ActivityInviteConfigVo> createOrUpdateInviteActivity(@RequestBody ActivityInviteConfigVo configVO) {
        ActivityInviteConfigVo activity = activityInviteConfigService.createOrUpdateInviteActivity(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 = "createdTime", value = "创建时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "activityType", value = "活动类型", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", paramType = "query")
    })
    @GetMapping("/getInviteActivityConfigList")
    public R<IPage<ActivityInviteConfig>> getInviteActivityConfigList(@RequestParam(defaultValue = "1") int pageNum,
                                                                      @RequestParam(defaultValue = "10") int pageSize,
                                                                      @RequestParam(required = false) String createdTime,
                                                                      @RequestParam(required = false) Integer activityType,
                                                                      @RequestParam(required = false) Integer status){
        IPage<ActivityInviteConfig> activitySignInConfigList = activityInviteConfigService.getInviteActivityConfigList(pageNum, pageSize,createdTime, activityType, status);
        return R.data(activitySignInConfigList);
    }

    @ApiOperation("获取邀请活动详情")
    @ApiOperationSupport(order = 3)
    @GetMapping("/getInviteActivitySignInConfigById")
    public R<ActivityInviteConfigVo> getInviteActivitySignInConfigById(@RequestParam(required = false) Long activityId) {
        log.info("ActivityConfigController.getActivitySignInConfigById={}", activityId);
        ActivityInviteConfigVo activityConfigVO = activityInviteConfigService.getInviteActivitySignInConfigById(activityId);
        return R.data(activityConfigVO);
    }

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

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

    @ApiOperation("获取个人邀请码")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getInviteInfo")
    public R<ActivityInviteInfoVo> getInviteInfo(Integer userId) {
        return R.data(activityInviteConfigService.getInviteInfo(userId));
    }


    /**
     * web-分页查询邀请活动
     */
    @ApiOperation("分页查询邀请记录")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "userId", value = "邀请人", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "invitePhone", value = "手机号", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "inviteStartTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "inviteEndTime", value = "结束时间", dataType = "String", paramType = "query")
    })
    @GetMapping("/getInviteActivityLogList")
    public R<IPage<ActivityInviteLogVo>> getInviteActivityLogList(@RequestParam(defaultValue = "1") int pageNum,
                                                                @RequestParam(defaultValue = "10") int pageSize,
                                                                @RequestParam(required = false) Integer userId,
                                                                  @RequestParam(required = false) String invitePhone,
                                                                  @RequestParam(required = false) String inviteStartTime,
                                                                  @RequestParam(required = false) String inviteEndTime){
        IPage<ActivityInviteLogVo> activityLogList = activityInviteConfigService.getInviteActivityLogList(pageNum, pageSize, userId, invitePhone, inviteStartTime, inviteEndTime);
        return R.data(activityLogList);
    }

    /**
     * 绑定邀请码
     */
    @ApiOperation("绑定邀请码")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invitedUserId", value = "被邀请用户ID", dataType = "Integer", paramType = "query", required = true),
            @ApiImplicitParam(name = "inviteCode", value = "邀请码", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/bindInviteCode")
    public R<String> bindInviteCode(@RequestParam(required = true) Integer invitedUserId,
                                    @RequestParam(required = true) String inviteCode){
        String result = activityInviteConfigService.bindInviteCode(invitedUserId, inviteCode);
        return R.data(result);
    }

}
