package com.yingpai.miniapp.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yingpai.miniapp.domain.bo.UserSubscriptionBo;
import com.yingpai.miniapp.domain.vo.UserSubscriptionVo;
import com.yingpai.miniapp.service.IUserSubscriptionService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 订阅管理
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/miniapp/subscription")
public class UserSubscriptionController extends BaseController {

    private final IUserSubscriptionService subscriptionService;

    /**
     * 查询订阅列表
     */
    @SaCheckPermission("miniapp:subscription:list")
    @GetMapping("/list")
    public TableDataInfo<UserSubscriptionVo> list(UserSubscriptionBo bo, PageQuery pageQuery) {
        return subscriptionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订阅列表
     */
    @SaCheckPermission("miniapp:subscription:export")
    @Log(title = "订阅管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserSubscriptionBo bo, HttpServletResponse response) {
        List<UserSubscriptionVo> list = subscriptionService.queryList(bo);
        ExcelUtil.exportExcel(list, "订阅数据", UserSubscriptionVo.class, response);
    }

    /**
     * 获取订阅详情
     */
    @SaCheckPermission("miniapp:subscription:query")
    @GetMapping("/{id}")
    public R<UserSubscriptionVo> getInfo(@PathVariable Long id) {
        return R.ok(subscriptionService.queryById(id));
    }

    /**
     * 修改订阅
     */
    @SaCheckPermission("miniapp:subscription:edit")
    @Log(title = "订阅管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        Integer status = Integer.valueOf(params.get("status").toString());
        return toAjax(subscriptionService.updateStatus(id, status));
    }

    /**
     * 删除订阅
     */
    @SaCheckPermission("miniapp:subscription:remove")
    @Log(title = "订阅管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(subscriptionService.deleteById(id));
    }
}
