package com.crm.pay.controller;

import com.crm.pay.service.FinanceTransferService;
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.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.entity.user.info.UserInfoEntity;
import com.crm.rely.backend.model.dto.finance.transfer.FinanceTransferSearchDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.finance.transfer.FinanceTransferAddEntity;
import com.crm.rely.backend.model.entity.finance.transfer.FinanceTransferDeleteEntity;
import com.crm.rely.backend.model.entity.finance.transfer.FinanceTransferSearchEntity;
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("/transfer")
public class FinanceTransferController {


    @Autowired
    private FinanceTransferService financeTransferService;

    @PostMapping("/apply")
    public BaseResultDto apply(@RequestBody @Validated FinanceTransferAddEntity entity, CustomInfoEntity infoEntity)
            throws Exception {
        if (entity.getSourceType().equals(1)) {
            if (entity.getWithdrawLogin() == null) {
                return BaseResultDto.error(Constants.PARAMETER_ERROR);
            }
            if (entity.getDepositLogin().equals(entity.getWithdrawLogin())) {

                return BaseResultDto.error(Constants.PARAMETER_ERROR);
            }
        }

        entity.setCId(infoEntity.getCId());
        entity.setCustomId(infoEntity.getId());

        return financeTransferService.save(entity);
    }

    @PostMapping("/search/list")
    public ResultWithPagerDto accountApplyData(@RequestBody FinanceTransferSearchEntity entity, CustomInfoEntity infoEntity) throws Exception {

        entity.setCustomId(infoEntity.getId());
        return financeTransferService.searchList(entity);
    }

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

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

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }

        ResultWithPagerDto<FinanceTransferSearchDto> resultWithPagerDto = financeTransferService.searchList(entity);
        return resultWithPagerDto;
    }

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

}
