package com.crm.pay.controller;

import com.crm.pay.service.FinanceTrainService;
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.BaseReportResultDto;
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.train.FinanceTrainDto;
import com.crm.rely.backend.model.dto.report.FinanceTrainReportListDto;
import com.crm.rely.backend.model.dto.report.FinanceTrainReportSumDto;
import com.crm.rely.backend.model.dto.report.FinanceTrainSalesReportListDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.custom.login.CustomLoginCheckByLoginEntity;
import com.crm.rely.backend.model.entity.finance.train.FinanceTrainAddEntity;
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;

import java.util.List;


@RestController
@RequestMapping("/finance/train")
public class FinanceTrainController {

    @Autowired
    private FinanceTrainService service;

    @PostMapping("/custom/validate/login")
    @FeignClientAnnotation
    public BaseResultDto<Boolean> validateLogin(@RequestBody @Validated CustomLoginCheckByLoginEntity entity) throws Exception {
        return BaseResultDto.success(service.validateLogin(entity));
    }

    @RequestMapping("/custom/pay")
    @FeignClientAnnotation
    public BaseResultDto trainPay(@RequestBody @Validated FinanceTrainAddEntity entity, CustomInfoEntity infoEntity) throws Exception {
        entity.setCustomId(infoEntity.getId());
        service.add(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/manager/approve")
    @FeignClientAnnotation
    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
            , CustomInfoEntity infoEntity) throws Exception {
        entity.setCustomId(infoEntity.getId());
        ResultWithPagerDto<FinanceTrainDto> resultWithPagerDto = service.searchPageList(entity);
        return resultWithPagerDto;
    }

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

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }
        ResultWithPagerDto<FinanceTrainDto> resultWithPagerDto = service.searchPageList(entity);
        return resultWithPagerDto;
    }

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

    @PostMapping("/manager/report")
    @FeignClientAnnotation
    public BaseReportResultDto<List<FinanceTrainReportListDto>, FinanceTrainReportSumDto> report(@RequestBody @Validated FinanceTrainSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {
        return service.report(entity);
    }

    @PostMapping("/manager/report/export")
    @FeignClientAnnotation
    public BaseResultDto<List<FinanceTrainReportListDto>> reportExport(@RequestBody @Validated FinanceTrainSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {
        return service.reportExport(entity);
    }

    @PostMapping("/manager/commission")
    @FeignClientAnnotation
    public BaseReportResultDto<List<FinanceTrainSalesReportListDto>, FinanceTrainReportSumDto> salesReport(@RequestBody @Validated FinanceTrainSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {
        return service.salesReport(entity);
    }

    @PostMapping("/manager/commission/export")
    @FeignClientAnnotation
    public BaseResultDto<List<FinanceTrainSalesReportListDto>> salesReportExport(@RequestBody @Validated FinanceTrainSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {
        return service.salesReportExport(entity);
    }

}