package com.zehong.web.controller.transaction;

import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.system.domain.*;
import com.zehong.system.domain.vo.*;
import com.zehong.system.service.ITBorrowingApplyForService;
import com.zehong.system.service.ITPurchaseService;
import com.zehong.system.service.ITStatisticsService;
import com.zehong.system.service.ITTradeProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/statistics")
public class TStatisticsController  extends BaseController {

    @Autowired
    private ITStatisticsService itStatisticsService;

    @Autowired
    private ITTradeProjectService tTradeProjectService;

    @Autowired
    private ITBorrowingApplyForService itBorrowingApplyForService;

    @Autowired
    private ITPurchaseService itPurchaseService;

    /**
     * 项目交易收入
     * @param tTradeProject 交易实体
     * @return
     */
    @GetMapping("/tradeIncomeStatistics")
    public AjaxResult tradeIncomeStatistics(TTradeProject tTradeProject){
        return AjaxResult.success(itStatisticsService.tradeIncomeStatistics(tTradeProject));
    }

    /**
     * 项目交易支出
     * @param tTradeProject 交易实体
     * @return
     */
    @GetMapping("/tradeExpensesStatistics")
    public AjaxResult tradeExpensesStatistics(TTradeProject tTradeProject){
        return AjaxResult.success(itStatisticsService.tradeExpensesStatistics(tTradeProject));
    }

    /**
     * 项目交易收入导出
     * @param tTradeProject
     * @return
     */
    @GetMapping("/tradeIncomeStatisticsExport")
    public AjaxResult tradeIncomeStatisticsExport(TTradeProject tTradeProject){
        List<TradeIncomeStatistics> list = itStatisticsService.tradeIncomeStatistics(tTradeProject);
        if("0".equals(tTradeProject.getSortType())){
            if("0".equals(tTradeProject.getSortType())){
                List<DeptTradeIncomeStatisticsVo> listVo = list.stream().map(item ->{
                    DeptTradeIncomeStatisticsVo vo = new DeptTradeIncomeStatisticsVo();
                    BeanUtils.copyProperties(item,vo);
                    return vo;
                }).collect(Collectors.toList());
                ExcelUtil<DeptTradeIncomeStatisticsVo> util = new ExcelUtil<>(DeptTradeIncomeStatisticsVo.class);
                return util.exportExcel(listVo, "项目交易收入统计");
            }

        }
        ExcelUtil<TradeIncomeStatistics> util = new ExcelUtil<>(TradeIncomeStatistics.class);
        return util.exportExcel(list, "项目交易收入统计");
    }

