package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.*;
import com.ym.mec.biz.dal.dto.*;
import com.ym.mec.biz.dal.entity.*;
import com.ym.mec.biz.dal.enums.GroupType;
import com.ym.mec.biz.dal.enums.KitGroupPurchaseTypeEnum;
import com.ym.mec.biz.dal.enums.OrderTypeEnum;
import com.ym.mec.biz.dal.enums.SporadicChargeTypeEnum;
import com.ym.mec.biz.dal.page.EndCourseScheduleQueryInfo;
import com.ym.mec.biz.dal.page.StudentPaymentOrderQueryInfo;
import com.ym.mec.biz.service.CourseScheduleService;
import com.ym.mec.biz.service.ExportService;
import com.ym.mec.biz.service.StudentPaymentOrderService;
import com.ym.mec.biz.service.StudentService;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.thirdparty.storage.StoragePluginContext;
import com.ym.mec.thirdparty.storage.provider.KS3StoragePlugin;
import com.ym.mec.util.collection.MapUtil;
import com.ym.mec.util.date.DateUtil;
import com.ym.mec.util.excel.POIUtil;
import com.ym.mec.util.upload.UploadUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.*;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    private StudentPaymentOrderService studentPaymentOrderService;
    @Autowired
    private StudentPaymentRouteOrderDao studentPaymentRouteOrderDao;
    @Autowired
    private VipGroupDao vipGroupDao;
    @Autowired
    private PracticeGroupDao practiceGroupDao;
    @Autowired
    private SellOrderDao sellOrderDao;
    @Autowired
    private StudentRegistrationDao studentRegistrationDao;
    @Autowired
    private MusicGroupDao musicGroupDao;
    @Autowired
    private StoragePluginContext storagePluginContext;
    @Autowired
    private ManagerDownloadDao managerDownloadDao;
    @Autowired
    private CooperationOrganDao cooperationOrganDao;
    @Autowired
    private ReplacementInstrumentActivityDao replacementInstrumentActivityDao;
    @Autowired
    private CourseScheduleService courseScheduleService;
    @Autowired
    private StudentService studentService;

    @Override
    @Async
    public void orderList(Map<String, Object> params, ManagerDownload managerDownload) throws Exception {
        List<StudentPaymentOrderExportDto> studentPaymentOrderExportDtos = studentPaymentOrderService.ExportQueryPage(params);
        long i = 1;
        Map<Integer, String> cooperationOrganMap = new HashMap<>();
        List<Integer> cooperationOrganIds = studentPaymentOrderExportDtos.stream()
                .filter(e -> OrderTypeEnum.OUTORDER.equals(e.getType()))
                .filter(e -> e.getMusicGroupId() != null)
                .map(e -> Integer.valueOf(e.getMusicGroupId()))
                .distinct()
                .collect(Collectors.toList());
        if (cooperationOrganIds.size() > 0) {
            List<CooperationOrgan> cooperationOrgans = cooperationOrganDao.getCooperationOrganByIds(cooperationOrganIds);
            cooperationOrganMap = cooperationOrgans.stream().collect(Collectors.toMap(CooperationOrgan::getId, CooperationOrgan::getName));
        }
        for (StudentPaymentOrderExportDto row : studentPaymentOrderExportDtos) {
            if (row.getActualAmount() == null) {
                row.setActualAmount(BigDecimal.ZERO);
            }
            if (row.getBalancePaymentAmount() == null) {
                row.setBalancePaymentAmount(BigDecimal.ZERO);
            }
            if (row.getMemo() == null) {
                row.setMemo("");
            }
            BigDecimal transferFee = BigDecimal.ZERO;
            if (row.getPaymentChannel() != null && row.getPaymentChannel().equals("ADAPAY")) {
                transferFee = row.getActualAmount().multiply(new BigDecimal("0.28")).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
            }
            row.setTransferFee(transferFee);

            String goodsName = "";
            if (row.getOrderDetailList() != null) {
                for (StudentPaymentOrderDetail orderDetail : row.getOrderDetailList()) {
                    switch (orderDetail.getType()) {
                        case COURSE:
                        case CLASSROOM:
                        case SINGLE:
                        case MIX:
                        case COMPREHENSIVE:
                        case ENLIGHTENMENT:
                        case TRAINING_SINGLE:
                        case TRAINING_MIX:
                        case MUSIC_NETWORK:
                        case HIGH:
                        case HIGH_ONLINE:
                        case HIGH_ONLINE_COURSE:
                            row.setMusicGroupCourseFee(row.getMusicGroupCourseFee().add(orderDetail.getPrice()));
                            break;
                        case MUSICAL:
                            if (orderDetail.getKitGroupPurchaseType() != null && orderDetail.getKitGroupPurchaseType().equals(KitGroupPurchaseTypeEnum.LEASE)) {
                                BigDecimal leaseFee = BigDecimal.ZERO;
                                if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                    leaseFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                                }
                                row.setLeaseFee(leaseFee);
                            } else {
                                row.setMusicalFee(orderDetail.getPrice());
                            }
                            break;
                        case ACCESSORIES:
                        case TEACHING:
                            row.setTeachingFee(row.getTeachingFee().add(orderDetail.getPrice()));
                            break;
                        case OTHER:
                            row.setOtherFee(row.getOtherFee().add(orderDetail.getPrice()));
                            break;
                        case MAINTENANCE:
                            BigDecimal repairFee = BigDecimal.ZERO;
                            if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                repairFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                            }
                            row.setMaintenanceFee(repairFee);
                            break;
                        case CLOUD_TEACHER:
                        case CLOUD_TEACHER_PLUS:
                            BigDecimal cloudTeacherFee = BigDecimal.ZERO;
                            if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                cloudTeacherFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                            }
                            row.setCloudTeacherFee(cloudTeacherFee);
                            break;
                        case DEGREE_REGISTRATION:
                            BigDecimal degreeFee = BigDecimal.ZERO;
                            if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                degreeFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                            }
                            row.setDegreeFee(degreeFee);
                            break;
                        case VIP:
                            BigDecimal vipCourseFee = BigDecimal.ZERO;
                            if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                vipCourseFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                            }
                            row.setVipCourseFee(vipCourseFee);
                            break;
                        case THEORY_COURSE:
                            BigDecimal theoryCourseFee = BigDecimal.ZERO;
                            if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                theoryCourseFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                            }
                            row.setTheoryCourseFee(theoryCourseFee);
                            break;
                        default:
                            break;
                    }
                }
            }
            if (row.getGoodsList() != null) {
                goodsName = row.getGoodsList().stream().map(Goods::getName).collect(Collectors.joining("|"));
            }
            if (StringUtils.isNotBlank(goodsName)) {
                row.setMemo(goodsName);
            }
            //专业
            if (row.getGroupType().equals(GroupType.VIP)) {
                row.setVipCourseFee(row.getActualAmount());
                VipGroup vipGroupInfo = vipGroupDao.findVipGroupInfo(Integer.parseInt(row.getMusicGroupId()), row.getClassGroupId());
                if (vipGroupInfo != null) {
                    row.setSchoolName(vipGroupInfo.getSchoolName());
                    row.setSubjectName(vipGroupInfo.getSubjectName());
                    row.setEduTeacher(vipGroupInfo.getEducationalTeacherName());
                }
            } else if (row.getGroupType().equals(GroupType.SPORADIC)) {
                //考级报名
                if (row.getType().equals(OrderTypeEnum.DEGREE_REGISTRATION)) {
                    String[] feeTypes = row.getMemo().split("；");
                    List<String> feeTypeList = Arrays.asList(feeTypes);
                    for (String feeTypeStr : feeTypeList) {
                        int index = feeTypeStr.lastIndexOf("：");
                        if (index < 0) {
                            continue;
                        }
                        BigDecimal typeFee = new BigDecimal(feeTypeStr.substring(index + 1));
                        if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                            typeFee = typeFee.multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                        }
                        if (feeTypeStr.contains("VIP")) {
                            row.setVipCourseFee(typeFee);
                        } else if (feeTypeStr.contains("网管课")) {
                            row.setPracticeCourseFee(typeFee);
                        } else if (feeTypeStr.contains("乐理课")) {
                            row.setTheoryCourseFee(typeFee);
                        }
                    }
                    row.setDegreeFee(row.getActualAmount().subtract(row.getVipCourseFee()).subtract(row.getPracticeCourseFee()).subtract(row.getTheoryCourseFee()));
                } else {
                    if (row.getChargeType() != null && SporadicChargeTypeEnum.LEVEL.getCode().equals(row.getChargeType())) {
                        row.setDegreeFee(row.getActualAmount());
                    } else if (row.getChargeType() != null && SporadicChargeTypeEnum.PRACTICE_GROUP_BUY.getCode().equals(row.getChargeType())) {
                        row.setPracticeCourseFee(row.getActualAmount());
                    } else if (row.getChargeType() != null && SporadicChargeTypeEnum.MUSIC_UPKEEP.getCode().equals(row.getChargeType())) {
                        row.setMaintenanceFee(row.getActualAmount());
                    } else if (row.getChargeType() != null && SporadicChargeTypeEnum.VISITING_FEE.getCode().equals(row.getChargeType())) {
                        row.setVisitFee(row.getActualAmount());
                    } else if (row.getChargeType() != null && (SporadicChargeTypeEnum.RECHARGE.getCode().equals(row.getChargeType()) ||
                            SporadicChargeTypeEnum.HIGH_ONLINE_ACTIVITY.getCode().equals(row.getChargeType()))) {
                        row.setRechargeFee(row.getActualAmount());
                    } else if (row.getChargeType() != null && SporadicChargeTypeEnum.CLOUD_TEACHER_BUY.getCode().equals(row.getChargeType())) {
                        row.setCloudTeacherFee(row.getActualAmount());
                    } else {
                        row.setOtherFee(row.getActualAmount());
                    }
                }
                if (row.getChargeType() != null) {
                    for (SporadicChargeTypeEnum chargeType : SporadicChargeTypeEnum.values()) {
                        if (!chargeType.getCode().equals(row.getChargeType())) continue;
                        row.setSporadicType(chargeType.getMsg());
                    }
                }
            } else if (row.getGroupType().equals(GroupType.PRACTICE)) {
                row.setPracticeCourseFee(row.getActualAmount());
                PracticeCourseDto practiceGroup = practiceGroupDao.findByGroupId(Integer.parseInt(row.getMusicGroupId()));
                if (practiceGroup != null) {
                    row.setSubjectName(practiceGroup.getSubjectName());
                    row.setEduTeacher(practiceGroup.getEduTeacherName());
                }
            } else if (row.getGroupType().equals(GroupType.REPLACEMENT)) {
                row.setMusicalFee(row.getActualAmount());
                ReplacementInstrumentActivityStatDto info = replacementInstrumentActivityDao.getInfo(Integer.parseInt(row.getMusicGroupId()));
                if (info != null) {
                    row.setSubjectName(info.getSubjectName());
                    row.setCooperationOrganName(info.getCooperationOrganName());
                    row.setMemo(info.getBrand() + "(" + info.getSpecification() + ")");
                    CooperationOrgan cooperationOrganEduTeacher = musicGroupDao.findCooperationOrganEduTeacher(info.getCooperationOrganId());
                    if (cooperationOrganEduTeacher != null) {
                        row.setEduTeacher(cooperationOrganEduTeacher.getLinkman());
                    }
                }
            } else {
                StudentPaymentOrderExportDto feeByType = sellOrderDao.getFeeByType(row.getId());
                BigDecimal childRepairFee = sellOrderDao.getChildRepair(row.getId());
                row.setMusicalFee(feeByType.getMusicalFee());
                row.setTeachingFee(feeByType.getTeachingFee().subtract(childRepairFee));
                row.setMaintenanceProductFee(feeByType.getRepairFee().add(childRepairFee));
                row.setOtherFee(feeByType.getOtherFee());
                if (row.getGroupType().equals(GroupType.MUSIC)) {
                    StudentRegistration studentRegistration = studentRegistrationDao.findStudentByMusicGroupIdAndUserId(row.getMusicGroupId(), row.getUserId());
                    if (studentRegistration != null) {
                        row.setSubjectName(studentRegistration.getSubjectName());
                        row.setSchoolName(studentRegistration.getSchoolName());
                        row.setCooperationOrganName(studentRegistration.getRemark());
                        row.setEduTeacher(studentRegistration.getName());
                    }
                    row.setMusicGroupCourseFee(feeByType.getActualAmount().subtract(feeByType.getMusicalFee()).subtract(row.getTeachingFee()).subtract(row.getMaintenanceFee()).subtract(row.getMaintenanceProductFee()).subtract(feeByType.getOtherFee()).subtract(row.getLeaseFee()).subtract(row.getCloudTeacherFee()));
                } else if (row.getType().equals(OrderTypeEnum.REPAIR)) {
                    row.setRepairFee(feeByType.getActualAmount().subtract(feeByType.getMusicalFee()).subtract(feeByType.getTeachingFee()).subtract(feeByType.getOtherFee()));
                } else if (row.getType().equals(OrderTypeEnum.OUTORDER)) {
                    row.setMusicGroupCourseFee(feeByType.getActualAmount().subtract(feeByType.getMusicalFee()).subtract(row.getTeachingFee()).subtract(row.getMaintenanceProductFee()).subtract(feeByType.getOtherFee()));
                    row.setTransferFee(BigDecimal.ZERO);
                }
            }
            //如果合作单位不存在取学员的第一个乐团的合作单位,乐团主管
            if (row.getCooperationOrganName() == null) {
                CooperationOrgan cooperationOrgan = studentService.getStudentEduTeacher(row.getUserId());
                if (cooperationOrgan != null) {
                    row.setCooperationOrganName(cooperationOrgan.getName());
                    row.setEduTeacher(cooperationOrgan.getLinkman() != null ? cooperationOrgan.getLinkman() : row.getEduTeacher());
                }
            }
            if (row.getType().equals(OrderTypeEnum.OUTORDER) && row.getMusicGroupId() != null) {
                if (cooperationOrganMap.containsKey(Integer.valueOf(row.getMusicGroupId()))) {
                    row.setCooperationOrganName(cooperationOrganMap.get(Integer.valueOf(row.getMusicGroupId())));
                    CooperationOrgan cooperationOrganEduTeacher = musicGroupDao.findCooperationOrganEduTeacher(Integer.valueOf(row.getMusicGroupId()));
                    if (cooperationOrganEduTeacher != null) {
                        row.setEduTeacher(cooperationOrganEduTeacher.getLinkman());
                    }
                }
            }
            if (row.getMusicGroupId() != null && row.getMusicGroupId().equals("null")) {
                row.setMusicGroupId("");
            }
            String paymentChannel = "";
            if (row.getPaymentChannel() == null) {
            } else if (row.getPaymentChannel().equals("YQPAY")) {
                paymentChannel = "双乾";
            } else if (row.getPaymentChannel().equals("ADAPAY")) {
                paymentChannel = "汇付";
            } else if (row.getPaymentChannel().equals("BALANCE")) {
                paymentChannel = "余额";
            }
            row.setPaymentChannel(paymentChannel);
            row.setId(i);
            row.setRepairFee(row.getRepairFee().add(row.getMaintenanceFee()).add(row.getMaintenanceProductFee()));
            i++;
        }

        String basePath = new ApplicationHome(this.getClass()).getSource().getParentFile().getPath();
        File file = new File(basePath + "/" + managerDownload.getName());
        FileOutputStream fileOutputStream = new FileOutputStream(file);

        HSSFWorkbook workbook = null;
        try {
            String[] header = {"序号", "学生姓名", "交易流水号", "订单编号", "收款渠道", "收款账户", "应付金额", "现金支付", "余额支付", "乐团课", "VIP课", "网管课", "乐理课", "考级", "维修费用", "云教练", "押金", "乐器", "教辅费用", "上门费", "账户充值", "其它", "手续费", "到账时间",
                    "关联乐团ID/VIP课ID", "课程形态", "零星收款类别", "专业", "分部", "教学点", "合作单位", "乐团主管", "备注"};
            String[] body = {"id", "user.username", "transNo", "orderNo", "paymentChannel", "merNos", "expectAmount", "actualAmount", "balancePaymentAmount", "musicGroupCourseFee", "vipCourseFee", "practiceCourseFee", "theoryCourseFee", "degreeFee", "repairFee", "cloudTeacherFee", "leaseFee", "musicalFee", "teachingFee", "visitFee", "rechargeFee", "otherFee", "transferFee", "payTime", "musicGroupId",
                    "groupType.desc", "sporadicType", "subjectName", "organName", "schoolName", "cooperationOrganName", "eduTeacher", "memo"};
            workbook = POIUtil.exportExcel(header, body, studentPaymentOrderExportDtos);
            workbook.write(fileOutputStream);
            fileOutputStream.getFD().sync();
            fileOutputStream.close();

            String folder = "download/" + UploadUtil.getFileFloder();
            String url = storagePluginContext.uploadFile(KS3StoragePlugin.PLUGIN_NAME, folder, file);
            //把记录插入下载表
            managerDownload.setFileUrl(url);
            managerDownload.setStatus(1);
            managerDownloadDao.update(managerDownload);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
                workbook.close();
                file.delete();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    @Async
    public void routeOrderList(Map<String, Object> params, ManagerDownload managerDownload) throws Exception {
        List<StudentPaymentOrderExportDto> studentPaymentOrderExportDtos = studentPaymentRouteOrderDao.ExportQueryPage(params);

        Map<Integer, String> cooperationOrganMap = new HashMap<>();
        List<Integer> cooperationOrganIds = studentPaymentOrderExportDtos.stream()
                .filter(e -> OrderTypeEnum.OUTORDER.equals(e.getType()))
                .filter(e -> e.getMusicGroupId() != null)
                .map(e -> Integer.valueOf(e.getMusicGroupId()))
                .distinct()
                .collect(Collectors.toList());
        if (cooperationOrganIds.size() > 0) {
            List<CooperationOrgan> cooperationOrgans = cooperationOrganDao.getCooperationOrganByIds(cooperationOrganIds);
            cooperationOrganMap = cooperationOrgans.stream().collect(Collectors.toMap(CooperationOrgan::getId, CooperationOrgan::getName));
        }
        long i = 1;
        for (StudentPaymentOrderExportDto row : studentPaymentOrderExportDtos) {
            if (row.getActualAmount() == null) {
                row.setActualAmount(BigDecimal.ZERO);
            }
            if (row.getBalancePaymentAmount() == null) {
                row.setBalancePaymentAmount(BigDecimal.ZERO);
            }
            if (row.getRouteAmount() == null) {
                row.setRouteAmount(BigDecimal.ZERO);
            }
            if (row.getMemo() == null) {
                row.setMemo("");
            }
            BigDecimal transferFee = BigDecimal.ZERO;
            if (row.getPaymentChannel() != null && row.getPaymentChannel().equals("ADAPAY")) {
                FeeFlagNumDto countFeeFlagNum = studentPaymentRouteOrderDao.getCountFeeFlagNum(row.getOrderNo());
                if (countFeeFlagNum.getTotalNum() > countFeeFlagNum.getYesNum() && row.getFeeFlag().equals("Y")) {
                    transferFee = row.getActualAmount().multiply(new BigDecimal("0.28")).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                }
                if (countFeeFlagNum.getTotalNum().equals(countFeeFlagNum.getYesNum())) {
                    BigDecimal totalTransferFee = row.getActualAmount().multiply(new BigDecimal("0.28")).divide(new BigDecimal(100), 2, BigDecimal.ROUND_HALF_UP);
                    transferFee = totalTransferFee.multiply(row.getRouteAmount()).divide(row.getActualAmount(), 2, BigDecimal.ROUND_HALF_UP);
                }
            }
            row.setTransferFee(transferFee);

            String goodsName = "";
            if (row.getOrderDetailList() != null) {
                for (StudentPaymentOrderDetail orderDetail : row.getOrderDetailList()) {
                    switch (orderDetail.getType()) {
                        case COURSE:
                        case CLASSROOM:
                        case SINGLE:
                        case MIX:
                        case COMPREHENSIVE:
                        case ENLIGHTENMENT:
                        case TRAINING_SINGLE:
                        case TRAINING_MIX:
                        case MUSIC_NETWORK:
                        case HIGH:
                        case HIGH_ONLINE:
                        case HIGH_ONLINE_COURSE:
                            row.setMusicGroupCourseFee(row.getMusicGroupCourseFee().add(orderDetail.getPrice()));
                            break;
                        case MUSICAL:
                            if (orderDetail.getKitGroupPurchaseType() != null && orderDetail.getKitGroupPurchaseType().equals(KitGroupPurchaseTypeEnum.LEASE)) {
                                BigDecimal leaseFee = orderDetail.getPrice();
                                if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                    leaseFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                                }
                                row.setLeaseFee(leaseFee);
                            } else {
                                row.setMusicalFee(orderDetail.getPrice());
                            }
                            break;
                        case ACCESSORIES:
                        case TEACHING:
                            row.setTeachingFee(row.getTeachingFee().add(orderDetail.getPrice()));
                            break;
                        case OTHER:
                            row.setOtherFee(row.getOtherFee().add(orderDetail.getPrice()));
                            break;
                        case MAINTENANCE:
                            BigDecimal maintenanceFee = BigDecimal.ZERO;
                            if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                maintenanceFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                            }
                            row.setMaintenanceFee(maintenanceFee);
                            break;
                        case CLOUD_TEACHER:
                        case CLOUD_TEACHER_PLUS:
                            BigDecimal cloudTeacherFee = BigDecimal.ZERO;
                            if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                cloudTeacherFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                            }
                            row.setCloudTeacherFee(cloudTeacherFee);
                            break;
                        case DEGREE_REGISTRATION:
                            BigDecimal degreeFee = BigDecimal.ZERO;
                            if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                degreeFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                            }
                            row.setDegreeFee(degreeFee);
                            break;
                        case VIP:
                            BigDecimal vipCourseFee = BigDecimal.ZERO;
                            if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                vipCourseFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                            }
                            row.setVipCourseFee(vipCourseFee);
                            break;
                        case THEORY_COURSE:
                            BigDecimal theoryCourseFee = BigDecimal.ZERO;
                            if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                                theoryCourseFee = orderDetail.getPrice().multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                            }
                            row.setTheoryCourseFee(theoryCourseFee);
                            break;
                        default:
                            break;
                    }
                }
            }
            if (row.getGoodsList() != null) {
                goodsName = row.getGoodsList().stream().map(Goods::getName).collect(Collectors.joining("|"));
            }
            if (StringUtils.isNotBlank(goodsName)) {
                row.setMemo(goodsName);
            }
            //专业
            if (row.getGroupType().equals(GroupType.VIP)) {
                row.setVipCourseFee(row.getActualAmount());
                VipGroup vipGroupInfo = vipGroupDao.findVipGroupInfo(Integer.parseInt(row.getMusicGroupId()), row.getClassGroupId());
                if (vipGroupInfo != null) {
                    row.setSchoolName(vipGroupInfo.getSchoolName());
                    row.setSubjectName(vipGroupInfo.getSubjectName());
                    row.setEduTeacher(vipGroupInfo.getEducationalTeacherName());
                }
            } else if (row.getGroupType().equals(GroupType.SPORADIC)) {
                //考级报名
                if (row.getType().equals(OrderTypeEnum.DEGREE_REGISTRATION)) {
                    String[] feeTypes = row.getMemo().split("；");
                    List<String> feeTypeList = Arrays.asList(feeTypes);
                    for (String feeTypeStr : feeTypeList) {
                        int index = feeTypeStr.lastIndexOf("：");
                        if (index < 0) {
                            continue;
                        }
                        BigDecimal typeFee = new BigDecimal(feeTypeStr.substring(index + 1));
                        if (row.getExpectAmount().compareTo(BigDecimal.ZERO) > 0) {
                            typeFee = typeFee.multiply(row.getActualAmount()).divide(row.getExpectAmount(), 2, BigDecimal.ROUND_DOWN);
                        }
                        if (feeTypeStr.contains("VIP")) {
                            row.setVipCourseFee(typeFee);
                        } else if (feeTypeStr.contains("网管课")) {
                            row.setPracticeCourseFee(typeFee);
                        } else if (feeTypeStr.contains("乐理课")) {
                            row.setTheoryCourseFee(typeFee);
                        }
                    }
                    row.setDegreeFee(row.getActualAmount().subtract(row.getVipCourseFee()).subtract(row.getPracticeCourseFee()).subtract(row.getTheoryCourseFee()));
                } else {
                    if (row.getChargeType() != null && SporadicChargeTypeEnum.LEVEL.getCode().equals(row.getChargeType())) {
                        row.setDegreeFee(row.getActualAmount());
                    } else if (row.getChargeType() != null && SporadicChargeTypeEnum.PRACTICE_GROUP_BUY.getCode().equals(row.getChargeType())) {
                        row.setPracticeCourseFee(row.getActualAmount());
                    } else if (row.getChargeType() != null && SporadicChargeTypeEnum.MUSIC_UPKEEP.getCode().equals(row.getChargeType())) {
                        row.setMaintenanceFee(row.getActualAmount());
                    } else if (row.getChargeType() != null && SporadicChargeTypeEnum.VISITING_FEE.getCode().equals(row.getChargeType())) {
                        row.setVisitFee(row.getActualAmount());
                    } else if (row.getChargeType() != null && (SporadicChargeTypeEnum.RECHARGE.getCode().equals(row.getChargeType()) ||
                            SporadicChargeTypeEnum.HIGH_ONLINE_ACTIVITY.getCode().equals(row.getChargeType()))) {
                        row.setRechargeFee(row.getActualAmount());
                    } else if (row.getChargeType() != null && SporadicChargeTypeEnum.CLOUD_TEACHER_BUY.getCode().equals(row.getChargeType())) {
                        row.setCloudTeacherFee(row.getActualAmount());
                    } else {
                        row.setOtherFee(row.getActualAmount());
                    }
                }
                if (row.getChargeType() != null) {
                    for (SporadicChargeTypeEnum chargeType : SporadicChargeTypeEnum.values()) {
                        if (!chargeType.getCode().equals(row.getChargeType())) continue;
                        row.setSporadicType(chargeType.getMsg());
                    }
                }
            } else if (row.getGroupType().equals(GroupType.PRACTICE)) {
                row.setPracticeCourseFee(row.getActualAmount());
                PracticeCourseDto practiceGroup = practiceGroupDao.findByGroupId(Integer.parseInt(row.getMusicGroupId()));
                if (practiceGroup != null) {
                    row.setSubjectName(practiceGroup.getSubjectName());
                    row.setEduTeacher(practiceGroup.getEduTeacherName());
                }
            } else if (row.getGroupType().equals(GroupType.REPLACEMENT)) {
                row.setMusicalFee(row.getActualAmount());
                ReplacementInstrumentActivityStatDto info = replacementInstrumentActivityDao.getInfo(Integer.parseInt(row.getMusicGroupId()));
                if (info != null) {
                    row.setSubjectName(info.getSubjectName());
                    row.setCooperationOrganName(info.getCooperationOrganName());
                    row.setMemo(info.getBrand() + "(" + info.getSpecification() + ")");
                    CooperationOrgan cooperationOrganEduTeacher = musicGroupDao.findCooperationOrganEduTeacher(info.getCooperationOrganId());
                    if (cooperationOrganEduTeacher != null) {
                        row.setEduTeacher(cooperationOrganEduTeacher.getLinkman());
                    }
                }
            } else {
                StudentPaymentOrderExportDto feeByType = sellOrderDao.getFeeByType(row.getId());
                BigDecimal childRepairFee = sellOrderDao.getChildRepair(row.getId());
                row.setMusicalFee(feeByType.getMusicalFee());
                row.setTeachingFee(feeByType.getTeachingFee().subtract(childRepairFee));
                row.setMaintenanceProductFee(feeByType.getRepairFee().add(childRepairFee));
                row.setOtherFee(feeByType.getOtherFee());
                if (row.getGroupType().equals(GroupType.MUSIC)) {
                    StudentRegistration studentRegistration = studentRegistrationDao.findStudentByMusicGroupIdAndUserId(row.getMusicGroupId(), row.getUserId());
                    if (studentRegistration != null) {
                        row.setSubjectName(studentRegistration.getSubjectName());
                        row.setSchoolName(studentRegistration.getSchoolName());
                        row.setCooperationOrganName(studentRegistration.getRemark());
                        row.setEduTeacher(studentRegistration.getName());
                    }
                    row.setMusicGroupCourseFee(feeByType.getActualAmount().subtract(feeByType.getMusicalFee()).subtract(row.getTeachingFee()).subtract(row.getMaintenanceFee()).subtract(row.getMaintenanceProductFee()).subtract(feeByType.getOtherFee()).subtract(row.getLeaseFee()).subtract(row.getCloudTeacherFee()));
                } else if (row.getType().equals(OrderTypeEnum.REPAIR)) {
                    row.setRepairFee(feeByType.getActualAmount().subtract(feeByType.getMusicalFee()).subtract(feeByType.getTeachingFee()).subtract(feeByType.getOtherFee()));
                } else if (row.getType().equals(OrderTypeEnum.OUTORDER)) {
                    row.setMusicGroupCourseFee(feeByType.getActualAmount().subtract(feeByType.getMusicalFee()).subtract(feeByType.getTeachingFee()).subtract(feeByType.getRepairFee()).subtract(feeByType.getOtherFee()));
                    row.setTransferFee(BigDecimal.ZERO);
                }
            }
            //如果合作单位不存在取学员的第一个乐团的合作单位和教学点
            if (row.getCooperationOrganName() == null) {
                CooperationOrgan cooperationOrgan = studentService.getStudentEduTeacher(row.getUserId());
                if (cooperationOrgan != null) {
                    row.setCooperationOrganName(cooperationOrgan.getName());
                    row.setEduTeacher(cooperationOrgan.getLinkman() != null ? cooperationOrgan.getLinkman() : row.getEduTeacher());
                }
            }
            if (row.getType().equals(OrderTypeEnum.OUTORDER) && row.getMusicGroupId() != null) {
                if (cooperationOrganMap.containsKey(Integer.valueOf(row.getMusicGroupId()))) {
                    row.setCooperationOrganName(cooperationOrganMap.get(Integer.valueOf(row.getMusicGroupId())));
                    CooperationOrgan cooperationOrganEduTeacher = musicGroupDao.findCooperationOrganEduTeacher(Integer.valueOf(row.getMusicGroupId()));
                    if (cooperationOrganEduTeacher != null) {
                        row.setEduTeacher(cooperationOrganEduTeacher.getLinkman());
                    }
                }
            }
            if (row.getMusicGroupId() != null && row.getMusicGroupId().equals("null")) {
                row.setMusicGroupId("");
            }
            String paymentChannel = "";
            if (row.getPaymentChannel() == null) {
            } else if (row.getPaymentChannel().equals("YQPAY")) {
                paymentChannel = "双乾";
            } else if (row.getPaymentChannel().equals("ADAPAY")) {
                paymentChannel = "汇付";
            } else if (row.getPaymentChannel().equals("BALANCE")) {
                paymentChannel = "余额";
            }
            row.setPaymentChannel(paymentChannel);
            i++;
        }

        Map<Long, List<StudentPaymentOrderExportDto>> orderMap = studentPaymentOrderExportDtos.stream().collect(Collectors.groupingBy(StudentPaymentOrderExportDto::getId));
        orderMap.forEach((orderId, orders) -> {
            int j = 1;
            BigDecimal routeMusicGroupCourseFee = BigDecimal.ZERO;
            BigDecimal routeVipCourseFee = BigDecimal.ZERO;
            BigDecimal routePracticeCourseFee = BigDecimal.ZERO;
            BigDecimal routeTheoryCourseFee = BigDecimal.ZERO;
            BigDecimal routeDegreeFee = BigDecimal.ZERO;
            BigDecimal routeRepairFee = BigDecimal.ZERO;
            BigDecimal routeMaintenanceFee = BigDecimal.ZERO;
            BigDecimal routeMaintenanceProductFee = BigDecimal.ZERO;
            BigDecimal routeLeaseFee = BigDecimal.ZERO;
            BigDecimal routeMusicalFee = BigDecimal.ZERO;
            BigDecimal routeTeachingFee = BigDecimal.ZERO;
            BigDecimal routeOtherFee = BigDecimal.ZERO;
            BigDecimal routeCloudTeacherFee = BigDecimal.ZERO;
            for (StudentPaymentOrderExportDto order : orders) {
                Date endDate = DateUtil.stringToDate("2021-04-01", "yyyy-MM-dd");
                if (order.getPayTime() == null || order.getPayTime().compareTo(endDate) < 0) {
                    break;
                }
                if (order.getSaleAmount().compareTo(BigDecimal.ZERO) > 0 && order.getServiceAmount().compareTo(BigDecimal.ZERO) == 0) {
                    order.setMusicGroupCourseFee(BigDecimal.ZERO);
                    order.setVipCourseFee(BigDecimal.ZERO);
                    order.setPracticeCourseFee(BigDecimal.ZERO);
                    order.setTheoryCourseFee(BigDecimal.ZERO);
                    order.setDegreeFee(BigDecimal.ZERO);
                    order.setCloudTeacherFee(BigDecimal.ZERO);
                    order.setRepairFee(order.getMaintenanceProductFee());
                    continue;
                } else if (order.getSaleAmount().compareTo(BigDecimal.ZERO) == 0 && order.getServiceAmount().compareTo(BigDecimal.ZERO) > 0) {
                    order.setRepairFee(order.getRepairFee().add(order.getMaintenanceFee()));
                    order.setCloudTeacherFee(order.getCloudTeacherFee());
                    order.setMusicalFee(BigDecimal.ZERO);
                    order.setTeachingFee(BigDecimal.ZERO);
                    order.setOtherFee(BigDecimal.ZERO);
                    order.setLeaseFee(BigDecimal.ZERO);
                    continue;
                }
                BigDecimal musicGroupCourseFee = BigDecimal.ZERO;
                BigDecimal vipCourseFee = BigDecimal.ZERO;
                BigDecimal practiceCourseFee = BigDecimal.ZERO;
                BigDecimal theoryCourseFee = BigDecimal.ZERO;
                BigDecimal degreeFee = BigDecimal.ZERO;
                BigDecimal repairFee = BigDecimal.ZERO;
                BigDecimal maintenanceFee = BigDecimal.ZERO;
                BigDecimal maintenanceProductFee = BigDecimal.ZERO;
                BigDecimal leaseFee = BigDecimal.ZERO;
                BigDecimal musicalFee = BigDecimal.ZERO;
                BigDecimal teachingFee = BigDecimal.ZERO;
                BigDecimal otherFee = BigDecimal.ZERO;
                BigDecimal cloudTeacherFee = BigDecimal.ZERO;

                if (order.getActualAmount() != null && order.getActualAmount().compareTo(BigDecimal.ZERO) > 0) {
                    musicGroupCourseFee = order.getMusicGroupCourseFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    vipCourseFee = order.getVipCourseFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    practiceCourseFee = order.getPracticeCourseFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    theoryCourseFee = order.getTheoryCourseFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    degreeFee = order.getDegreeFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    repairFee = order.getRepairFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    maintenanceFee = order.getMaintenanceFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    maintenanceProductFee = order.getMaintenanceProductFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    leaseFee = order.getLeaseFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    musicalFee = order.getMusicalFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    teachingFee = order.getTeachingFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    otherFee = order.getOtherFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                    cloudTeacherFee = order.getCloudTeacherFee().multiply(order.getRouteAmount()).divide(order.getActualAmount(), 2, BigDecimal.ROUND_DOWN);
                }

                if (order.getGroupType().equals(GroupType.VIP)) {
                    vipCourseFee = order.getRouteAmount();
                } else if (order.getGroupType().equals(GroupType.SPORADIC)) {
                    //考级报名
                    if (order.getType().equals(OrderTypeEnum.DEGREE_REGISTRATION)) {
                        degreeFee = order.getRouteAmount().subtract(vipCourseFee).subtract(practiceCourseFee).subtract(theoryCourseFee);
                    } else {
                        if (order.getChargeType() != null && SporadicChargeTypeEnum.LEVEL.getCode().equals(order.getChargeType())) {
                            degreeFee = order.getRouteAmount();
                        } else if (order.getChargeType() != null && SporadicChargeTypeEnum.PRACTICE_GROUP_BUY.getCode().equals(order.getChargeType())) {
                            practiceCourseFee = order.getRouteAmount();
                        } else {
                            otherFee = order.getRouteAmount();
                        }
                    }
                } else if (order.getGroupType().equals(GroupType.PRACTICE)) {
                    practiceCourseFee = order.getRouteAmount();
                } else {
                    if (order.getGroupType().equals(GroupType.MUSIC)) {
                        musicGroupCourseFee = order.getRouteAmount().subtract(musicalFee).subtract(teachingFee).subtract(maintenanceFee).subtract(maintenanceProductFee).subtract(otherFee).subtract(leaseFee).subtract(cloudTeacherFee);
                    } else if (order.getType().equals(OrderTypeEnum.REPAIR)) {
                        repairFee = order.getRouteAmount().subtract(musicalFee).subtract(teachingFee).subtract(otherFee);
                    } else if (order.getType().equals(OrderTypeEnum.OUTORDER)) {
                        musicGroupCourseFee = order.getRouteAmount().subtract(musicalFee).subtract(teachingFee).subtract(maintenanceProductFee).subtract(otherFee);
                    }
                }

                if (j == orders.size()) {
                    musicGroupCourseFee = order.getMusicGroupCourseFee().subtract(routeMusicGroupCourseFee);
                    vipCourseFee = order.getVipCourseFee().subtract(routeVipCourseFee);
                    practiceCourseFee = order.getPracticeCourseFee().subtract(routePracticeCourseFee);
                    theoryCourseFee = order.getTheoryCourseFee().subtract(routeTheoryCourseFee);
                    degreeFee = order.getDegreeFee().subtract(routeDegreeFee);
                    repairFee = order.getRepairFee().subtract(routeRepairFee);
                    maintenanceFee = order.getMaintenanceFee().subtract(routeMaintenanceFee);
                    maintenanceProductFee = order.getMaintenanceProductFee().subtract(routeMaintenanceProductFee);
                    leaseFee = order.getLeaseFee().subtract(routeLeaseFee);
                    musicalFee = order.getMusicalFee().subtract(routeMusicalFee);
                    teachingFee = order.getTeachingFee().subtract(routeTeachingFee);
                    otherFee = order.getOtherFee().subtract(routeOtherFee);
                    cloudTeacherFee = order.getCloudTeacherFee().subtract(routeCloudTeacherFee);
                }
                j++;

                routeMusicGroupCourseFee = routeMusicGroupCourseFee.add(musicGroupCourseFee);
                routeVipCourseFee = routeVipCourseFee.add(vipCourseFee);
                routePracticeCourseFee = routePracticeCourseFee.add(practiceCourseFee);
                routeTheoryCourseFee = routeTheoryCourseFee.add(theoryCourseFee);
                routeDegreeFee = routeDegreeFee.add(degreeFee);
                routeRepairFee = routeRepairFee.add(repairFee);
                routeMaintenanceFee = routeMaintenanceFee.add(maintenanceFee);
                routeMaintenanceProductFee = routeMaintenanceProductFee.add(maintenanceProductFee);
                routeLeaseFee = routeLeaseFee.add(leaseFee);
                routeMusicalFee = routeMusicalFee.add(musicalFee);
                routeTeachingFee = routeTeachingFee.add(teachingFee);
                routeOtherFee = routeOtherFee.add(otherFee);
                routeCloudTeacherFee = routeCloudTeacherFee.add(cloudTeacherFee);

                order.setMusicGroupCourseFee(musicGroupCourseFee);
                order.setVipCourseFee(vipCourseFee);
                order.setPracticeCourseFee(practiceCourseFee);
                order.setTheoryCourseFee(theoryCourseFee);
                order.setDegreeFee(degreeFee);
                order.setRepairFee(repairFee.add(maintenanceFee).add(maintenanceProductFee).add(repairFee));
                order.setMaintenanceFee(maintenanceFee);
                order.setMaintenanceProductFee(maintenanceProductFee);
                order.setLeaseFee(leaseFee);
                order.setMusicalFee(musicalFee);
                order.setTeachingFee(teachingFee);
                order.setOtherFee(otherFee);
                order.setCloudTeacherFee(cloudTeacherFee);
            }
        });
        String[] organIdArr = params.get("oldOrganId").toString().split(",");
        List<String> organIds = Arrays.asList(organIdArr);
        studentPaymentOrderExportDtos.removeIf(order -> !organIds.contains(order.getOrganId().toString()));

        String basePath = new ApplicationHome(this.getClass()).getSource().getParentFile().getPath();
        File file = new File(basePath + "/" + managerDownload.getName());
        FileOutputStream fileOutputStream = new FileOutputStream(file);

        HSSFWorkbook workbook = null;
        try {
            String[] header = {"序号", "学生姓名", "交易流水号", "订单编号", "收款渠道", "收款账户", "应付金额", "现金支付", "余额支付", "分润账户", "分润金额", "分配余额", "乐团课", "VIP课", "网管课", "乐理课", "考级", "维修费用", "云教练", "押金", "乐器", "教辅费用", "上门费", "账户充值", "其它", "手续费", "到账时间",
                    "关联乐团ID/VIP课ID", "课程形态", "零星收款类别", "专业", "分部", "教学点", "合作单位", "乐团主管", "备注"};
            String[] body = {"id", "user.username", "transNo", "orderNo", "paymentChannel", "merNos", "expectAmount", "actualAmount", "balancePaymentAmount", "routeMerNo", "routeAmount", "routeBalance", "musicGroupCourseFee", "vipCourseFee", "practiceCourseFee", "theoryCourseFee", "degreeFee", "repairFee", "cloudTeacherFee", "leaseFee", "musicalFee", "teachingFee", "visitFee", "rechargeFee", "otherFee", "transferFee", "payTime", "musicGroupId",
                    "groupType.desc", "sporadicType", "subjectName", "organName", "schoolName", "cooperationOrganName", "eduTeacher", "memo"};
            workbook = POIUtil.exportExcel(header, body, studentPaymentOrderExportDtos);
            workbook.write(fileOutputStream);
            fileOutputStream.getFD().sync();
            fileOutputStream.close();

            String folder = "download/" + UploadUtil.getFileFloder();
            String url = storagePluginContext.uploadFile(KS3StoragePlugin.PLUGIN_NAME, folder, file);
            //把记录插入下载表
            managerDownload.setFileUrl(url);
            managerDownload.setStatus(1);
            managerDownloadDao.update(managerDownload);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
                workbook.close();
                file.delete();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    @Async
    public void superFindCourseSchedules(EndCourseScheduleQueryInfo queryInfo, ManagerDownload managerDownload) throws FileNotFoundException {
        List<CourseScheduleEndDto> rows = courseScheduleService.endFindCourseSchedules(queryInfo).getRows();
        for (CourseScheduleEndDto row : rows) {
            row.setIsComplaints(StringUtils.equals(row.getIsComplaints(), "1") ? "有" : "无");
        }
        String basePath = new ApplicationHome(this.getClass()).getSource().getParentFile().getPath();
        File file = new File(basePath + "/" + managerDownload.getName());
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        OutputStream ouputStream = null;
        try {
            HSSFWorkbook workbook = POIUtil.exportExcel(new String[]{"分部名称", "乐团主管", "课程编号", "开始时间", "结束时间",
                    "班级名称", "班级声部", "课程名称", "课程类型", "教学模式",
                    "教学点", "课程状态", "指导老师", "学员编号", "是否点名", "是否有考勤申诉", "预计上课人数"}, new String[]{
                    "organName", "educationTeacherName", "id", "startClassTime", "endClassTime", "classGroupName", "subjectName", "name",
                    "groupType.desc", "teachMode.msg", "schoolName", "status.msg", "teacherName", "studentId", "isCallNames.msg", "isComplaints", "studentNum"}, rows);
            workbook.write(fileOutputStream);
            fileOutputStream.getFD().sync();
            fileOutputStream.close();

            String folder = "download/" + UploadUtil.getFileFloder();
            String url = storagePluginContext.uploadFile(KS3StoragePlugin.PLUGIN_NAME, folder, file);
            //把记录插入下载表
            managerDownload.setFileUrl(url);
            managerDownload.setStatus(1);
            managerDownloadDao.update(managerDownload);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ouputStream != null) {
                try {
                    ouputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
