package com.dayou.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.dayou.annotation.DataPermission;
import com.dayou.dto.FinanceRealFundExportDTO;
import com.dayou.dto.FinanceRealNotClaimDTO;
import com.dayou.utils.ExcelOneToManyExportUtils;
import com.dayou.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.RestController;
import com.dayou.service.IFinanceRealFundService;
import com.dayou.entity.FinanceRealFund;
import com.dayou.common.RestResponse;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static com.dayou.common.Constants.FINANCE_DEPARTMENT;
import static com.dayou.common.Constants.MANAGER_OFFICE;

/**
 * 财务实收款
 *
 * @author wucl
 * @since 2023-12-08
 */
@RestController
@RequestMapping("financeRealFund")
@Slf4j
public class FinanceRealFundController extends BaseController {
    @Autowired
    private IFinanceRealFundService financeRealFundService;

    /**
    * 财务实收款列表
    */
    @GetMapping("")
    public RestResponse<Page<FinanceRealFundVO>> page(FinanceRealFund financeRealFund, Page page,String keyword){
        Page<FinanceRealFundVO> pages=financeRealFundService.selectPage(page,financeRealFund,keyword);
        return RestResponse.data(pages);
    }

    /**
     * 财务实收认领列表
     */
    @GetMapping("/claim")
    public RestResponse<Page<FinanceRealFundVO>> claimPage(FinanceRealFund financeRealFund, Page page,String keyword){
        Page<FinanceRealFundVO> pages=financeRealFundService.claimPage(page,financeRealFund,keyword);
        return RestResponse.data(pages);
    }

    /**
     * 财务实收款详情
     */
    @GetMapping("/{id}")
    public RestResponse<FinanceRealFund> detail(@PathVariable Long id){
        FinanceRealFund xFinanceRealFund =financeRealFundService.detail(id);
        return RestResponse.data(xFinanceRealFund);
     }

    /**
     * 财务实收款新增
     */
    @PostMapping("")
    public RestResponse<Boolean> save(@RequestBody FinanceRealFund financeRealFund) {
        Boolean ret = financeRealFundService.add(financeRealFund);
        return RestResponse.data(ret);
    }

    /**
     * 财务实收款更新
     */
    @PutMapping("")
    public RestResponse<Boolean> update(@RequestBody FinanceRealFund financeRealFund) {
        Boolean ret = financeRealFundService.update(financeRealFund);
        return RestResponse.data(ret);
    }

    /**
     * 财务实收款删除
     */
    @DeleteMapping("/{id}")
    public RestResponse<Boolean> delete(@PathVariable Long id) {
        Boolean ret = financeRealFundService.delete(id);
        return RestResponse.data(ret);
    }

    /**
     * 大中型产品逾期未收款列表
     * @param overdueVO
     * @param page
     * @return
     */
    @DataPermission(postChild = false,ignoreDepartment = {MANAGER_OFFICE,FINANCE_DEPARTMENT})
    @GetMapping("/overdue/major")
    public RestResponse<Page<MajorProdOverdueVO>> overdueMajor(MajorProdOverdueVO overdueVO, Page page){
        Page<MajorProdOverdueVO> result = financeRealFundService.overdueMajor(overdueVO,page);
        return RestResponse.data(result);
    }



    /**
     * 大中型产品逾期未收款列表导出
     * @param overdueVO
     * @return
     */
    @DataPermission(postChild = false,ignoreDepartment = {MANAGER_OFFICE,FINANCE_DEPARTMENT})
    @GetMapping("/overdue/major/export")
    public void overdueMajorExport(MajorProdOverdueVO overdueVO, HttpServletResponse response)throws IOException {
        List<MajorProdOverdueVO> result = financeRealFundService.overdueMajorExport(overdueVO);
        exportPlus(response,"大中型产品逾期未收款列表",result, MajorProdOverdueVO.class);
    }

    /**
     * 大中型评估人员结算产品
     * @param settleVO
     * @param page
     * @return
     */
    @GetMapping("/major/evaluator/settle/prod")
    public RestResponse<Page<SettleMajorProductionVO>> majorEvaluatorSettleProd(SettleMajorProductionVO settleVO, Page page,String keyword){
        Page<SettleMajorProductionVO> result = financeRealFundService.majorEvaluatorSettleProd(settleVO,page,keyword);
        return RestResponse.data(result);
    }

