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.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreDynamicDiscount;
import shop.alien.entity.store.vo.StoreDynamicDiscountInviteLogVo;
import shop.alien.entity.store.vo.StoreDynamicDiscountVo;
import shop.alien.store.service.StoreDynamicDiscountInviteLogService;
import shop.alien.store.service.StoreDynamicDiscountReceiveLogService;
import shop.alien.store.service.StoreDynamicDiscountService;
import shop.alien.util.common.RandomCreateUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商户动态折扣 前端控制器
 *
 * @author ssk
 * @since 2025-03-18
 */
@Api(tags = {"动态折扣"})
@ApiSort(18)
@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/dynamicDiscount")
@RequiredArgsConstructor
public class StoreDynamicDiscountController {

    private final StoreDynamicDiscountService storeDynamicDiscountService;

    private final StoreDynamicDiscountReceiveLogService storeDynamicDiscountReceiveLogService;

    private final StoreDynamicDiscountInviteLogService storeDynamicDiscountInviteLogService;

    @ApiOperation("新增或修改动态折扣")
    @ApiOperationSupport(order = 1)
    @PostMapping("/saveOrUpdate")
    public R<Boolean> saveOrUpdate(@RequestBody StoreDynamicDiscount storeDynamicDiscount) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        storeDynamicDiscount.setActivityNo(simpleDateFormat.format(new Date()) + RandomCreateUtil.getRandomNum(1000, 9999));
        if (storeDynamicDiscountService.saveOrUpdate(storeDynamicDiscount)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("删除动态折扣")
    @ApiOperationSupport(order = 2)
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/delete")
    public R<Boolean> delete(Integer id) {
        if (storeDynamicDiscountService.removeById(id)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("分页查询动态折扣")
    @ApiOperationSupport(order = 3)
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true), @ApiImplicitParam(name = "pageSize", value = "页容", dataType = "int", paramType = "query", required = true), @ApiImplicitParam(name = "status", value = "审核状态, 0:待审核, 1:已通过, 2:已拒绝", dataType = "String", paramType = "query"), @ApiImplicitParam(name = "date", value = "创建时间", dataType = "String", paramType = "query")})
    @GetMapping("/getPage")
    public R<IPage<StoreDynamicDiscountVo>> getPage(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize, @RequestParam(required = false) Integer status, @RequestParam(required = false) String date) {
        log.info("StoreDynamicDiscountController.getPage?pageNum={},pageSize={},status={},date={}", pageNum, pageSize, status, date);
        return R.data(storeDynamicDiscountService.getPage(pageNum, pageSize, status, date));
    }

    @ApiOperation("web-导出Excel")
    @ApiOperationSupport(order = 4)
    @GetMapping("/export")
    public ResponseEntity<byte[]> exportToExcel() {
        log.info("StoreDynamicDiscountController.exportToExcel");
        return storeDynamicDiscountService.exportToExcel();
    }

    @ApiOperation("用户参与活动")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({@ApiImplicitParam(name = "discountId", value = "折扣活动id", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/userParticipation")
    public R<Boolean> userParticipation(Integer discountId, Integer userId) {
        if (storeDynamicDiscountReceiveLogService.addLog(discountId, userId)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("其他用户受邀参与")
    @ApiOperationSupport(order = 6)
    @ApiImplicitParams({@ApiImplicitParam(name = "discountId", value = "折扣活动id", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "invitedUserId", value = "被邀用户id", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "invitedNum", value = "邀请码", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/userAcceptInvitation")
    public R<Boolean> userAcceptInvitation(Integer discountId, Integer invitedUserId, String invitedNum) {
        String msg = storeDynamicDiscountInviteLogService.userAcceptInvitation(discountId, invitedUserId, invitedNum);
        if ("success".equals(msg)) {
            return R.success("成功");
        }
        return R.fail(msg);
    }

    @ApiOperation("用户领取折扣")
    @ApiOperationSupport(order = 7)
    @ApiImplicitParams({@ApiImplicitParam(name = "discountId", value = "折扣活动id", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/userReceive")
    public R<Boolean> userReceive(Integer discountId, Integer userId) {
        if (storeDynamicDiscountReceiveLogService.userReceive(discountId, userId)) {
            return R.success("成功");
        }
        return R.fail("失败");
    }

    @ApiOperation("已邀请的用户列表")
    @ApiOperationSupport(order = 8)
    @ApiImplicitParams({@ApiImplicitParam(name = "discountId", value = "折扣活动id", dataType = "Integer", paramType = "query", required = true), @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Integer", paramType = "query", required = true)})
    @GetMapping("/getInviteLog")
    public R<List<StoreDynamicDiscountInviteLogVo>> getInviteLog(Integer discountId, Integer userId) {
        return R.data(storeDynamicDiscountInviteLogService.getInviteLog(discountId, userId));
    }

}
