package com.zhongshu.card.server.core.controller.projectAbout;

import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.type.AuthType;
import com.zhongshu.card.client.model.base.IDParam;
import com.zhongshu.card.client.model.base.IDsParam;
import com.zhongshu.card.client.model.base.OidModel;
import com.zhongshu.card.client.model.base.ProjectOidParam;
import com.zhongshu.card.client.model.school.*;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.type.CardCancelState;
import com.zhongshu.card.server.core.service.projectAbout.CardInfoPoolService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 项目的卡池管理
 *
 * @author TRX
 * @date 2024/6/5
 */
@RestController
@RequestMapping("/org/cardPool")
@Tag(name = "项目-卡片池管理")
public class CardInfoPoolController {

    @Autowired
    CardInfoPoolService cardInfoPoolService;

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "保存卡池卡片信息", description = "保存卡池卡片信息 新增加编辑")
    @RequestMapping(value = {"saveCardInfoPool"}, method = {RequestMethod.POST})
    public ResultContent saveCardInfoPool(@RequestBody CardInfoPoolParam param) {
        return cardInfoPoolService.saveCardInfoPool(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "池卡卡片列表-分页查询", description = "池卡卡片列表-分页查询")
    @RequestMapping(value = {"page"}, method = {RequestMethod.POST})
    public ResultContent<Page<CardInfoPoolModel>> page(
            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) CardInfoPoolSearch param) {
        return cardInfoPoolService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "池卡卡片详情", description = "池卡卡片详情")
    @RequestMapping(value = {"getCardInfoPoolDetail"}, method = {RequestMethod.POST})
    public ResultContent<CardInfoPoolModel> getCardInfoPoolDetail(@RequestBody IDParam param) {
        return cardInfoPoolService.getCardInfoPoolDetail(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "池卡恢复卡片", description = "池卡恢复卡片")
    @RequestMapping(value = {"restoreCardInfoPool"}, method = {RequestMethod.POST})
    public ResultContent<CardInfoPoolModel> restoreCardInfoPool(@RequestBody CardInfoPoolRestoreParam param) {
        return cardInfoPoolService.restoreCardInfoPool(param);
    }

    // -----------------------作废 -----------------------
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "作废恢复", description = "作废恢复")
    @RequestMapping(value = {"cancelRestore"}, method = {RequestMethod.POST})
    public ResultContent<CardInfoPoolModel> cancelRestore(
            @RequestBody CardInfoPoolChangeParam param) {
        param.setCancelState(CardCancelState.Common);
        return cardInfoPoolService.cancelStateChange(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "作废", description = "作废")
    @RequestMapping(value = {"cancelCard"}, method = {RequestMethod.POST})
    public ResultContent cancelCard(
            @RequestBody CardInfoPoolChangeParam param) {
        param.setCancelState(CardCancelState.Cancel);
        return cardInfoPoolService.cancelStateChange(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "池卡删除卡片", description = "池卡删除卡片")
    @RequestMapping(value = {"deleteCardInfoPool"}, method = {RequestMethod.POST})
    public ResultContent<CardInfoPoolModel> deleteCardInfoPool(@RequestBody IDParam param) {
        Assert.hasText(param.getId(), "id不能为空");
        return cardInfoPoolService.deleteCardInfoPool(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "池卡批量删除卡片", description = "池卡批量删除卡片")
    @RequestMapping(value = {"deleteByIds"}, method = {RequestMethod.POST})
    public ResultContent<CardInfoPoolModel> deleteByIds(@RequestBody @Valid IDsParam param) {
        return cardInfoPoolService.deleteByIds(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @RequestMapping(value = "importCardPool", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = RequestMethod.POST)
    @Operation(summary = "园区卡池导入", description = "园区卡池导入")
    public ResultContent<ImportResultModel> importCardPool(HttpServletRequest request,
            HttpServletResponse response,
            @RequestPart("file") MultipartFile file,
            ProjectOidParam param1) throws IOException {
        if (file == null || file.isEmpty()) {
            return ResultContent.buildFail("文件不能为空");
        }
        OidModel param = new OidModel();
        param.setOid(param1.getProjectOid());
        return cardInfoPoolService.importCardPool(request, response, file, param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @RequestMapping(value = "exportProjectCardPool", method = RequestMethod.POST)
    @Operation(summary = "导出项目卡片", description = "导出项目卡片")
    public void exportProjectCardPool(HttpServletRequest request, HttpServletResponse response,
            CardInfoPoolSearch param) throws IOException {
        cardInfoPoolService.exportProjectCard(request, response, param);
    }

    @Operation(summary = "池卡搜索卡片（可用）", description = "池卡搜索卡片")
    @RequestMapping(value = {"queryPoolCard"}, method = {RequestMethod.POST})
    public ResultContent<List<CardInfoPoolSimpleModel>> queryPoolCard(
            @RequestBody @Valid CardInfoPoolQueryParam param) {
        return cardInfoPoolService.queryPoolCard(param);
    }

}