    /**
     * 大中型评估人员结算产品导出
     * @param production
     * @return
     */
    @GetMapping("/major/evaluator/settle/export")
    public void majorEvaluatorSettleExport(SettleMajorProductionVO production,Page page, HttpServletResponse response,String keyword) throws IOException, NoSuchFieldException, IllegalAccessException {
        List<SettleMajorProductionVO> result = financeRealFundService.majorEvaluatorSettleExport(production,page,keyword);
        exportPlus(response,"大中型评估人员结算项目",result, SettleMajorProductionVO.class);
//        ExcelOneToManyExportUtils.exportOneToMany(response,result, SettleMajorProductionVO.class, SettleProInvoiceVo.class,"大中型评估人员结算项目", "invoices");
    }

    /**
     * 大中型市场人员结算产品
     * @param settleVO
     * @param page
     * @return
     */
    @GetMapping("/major/market/settle/prod")
    public RestResponse<Page<SettleMajorProductionVO>> majorMarketSettleProd(SettleMajorProductionVO settleVO, Page page,String keyword){
        Page<SettleMajorProductionVO> result = financeRealFundService.majorMarketSettleProd(settleVO,page,keyword);
        return RestResponse.data(result);
    }

    /**
     * 大中型市场人员结算产品导出
     * @param production
     * @return
     */
    @GetMapping("/major/market/settle/export")
    public void majorMarketSettleExport(SettleMajorProductionVO production,Page page, HttpServletResponse response,String keyword) throws IOException, NoSuchFieldException, IllegalAccessException {
        List<SettleMajorProductionVO> result = financeRealFundService.majorMarketSettleExport(production,page,keyword);
        exportPlus(response,"大中型市场人员结算项目",result, SettleMajorProductionVO.class);
//        ExcelOneToManyExportUtils.exportOneToMany(response,result, SettleMajorProductionVO.class, SettleProInvoiceVo.class,"大中型市场人员结算项目", "invoices");
    }

    /**
     * 大中型评估人员总提成列表
     * @param vo
     * @return
     */
    @DataPermission(ignoreDepartment= {MANAGER_OFFICE,FINANCE_DEPARTMENT})
    @GetMapping("/commission/major/evaluator")
    public RestResponse<List<UserCommissionTotalAmountVO>> majorEvaluatorCommission(UserCommissionTotalAmountVO vo){
        List<UserCommissionTotalAmountVO> ret = financeRealFundService.majorEvaluatorCommission(vo);
        return RestResponse.data(ret);
    }

    /**
     * 大中型评估人员总提成列表
     * @param vo
     * @return
     */
    @GetMapping("/commission/major/evaluator/detail")
    public RestResponse<List<UserCommissionProdVO>> majorEvaluatorCommissionDetail(UserCommissionProdVO vo){
        List<UserCommissionProdVO> ret = financeRealFundService.majorEvaluatorCommissionDetail(vo);
        return RestResponse.data(ret);
    }

    /**
     * 大中型评估人员总提成导出
     * @param vo
     * @return
     */
    @GetMapping("/major/evaluator/commission/total/export")
    public void majorEvaluatorTotalAmountExport(@Valid UserCommissionTotalAmountVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionTotalAmountVO> result = financeRealFundService.majorEvaluatorTotalAmountExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        exportPlus(response,"大中型评估人员总提成金额"+ startDate +"至"+ endDate,result, UserCommissionTotalAmountVO.class);
    }

    /**
     * 大中型评估人员总提成导出
     * @param vo
     * @return
     */
    @GetMapping("/major/evaluator/commission/current/export")
    public void evaluatorCurUserDetailExport(@Valid UserCommissionProdVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionProdVO> result = financeRealFundService.evaluatorCurUserDetailExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        String userName = "";
        if (CollectionUtil.isNotEmpty(result)){
            userName = result.get(0).getUserName();
        }
        exportPlus(response,userName+"_大中型评估人员提成详情"+ startDate +"至"+ endDate,result, UserCommissionProdVO.class);
    }

    /**
     * 大中型评估所有人员总提成导出
     * @param vo
     * @return
     */
    @GetMapping("/major/evaluator/commission/all/export")
    public void evaluatorAllUserDetailExport(@Valid UserCommissionProdVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionProdVO> result = financeRealFundService.evaluatorAllUserDetailExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        exportPlus(response,"大中型评估人员提成详情"+ startDate +"至"+ endDate,result, UserCommissionProdVO.class);
    }

