package com.crm.manager.controller;

import com.crm.manager.service.FinanceTrainFeignService;
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.train.FinanceTrainDto;
import com.crm.rely.backend.model.entity.finance.train.FinanceTrainApproveEntity;
import com.crm.rely.backend.model.entity.finance.train.FinanceTrainSearchListEntity;
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/train")
public class FinanceTrainController {

    @Autowired
    private FinanceTrainFeignService service;


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

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

    @PostMapping("/search/list")
    public ResultWithPagerDto<FinanceTrainDto> searchList(@RequestBody @Validated FinanceTrainSearchListEntity entity) throws Exception {

        ResultWithPagerDto<FinanceTrainDto> resultWithPagerDto = service.searchList(entity);
        return resultWithPagerDto;
    }

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

}