package com.crm.pay.controller;

import com.crm.pay.service.FinanceWithdrawService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.constant.PrefixEnum;
import com.crm.rely.backend.core.constant.StatusConstants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.model.dto.finance.withdraw.FinanceWithdrawDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.finance.withdraw.FinanceWithdrawApproveEntity;
import com.crm.rely.backend.model.entity.finance.withdraw.FinanceWithdrawBankAddEntity;
import com.crm.rely.backend.model.entity.finance.withdraw.FinanceWithdrawDeleteEntity;
import com.crm.rely.backend.model.entity.finance.withdraw.FinanceWithdrawSearchListEntity;
import com.crm.rely.backend.model.entity.user.info.UserInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 出金
 */
@RestController
@RequestMapping("/withdraw")
public class FinanceWithdrawController {

    @Autowired
    private FinanceWithdrawService financeWithdrawService;


    @PostMapping("/apply/bank")
    public BaseResultDto bank(@RequestBody @Validated FinanceWithdrawBankAddEntity entity, CustomInfoEntity infoEntity)
            throws Exception {

        entity.setCId(infoEntity.getCId());
        entity.setCustomId(infoEntity.getId());
//        entity.setPayType(PayConstants.UNION_PAY_TELEGRAPHIC_REMIT);
        if (entity.getSourceType().equals(1) && entity.getLogin() == null) {
            return BaseResultDto.error(Constants.PARAMETER_ERROR);
        }
        if (entity.getSourceType().compareTo(1) < 0 || entity.getSourceType().compareTo(3) > 0) {
            return BaseResultDto.error(Constants.PARAMETER_ERROR);
        }
        return financeWithdrawService.save(entity);
    }

    //
//    @PostMapping("/apply/bank/telegraphic")
//    public BaseResultDto bankTelegraphic(@RequestBody @Validated FinanceWithdrawBankTelegraphicAddEntity entity,
//    InfoEntity infoEntity)
//            throws Exception {
//
//        if (!validateLogin(entity)) {
//            return BaseResultDto.error(PayConstant.PAY_LOGIN_NOT_EMPTY);
//        }
//        validatedCloseFunction(infoEntity.getCustomInfo().getId(), entity.getLogin(), Constants
//        .CLOSE_FUNCTION_WITHDRAW);
//
//        entity.setCId(infoEntity.getCustomInfo().getCId());
//        entity.setCustomId(infoEntity.getCustomInfo().getId());
//        entity.setChannelType(PayConstant.CHANNEL_TYPE_BANK_TELEGRAPHIC);
//
//
//        return financeWithdrawService.save(entity);
//    }
//
//    @PostMapping("/apply/digital/currency")
//    public BaseResultDto digitalCurrency(@RequestBody @Validated FinanceWithdrawDigitalCurrencyAddEntity entity,
//    CustomInfoEntity infoEntity)
//            throws Exception {
//
//        entity.setCId(infoEntity.getCId());
//        entity.setCustomId(infoEntity.getId());
//        entity.setChannelType(PayConstant.CHANNEL_TYPE_DIGITAL_CURRENCY);
//
//        return financeWithdrawService.save(entity);
//    }
    @PostMapping("/search/list")
    public ResultWithPagerDto<FinanceWithdrawDto> searchCustomList(@RequestBody @Validated FinanceWithdrawSearchListEntity entity, CustomInfoEntity infoEntity) throws Exception {

        entity.setCustomId(infoEntity.getId());
        ResultWithPagerDto<FinanceWithdrawDto> resultWithPagerDto = financeWithdrawService.searchList(entity);
        return resultWithPagerDto;
    }

    @PostMapping("/manager/approve")
    @FeignClientAnnotation
    public BaseResultDto approve(@RequestBody @Validated FinanceWithdrawApproveEntity entity) throws Exception {
        if (!StatusConstants.PASSED_STATUS.equals(entity.getStatus()) && !StatusConstants.REJECTED_STATUS.equals(entity.getStatus())) {
            return BaseResultDto.error(Constants.NOT_PERMIT);
        }

        financeWithdrawService.approve(entity);
        return BaseResultDto.success();
    }


    @PostMapping("/manager/delete")
    @FeignClientAnnotation
    public BaseResultDto delete(@RequestBody @Validated FinanceWithdrawDeleteEntity entity) throws Exception {
        financeWithdrawService.delete(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/search/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<FinanceWithdrawDto> searchList(@RequestBody @Validated FinanceWithdrawSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }
        ResultWithPagerDto<FinanceWithdrawDto> resultWithPagerDto = financeWithdrawService.searchList(entity);
        return resultWithPagerDto;
    }

    @PostMapping("/manager/search/single")
    @FeignClientAnnotation
    public BaseResultDto<FinanceWithdrawDto> searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        FinanceWithdrawDto financeWithdrawDto = financeWithdrawService.searchSingle(entity.getId());
        return BaseResultDto.success(financeWithdrawDto);
    }
}