    /**
     * 大中型市场人员总提成列表
     * @param vo
     * @return
     */
    @DataPermission(postChild = false,ignoreDepartment = {MANAGER_OFFICE,FINANCE_DEPARTMENT})
    @GetMapping("/commission/major/market")
    public RestResponse<List<UserCommissionTotalAmountVO>> majorMarketCommission(UserCommissionTotalAmountVO vo){
        List<UserCommissionTotalAmountVO> ret = financeRealFundService.majorMarketCommission(vo);
        return RestResponse.data(ret);
    }

    /**
     * 大中型市场人员总提成列表
     * @param vo
     * @return
     */
    @GetMapping("/commission/major/market/detail")
    public RestResponse<List<UserCommissionProdVO>> majorMarketCommissionDetail(UserCommissionProdVO vo){
        List<UserCommissionProdVO> ret = financeRealFundService.majorMarketCommissionDetail(vo);
        return RestResponse.data(ret);
    }

    /**
     * 大中型市场人员总提成导出
     * @param vo
     * @return
     */
    @GetMapping("/major/market/commission/total/export")
    public void majorMarketTotalAmountExport(@Valid UserCommissionTotalAmountVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionTotalAmountVO> result = financeRealFundService.majorMarketTotalAmountExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        exportPlus(response,"大中型市场人员总提成金额"+ startDate +"至"+ endDate,result, UserCommissionTotalAmountVO.class);
    }

    /**
     * 大中型市场人员总提成导出
     * @param vo
     * @return
     */
    @GetMapping("/major/market/commission/current/export")
    public void marketCurUserDetailExport(@Valid UserCommissionProdVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionProdVO> result = financeRealFundService.marketCurUserDetailExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        String userName = "";
        if (CollectionUtil.isNotEmpty(result)){
            userName = result.get(0).getUserName();
        }
        exportPlus(response,userName+"_大中型市场人员提成详情"+ startDate +"至"+ endDate,result, UserCommissionProdVO.class);
    }

    /**
     * 大中型市场所有人员总提成导出
     * @param vo
     * @return
     */
    @GetMapping("/major/market/commission/all/export")
    public void marketAllUserDetailExport(@Valid UserCommissionProdVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionProdVO> result = financeRealFundService.marketAllUserDetailExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        exportPlus(response,"大中型市场人员提成详情"+ startDate +"至"+ endDate,result, UserCommissionProdVO.class);
    }

    /**
     * 个贷产品逾期未收款列表
     * @param overdueVO
     * @param page
     * @return
     */
    @DataPermission(postChild = false,ignoreDepartment = {MANAGER_OFFICE,FINANCE_DEPARTMENT})
    @GetMapping("/overdue/personal")
    public RestResponse<Page<PersonalOverdueVO>> overduePersonal(PersonalOverdueVO overdueVO, Page page){
        Page<PersonalOverdueVO> result = financeRealFundService.overduePersonal(overdueVO,page);
        return RestResponse.data(result);
    }

    /**
     * 个贷产品逾期未收款导出
     * @param overdueVO
     * @return
     */
    @DataPermission(postChild = false,ignoreDepartment = {MANAGER_OFFICE,FINANCE_DEPARTMENT})
    @GetMapping("/overdue/personal/export")
    public void overduePersonal(PersonalOverdueVO overdueVO, HttpServletResponse response) throws IOException {
        List<PersonalOverdueVO> list = financeRealFundService.overduePersonalList(overdueVO);
        exportPlus(response, "个贷逾期未收款列表", list, PersonalOverdueVO.class);
    }

    /**
     * 资产评估人员提成计算
     * @param vo vo
     * @return List<UserCommissionTotalAmountVO>
     */
    @GetMapping("/commission/assets/evaluator")
    public RestResponse<List<UserCommissionTotalAmountVO>> assetsEvaluatorCommission(UserCommissionTotalAmountVO vo) {
        return RestResponse.data(financeRealFundService.assetsEvaluatorCommission(vo));
    }

    /**
     * 资产评估人员提成详情
     * @param vo vo
     * @return List<UserCommissionTotalAmountVO>
     */
    @GetMapping("/commission/assets/evaluator/detail")
    public RestResponse<List<UserCommissionProdAssetsVO>> assetsEvaluatorCommissionDetail(UserCommissionProdVO vo) {
        return RestResponse.data(financeRealFundService.assetsEvaluatorCommissionDetail(vo));
    }

    /**
     * 资产评估人员合计导出
     * @param vo vo
     */
    @GetMapping("/assets/evaluator/commission/total/export")
    public void assetsEvaluatorTotalAmountExport(@Valid UserCommissionTotalAmountVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionTotalAmountVO> result = financeRealFundService.assetsEvaluatorTotalAmountExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        exportPlus(response,"资产评估人员总提成金额"+ startDate +"至"+ endDate,result, UserCommissionTotalAmountVO.class);
    }

