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

import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.helper.AuthHelper;
import com.github.microservice.auth.security.type.AuthType;
import com.zhongshu.card.client.model.base.CodeParam;
import com.zhongshu.card.client.model.base.IDParam;
import com.zhongshu.card.client.model.base.OidModel;
import com.zhongshu.card.client.model.base.ProjectOidParam;
import com.zhongshu.card.client.model.org.OrgUserDetailParam;
import com.zhongshu.card.client.model.school.*;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.service.school.CardInfoService;
import com.zhongshu.card.client.service.school.SchoolUserService;
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.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("/school/card")
@Tag(name = "项目-卡片管理")
public class CardController {

    @Autowired
    private SchoolUserService schoolUserService;

    @Autowired
    private CardInfoService cardInfoService;

    @Autowired
    private AuthHelper authHelper;

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到当前用户所在项目-机构的所有卡片", description = "得到当前用户所在项目-机构的所有卡片")
    @RequestMapping(value = {"getCurrentUserOrgAll"}, method = {RequestMethod.GET})
    public ResultContent<List<CardInfoModel>> getCurrentUserOrgAll(OidModel param) {
        return cardInfoService.getCurrentUserOrgAll(param.getOid());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "查询机构用户卡变列表", description = "查询机构用户卡变列表")
    @RequestMapping(value = {"getOrgCardList"}, method = {RequestMethod.POST})
    public ResultContent<List<CardInfoModel>> getOrgCardList(@RequestBody @Valid IDParam param) {
        return cardInfoService.getOrgCardList(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到当前用户所有卡片", description = "得到当前用户所有卡片(不区分机构)")
    @RequestMapping(value = {"getCurrentUserAllCard"}, method = {RequestMethod.GET})
    public ResultContent<List<CardInfoModel>> getCurrentUserAllCard() {
        return schoolUserService.getCurrentUserAllCard();
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到指定用户在机构的所有卡片", description = "得到指定用户在学校的所有卡片")
    @RequestMapping(value = {"getUserOrgAllCard"}, method = {RequestMethod.POST})
    public ResultContent<List<CardInfoModel>> getUserOrgAllCard(@RequestBody @Valid OrgUserDetailParam param) {
        List<CardInfoModel> cardInfoModels = cardInfoService.getUserOrgAllCard(param.getUserId(), param.getOid());
        return ResultContent.buildSuccess(cardInfoModels);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "用户添加-编辑卡片信息", description = "用户添加-编辑卡片信息")
    @RequestMapping(value = {"addCardInfo"}, method = {RequestMethod.POST})
    public ResultContent addCardInfo(@RequestBody CardInfoParam param) {
        param.setUserId(authHelper.getCurrentUser().getUserId());
        return cardInfoService.addCardInfo(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除卡片", description = "用户添加-编辑卡片信息")
    @RequestMapping(value = {"deleteCardInfo"}, method = {RequestMethod.POST})
    public ResultContent deleteCardInfo(@RequestBody IDParam param) {
        return cardInfoService.deleteCardInfo(param.getId());
    }


    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "挂失卡片（通过数据ID挂失自己的）", description = "挂失卡片（通过数据ID挂失自己的）")
    @RequestMapping(value = {"cardLossById"}, method = {RequestMethod.POST})
    public ResultContent cardLossById(@RequestBody CardLossParam param) {
        return cardInfoService.cardLossById(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "挂失卡片（通过卡片code挂失别人的）", description = "挂失卡片（通过卡片code挂失别人的）")
    @RequestMapping(value = {"cardLossByCode"}, method = {RequestMethod.POST})
    public ResultContent cardLossByCode(@RequestBody CardLossParam param) {
        param.setVerifyPassword(Boolean.TRUE);
        return cardInfoService.cardLossByIccId(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "挂失卡片（管理员操作挂失卡片）", description = "挂失卡片（管理员操作挂失卡片）")
    @RequestMapping(value = {"managerCardLossByCode"}, method = {RequestMethod.POST})
    public ResultContent managerCardLossByCode(@RequestBody CardLossParam param) {
        param.setVerifyPassword(Boolean.FALSE);
        return cardInfoService.cardLossByIccId(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "卡片启用", description = "卡片启用")
    @RequestMapping(value = {"cardEnableById"}, method = {RequestMethod.POST})
    public ResultContent cardEnableById(@RequestBody CardLossParam param) {
        return cardInfoService.cardEnableById(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "卡片启用(帮别人启用)", description = "卡片启用")
    @RequestMapping(value = {"cardEnableByCode"}, method = {RequestMethod.POST})
    public ResultContent cardEnableByCode(@RequestBody CardLossParam param) {
        param.setVerifyPassword(Boolean.TRUE);
        return cardInfoService.cardEnableByIccId(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "卡片启用(管理员操作启用)", description = "卡片启用")
    @RequestMapping(value = {"managerCardEnableByCode"}, method = {RequestMethod.POST})
    public ResultContent managerCardEnableByCode(@RequestBody CardLossParam param) {
        param.setVerifyPassword(Boolean.FALSE);
        return cardInfoService.cardEnableByIccId(param);
    }

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

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "code查询卡片详情", description = "code查询卡片详情")
    @RequestMapping(value = {"getCardDetailByCode"}, method = {RequestMethod.GET})
    public ResultContent<CardInfoSimpleModel> getCardDetailByCode(@Parameter(name = "code", description = "code", required = true) @RequestParam(name = "code") String code) {
        return cardInfoService.getCardDetailByCode(code);
    }

    //----------------------------园区卡片操作 start ------------------------
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "卡片列表-分页查询（园区使用）", description = "卡片列表-分页查询")
    @RequestMapping(value = {"page"}, method = {RequestMethod.POST})
    public ResultContent<Page<CardInfoModel>> page(
            @Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) CardInfoSearch param) {
        return cardInfoService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "挂失卡片（园区操作）", description = "挂失卡片（园区操作）")
    @RequestMapping(value = {"lossById"}, method = {RequestMethod.POST})
    public ResultContent lossById(@RequestBody IDParam param) {
        return cardInfoService.lossById(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "挂失启用（园区操作）", description = "挂失启用（园区操作）")
    @RequestMapping(value = {"enableById"}, method = {RequestMethod.POST})
    public ResultContent enableById(@RequestBody IDParam param) {
        return cardInfoService.enableById(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "作废卡片（园区操作）", description = "作废卡片（园区操作）")
    @RequestMapping(value = {"cardCancel"}, method = {RequestMethod.POST})
    public ResultContent cardCancel(@RequestBody IDParam param) {
        return cardInfoService.cardCancel(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "挂恢复作废（园区操作）", description = "挂恢复作废（园区操作）")
    @RequestMapping(value = {"restoreCardCancel"}, method = {RequestMethod.POST})
    public ResultContent restoreCardCancel(@RequestBody IDParam param) {
        return cardInfoService.restoreCardCancel(param.getId());
    }

    //----------------------------园区卡片操作 end --------------------------

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "从卡池选择卡片绑定到个人(已登录)", description = "从卡池选择卡片绑定到个人")
    @RequestMapping(value = {"bindCardFromPool"}, method = {RequestMethod.POST})
    public ResultContent bindCardFromPool(@RequestBody @Valid CardInfoPoolBind2UserParam param) {
        return cardInfoService.bindCardFromPool(param.getPoolId(), param.getUserId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "从卡池选择卡片绑定机构用户", description = "从卡池选择卡片绑定机构用户")
    @RequestMapping(value = {"bindCardFromPoolByOrgId"}, method = {RequestMethod.POST})
    public ResultContent bindCardFromPoolByOrgId(@RequestBody @Valid CardInfoPoolBind2OrgUserParam param) {
        return cardInfoService.bindCardFromPoolByOrgId(param.getPoolId(), param.getId(), "");
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "根据卡号绑定（小程序使用）", description = "根据卡号绑定")
    @RequestMapping(value = {"bindByCode"}, method = {RequestMethod.POST})
    public ResultContent bindByCode(@RequestBody @Valid CodeParam param) {
        return cardInfoService.bindByCode(param.getCode());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "解除绑定", description = "解除绑定")
    @RequestMapping(value = {"unBindCard"}, method = {RequestMethod.POST})
    public ResultContent<CardInfoModel> unBindCard(@RequestBody IDParam param) {
        return cardInfoService.unBindCard(param.getId());
    }

    @Operation(summary = "从卡池选择卡片绑定到个人（未登录）", description = "从卡池选择卡片绑定到个人")
    @RequestMapping(value = {"bindCardFromPoolNotLogged"}, method = {RequestMethod.POST})
    public ResultContent bindCardFromPoolNotLogged(@RequestBody @Valid CardInfoPoolBind2UserParam param) {
        return cardInfoService.bindCardFromPool(param.getPoolId(), param.getUserId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @RequestMapping(value = "importCard", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = RequestMethod.POST)
    @Operation(summary = "导入用户和卡片的关系", description = "导入用户和卡片的关系")
    public ResultContent<ImportResultModel> importCard(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 cardInfoService.importCard(request, response, file, param);
    }

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

}
