package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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.dto.PlatformShopMembershipDeleteRequest;
import shop.alien.entity.store.vo.PlatformShopMembershipAdminRowVo;
import shop.alien.store.service.PlatformShopMembershipAdminMutationService;
import shop.alien.store.service.PlatformShopMembershipAdminQueryService;

import javax.validation.Valid;

/**
 * 平台端 — 已开通会员卡的店铺列表
 */
@Slf4j
@Api(tags = {"平台-店铺会员卡商户列表"})
@CrossOrigin
@RestController
@RequestMapping("/platformShopMembershipAdmin")
@RequiredArgsConstructor
public class PlatformShopMembershipAdminController {

    private final PlatformShopMembershipAdminQueryService adminQueryService;
    private final PlatformShopMembershipAdminMutationService adminMutationService;

    @ApiOperation("分页查询（门店名称/联系人/电话模糊；状态：0正常 1即将到期(一个月内) 2已过期）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "门店名、联系人、电话", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "membershipUiStatus", value = "0正常 1即将到期(一个月内) 2已过期", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "expiryStart", value = "到期时间起 yyyy-MM-dd", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "expiryEnd", value = "到期时间止 yyyy-MM-dd", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", paramType = "query", dataType = "int")
    })
    @GetMapping("/storePage")
    public R<IPage<PlatformShopMembershipAdminRowVo>> storePage(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer membershipUiStatus,
            @RequestParam(required = false) String expiryStart,
            @RequestParam(required = false) String expiryEnd,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return R.data(adminQueryService.pageStores(keyword, membershipUiStatus, expiryStart, expiryEnd, pageNum, pageSize));
    }

    @ApiOperation("导出 Excel（筛选条件与分页列表一致，导出全部匹配行）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "门店名、联系人、电话", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "membershipUiStatus", value = "0正常 1即将到期(一个月内) 2已过期", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "expiryStart", value = "到期时间起 yyyy-MM-dd", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "expiryEnd", value = "到期时间止 yyyy-MM-dd", paramType = "query", dataType = "String")
    })
    @GetMapping("/storeExport")
    public ResponseEntity<byte[]> storeExport(
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer membershipUiStatus,
            @RequestParam(required = false) String expiryStart,
            @RequestParam(required = false) String expiryEnd) {
        return adminQueryService.exportStores(keyword, membershipUiStatus, expiryStart, expiryEnd);
    }

    @ApiOperation("逻辑删除店铺会员卡（单个传 1 个 storeId；批量传多个。仅删除 platform_shop_membership 当前权益记录，不删订单）")
    @PostMapping("/membership/logicDelete")
    public R<Integer> logicDeleteMembership(@Valid @RequestBody PlatformShopMembershipDeleteRequest body) {
        try {
            return R.data(adminMutationService.logicDeleteByStoreIds(body.getStoreIds()));
        } catch (IllegalArgumentException e) {
            return R.fail(e.getMessage());
        }
    }
}