    /**
     * 项目交易支出 导出
     * @param tTradeProject 交易实体
     * @return
     */
    @GetMapping("/tradeExpensesStatisticsExport")
    public AjaxResult tradeExpensesStatisticsExport(TTradeProject tTradeProject){
        List<TradeExpensesStatistics> list = itStatisticsService.tradeExpensesStatistics(tTradeProject);
        if("0".equals(tTradeProject.getSortType())){
            List<DeptTradeExpensesStatisticsVo> listVo = list.stream().map(item ->{
                DeptTradeExpensesStatisticsVo vo = new DeptTradeExpensesStatisticsVo();
                BeanUtils.copyProperties(item,vo);
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<DeptTradeExpensesStatisticsVo> util = new ExcelUtil<>(DeptTradeExpensesStatisticsVo.class);
            return util.exportExcel(listVo, "项目交易支出统计");
        }
        ExcelUtil<TradeExpensesStatistics> util = new ExcelUtil<>(TradeExpensesStatistics.class);
        return util.exportExcel(list, "项目交易支出统计");
    }

    /**
     * 交易项目详情导出
     * @param tTradeProject
     * @return
     */
    @GetMapping("/tradeStatisticsDetailExport")
    public AjaxResult tradeStatisticsDetailExport(TTradeProject tTradeProject){
        startPage();
        List<TTradeProject> list = tTradeProjectService.selectTTradeProjectList(tTradeProject);
        if(null != tTradeProject.getApplyDeptId()){
            List<TradeIncomeStatisticsDetailExportVo> listVo = list.stream().map(item ->{
                TradeIncomeStatisticsDetailExportVo vo = new TradeIncomeStatisticsDetailExportVo();
                BeanUtils.copyProperties(item,vo);
                vo.setCreateTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(vo.getCreateTime()));
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<TradeIncomeStatisticsDetailExportVo> util = new ExcelUtil<>(TradeIncomeStatisticsDetailExportVo.class);
            return util.exportExcel(listVo, "交易项目收入统计详情数据");
        }

        List<TradeExpensesStatisticsDetailExportVo> listVo = list.stream().map(item ->{
            TradeExpensesStatisticsDetailExportVo vo = new TradeExpensesStatisticsDetailExportVo();
            BeanUtils.copyProperties(item,vo);
            vo.setCreateTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(vo.getCreateTime()));
            return vo;
        }).collect(Collectors.toList());
        ExcelUtil<TradeExpensesStatisticsDetailExportVo> util = new ExcelUtil<>(TradeExpensesStatisticsDetailExportVo.class);
        return util.exportExcel(listVo, "交易项目支出统计详情数据");

    }

    /**
     * 借支
     * @param tBorrowingApplyFor 借支实体
     * @return
     */
    @GetMapping("/borrowingStatistics")
    public AjaxResult borrowingStatistics(TBorrowingApplyFor tBorrowingApplyFor){
        return AjaxResult.success(itStatisticsService.borrowingStatistics(tBorrowingApplyFor));
    }

    /**
     * 借支统计导出
     * @param tBorrowingApplyFor
     * @return
     */
    @GetMapping("/borrowingStatisticsExport")
    public AjaxResult borrowingStatisticsExport(TBorrowingApplyFor tBorrowingApplyFor){
        List<TBorrowingStatistics> list = itStatisticsService.borrowingStatistics(tBorrowingApplyFor);
        if("0".equals(tBorrowingApplyFor.getSortType())){
            List<DeptTBorrowingStatisticsVo> listVo = list.stream().map(item ->{
                DeptTBorrowingStatisticsVo vo = new DeptTBorrowingStatisticsVo();
                BeanUtils.copyProperties(item,vo);
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<DeptTBorrowingStatisticsVo> util = new ExcelUtil<>(DeptTBorrowingStatisticsVo.class);
            return util.exportExcel(listVo, "借支统计");
        }
        ExcelUtil<TBorrowingStatistics> util = new ExcelUtil<>(TBorrowingStatistics.class);
        return util.exportExcel(list, "借支统计");
    }

    /**
     * 借支详情导出
     * @param tBorrowingApplyFor
     * @return
     */
    @GetMapping("/borrowingStatisticsDetailExport")
    public AjaxResult borrowingStatisticsDetailExport(TBorrowingApplyFor tBorrowingApplyFor){
        startPage();
        List<TBorrowingApplyFor> list = itBorrowingApplyForService.selectTBorrowingApplyForList(tBorrowingApplyFor);
        List<TBorrowingStatisticsDetailExportVo> listVo = list.stream().map(item ->{
            TBorrowingStatisticsDetailExportVo vo = new TBorrowingStatisticsDetailExportVo();
            BeanUtils.copyProperties(item,vo);
            vo.setApprovalStatusType(getApproval(vo.getApprovalStatus()));
            vo.setRegistrationDateStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(vo.getRegistrationDate()));
            return vo;
        }).collect(Collectors.toList());
        ExcelUtil<TBorrowingStatisticsDetailExportVo> util = new ExcelUtil<>(TBorrowingStatisticsDetailExportVo.class);
        return util.exportExcel(listVo, "借支统计详情数据");
    }

    private String getApproval(Integer approvalStatus){
        switch (approvalStatus){
            case 1:
                return "待核算部审批";
            case 2:
                return "完成";
            case 3:
                return "驳回";
            default:
                return "";
        }
    }

    /**
     * 采购统计
     * @param tPurchase  采购实体
     * @return
     */
    @GetMapping("/purchaseStatistics")
    public AjaxResult purchaseStatistics(TPurchase tPurchase){
        return AjaxResult.success(itStatisticsService.purchaseStatistics(tPurchase));
    }

    /**
     * 报销统计导出
     * @param tPurchase  采购实体
     * @return
     */
    @GetMapping("/purchaseStatisticsExport")
    public AjaxResult purchaseStatisticsExport(TPurchase tPurchase){
        List<TPurchaseStatistics> list = itStatisticsService.purchaseStatistics(tPurchase);
        if("0".equals(tPurchase.getSortType())){
            List<DeptTPurchaseStatisticsVo> listVo = list.stream().map(item ->{
                DeptTPurchaseStatisticsVo vo = new DeptTPurchaseStatisticsVo();
                BeanUtils.copyProperties(item,vo);
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<DeptTPurchaseStatisticsVo> util = new ExcelUtil<>(DeptTPurchaseStatisticsVo.class);
            return util.exportExcel(listVo, "0".equals(tPurchase.getPurchaseType())?"物品报销统计":"费用报销统计");
        }

        if("1".equals(tPurchase.getSortType())){
            List<HandleTPurchaseStatisticsVo> listVo = list.stream().map(item ->{
                HandleTPurchaseStatisticsVo vo = new HandleTPurchaseStatisticsVo();
                BeanUtils.copyProperties(item,vo);
                return vo;
            }).collect(Collectors.toList());
            ExcelUtil<HandleTPurchaseStatisticsVo> util = new ExcelUtil<>(HandleTPurchaseStatisticsVo.class);
            return util.exportExcel(listVo, "0".equals(tPurchase.getPurchaseType())?"物品报销统计":"费用报销统计");
        }
        ExcelUtil<TPurchaseStatistics> util = new ExcelUtil<>(TPurchaseStatistics.class);
        return util.exportExcel(list, "0".equals(tPurchase.getPurchaseType())?"物品报销统计":"费用报销统计");
    }

    /**
     * 报销统计导出
     * @param tPurchase  采购实体
     * @return
     */
    @GetMapping("/purchaseStatisticsDetailExport")
    public AjaxResult purchaseStatisticsDetailExport(TPurchase tPurchase){
        List<TPurchase> list = itPurchaseService.selectTPurchaseList(tPurchase);
        List<PurchaseStatisticsDetailExportVo> listVo = list.stream().map(item ->{
            PurchaseStatisticsDetailExportVo vo = new PurchaseStatisticsDetailExportVo();
            BeanUtils.copyProperties(item,vo);
            vo.setCreateTimeStr(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(vo.getCreateTime()));
            return vo;
        }).collect(Collectors.toList());
        ExcelUtil<PurchaseStatisticsDetailExportVo> util = new ExcelUtil<>(PurchaseStatisticsDetailExportVo.class);
        return util.exportExcel(listVo, "0".equals(tPurchase.getPurchaseType())?"物品报销统计详情数据":"费用报销统计详情数据");
    }

}