    /**
     * 资产单个评估人员提成信息导出
     * @param vo vo
     */
    @GetMapping("/assets/evaluator/commission/current/export")
    public void assetsEvaluatorCurUserDetailExport(@Valid UserCommissionProdVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionProdAssetsVO> result = financeRealFundService.assetsEvaluatorCurUserDetailExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        String userName = "";
        if (CollectionUtil.isNotEmpty(result)){
            userName = result.get(0).getUserName();
        }
        exportPlus(response,userName+"_资产评估人员提成详情"+ startDate +"至"+ endDate,result, UserCommissionProdAssetsVO.class);
    }

    /**
     * 资产所有评估人员提成信息导出
     * @param vo vo
     */
    @GetMapping("/assets/evaluator/commission/all/export")
    public void assetsEvaluatorAllUserDetailExport(@Valid UserCommissionProdVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionProdAssetsVO> result = financeRealFundService.assetsEvaluatorAllUserDetailExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        exportPlus(response,"资产评估人员提成详情"+ startDate +"至"+ endDate,result, UserCommissionProdAssetsVO.class);
    }

    /**
     * 资产市场人员提成计算
     * @param vo vo
     * @return List<UserCommissionTotalAmountVO>
     */
    @GetMapping("/commission/assets/market")
    public RestResponse<List<UserCommissionTotalAmountVO>> assetsMarketCommission(UserCommissionTotalAmountVO vo){
        List<UserCommissionTotalAmountVO> ret = financeRealFundService.assetsMarketCommission(vo);
        return RestResponse.data(ret);
    }

    /**
     * 资产市场人员提成详情
     * @param vo vo
     * @return List<UserCommissionTotalAmountVO>
     */
    @GetMapping("/commission/assets/market/detail")
    public RestResponse<List<UserCommissionProdVO>> assetsMarketCommissionDetail(UserCommissionProdVO vo) {
        return RestResponse.data(financeRealFundService.assetsMarketCommissionDetail(vo));
    }

    /**
     * 资产市场人员提成合计导出
     * @param vo vo
     */
    @GetMapping("/assets/market/commission/total/export")
    public void assetsMarketTotalAmountExport(@Valid UserCommissionTotalAmountVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionTotalAmountVO> result = financeRealFundService.assetsMarketTotalAmountExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        exportPlus(response,"资产市场人员总提成金额"+ startDate +"至"+ endDate,result, UserCommissionTotalAmountVO.class);
    }

    /**
     * 资产单个市场人员提成信息导出
     * @param vo vo
     */
    @GetMapping("/assets/market/commission/current/export")
    public void assetsMarketCurUserDetailExport(@Valid UserCommissionProdVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionProdVO> result = financeRealFundService.assetsMarketCurUserDetailExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        String userName = "";
        if (CollectionUtil.isNotEmpty(result)){
            userName = result.get(0).getUserName();
        }
        exportPlus(response,userName+"_资产市场人员提成详情"+ startDate +"至"+ endDate,result, UserCommissionProdVO.class);
    }

    /**
     * 资产所有市场人员提成信息导出
     * @param vo vo
     */
    @GetMapping("/assets/market/commission/all/export")
    public void assetsMarketAllUserDetailExport(@Valid UserCommissionProdVO vo, HttpServletResponse response)throws IOException {
        List<UserCommissionProdVO> result = financeRealFundService.assetsMarketAllUserDetailExport(vo);
        String startDate = vo.getStartDate().split(" ")[0];
        String endDate = vo.getEndDate().split(" ")[0];
        exportPlus(response,"资产市场人员提成详情"+ startDate +"至"+ endDate,result, UserCommissionProdVO.class);
    }

    /**
     * 资产产品逾期未收款列表
     * @param overdueVO vo
     * @param page 分页信息
     * @return RestResponse<Page<AssetsProdOverdueVO>>
     */
    @DataPermission(postChild = false,ignoreDepartment = {MANAGER_OFFICE,FINANCE_DEPARTMENT})
    @GetMapping("/overdue/assets")
    public RestResponse<Page<AssetsProdOverdueVO>> overdueAssets(AssetsProdOverdueVO overdueVO, Page page){
        Page<AssetsProdOverdueVO> result = financeRealFundService.overdueAssets(overdueVO,page);
        return RestResponse.data(result);
    }

