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

import cn.ezhizao.framework.web.controller.BaseController;
import cn.ezhizao.framework.web.domain.AjaxResult;
import cn.ezhizao.framework.web.page.TableDataInfo;
import cn.ezhizao.project.business.IndividualIncomeTax.domain.BizFinancialIndividualIncomeTax;
import cn.ezhizao.project.business.IndividualIncomeTax.domain.BizFinancialIndividualIncomeTaxDetail;
import cn.ezhizao.project.business.IndividualIncomeTax.service.IBizFinancialIndividualIncomeTaxDetailService;
import cn.ezhizao.project.business.IndividualIncomeTax.service.IBizFinancialIndividualIncomeTaxService;
import cn.ezhizao.project.business.entrust.domain.BizEntrust;
import cn.ezhizao.project.business.entrust.service.IBizEntrustService;
import cn.ezhizao.project.business.housingFund.domain.*;
import cn.ezhizao.project.business.housingFund.service.*;
import cn.ezhizao.project.business.keepAccounts.domain.BizFinancialKeepAccount;
import cn.ezhizao.project.business.keepAccounts.domain.BizFinancialKeepAccountDetail;
import cn.ezhizao.project.business.keepAccounts.service.IBizFinancialKeepAccountDetailService;
import cn.ezhizao.project.business.keepAccounts.service.IBizFinancialKeepAccountService;
import cn.ezhizao.project.business.receiveTicket.domain.BizFinancialReceiveTicket;
import cn.ezhizao.project.business.receiveTicket.domain.BizFinancialReceiveTicketDetail;
import cn.ezhizao.project.business.receiveTicket.service.IBizFinancialReceiveTicketDetailService;
import cn.ezhizao.project.business.receiveTicket.service.IBizFinancialReceiveTicketService;
import cn.ezhizao.project.business.reportTax.domain.BizFinancialReportTax;
import cn.ezhizao.project.business.reportTax.domain.BizFinancialReportTaxDetail;
import cn.ezhizao.project.business.reportTax.service.IBizFinancialReportTaxDetailService;
import cn.ezhizao.project.business.reportTax.service.IBizFinancialReportTaxService;
import cn.ezhizao.project.business.salary.domain.BizFinancialSalary;
import cn.ezhizao.project.business.salary.domain.BizFinancialSalaryDetail;
import cn.ezhizao.project.business.salary.domain.BizFinancialSalaryDetailEmployee;
import cn.ezhizao.project.business.salary.service.IBizFinancialSalaryDetailEmployeeService;
import cn.ezhizao.project.business.salary.service.IBizFinancialSalaryDetailService;
import cn.ezhizao.project.business.salary.service.IBizFinancialSalaryService;
import cn.ezhizao.project.business.socialSecurity.domain.*;
import cn.ezhizao.project.business.socialSecurity.service.*;
import cn.ezhizao.project.business.workOrder.domain.BizWorkOrder;
import cn.ezhizao.project.business.workOrder.domain.BizWorkOrderRecord;
import cn.ezhizao.project.business.workOrder.service.IBizWorkOrderRecordService;
import cn.ezhizao.project.business.workOrder.service.IBizWorkOrderService;
import cn.ezhizao.project.system.domain.SysFileStorage;
import cn.ezhizao.project.system.service.ISysFileStorageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/business/workOrderService")
public class WorkOrderServiceController extends BaseController {
    @Resource
    private IBizWorkOrderService bizWorkOrderService;
    @Resource
    private IBizWorkOrderRecordService bizWorkOrderRecordService;
    @Resource
    private IBizEntrustService entrustService;
    @Resource
    private IBizFinancialKeepAccountDetailService keepAccountDetailService;
    @Resource
    private IBizFinancialKeepAccountService keepAccountService;
    @Resource
    private IBizFinancialSalaryService bizFinancialSalaryService;
    @Resource
    private IBizFinancialSalaryDetailService bizFinancialSalaryDetailService;
    @Resource
    private IBizFinancialSalaryDetailEmployeeService bizFinancialSalaryDetailEmployeeService;
    @Resource
    private IBizFinancialReceiveTicketService bizFinancialReceiveTicketService;
    @Resource
    private IBizFinancialReceiveTicketDetailService bizFinancialReceiveTicketDetailService;
    @Resource
    private IBizFinancialReportTaxDetailService bizFinancialReportTaxDetailService;
    @Resource
    private IBizFinancialReportTaxService bizFinancialReportTaxService;
    @Resource
    private IBizFinancialIndividualIncomeTaxService bizFinancialIndividualIncomeTaxService;
    @Resource
    private IBizFinancialIndividualIncomeTaxDetailService bizFinancialIndividualIncomeTaxDetailService;
    @Resource
    private IBizSocialSecurityConfirmService bizSocialSecurityConfirmService;
    @Resource
    private IBizSocialSecurityConfirmDetailService bizSocialSecurityConfirmDetailService;
    @Resource
    private IBizSocialSecurityConfirmDetailEmployeeService bizSocialSecurityConfirmDetailEmployeeService;
    @Resource
    private IBizSocialSecurityDeclareService bizSocialSecurityDeclareService;
    @Resource
    private IBizSocialSecurityDeclareDetailService bizSocialSecurityDeclareDetailService;
    @Resource
    private IBizHousingFundConfirmService bizHousingFundConfirmService;
    @Resource
    private IBizHousingFundConfirmDetailService bizHousingFundConfirmDetailService;
    @Resource
    private IBizHousingFundConfirmDetailEmployeeService bizHousingFundConfirmDetailEmployeeService;
    @Resource
    private IBizHousingFundDeclareService bizHousingFundDeclareService;
    @Resource
    private IBizHousingFundDeclareDetailService bizHousingFundDeclareDetailService;
    @Resource
    private ISysFileStorageService fileStorageService;

