package cn.ezhizao.project.business.ticketReport.controller;

import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.keepAccounts.domain.BizFinancialKeepAccount;
import cn.ezhizao.project.business.keepAccounts.service.IBizFinancialKeepAccountService;
import cn.ezhizao.project.business.ticketReport.domain.BizFinancialTicketReportDetail;
import cn.ezhizao.project.business.ticketReport.service.IBizFinancialTicketReportDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "收票 数据接口")
@RequestMapping(value = "/business/financialTicketReport")
public class BizFinancialTicketReportController extends BaseController {
    @Resource
    IBizFinancialKeepAccountService financialKeepAccountService;
    @Resource
    IBizFinancialTicketReportDetailService financialTicketReportDetailService;


    @ApiOperation(value = "带条件List 分页查询", notes = "带条件List 分页查询")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo getList(@RequestBody BizFinancialKeepAccount conditions) throws NoSuchFieldException, IllegalAccessException {
        setTenantId(conditions);
        startPage();
        List<BizFinancialKeepAccount> list = financialTicketReportDetailService.getMainList(conditions);
        if (!list.isEmpty()) {
            BizFinancialTicketReportDetail detailConditions = new BizFinancialTicketReportDetail();

            detailConditions.setYear(conditions.getYear());
            detailConditions.setIds(list.stream().map(BizFinancialKeepAccount::getId).collect(Collectors.toList()));
            List<BizFinancialTicketReportDetail> details = financialTicketReportDetailService.getList(detailConditions);
            list.forEach(l -> {
                l.setReceiveTickets(details.stream().filter(v -> l.getId().equals(v.getId()) && v.getType().equals(0)).collect(Collectors.toList()));
                l.setScanTickets(details.stream().filter(v -> l.getId().equals(v.getId()) && v.getType().equals(1)).collect(Collectors.toList()));
                l.setSalaryTickets(details.stream().filter(v -> l.getId().equals(v.getId()) && v.getType().equals(2)).collect(Collectors.toList()));
            });
        }

        return getDataTable(list);
    }
}
