package com.crm.manager.controller;

import com.crm.manager.service.FinanceWithdrawFeignService;
import com.crm.rely.backend.core.constant.Constants;
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.finance.withdraw.FinanceWithdrawApproveEntity;
import com.crm.rely.backend.model.entity.finance.withdraw.FinanceWithdrawDeleteEntity;
import com.crm.rely.backend.model.entity.finance.withdraw.FinanceWithdrawSearchListEntity;
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("/finance/withdraw")
public class FinanceWithdrawController {

    @Autowired
    private FinanceWithdrawFeignService financeWithdrawFeignService;

    @PostMapping("/approve")
    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);
        }

        return financeWithdrawFeignService.approve(entity);
    }

    @PostMapping("/delete")
    public BaseResultDto delete(@RequestBody @Validated FinanceWithdrawDeleteEntity entity) throws Exception {

        return financeWithdrawFeignService.delete(entity);
    }

    @PostMapping("/search/list")
    public BaseResultDto searchList(@RequestBody @Validated FinanceWithdrawSearchListEntity entity) throws Exception {

        ResultWithPagerDto<FinanceWithdrawDto> resultWithPagerDto = financeWithdrawFeignService.searchList(entity);
        return resultWithPagerDto;
    }

    @PostMapping("/search/single")
    public BaseResultDto searchSingle(@RequestBody @Validated SingleLongEntity entity) throws Exception {
        BaseResultDto<FinanceWithdrawDto> resultDto = financeWithdrawFeignService.searchSingle(entity);
        return resultDto;
    }

}