    /**
     * 资产产品逾期未收款列表导出
     * @param overdueVO vo
     */
    @DataPermission(postChild = false,ignoreDepartment = {MANAGER_OFFICE,FINANCE_DEPARTMENT})
    @GetMapping("/overdue/assets/export")
    public void overdueAssetsExport(AssetsProdOverdueVO overdueVO, HttpServletResponse response) throws IOException {
        List<AssetsProdOverdueVO> result = financeRealFundService.overdueAssetsExport(overdueVO);
        exportPlus(response,"资产产品逾期未收款列表",result, AssetsProdOverdueVO.class);
    }

    /**
     * 资产市场人员结算产品
     * @param settleVO vo
     * @param page 分页参数
     * @return RestResponse<Page<SettleAssetsProductionVO>>
     */
    @GetMapping("/assets/market/settle/prod")
    public RestResponse<Page<SettleAssetsProductionVO>> assetsMarketSettleProd(SettleAssetsProductionVO settleVO, Page page,String keyword){
        Page<SettleAssetsProductionVO> result = financeRealFundService.assetsMarketSettleProd(settleVO,page,keyword);
        return RestResponse.data(result);
    }

    /**
     * 资产市场人员结算产品导出
     * @param settleVO vo
     */
    @GetMapping("/assets/market/settle/export")
    public void assetsMarketSettleExport(SettleAssetsProductionVO settleVO, HttpServletResponse response, Page page,String keyword) throws IOException, NoSuchFieldException, IllegalAccessException {
        List<SettleAssetsProductionVO> result = financeRealFundService.assetsMarketSettleExport(settleVO,page,keyword);
//        exportPlus(response,"资产市场人员结算项目",result, SettleAssetsProductionVO.class);
        ExcelOneToManyExportUtils.exportOneToMany(response,result, SettleAssetsProductionVO.class, SettleProInvoiceVo.class,"资产市场人员结算项目", "invoices");
    }

    /**
     * 资产评估人员结算产品
     * @param settleVO vo
     * @param page 分页参数
     * @return RestResponse<Page<SettleAssetsProductionVO>>
     */
    @GetMapping("/assets/evaluator/settle/prod")
    public RestResponse<Page<SettleAssetsProductionVO>> assetsEvaluatorSettleProd(SettleAssetsProductionVO settleVO, Page page,String keyword){
        Page<SettleAssetsProductionVO> result = financeRealFundService.assetsEvaluatorSettleProd(settleVO,page,keyword);
        return RestResponse.data(result);
    }

    /**
     * 资产评估人员结算产品导出
     * @param settleVO vo
     */
    @GetMapping("/assets/evaluator/settle/export")
    public void assetsEvaluatorSettleExport(SettleAssetsProductionVO settleVO, HttpServletResponse response,Page page,String keyword) throws IOException, NoSuchFieldException, IllegalAccessException {
        List<SettleAssetsProductionVO> result = financeRealFundService.assetsEvaluatorSettleExport(settleVO,page,keyword);
//        exportPlus(response,"资产评估人员结算项目",result, SettleAssetsProductionVO.class);
        ExcelOneToManyExportUtils.exportOneToMany(response,result, SettleAssetsProductionVO.class, SettleProInvoiceVo.class,"资产评估人员结算项目", "invoices");
    }

    /**
     * 实收款已认领导出
     * @param keyword
     * @param response
     * @throws IOException
     */
    @GetMapping("/export")
    public void financeRealFundExport(FinanceRealFundVO vo, String keyword,HttpServletResponse response) throws IOException {
        List<FinanceRealFundExportDTO> data = financeRealFundService.realClaimedExport(vo,keyword);
        exportPlus(response,"实收款已认领导出",data, FinanceRealFundExportDTO.class);
    }

    /**
     * 实收款未认领导出
     * @param keyword
     * @param response
     * @throws IOException
     */
    @GetMapping("/not/claim/export")
    public void financeRealFundNotClaimExport(FinanceRealFundVO vo, String keyword,HttpServletResponse response) throws IOException {
        List<FinanceRealNotClaimDTO> data = financeRealFundService.financeRealFundNotClaimExport(vo,keyword);
        exportPlus(response,"实收款未认领导出",data, FinanceRealNotClaimDTO.class);
    }

    /**
     * 财务实收款已认领列表
     */
    @GetMapping("/real/claimed")
    public RestResponse<Page<FinanceRealFundVO>> realClaimed(FinanceRealFundVO vo, Page page,String keyword){
        Page<FinanceRealFundVO> pages=financeRealFundService.realClaimed(page,vo,keyword);
        return RestResponse.data(pages);
    }
}