    @PreAuthorize("@ss.hasPermi('business:customer:service:loop:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizWorkOrder bizWorkOrder) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizWorkOrder);
        bizWorkOrder.setType(1);
        bizWorkOrder.setIsStop(0);
        startPage();
        startOrderBy();
        List<BizWorkOrder> list = bizWorkOrderService.getList(bizWorkOrder);
        BizWorkOrderRecord bizWorkOrderRecord = new BizWorkOrderRecord();
        bizWorkOrderRecord.setWorkOrderIds(list.stream().map(BizWorkOrder::getId).collect(Collectors.toList()));
        bizWorkOrderRecord.setYear(bizWorkOrder.getYear());
//        bizWorkOrderRecord.setMonth(bizWorkOrder.getMonth());
        List<BizWorkOrderRecord> records = bizWorkOrderRecordService.getList(bizWorkOrderRecord);
        BizEntrust entrustConditions = new BizEntrust();
        entrustConditions.setWorkOrderIds(list.stream().map(BizWorkOrder::getId).collect(Collectors.toList()));
        entrustConditions.setCurrentYear(bizWorkOrder.getYear());
        List<BizEntrust> entrusts = entrustService.getList(entrustConditions);
        list.forEach(v ->  {
            v.setRecords(records.stream().filter(e -> e.getWorkOrderId().equals(v.getId())).collect(Collectors.toList()));
            v.setEntrusts(entrusts.stream().filter(e -> e.getWorkOrderId().equals(v.getId())).collect(Collectors.toList()));
        });
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('business:customer:service:once:list')")
    @GetMapping("/onceList")
    public TableDataInfo onceList(BizWorkOrder bizWorkOrder) throws NoSuchFieldException, IllegalAccessException
    {
        setTenantId(bizWorkOrder);
        bizWorkOrder.setType(2);
        startPage();
        startOrderBy();
        List<BizWorkOrder> list = bizWorkOrderService.getList(bizWorkOrder);
        BizWorkOrderRecord bizWorkOrderRecord = new BizWorkOrderRecord();
        bizWorkOrderRecord.setWorkOrderIds(list.stream().map(BizWorkOrder::getId).collect(Collectors.toList()));
        List<BizWorkOrderRecord> records = bizWorkOrderRecordService.getList(bizWorkOrderRecord);
        BizEntrust entrustConditions = new BizEntrust();
        entrustConditions.setWorkOrderIds(list.stream().map(BizWorkOrder::getId).collect(Collectors.toList()));
        List<BizEntrust> entrusts = entrustService.getList(entrustConditions);
        list.forEach(v -> {
            v.setRecords(records.stream().filter(e -> e.getWorkOrderId().equals(v.getId())).collect(Collectors.toList()));
            v.setEntrusts(entrusts.stream().filter(e -> e.getWorkOrderId().equals(v.getId())).collect(Collectors.toList()));
        });
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasAnyPermi('business:customer:service:loop:edit,business:customer:service:loop:view,business:customer:service:loop:verify')")
    @PostMapping("/getDetail")
    public AjaxResult getDetail(@RequestBody BizWorkOrderRecord bizWorkOrderRecord) {
        List<BizWorkOrderRecord> bizWorkOrderRecords = bizWorkOrderRecordService.getList(bizWorkOrderRecord);
        BizWorkOrderRecord record = bizWorkOrderRecords.size() > 0 ? bizWorkOrderRecords.get(0) : null;
        if (record == null) {
            record = bizWorkOrderRecord;
        }
        BizWorkOrder bizWorkOrder = bizWorkOrderService.getById(bizWorkOrderRecord.getWorkOrderId());
        if (bizWorkOrder.getTaskTypeId().equals(1L)) {
            // 收票
            BizFinancialReceiveTicket ticketCondition = new BizFinancialReceiveTicket();
            ticketCondition.setWorkOrderId(bizWorkOrder.getId());
            BizFinancialReceiveTicket bizFinancialReceiveTicket = bizFinancialReceiveTicketService.getList(ticketCondition).stream().findFirst().orElse(null);
            if (bizFinancialReceiveTicket == null) {
                return error("收票任务不存在");
            }
            BizFinancialReceiveTicketDetail ticketDetailCondition = new BizFinancialReceiveTicketDetail();
            ticketDetailCondition.setFinancialReceiveTicketId(bizFinancialReceiveTicket.getId());
            ticketDetailCondition.setYear(bizWorkOrderRecord.getYear());
            ticketDetailCondition.setMonth(bizWorkOrderRecord.getMonth());
            BizFinancialReceiveTicketDetail bizFinancialReceiveTicketDetail = bizFinancialReceiveTicketDetailService.getList(ticketDetailCondition).stream().findFirst().orElse(null);
            if (bizFinancialReceiveTicketDetail != null) {
                record.setTicket(bizFinancialReceiveTicketDetail);
            } else {
                record.setTicket(ticketDetailCondition);
            }
            // 工资
            BizFinancialSalary salaryCondition = new BizFinancialSalary();
            salaryCondition.setWorkOrderId(bizWorkOrder.getId());
            BizFinancialSalary bizFinancialSalary = bizFinancialSalaryService.getList(salaryCondition).stream().findFirst().orElse(null);
            if (bizFinancialSalary == null) {
                return error("工资任务不存在");
            }
            BizFinancialSalaryDetail salaryDetailCondition = new BizFinancialSalaryDetail();
            salaryDetailCondition.setFinancialSalaryId(bizFinancialSalary.getId());
            salaryDetailCondition.setYear(bizWorkOrderRecord.getYear());
            salaryDetailCondition.setMonth(bizWorkOrderRecord.getMonth());
            BizFinancialSalaryDetail bizFinancialSalaryDetail = bizFinancialSalaryDetailService.getList(salaryDetailCondition).stream().findFirst().orElse(null);
            if (bizFinancialSalaryDetail != null) {
                List<BizFinancialSalaryDetailEmployee> employees = bizFinancialSalaryDetailEmployeeService.query().eq("financial_salary_detail_id", bizFinancialSalaryDetail.getId()).list();
                bizFinancialSalaryDetail.setDetails(employees);
                record.setSalary(bizFinancialSalaryDetail);
            } else {
                salaryDetailCondition.setDetails(new ArrayList<>());
                record.setSalary(salaryDetailCondition);
            }
            // 记账
            BizFinancialKeepAccount keepAccountCondition = new BizFinancialKeepAccount();
            keepAccountCondition.setWorkOrderId(bizWorkOrder.getId());
            BizFinancialKeepAccount bizFinancialKeepAccount = keepAccountService.getList(keepAccountCondition).stream().findFirst().orElse(null);
            if (bizFinancialKeepAccount == null) {
                return error("记账任务不存在");
            }
            BizFinancialKeepAccountDetail keepAccountDetailCondition = new BizFinancialKeepAccountDetail();
            keepAccountDetailCondition.setFinancialKeepAccountId(bizFinancialKeepAccount.getId());
            keepAccountDetailCondition.setYear(bizWorkOrderRecord.getYear());
            keepAccountDetailCondition.setMonth(bizWorkOrderRecord.getMonth());
            BizFinancialKeepAccountDetail bizFinancialKeepAccountDetail = keepAccountDetailService.getList(keepAccountDetailCondition).stream().findFirst().orElse(null);
            if (bizFinancialKeepAccountDetail != null) {
                record.setKeepAccount(bizFinancialKeepAccountDetail);
            } else {
                record.setKeepAccount(keepAccountDetailCondition);
            }
            // 报税
            BizFinancialReportTax reportTaxCondition = new BizFinancialReportTax();
            reportTaxCondition.setWorkOrderId(bizWorkOrder.getId());
            BizFinancialReportTax reportTax = bizFinancialReportTaxService.getList(reportTaxCondition).stream().findFirst().orElse(null);
            if (reportTax == null) {
                return error("报税任务不存在");
            }
            BizFinancialReportTaxDetail reportTaxDetailCondition = new BizFinancialReportTaxDetail();
            reportTaxDetailCondition.setFinancialReportTaxId(reportTax.getId());
            reportTaxDetailCondition.setYear(bizWorkOrderRecord.getYear());
            reportTaxDetailCondition.setMonth(bizWorkOrderRecord.getMonth());
            BizFinancialReportTaxDetail bizFinancialReportTaxDetail = bizFinancialReportTaxDetailService.getList(reportTaxDetailCondition).stream().findFirst().orElse(null);
            if (bizFinancialReportTaxDetail != null) {
                record.setReportTax(bizFinancialReportTaxDetail);
            } else {
                record.setReportTax(reportTaxDetailCondition);
            }
            // 个税
            BizFinancialIndividualIncomeTax individualCondition = new BizFinancialIndividualIncomeTax();
            individualCondition.setWorkOrderId(bizWorkOrder.getId());
            BizFinancialIndividualIncomeTax individualIncomeTax = bizFinancialIndividualIncomeTaxService.getList(individualCondition).stream().findFirst().orElse(null);
            if (individualIncomeTax == null) {
                return error("个税任务不存在");
            }
            BizFinancialIndividualIncomeTaxDetail individualIncomeTaxDetailCondition = new BizFinancialIndividualIncomeTaxDetail();
            individualIncomeTaxDetailCondition.setFinancialIndividualIncomeTaxId(individualIncomeTax.getId());
            individualIncomeTaxDetailCondition.setYear(bizWorkOrderRecord.getYear());
            individualIncomeTaxDetailCondition.setMonth(bizWorkOrderRecord.getMonth());
            BizFinancialIndividualIncomeTaxDetail individualIncomeTaxDetail = bizFinancialIndividualIncomeTaxDetailService.getList(individualIncomeTaxDetailCondition).stream().findFirst().orElse(null);
            if (individualIncomeTaxDetail != null) {
                individualIncomeTaxDetail.setFiles(bizFinancialIndividualIncomeTaxDetailService.getFiles(individualIncomeTaxDetail.getId()));
                record.setIndividual(individualIncomeTaxDetail);
            } else {
                individualIncomeTaxDetailCondition.setFiles(new ArrayList<>());
                record.setIndividual(individualIncomeTaxDetailCondition);
            }

        }
        if (bizWorkOrder.getTaskTypeId().equals(2L)) {
            // 社保人员信息
            BizSocialSecurityConfirm confirmCondition = new BizSocialSecurityConfirm();
            confirmCondition.setWorkOrderId(bizWorkOrder.getId());
            BizSocialSecurityConfirm confirm = bizSocialSecurityConfirmService.getListByBean(confirmCondition).stream().findFirst().orElse(null);
            if (confirm == null) {
                return error("社保人员录入任务不存在");
            }
            BizSocialSecurityConfirmDetail confirmDetailCondition = new BizSocialSecurityConfirmDetail();
            confirmDetailCondition.setSocialSecurityConfirmId(confirm.getId());
            confirmDetailCondition.setYear(bizWorkOrderRecord.getYear());
            confirmDetailCondition.setMonth(bizWorkOrderRecord.getMonth());
            BizSocialSecurityConfirmDetail confirmDetail = bizSocialSecurityConfirmDetailService.getListByBean(confirmDetailCondition).stream().findFirst().orElse(null);
            if (confirmDetail != null) {
                List<BizSocialSecurityConfirmDetailEmployee> employees = bizSocialSecurityConfirmDetailEmployeeService.query().eq("social_security_confirm_detail_id", confirmDetail.getId()).list();
                confirmDetail.setDetails(employees);
                record.setSocialSecurityConfirm(confirmDetail);
            } else {
                confirmDetailCondition.setDetails(new ArrayList<>());
                record.setSocialSecurityConfirm(confirmDetailCondition);
            }
            // 社保申报信息
            BizSocialSecurityDeclare declareCondition = new BizSocialSecurityDeclare();
            declareCondition.setWorkOrderId(bizWorkOrder.getId());
            BizSocialSecurityDeclare declare = bizSocialSecurityDeclareService.getListByBean(declareCondition).stream().findFirst().orElse(null);
            if (declare == null) {
                return error("社保申报任务不存在");
            }
            BizSocialSecurityDeclareDetail declareDetailCondition = new BizSocialSecurityDeclareDetail();
            declareDetailCondition.setSocialSecurityDeclareId(declare.getId());
            declareDetailCondition.setYear(bizWorkOrderRecord.getYear());
            declareDetailCondition.setMonth(bizWorkOrderRecord.getMonth());
            BizSocialSecurityDeclareDetail declareDetail = bizSocialSecurityDeclareDetailService.getListByBean(declareDetailCondition).stream().findFirst().orElse(null);
            if (declareDetail != null) {
                List<SysFileStorage> files = fileStorageService.query().eq("master_table_name", "biz_social_security_declare_detail").eq("master_id", declareDetail.getId()).list();
                files.forEach(l -> l.setId(null));
                declareDetail.setEvidenceFiles(files);
                record.setSocialSecurityDeclare(declareDetail);
            } else {
                declareDetailCondition.setEvidenceFiles(new ArrayList<>());
                record.setSocialSecurityDeclare(declareDetailCondition);
            }
        }
        if (bizWorkOrder.getTaskTypeId().equals(3L)) {
            // 公积金人员信息
            BizHousingFundConfirm confirmCondition = new BizHousingFundConfirm();
            confirmCondition.setWorkOrderId(bizWorkOrder.getId());
            BizHousingFundConfirm confirm = bizHousingFundConfirmService.getListByBean(confirmCondition).stream().findFirst().orElse(null);
            if (confirm == null) {
                return error("公积金人员录入任务不存在");
            }
            BizHousingFundConfirmDetail confirmDetailCondition = new BizHousingFundConfirmDetail();
            confirmDetailCondition.setHousingFundConfirmId(confirm.getId());
            confirmDetailCondition.setYear(bizWorkOrderRecord.getYear());
            confirmDetailCondition.setMonth(bizWorkOrderRecord.getMonth());
            BizHousingFundConfirmDetail confirmDetail = bizHousingFundConfirmDetailService.getListByBean(confirmDetailCondition).stream().findFirst().orElse(null);
            if (confirmDetail != null) {
                List<BizHousingFundConfirmDetailEmployee> employees = bizHousingFundConfirmDetailEmployeeService.query().eq("housing_fund_confirm_detail_id", confirmDetail.getId()).list();
                confirmDetail.setDetails(employees);
                record.setHousingFundConfirm(confirmDetail);
            } else {
                confirmDetailCondition.setDetails(new ArrayList<>());
                record.setHousingFundConfirm(confirmDetailCondition);
            }
            // 公积金申报信息
            BizHousingFundDeclare declareCondition = new BizHousingFundDeclare();
            declareCondition.setWorkOrderId(bizWorkOrder.getId());
            BizHousingFundDeclare declare = bizHousingFundDeclareService.getListByBean(declareCondition).stream().findFirst().orElse(null);
            if (declare == null) {
                return error("公积金申报任务不存在");
            }
            BizHousingFundDeclareDetail declareDetailCondition = new BizHousingFundDeclareDetail();
            declareDetailCondition.setHousingFundDeclareId(declare.getId());
            declareDetailCondition.setYear(bizWorkOrderRecord.getYear());
            declareDetailCondition.setMonth(bizWorkOrderRecord.getMonth());
            BizHousingFundDeclareDetail declareDetail = bizHousingFundDeclareDetailService.getListByBean(declareDetailCondition).stream().findFirst().orElse(null);
            if (declareDetail != null) {
                List<SysFileStorage> files = fileStorageService.query().eq("master_table_name", "biz_housing_fund_declare_detail").eq("master_id", declareDetail.getId()).list();
                files.forEach(l -> l.setId(null));
                declareDetail.setEvidenceFiles(files);
                record.setHousingFundDeclare(declareDetail);
            } else {
                declareDetailCondition.setEvidenceFiles(new ArrayList<>());
                record.setHousingFundDeclare(declareDetailCondition);
            }
        }
        return success(record);
    }
    @PreAuthorize("@ss.hasAnyPermi('business:customer:service:once:edit,business:customer:service:once:view,business:customer:service:once:verify')")
    @PostMapping("/getOnceDetail")
    public AjaxResult getOnceDetail(@RequestBody BizWorkOrderRecord bizWorkOrderRecord) {
        List<BizWorkOrderRecord> bizWorkOrderRecords = bizWorkOrderRecordService.getList(bizWorkOrderRecord);
        BizWorkOrderRecord record = bizWorkOrderRecords.size() > 0 ? bizWorkOrderRecords.get(0) : null;
        if (record == null) {
            record = bizWorkOrderRecord;
        }
        BizWorkOrder bizWorkOrder = bizWorkOrderService.getById(bizWorkOrderRecord.getWorkOrderId());
        return success(record);
    }

    @PreAuthorize("@ss.hasAnyPermi('business:customer:service:loop:edit')")
    @PostMapping("/saveDetail")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveDetail(@RequestBody BizWorkOrderRecord bizWorkOrderRecord) throws NoSuchFieldException, IllegalAccessException {
        BizWorkOrder workOrder = bizWorkOrderService.getById(bizWorkOrderRecord.getWorkOrderId());
        setTenantId(bizWorkOrderRecord);
        boolean status = bizWorkOrderRecordService.saveOrUpdate(bizWorkOrderRecord);
        if (workOrder.getTaskTypeId().equals(1L)) {
            // 保存所有记账项
            BizFinancialSalaryDetail salaryDetail = bizWorkOrderRecord.getSalary();
            salaryDetail.setCompanyId(workOrder.getCompanyId());
            salaryDetail.setStatus(bizWorkOrderRecord.getStatus());
            BizFinancialReceiveTicketDetail receiveTicketDetail = bizWorkOrderRecord.getTicket();
            receiveTicketDetail.setCompanyId(workOrder.getCompanyId());
            receiveTicketDetail.setStatus(bizWorkOrderRecord.getStatus());
            BizFinancialKeepAccountDetail keepAccountDetail = bizWorkOrderRecord.getKeepAccount();
            keepAccountDetail.setCompanyId(workOrder.getCompanyId());
            keepAccountDetail.setStatus(bizWorkOrderRecord.getStatus());
            BizFinancialReportTaxDetail reportTaxDetail = bizWorkOrderRecord.getReportTax();
            reportTaxDetail.setCompanyId(workOrder.getCompanyId());
            reportTaxDetail.setStatus(bizWorkOrderRecord.getStatus());
            BizFinancialIndividualIncomeTaxDetail individualIncomeTaxDetail = bizWorkOrderRecord.getIndividual();
            individualIncomeTaxDetail.setCompanyId(workOrder.getCompanyId());
            individualIncomeTaxDetail.setStatus(bizWorkOrderRecord.getStatus());
            bizFinancialSalaryDetailService.saveOrUpdate(salaryDetail);
            BizFinancialSalaryDetailEmployee delEmployeeCondition = new BizFinancialSalaryDetailEmployee();
            delEmployeeCondition.setFinancialSalaryDetailId(salaryDetail.getId());
            bizFinancialSalaryDetailEmployeeService.physicalDelete(delEmployeeCondition);
            salaryDetail.getDetails().forEach(l -> {
                l.setFinancialSalaryDetailId(salaryDetail.getId());
                l.setCompanyId(workOrder.getCompanyId());
                l.setFinancialSalaryId(salaryDetail.getFinancialSalaryId());
            });
            bizFinancialSalaryDetailEmployeeService.saveBatch(salaryDetail.getDetails());
            bizFinancialReceiveTicketDetailService.saveOrUpdate(receiveTicketDetail);
            keepAccountDetailService.saveOrUpdate(keepAccountDetail);
            bizFinancialReportTaxDetailService.saveOrUpdate(reportTaxDetail);
            bizFinancialIndividualIncomeTaxDetailService.saveOrUpdate(individualIncomeTaxDetail);
        }
        if(workOrder.getTaskTypeId().equals(2L)) {
            // 保存所有社保项
            BizSocialSecurityConfirmDetail confirmDetail = bizWorkOrderRecord.getSocialSecurityConfirm();
            confirmDetail.setCompanyId(workOrder.getCompanyId());
            confirmDetail.setStatus(bizWorkOrderRecord.getStatus());
            BizSocialSecurityDeclareDetail declareDetail = bizWorkOrderRecord.getSocialSecurityDeclare();
            declareDetail.setCompanyId(workOrder.getCompanyId());
            declareDetail.setStatus(bizWorkOrderRecord.getStatus());
            bizSocialSecurityConfirmDetailService.saveOrUpdate(confirmDetail);
            Map<String, Object> delEmployeeCondition = new HashMap<>();
            delEmployeeCondition.put("socialSecurityConfirmDetailId", confirmDetail.getId());
            bizSocialSecurityConfirmDetailEmployeeService.physicallyDelete(delEmployeeCondition);
            confirmDetail.getDetails().forEach(l -> {
                l.setSocialSecurityConfirmDetailId(confirmDetail.getId());
                l.setSocialSecurityConfirmId(confirmDetail.getSocialSecurityConfirmId());
                l.setCompanyId(workOrder.getCompanyId());
            });
            bizSocialSecurityConfirmDetailEmployeeService.saveBatch(confirmDetail.getDetails());
            bizSocialSecurityDeclareDetailService.saveOrUpdate(declareDetail);
            List<SysFileStorage> fileStorages = declareDetail.getEvidenceFiles();
            fileStorages.forEach(l -> {
                l.setMasterId(declareDetail.getId());
                l.setMasterTableName("biz_social_security_declare_detail");
            });
            SysFileStorage delCondition = new SysFileStorage();
            delCondition.setMasterId(declareDetail.getId());
            delCondition.setMasterTableName("biz_social_security_declare_detail");
            fileStorageService.physicalDelete(delCondition);
            fileStorageService.saveOrUpdateBatch(fileStorages);
        }
        if(workOrder.getTaskTypeId().equals(3L)) {
            // 保存所有公积金项
            BizHousingFundConfirmDetail confirmDetail = bizWorkOrderRecord.getHousingFundConfirm();
            BizHousingFundDeclareDetail declareDetail = bizWorkOrderRecord.getHousingFundDeclare();
            confirmDetail.setStatus(bizWorkOrderRecord.getStatus());
            declareDetail.setStatus(bizWorkOrderRecord.getStatus());
            confirmDetail.setCompanyId(workOrder.getCompanyId());
            bizHousingFundConfirmDetailService.saveOrUpdate(confirmDetail);
            Map<String, Object> delEmployeeCondition = new HashMap<>();
            delEmployeeCondition.put("housingFundConfirmDetailId", confirmDetail.getId());
            bizHousingFundConfirmDetailEmployeeService.physicallyDelete(delEmployeeCondition);
            confirmDetail.getDetails().forEach(l -> {
                l.setHousingFundConfirmDetailId(confirmDetail.getId());
                l.setHousingFundConfirmId(confirmDetail.getHousingFundConfirmId());
                l.setCompanyId(workOrder.getCompanyId());
            });
            bizHousingFundConfirmDetailEmployeeService.saveBatch(confirmDetail.getDetails());
            declareDetail.setCompanyId(workOrder.getCompanyId());
            bizHousingFundDeclareDetailService.saveOrUpdate(declareDetail);
            List<SysFileStorage> fileStorages = declareDetail.getEvidenceFiles();
            fileStorages.forEach(l -> {
                l.setMasterId(declareDetail.getId());
                l.setMasterTableName("biz_housing_fund_declare_detail");
            });
            SysFileStorage delCondition = new SysFileStorage();
            delCondition.setMasterId(declareDetail.getId());
            delCondition.setMasterTableName("biz_housing_fund_declare_detail");
            fileStorageService.physicalDelete(delCondition);
            fileStorageService.saveOrUpdateBatch(fileStorages);
        }
        return toAjax(status);
    }
    @PreAuthorize("@ss.hasAnyPermi('business:customer:service:once:edit')")
    @PostMapping("/saveOnceDetail")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult saveOnceDetail(@RequestBody BizWorkOrderRecord bizWorkOrderRecord) throws NoSuchFieldException, IllegalAccessException {
        BizWorkOrder workOrder = bizWorkOrderService.getById(bizWorkOrderRecord.getWorkOrderId());
        setTenantId(bizWorkOrderRecord);
        boolean status = bizWorkOrderRecordService.saveOrUpdate(bizWorkOrderRecord);
        return toAjax(status);
    }
}
