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

import com.github.microservice.auth.security.annotations.ResourceAuth;
import com.github.microservice.auth.security.type.AuthType;
import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.base.IDParam;
import com.zhongshu.card.client.model.base.ProjectOidParam;
import com.zhongshu.card.client.model.org.BelongOigParam;
import com.zhongshu.card.client.model.paySetting.payConfig.FrictionlessUnionConfigParam;
import com.zhongshu.card.client.model.paySetting.payConfig.LaKaLaCollectionConfigParam;
import com.zhongshu.card.client.model.paySetting.paySetting.*;
import com.zhongshu.card.client.model.paySetting.withdrawal.OrgWithdrawalParam;
import com.zhongshu.card.client.model.payment.PaymentTypeParam;
import com.zhongshu.card.client.type.DataState;
import com.zhongshu.card.server.core.service.paySetting.ProjectPaySettingServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 机构的支付配置
 *
 * @author TRX
 * @date 2024/7/26
 */
@RestController
@RequestMapping("/project/paySetting")
@Tag(name = "机构-支付设置")
public class ProjectPaySettingController {

    @Autowired
    private ProjectPaySettingServiceImpl projectPaySettingService;

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "关联支付产品", description = "关联支付产品")
    @RequestMapping(value = "savePayChannel", method = {RequestMethod.POST})
    public ResultContent savePayChannel(@RequestBody ProjectPaySettingParam param) {
        return projectPaySettingService.savePayChannel(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "得到项目所有的支付渠道参数配置", description = "得到项目所有的支付渠道参数配置")
    @RequestMapping(value = "getAllProjectPaySetting", method = {RequestMethod.POST})
    public ResultContent<List<ProjectPaySettingModel>> getAllProjectPaySetting(@RequestBody ProjectOidParam param) {
        return projectPaySettingService.getAllProjectPaySetting(param.getProjectOid());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "删除参数配置", description = "删除参数配置")
    @RequestMapping(value = "deleteInfo", method = {RequestMethod.POST})
    public ResultContent deleteInfo(@RequestBody IDParam param) {
        return projectPaySettingService.deleteInfo(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "启用支付方式", description = "启用支付方式")
    @RequestMapping(value = "enablePay", method = {RequestMethod.POST})
    public ResultContent enablePay(@RequestBody IDParam param) {
        return projectPaySettingService.changeState(param.getId(), DataState.Enable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "停用支付方式", description = "停用支付方式")
    @RequestMapping(value = "disablePay", method = {RequestMethod.POST})
    public ResultContent disablePay(@RequestBody IDParam param) {
        return projectPaySettingService.changeState(param.getId(), DataState.Disable);
    }

    //------------------------------------------------机构支付参数配置 start-----------------------

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "数据列表-分页查询", description = "数据列表-分页查询")
    @RequestMapping(value = {"page"}, method = {RequestMethod.POST})
    public ResultContent<Page<ProjectPaySettingInfoModel>> page(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable, @Parameter(required = false) ProjectPaySettingInfoSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        return projectPaySettingService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "项目下所有主账户数据列表", description = "项目下所有主账户数据列表")
    @RequestMapping(value = {"getMainList"}, method = {RequestMethod.POST})
    public ResultContent<Page<ProjectPaySettingInfoModel>> getMainList(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) ProjectPaySettingInfoSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        param.setIsDefault(Boolean.TRUE);
        if (param.getChannelType() == null) {
            return ResultContent.buildFail("channelType不能为空");
        }
        return projectPaySettingService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "项目下所有子商户数据列表", description = "项目下所有子商户数据列表")
    @RequestMapping(value = {"getChildList"}, method = {RequestMethod.POST})
    public ResultContent<Page<ProjectPaySettingInfoModel>> getChildList(@Parameter(hidden = true) @PageableDefault(page = 0, size = 10) Pageable pageable,
            @Parameter(required = false) ProjectPaySettingInfoSearch param) {
        Assert.hasText(param.getProjectOid(), "projectOid不能为空");
        param.setIsDefault(Boolean.FALSE);
        if (param.getChannelType() == null) {
            return ResultContent.buildFail("channelType不能为空");
        }
        return projectPaySettingService.page(param, pageable);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "账户-机构支付账户列表（所有）", description = "账户-机构支付账户列表")
    @RequestMapping(value = "getOrgPaySettingInfo", method = {RequestMethod.POST})
    public ResultContent<List<ProjectPaySettingInfoModel>> getProjectPaySettingInfo(@RequestBody @Valid BelongOigParam param) {
        return projectPaySettingService.getOrgPaySettingInfo(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "账户-配置字段", description = "账户-配置字段")
    @RequestMapping(value = "getFieldByConfigId", method = {RequestMethod.POST})
    public ResultContent<PayConfigFieldModel> getFieldByConfigId(@RequestBody PaymentTypeParam param) {
        if (param.getPaymentType() == null) {
            return ResultContent.buildFail("paymentType不能为空");
        }
        return projectPaySettingService.getFieldByConfigId(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "账户-删除", description = "账户-删除")
    @RequestMapping(value = "deletePaySettingInfo", method = {RequestMethod.POST})
    public ResultContent deletePaySettingInfo(@RequestBody IDParam param) {
        return projectPaySettingService.deletePaySettingInfo(param.getId());
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "账户-详情", description = "账户-详情")
    @RequestMapping(value = "getPaySettingInfo", method = {RequestMethod.POST})
    public ResultContent getPaySettingInfo(@RequestBody IDParam param) {
        return projectPaySettingService.getPaySettingInfo(param.getId());
    }

    //-----------------------------提供的支付渠道参数保存 start -----------------------
    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "保存项目的微信支付设置", description = "保存项目的微信支付设置")
    @RequestMapping(value = "saveWxPayConfig", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = {RequestMethod.POST})
    public ResultContent saveWxPayConfig(WxPayConfigParam param) {
        return projectPaySettingService.saveWxPayConfig(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "保存项目的拉卡拉支付设置", description = "保存项目的拉卡拉支付设置")
    @RequestMapping(value = "saveLaKaLaConfig", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, method = RequestMethod.POST)
    public ResultContent saveLaKaLaConfig(LaKaLaCollectionConfigParam param) {
        return projectPaySettingService.saveLaKaLaConfig(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "保存银联云闪付支付配置", description = "保存银联云闪付支付配置")
    @RequestMapping(value = "saveUnionPayConfig", method = RequestMethod.POST)
    public ResultContent saveUnionPayConfig(@RequestBody @Valid FrictionlessUnionConfigParam param) {
        return projectPaySettingService.saveUnionPayConfig(param);
    }

    @ResourceAuth(value = "user", type = AuthType.User)
    @Operation(summary = "选择机构查询提现的数据", description = "选择机构查询提现的数据")
    @RequestMapping(value = {"getOrgWithdrawal"}, method = {RequestMethod.POST})
    public ResultContent getOrgWithdrawal(@RequestBody OrgWithdrawalParam param) {
        return projectPaySettingService.getOrgWithdrawal(param);
    }

}
