package com.yh.saas.plugin.yiliangyiyun.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaService;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.winsea.svc.base.base.entity.CommonCompany;
import com.winsea.svc.base.base.entity.CommonRoleResource;
import com.winsea.svc.base.base.entity.CommonStaff;
import com.winsea.svc.base.base.service.ICommonCompanyService;
import com.winsea.svc.base.base.service.ICommonRoleResourceService;
import com.winsea.svc.base.base.service.ICommonStaffService;
import com.winsea.svc.base.base.util.DateUtils;
import com.winsea.svc.base.security.entity.User;
import com.winsea.svc.base.security.util.AuthSecurityUtils;
import com.winsea.svc.base.workflow.entity.Workflow;
import com.yh.saas.common.support.util.IdGenerator;
import com.yh.saas.plugin.yiliangyiyun.constant.StatusEnum;
import com.yh.saas.plugin.yiliangyiyun.entity.*;
import com.yh.saas.plugin.yiliangyiyun.entity.view.ExportVView;
import com.yh.saas.plugin.yiliangyiyun.entity.view.PaymentView;
import com.yh.saas.plugin.yiliangyiyun.exception.YException;
import com.yh.saas.plugin.yiliangyiyun.exception.YExceptionEnum;
import com.yh.saas.plugin.yiliangyiyun.mapper.PaymentManagementMapper;
import com.yh.saas.plugin.yiliangyiyun.service.*;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yh.saas.plugin.yiliangyiyun.util.QRCodeUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.entity.ContentType;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 付款管理 服务实现类
 * </p>
 *
 * @author Gdc
 * @since 2021-09-06
 */
@SofaService
@Slf4j
public class PaymentManagementServiceImpl extends ServiceImpl<PaymentManagementMapper, PaymentManagement> implements IPaymentManagementService {

    @Autowired
    private INewWorkflowService workflowService;
    @SofaReference
    private ICommonRoleResourceService roleResourceService;
    @Autowired
    private IQualityInspectionManagementService qualityInspectionManagementService;
    @Autowired
    private IWeighingManagementService weighingManagementService;
    @Autowired
    private ICommonCompanyService commonCompanyService;
    @Autowired
    private IIdentityAuthenticationInfoService iIdentityAuthenticationInfoService;
    @Autowired
    private IContractManagementInfoService ContractManagementInfoService;
    @Autowired
    private IWarehouseInOutInfoService warehouseInOutInfoService;
    @Autowired
    private IWarehouseInOutDetailService warehouseInOutDetailService;
    @Autowired
    private IWarehouseBaseInfoService warehouseBaseInfoService;
    @Autowired
    private IWarehousePositionInfoService warehousePositionInfoService;
    @Autowired
    private ICommonStaffService staffService;
    @Autowired
    private IWarehousePositionStorageInfoService warehousePositionStorageInfoService;
    @Autowired
    private IWeightedDetailsService weightedDetailsService;
    @Autowired
    private ICostManagementInfoService costManagementInfoService;
    @Autowired
    private ICommonSysParameterService iCommonSysParameterService;
    @Autowired
    private IPurchasePriceDetailService purchasePriceDetailService;
    @Autowired
    private IPurchasePriceService purchasePriceService;
    @Value("${file-root-path}")
    private String localPath;
    @Autowired
    private QRCodeUtil qrCodeUtil ;
    @Autowired
    private OSSClient ossClient;
    @Value("${oss.endpoint.internal:}")
    private String baseUrl;
    @Getter
    @Value("${oss.bucket.name:}")
    private String bucket;
    /**
     * 付款管理列表
     *
     * @param paymentManagement
     * @return
     */
    @Override
    public Page<PaymentManagement> selectPaymentManagement(PaymentManagement paymentManagement) {
        Map<String, Object> pageView = new HashMap<>();
        pageView.put("startRecord", (paymentManagement.getCurrentPage() - 1)
                * paymentManagement.getPageSize());
        List<String> businessKeys = null;
        if ("1".equals(paymentManagement.getSearchType())) {
            businessKeys = workflowService.getTaskBusinessKeysByCode("PAYMENT-MANAGEMENT-APPROVE");
        }
        List<String> statusSet = new ArrayList<>();
        List<String> resourceIdList = this.getResourceIdList();
        if (resourceIdList != null && resourceIdList.contains("procurementInReport-Edit")) { // 填写，提交    权限做完以后替换
            List<String> statusList = Lists.newArrayList(StatusEnum.PUR_RETURN.getFlag());
            statusSet.addAll(statusList);
        }
        //  公司ID
        pageView.put("compId", paymentManagement.getCompId());
        pageView.put("searchKeyWord", paymentManagement.getSearchKeyWord());
        pageView.put("searchType", paymentManagement.getSearchType());
        pageView.put("pcFlag", paymentManagement.getPcFlag());
        pageView.put("pageSize", paymentManagement.getPageSize());
        pageView.put("startDate", paymentManagement.getStartDate());
        pageView.put("endDate", paymentManagement.getEndDate());
        pageView.put("invoicing", paymentManagement.getInvoicing());
        pageView.put("appFlag", paymentManagement.getAppFlag());
        pageView.put("warehouseName", paymentManagement.getWarehouseName());
        pageView.put("goodsName", paymentManagement.getGoodsName());
        pageView.put("customerPhone", paymentManagement.getCustomerPhone());
        pageView.put("contractNo", paymentManagement.getContractNo());
        pageView.put("currentPage", paymentManagement.getCurrentPage());
        pageView.put("managementType", paymentManagement.getManagementType());
        pageView.put("businessKeys", businessKeys);
        pageView.put("statusSet", statusSet);
        // 查询服务商总数
        Integer dataCount = baseMapper.getCountByCondition(pageView);
        List<PaymentManagement> dataList = baseMapper.getListByCondition(pageView);
        if (!CollectionUtils.isEmpty(dataList)) {
            dataList.forEach(paymentManagement1 -> {
//                CommonCompany commonCompany = commonCompanyService.selectOne(new EntityWrapper<CommonCompany>()
//                        .eq("comp_id", paymentManagement1.getCompId()));
//                //公司名
//                if (commonCompany != null) {
//                    paymentManagement1.setCompName(commonCompany.getCompName());
//                }
//                IdentityAuthenticationInfo identityAuthenticationInfo = iIdentityAuthenticationInfoService.selectOne(new EntityWrapper<IdentityAuthenticationInfo>()
//                        .eq("customer_name", paymentManagement1.getCustomerName())
//                        .eq("customer_phone", paymentManagement1.getCustomerPhone())
////                        .eq("cover", "0")
//                        .eq("customer_type_flag", "1"));
//                if (identityAuthenticationInfo != null) {
//                    paymentManagement1.setIdentityAuthenticationInfo(identityAuthenticationInfo);
//                }
//                WeighingManagement weighingManagement = weighingManagementService.selectOne(new EntityWrapper<WeighingManagement>()
//                        .eq("relation_id", paymentManagement1.getRelationId())
//                        .eq("comp_id", paymentManagement1.getCompId()));
//                if (weighingManagement != null) {
//                    paymentManagement1.setContractPrice(weighingManagement.getContractPrice());
//                    paymentManagement1.setWeighingPictureAddress(weighingManagement.getPictureAddress());
//                    paymentManagement1.setWeighingManagement(weighingManagement);
//                }
//                QualityInspectionManagement qualityInspectionManagement = qualityInspectionManagementService.selectOne(new EntityWrapper<QualityInspectionManagement>()
//                        .eq("relation_id", paymentManagement1.getRelationId())
//                        .eq("comp_id", paymentManagement1.getCompId()));
//                if (qualityInspectionManagement != null) {
//                    paymentManagement1.setQualityInspectionPictureAddress(qualityInspectionManagement.getPictureAddress());
//                    paymentManagement1.setQualityInspectionManagement(qualityInspectionManagement);
//                }
                String taskId = "";
                // 只有待审核状态才有taskId
                if (StringUtils.isNotBlank(paymentManagement1.getWorkflowId())) {
                    JSONObject jsonObject = workflowService.getActiveTask(Lists.newArrayList(paymentManagement1.getWorkflowId()), paymentManagement1.getId());
                    taskId = jsonObject.getString("taskId");
                    paymentManagement1.setTaskId(taskId);
                }
            });

        }
        Page<PaymentManagement> page = new Page<>();
        page.setRecords(dataList == null ? Lists.newArrayList() : dataList);
        page.setTotal(dataCount == null ? 0 : dataCount);
        page.setCurrent(paymentManagement.getCurrentPage());
        page.setSize(paymentManagement.getPageSize());
        return page;
    }

    /**
     * 查看
     *
     * @param id
     * @return
     */
    @Override
    public PaymentManagement getInfo(String id) {
        PaymentManagement paymentManagement = this.selectById(id);
        //质检信息
        QualityInspectionManagement qualityInspectionManagement = qualityInspectionManagementService.selectOne(new EntityWrapper<QualityInspectionManagement>()
                .eq("relation_id", paymentManagement.getRelationId())
                .eq("comp_id", paymentManagement.getCompId()));
        //检斤信息
        WeighingManagement weighingManagement = weighingManagementService.selectOne(new EntityWrapper<WeighingManagement>()
                .eq("relation_id", paymentManagement.getRelationId())
                .eq("comp_id", paymentManagement.getCompId()));
        //公司信息
        CommonCompany commonCompany = commonCompanyService.selectOne(new EntityWrapper<CommonCompany>()
                .eq("comp_id", paymentManagement.getCompId()));
        //合同信息
        ContractManagementInfo contractManagementInfo=ContractManagementInfoService.selectOne(new EntityWrapper<ContractManagementInfo>()
                .eq("comp_id",paymentManagement.getCompId())
                .eq("contract_no",paymentManagement.getContractNo()));
        if (contractManagementInfo!=null){
            paymentManagement.setContractManagement(contractManagementInfo);
        }
        //仓库信息
        WarehouseBaseInfo warehouseBaseInfo=warehouseBaseInfoService.selectOne(new EntityWrapper<WarehouseBaseInfo>()
                .eq("comp_id",qualityInspectionManagement.getCompId())
                .eq("warehouse_name",qualityInspectionManagement.getWarehouseName()));
        qualityInspectionManagement.setCodeUrl(qrCodeUtil.generateQRCodeImage(qualityInspectionManagement.getCompId()+"&"+qualityInspectionManagement.getQualityNo()+"&"+warehouseBaseInfo.getId(),800,800,"",qualityInspectionManagement.getId()));
        //公司名
        if (commonCompany != null) {
            paymentManagement.setCompName(commonCompany.getCompName());
            paymentManagement.setTaxRegistrationNo(commonCompany.getCompImo());
        }
        //联系人
        if(warehouseBaseInfo != null){
            paymentManagement.setContacts(warehouseBaseInfo.getPersonCharge());
            paymentManagement.setContactsPhone(warehouseBaseInfo.getPersonPhone());
        }
        PurchasePrice purchasePrice = purchasePriceService.selectOne(new EntityWrapper<PurchasePrice>()
                .eq("warehouse_name", qualityInspectionManagement.getWarehouseName())
                .eq("goods_name", qualityInspectionManagement.getGoodsName())
                .eq("status_flag", "3"));
        if (purchasePrice != null) {
            if (purchasePrice.getParamType().equals("1")) {
                PurchasePriceDetail purchasePriceDetail = purchasePriceDetailService.selectOne(new EntityWrapper<PurchasePriceDetail>()
                        .eq("main_id", purchasePrice.getId())
                        .like("level", qualityInspectionManagement.getGrade().replace("品", ""))
                        .eq("delete_flag", "0"));
                String[] point = purchasePriceDetail.getPoint().split(",");
                String[] pointPrice = purchasePriceDetail.getPointPrice().split(",");
                Float param = 0f;
                for (int i = 0; i < point.length - 1; i++) {
                    if (Float.parseFloat(qualityInspectionManagement.getWaterContent()) >= Float.parseFloat(point[i])
                            && Float.parseFloat(qualityInspectionManagement.getWaterContent()) < Float.parseFloat(point[i + 1])) {
                        param += (Float.parseFloat(qualityInspectionManagement.getWaterContent()) - Float.parseFloat(point[i])) * Float.parseFloat(pointPrice[i]);
                        break;
                    } else {
                        param += (Float.parseFloat(point[i + 1]) - Float.parseFloat(point[i])) * Float.parseFloat(pointPrice[i]);
                    }
                }
                param += qualityInspectionManagement.getBuckleMiscellaneous();
                DecimalFormat df = new DecimalFormat("#.00");
                param = Float.parseFloat(df.format(param));
                paymentManagement.setParam(param);
                paymentManagement.setWeightDeduction(param);
            } else {
                paymentManagement.setWeightDeduction(weighingManagement.getBuckleMiscellaneous());
            }
            qualityInspectionManagement.setWaterMin(purchasePrice.getWaterMin());
        }
        IdentityAuthenticationInfo identityAuthenticationInfo = iIdentityAuthenticationInfoService.selectOne(new EntityWrapper<IdentityAuthenticationInfo>()
                .eq("customer_name", paymentManagement.getCustomerName())
                .eq("customer_phone", paymentManagement.getCustomerPhone())
//                .eq("cover", "0")
                .eq("customer_type_flag", "1"));
        paymentManagement.setIdentityAuthenticationInfo(identityAuthenticationInfo);
        paymentManagement.setQualityInspectionManagement(qualityInspectionManagement);
        paymentManagement.setWeighingManagement(weighingManagement);
        return paymentManagement;
    }

    /**
     * 结算
     *
     * @param paymentManagement
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editInfo(PaymentManagement paymentManagement) {
        this.updateById(paymentManagement);
        PaymentManagement paymentManagement1 = this.selectById(paymentManagement.getId());
        paymentManagement1.setSettlementDate(new Date());
        //未付
        paymentManagement1.setAmountNotPayable(paymentManagement.getAmountIngPayable() - paymentManagement1.getAmountEdPayable());
        if ("1".equals(paymentManagement1.getReverseFlag())){
            paymentManagement1.setReverseFlag("0");
            this.updateById(paymentManagement1);
            workflowService.activateInstance(paymentManagement.getWorkflowId(),paymentManagement.getId());
        }
        else {
            this.openAuditFlow(paymentManagement1);
        }
        //查询关联流转记录
        QualityInspectionManagement qualityInspectionManagement = qualityInspectionManagementService.selectOne(new EntityWrapper<QualityInspectionManagement>()
                .eq("relation_id", paymentManagement1.getRelationId())
                .eq("comp_id", paymentManagement1.getCompId()));
        WarehouseInOutInfo warehouseInOutInfo=warehouseInOutInfoService.selectById(qualityInspectionManagement.getWarehouseInOutId());
        if (warehouseInOutInfo!=null){
            warehouseInOutInfo.setPaymentId(paymentManagement.getId());
            warehouseInOutInfoService.updateById(warehouseInOutInfo);
        }
        else{
            WarehouseInOutInfo warehouseInOutInfo1 = new WarehouseInOutInfo();
            WarehouseBaseInfo warehouseBaseInfo = warehouseBaseInfoService.selectOne(new EntityWrapper<WarehouseBaseInfo>()
                    .eq("warehouse_name", paymentManagement1.getWarehouseName())
                    .eq("comp_id", qualityInspectionManagement.getCompId()));
            //生成流转信息
            warehouseInOutInfo1.setId(IdGenerator.generateUUID());
            warehouseInOutInfo1.setPaymentId(paymentManagement.getId());
            warehouseInOutInfo1.setWarehouseName(paymentManagement1.getWarehouseName());
            warehouseInOutInfo1.setGrossWeight(paymentManagement1.getGrossWeight() / 1000);
            warehouseInOutInfo1.setTare(paymentManagement1.getTare() / 1000);
            warehouseInOutInfo1.setBaseId(warehouseBaseInfo.getId());
            //查到仓位编号
            WarehousePositionInfo warehousePositionInfo = warehousePositionInfoService.selectOne(new EntityWrapper<WarehousePositionInfo>()
                    .eq("base_id", warehouseBaseInfo.getId())
                    .eq("bin_number", qualityInspectionManagement.getBinNumber()));
            warehouseInOutInfo1.setPositionId(warehousePositionInfo.getId());
            warehouseInOutInfo1.setBinNumber(qualityInspectionManagement.getBinNumber());
            warehouseInOutInfo1.setGoodsName(paymentManagement1.getGoodsName());
            CommonSysParameter commonSysParameter = iCommonSysParameterService.selectOne(new EntityWrapper<CommonSysParameter>().eq("const_id", "CON2").eq("const_value", paymentManagement1.getGoodsName()));
            warehouseInOutInfo1.setGoodsNameKey(commonSysParameter.getConstKey());
            warehouseInOutInfo1.setInOutType("收购入库");
            warehouseInOutInfo1.setInOutFlag("2");
            warehouseInOutInfo1.setCarNo(paymentManagement1.getCarNo());
            warehouseInOutInfo1.setContractNo(paymentManagement1.getPaymentNo());
            warehouseInOutInfo1.setAgent(paymentManagement.getSettlementClerk());
            warehouseInOutInfo1.setInOutDate(new Date());
            warehouseInOutInfo1.setGrade(qualityInspectionManagement.getGrade());
            //潮粮取纯重，干粮取净重
//        if ("潮粮".equals(paymentManagement1.getType())) {
//            warehouseInOutInfo1.setPureWeight(paymentManagement.getPureWeight() / 1000);
//            warehouseInOutInfo1.setNetWeight(paymentManagement.getNetWeight() / 1000);
//        } else {
//            warehouseInOutInfo1.setNetWeight(paymentManagement.getNetWeight() / 1000);
//
//        }
            if (paymentManagement.getPureWeight() != null) {
                warehouseInOutInfo1.setPureWeight(paymentManagement.getPureWeight() / 1000);
            }
            warehouseInOutInfo1.setNetWeight(Double.valueOf(paymentManagement.getNetWeight() / 1000));
            warehouseInOutInfo1.setStatusFlag("3");
            warehouseInOutInfo1.setDeductionWeight(paymentManagement.getWeightDeduction());
            warehouseInOutInfoService.insert(warehouseInOutInfo1);
            float original = 0f;
            // 查询货物库存量
            WarehousePositionStorageInfo warehousePositionStorageInfo = warehousePositionStorageInfoService.selectOne(new EntityWrapper<WarehousePositionStorageInfo>()
                    .eq("position_id", warehouseInOutInfo1.getPositionId())
                    .eq("goods_name_key", warehouseInOutInfo1.getGoodsNameKey())
                    .eq("delete_flag", "0"));
            // 判断是否有货
            if (warehousePositionStorageInfo != null) {
                original = Float.valueOf(String.valueOf(warehousePositionStorageInfo.getStorage()));
                warehousePositionStorageInfo.setGoodsNameKey(warehouseInOutInfo1.getGoodsNameKey());
                // 更新库存量
                warehousePositionStorageInfo.setStorage(warehousePositionStorageInfo.getStorage() + Float.valueOf(String.valueOf(warehouseInOutInfo1.getNetWeight())));
                warehousePositionStorageInfoService.updateById(warehousePositionStorageInfo);
            }
            // 没有货
            else {
                original = 0f;
                WarehousePositionStorageInfo warehousePositionStorageInfo1 = new WarehousePositionStorageInfo();
                warehousePositionStorageInfo1.setId(IdGenerator.generateUUID());
                warehousePositionStorageInfo1.setPositionId(warehouseInOutInfo1.getPositionId());
                warehousePositionStorageInfo1.setBinNumber(warehouseInOutInfo1.getBinNumber());
                warehousePositionStorageInfo1.setGoodsName(warehouseInOutInfo1.getGoodsName());
                warehousePositionStorageInfo1.setGoodsNameKey(warehouseInOutInfo1.getGoodsNameKey());
                warehousePositionStorageInfo1.setStorage(warehouseInOutInfo1.getNetWeight());
                warehousePositionStorageInfoService.insert(warehousePositionStorageInfo1);
            }
            WarehouseInOutDetail warehouseInOutDetail = new WarehouseInOutDetail();
            warehouseInOutDetail.setId(IdGenerator.generateUUID());
            warehouseInOutDetail.setInfoId(warehouseInOutInfo1.getId());
            warehouseInOutDetail.setQualityInspector(qualityInspectionManagement.getQualityInspector());
            warehouseInOutDetail.setWaterContent(qualityInspectionManagement.getWaterContent());
            warehouseInOutDetail.setImpurity(qualityInspectionManagement.getImpurity());
            warehouseInOutDetail.setBulkDensity(qualityInspectionManagement.getBulkDensity());
            warehouseInOutDetail.setMildewGrain(qualityInspectionManagement.getMildewGrain());
            warehouseInOutDetail.setJiaorenli(qualityInspectionManagement.getJiaorenli());
            warehouseInOutDetail.setImperfectGrain(qualityInspectionManagement.getImperfectGrain());
            warehouseInOutDetailService.insert(warehouseInOutDetail);
            WeightedDetails weightedDetails = new WeightedDetails();
            weightedDetails.setCompId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
            weightedDetails.setId(IdGenerator.generateUUID());
            weightedDetails.setWarehouseId(warehouseInOutInfo1.getBaseId());
            weightedDetails.setWarehouseName(warehouseInOutInfo1.getWarehouseName());
            weightedDetails.setGoodsNameKey(warehouseInOutInfo1.getGoodsNameKey());
            weightedDetails.setGoodsName(warehouseInOutInfo1.getGoodsName());
            weightedDetails.setGrainType(warehouseInOutInfo1.getType());
            weightedDetails.setInTypeKey(warehouseInOutInfo1.getInOutTypeKey());
            weightedDetails.setInType(warehouseInOutInfo1.getInOutType());
            weightedDetails.setNetWeight(Float.valueOf(String.valueOf(warehouseInOutInfo1.getNetWeight())));
            weightedDetails.setPureWeight(warehouseInOutInfo1.getPureWeight());
            weightedDetails.setUnitPrice(warehouseInOutInfo1.getCost());
            weightedDetails.setDeductionAmount(warehouseInOutInfo1.getDeductionAmount());
            weightedDetails.setFreight(warehouseInOutInfo1.getFreight());
            weightedDetails.setOriginalStock(original);

            // 查询入库全加权成本
            List<WeightedDetails> weightedDetailsList = weightedDetailsService.selectList(new EntityWrapper<WeightedDetails>().eq("warehouse_id", warehouseInOutInfo1.getBaseId())
                    .eq("goods_name", warehouseInOutInfo1.getGoodsName()).orderBy("update_date", false));
            if (CollectionUtils.isEmpty(weightedDetailsList)) {
                Double newCost = (paymentManagement.getAmountIngPayable() - paymentManagement.getAmountIngPayable() * 0.09) / (Double.valueOf(String.valueOf(warehouseInOutInfo1.getNetWeight())) + original);
                weightedDetails.setCostBefore(Float.valueOf(String.valueOf(newCost)));
                weightedDetails.setCostAfter(newCost);
            } else {
                weightedDetails.setCostBefore(weightedDetailsList.get(0).getCostBefore());
                Double newCost = ((paymentManagement.getAmountIngPayable() - paymentManagement.getAmountIngPayable() * 0.09) + original * weightedDetails.getCostBefore()) / (Double.valueOf(String.valueOf(warehouseInOutInfo1.getNetWeight())) + original);
                weightedDetails.setCostAfter(newCost);
            }
            // 计算新加权成本


            weightedDetailsService.insert(weightedDetails);
            // 修改成本信息表
            CostManagementInfo costManagementInfo = costManagementInfoService.selectOne(new EntityWrapper<CostManagementInfo>().eq("warehouse_id", weightedDetails.getWarehouseId())
                    .eq("comp_id", paymentManagement1.getCompId())
                    .eq("goods_name_key", weightedDetails.getGoodsNameKey()));
            List<WeightedDetails> weightedDetailsLists = weightedDetailsService.selectList(new EntityWrapper<WeightedDetails>().eq("warehouse_id", warehouseInOutInfo1.getBaseId())
                    .eq("goods_name_key", warehouseInOutInfo1.getGoodsNameKey()).orderBy("update_date", false));
            Double net = 0d;
            List<WarehousePositionInfo> warehousePositionInfos = warehousePositionInfoService.selectList(new EntityWrapper<WarehousePositionInfo>()
                    .eq("base_id", weightedDetails.getWarehouseId()));
            if (!CollectionUtils.isEmpty(warehousePositionInfos)) {
                for (int i = 0; i < warehousePositionInfos.size(); i++) {
                    List<WarehousePositionStorageInfo> warehousePositionStorageInfos = warehousePositionStorageInfoService.selectList(new EntityWrapper<WarehousePositionStorageInfo>()
                            .eq("position_id", warehousePositionInfos.get(i).getId())
                            .eq("goods_name_key", weightedDetails.getGoodsNameKey()));
                    for (int j = 0; j < warehousePositionStorageInfos.size(); j++) {
                        net = net + warehousePositionStorageInfos.get(j).getStorage();
                    }
                }
            }
            if (costManagementInfo == null) {
                CostManagementInfo costManagementInfo1 = new CostManagementInfo();
                costManagementInfo1.setCompId(AuthSecurityUtils.getCurrentUserInfo().getCompId());
                costManagementInfo1.setId(IdGenerator.generateUUID());
                costManagementInfo1.setWarehouseId(weightedDetails.getWarehouseId());
                costManagementInfo1.setWarehouseName(weightedDetails.getWarehouseName());
                costManagementInfo1.setGoodsNameKey(weightedDetails.getGoodsNameKey());
                costManagementInfo1.setGoodsName(weightedDetails.getGoodsName());
                costManagementInfo1.setWarehouseType(warehouseBaseInfo.getWarehouseType());

                costManagementInfo1.setStorage(net);
                costManagementInfo1.setCost(weightedDetails.getCostAfter());
                costManagementInfoService.insert(costManagementInfo1);
            } else {
                costManagementInfo.setCost(weightedDetails.getCostAfter());
                costManagementInfo.setStorage(net);
                costManagementInfoService.updateById(costManagementInfo);
            }
        }
        return "OK";
    }

    /**
     * 付款
     *
     * @param paymentManagement
     * @return
     */
    @Override
    public String payMoney(PaymentManagement paymentManagement) {
        List<PaymentManagement> paymentManagementList = paymentManagement.getPaymentManagementList();
        CommonStaff commonStaff = staffService.getInfo(AuthSecurityUtils.getCurrentUserId());
        paymentManagement.setCashier(commonStaff.getStaffName());
        Double money = paymentManagement.getMoney();
        //付款多条
        if (!CollectionUtils.isEmpty(paymentManagementList)) {
            for (PaymentManagement paymentManagement1 : paymentManagementList) {
                PaymentManagement paymentManagement2 = this.selectById(paymentManagement1.getId());
                //付款金额大于本次循环未付金额
                if (money >= paymentManagement1.getAmountNotPayable()) {
                    BigDecimal moneyx = new BigDecimal(Double.toString(money));
                    BigDecimal moneyy = new BigDecimal(Double.toString(paymentManagement1.getAmountNotPayable()));
                    money = moneyx.subtract(moneyy).doubleValue();
                    //全部付款
                    paymentManagement1.setStatus(StatusEnum.PUR_PAYaLL.getName());
                    paymentManagement1.setStatusFlag(StatusEnum.PUR_PAYaLL.getFlag());
                    paymentManagement1.setPaymentDate(paymentManagement.getPaymentDate());
                    paymentManagement1.setPaymentScreenshot(paymentManagement2.getPaymentScreenshot() != null ? paymentManagement2.getPaymentScreenshot() + '$' + paymentManagement.getPaymentScreenshot() : paymentManagement.getPaymentScreenshot());
                    paymentManagement1.setAmountEdPayable(paymentManagement1.getAmountEdPayable() + paymentManagement1.getAmountNotPayable());
                    paymentManagement1.setAmountNotPayable(0.0d);
                    this.updateById(paymentManagement1);
                } else if (money > 0) {
                    BigDecimal moneyx = new BigDecimal(Double.toString(money));
                    BigDecimal moneyy = new BigDecimal(Double.toString(paymentManagement1.getAmountNotPayable()));
                    Double moneyTmp = moneyx.subtract(moneyy).doubleValue();
                    //部分付款
                    paymentManagement1.setStatus(StatusEnum.PUR_PAY.getName());
                    paymentManagement1.setStatusFlag(StatusEnum.PUR_PAY.getFlag());
                    paymentManagement1.setPaymentDate(paymentManagement.getPaymentDate());
                    paymentManagement1.setPaymentScreenshot(paymentManagement2.getPaymentScreenshot() != null ? paymentManagement2.getPaymentScreenshot() + '$' + paymentManagement.getPaymentScreenshot() : paymentManagement.getPaymentScreenshot());
                    paymentManagement1.setAmountEdPayable(paymentManagement1.getAmountEdPayable() + money);
                    paymentManagement1.setAmountNotPayable(paymentManagement1.getAmountNotPayable() - money);
                    this.updateById(paymentManagement1);
                    money = moneyTmp;
                } else {
                    break;
                }
            }
        }
        return "ok";
    }

    /**
     * 收款
     *
     * @param paymentManagement
     * @return
     */
    @Override
    public String collectMoney(PaymentManagement paymentManagement) {
        List<PaymentManagement> paymentManagementList = paymentManagement.getPaymentManagementList();
        Double money = paymentManagement.getMoney();
        //收款多条
        if (!CollectionUtils.isEmpty(paymentManagementList)) {
            for (PaymentManagement paymentManagement1 : paymentManagementList) {
                PaymentManagement paymentManagement2 = this.selectById(paymentManagement1.getId());
                //收款金额大于本次循环未收金额
                if (money >= paymentManagement1.getAmountNotCollectable()) {
                    money = money - paymentManagement1.getAmountNotCollectable();
                    //全部收款
                    paymentManagement1.setStatus(StatusEnum.PUR_COLLECTaLL.getName());
                    paymentManagement1.setStatusFlag(StatusEnum.PUR_COLLECTaLL.getFlag());
                    paymentManagement1.setCollectionDate(paymentManagement.getCollectionDate());
                    paymentManagement1.setCollectionScreenshot(paymentManagement2.getCollectionScreenshot() != null ? paymentManagement2.getCollectionScreenshot() + '$' + paymentManagement.getCollectionScreenshot() : paymentManagement.getCollectionScreenshot());
                    paymentManagement1.setAmountEdCollectionable(paymentManagement1.getAmountEdCollectionable() + paymentManagement1.getAmountNotCollectable());
                    paymentManagement1.setAmountNotCollectable(0.0d);
                    this.updateById(paymentManagement1);
                } else if (money > 0) {
                    Double moneyTmp = money - paymentManagement1.getAmountNotCollectable();
                    //部分收款
                    paymentManagement1.setStatus(StatusEnum.PUR_COLLECT.getName());
                    paymentManagement1.setStatusFlag(StatusEnum.PUR_COLLECT.getFlag());
                    paymentManagement1.setCollectionDate(paymentManagement.getCollectionDate());
                    paymentManagement1.setCollectionScreenshot(paymentManagement2.getCollectionScreenshot() != null ? paymentManagement2.getCollectionScreenshot() + '$' + paymentManagement.getCollectionScreenshot() : paymentManagement.getCollectionScreenshot());
                    paymentManagement1.setAmountEdCollectionable(paymentManagement1.getAmountEdCollectionable() + money);
                    paymentManagement1.setAmountNotCollectable(paymentManagement1.getAmountNotCollectable() - money);
                    this.updateById(paymentManagement1);
                    money = moneyTmp;
                } else {
                    break;
                }
            }
        }
        return "OK";
    }

    /**
     * 合同编号下拉列表
     *
     * @param compId
     */
    @Override
    public List<ContractManagementInfo> selectContractNoList(String compId) {
        //查销售合同
        List<ContractManagementInfo> contractManagementInfoList = ContractManagementInfoService.selectList(new EntityWrapper<ContractManagementInfo>()
                .eq("comp_id", compId)
                .eq("contract_type", "1")
                .eq("delete_flag", "0")
                .orderBy("update_date", false));
        for (ContractManagementInfo contractManagementInfo : contractManagementInfoList) {
//            Float money = 0.0f;
            Double collectMoney = 0.0d;
            //查收款管理列表
            List<PaymentManagement> paymentManagementList = this.selectList(new EntityWrapper<PaymentManagement>()
                    .eq("contract_no", contractManagementInfo.getContractNo())
                    .eq("delete_flag", "0"));
            if (!CollectionUtils.isEmpty(paymentManagementList)) {
                for (PaymentManagement paymentManagement : paymentManagementList) {
//                    money = money + paymentManagement.getAmountNotPayable();money > 0 ||
                    collectMoney = collectMoney + paymentManagement.getAmountNotCollectable();
                }
                //有未付/未收金额
                if (collectMoney > 0) {
                    contractManagementInfo.setListStatus("待付款");
                } else {
                    contractManagementInfo.setListStatus("已付款");
                }
            }
        }
        return contractManagementInfoList;
    }

    /**
     * 添加备注
     *
     * @param paymentManagement
     * @return
     */
    @Override
    public String addRemarks(PaymentManagement paymentManagement) {
        List<PaymentManagement> paymentManagementList = paymentManagement.getPaymentManagementList();
        if (!CollectionUtils.isEmpty(paymentManagementList)) {
            for (PaymentManagement paymentManagement1 : paymentManagementList) {
                //关联的合同号加备注信息
                ContractManagementInfo contractManagementInfo = ContractManagementInfoService.selectOne(new EntityWrapper<ContractManagementInfo>()
                        .eq("contract_no", paymentManagement1.getContractNo())
                        .eq("comp_id", paymentManagement1.getCompId()));
                if (contractManagementInfo != null) {
                    contractManagementInfo.setRemarks(paymentManagement.getRemarks());
                    ContractManagementInfoService.updateById(contractManagementInfo);
                }
                paymentManagement1.setRemarks(paymentManagement.getRemarks());
                this.updateById(paymentManagement1);
            }
        }
        return "OK";
    }

    /**
     * 编辑应收
     *
     * @param paymentManagement
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editCollect(PaymentManagement paymentManagement) {
        if ("1".equals(paymentManagement.getReverseFlag())){
            paymentManagement.setReverseFlag("0");
        }
        boolean one = this.updateById(paymentManagement);
        if (one) {
            return "OK";
        }
        return "NG";
    }

    /**
     * 删除
     *
     * @param id
     */
    @Override
    public void deleteInfo(String id) {
        PaymentManagement paymentManagement = this.selectById(id);
        if (paymentManagement != null) {
            this.deleteById(paymentManagement.getId());
//            //质检信息
//            QualityInspectionManagement qualityInspectionManagement = qualityInspectionManagementService.selectOne(new EntityWrapper<QualityInspectionManagement>()
//                    .eq("relation_id", paymentManagement.getRelationId())
//                    .eq("comp_id", paymentManagement.getCompId()));
//            //检斤信息
//            WeighingManagement weighingManagement = weighingManagementService.selectOne(new EntityWrapper<WeighingManagement>()
//                    .eq("relation_id", paymentManagement.getRelationId())
//                    .eq("comp_id", paymentManagement.getCompId()));
//            qualityInspectionManagement.deleteById(qualityInspectionManagement.getId());
//            weighingManagement.deleteById(weighingManagement.getId());

        }
    }

    @Override
    public Float cumulant(PaymentManagement paymentManagement) {
        if(paymentManagement.getCustomerNumberCard() == null || paymentManagement.getCustomerNumberCard().isEmpty()){
            return 0f;
        }
        Float cumulant = 0.0f;
        //本年第一天零点
        SimpleDateFormat now = new SimpleDateFormat("yyyy-01-01 00:00:00");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.YEAR, 0);
        Date y = c.getTime();
        String year = now.format(y);
        if (paymentManagement.getCompId() == null || paymentManagement.getCompId().isEmpty()) {
            paymentManagement.setCompId(AuthSecurityUtils.getStaffById(AuthSecurityUtils.getCurrentUserId()).getCompId());
        }
        if (paymentManagement.getGoodsName().contains("玉米")){
            paymentManagement.setGoodsName("玉米");
        }
        Map<String, Object> pageView = new HashMap<>();

        //  公司ID
        pageView.put("compId", paymentManagement.getCompId());
        pageView.put("goodsName", paymentManagement.getGoodsName());
        pageView.put("customerNumberCard", paymentManagement.getCustomerNumberCard());
        pageView.put("createDate", year);

        // 查询服务商总数
        cumulant = baseMapper.getCustomerCount(pageView);
        cumulant += 100;
        return cumulant;
    }

    @Override
    public String openAuditFlow(PaymentManagement paymentManagement1) {
        PaymentManagement paymentManagement = this.selectById(paymentManagement1.getId());
        boolean isStartWorkflow = org.apache.commons.lang3.StringUtils.isBlank(paymentManagement.getWorkflowId());
        // 不是退回的单子
        if (isStartWorkflow) {
            Workflow workflow = workflowService
                    .findLatestWorkflowByBusinessCodeByApp(paymentManagement.getCompId(), "PAYMENT-MANAGEMENT-APPROVE");
            // 没配置审核流程，直接结束并处理信息
            if (workflow == null) {
                throw new YException(YExceptionEnum.PURCHASE_ORDER_ERROR);
            }
            // 开启审核流
            else {

                // 设置状态 已提交审核
                paymentManagement.setWorkflowId(workflow.getId());
                this.updateById(paymentManagement);
                workflowService.startInstance(workflow.getId(), paymentManagement.getId());
            }
        }
        // 退回的单子 再启用
        else {
            this.updateById(paymentManagement);
            workflowService.activateInstance(paymentManagement.getWorkflowId(), paymentManagement.getId());
        }
        return "OK";
    }

    @Override
    public String selectTaskId(PaymentManagement paymentManagement) {
        String taskIdNull = "";
        List<PaymentManagement> paymentManagementList = this.selectList(new EntityWrapper<PaymentManagement>()
                .eq("management_type", paymentManagement.getManagementType())
                .eq("comp_id", paymentManagement.getCompId())
                .eq("warehouse_name", paymentManagement.getWarehouseName()));
        if (!CollectionUtils.isEmpty(paymentManagementList)) {
            for (PaymentManagement paymentManagement1 : paymentManagementList) {
                String taskId = "";
                // 只有待审核状态才有taskId
                if (StringUtils.isNotBlank(paymentManagement1.getWorkflowId())) {
                    JSONObject jsonObject = workflowService.getActiveTask(Lists.newArrayList(paymentManagement1.getWorkflowId()), paymentManagement1.getId());
                    taskId = jsonObject.getString("taskId");
                    paymentManagement1.setTaskId(taskId);
                }
                if (paymentManagement1.getTaskId() != null) {
                    taskIdNull = "true";
                } else {
                    taskIdNull = "false";
                }
            }
        }
        return taskIdNull;
    }

    /**
     * 反结算
     * @param paymentManagement
     * @return
     */
    @Override
    public String ReSettlement(PaymentManagement paymentManagement) {
        workflowService.suspendInstance(paymentManagement.getWorkflowId(), paymentManagement.getId());
        paymentManagement.setReverseFlag("1");
        this.updateById(paymentManagement);
        WarehouseInOutInfo warehouseInOutInfo=warehouseInOutInfoService.selectOne(new EntityWrapper<WarehouseInOutInfo>()
        .eq("payment_id",paymentManagement.getId()));
        if (warehouseInOutInfo!=null){
            WarehousePositionStorageInfo warehousePositionStorageInfo = warehousePositionStorageInfoService.selectOne(new EntityWrapper<WarehousePositionStorageInfo>()
                    .eq("position_id", warehouseInOutInfo.getPositionId())
                    .eq("goods_name_key", warehouseInOutInfo.getGoodsNameKey()));
            if (warehousePositionStorageInfo!=null){
                warehousePositionStorageInfo.setStorage(warehousePositionStorageInfo.getStorage()-warehouseInOutInfo.getNetWeight());
                warehousePositionStorageInfoService.updateById(warehousePositionStorageInfo);
            }
            warehouseInOutInfoService.deleteById(warehouseInOutInfo.getId());
        }
        return "OK";
    }


    private List<String> getResourceIdList() {
        User currentUser = AuthSecurityUtils.getCurrentUserInfo();
        if (currentUser == null) {
            return null;
        }
        // 当前登录人主要角色
        return roleResourceService.getBindResourcesByUserId(currentUser.getUserId()).stream()
                .map(CommonRoleResource::getResourceId).collect(Collectors.toList());
    }


    /**
     * 导出
     *
     * @return
     * @throws Exception
     */
    @Override
    public void export(PaymentManagement paymentManagement, HttpServletResponse response) throws Exception {


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 查询
//        Page<ContractManagementInfo> contractManagementInfoPage = this.selectInfo(example);
//        List<ContractManagementInfo> contractManagementInfos = contractManagementInfoPage.getRecords();


        // 1.Excel的头部信息
        String headerTitle = "";

        // 2.创建一个webbook 对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();

        // 3.在webbook中添加一个sheet，对应Excel文件中的sheet
        HSSFSheet sheet;

        sheet = wb.createSheet("付款管理信息");
        headerTitle = "付款管理信息";

        // 4.设置打印参数
        sheet.setMargin(HSSFSheet.TopMargin, (short) 1);// 页边距（上）
        sheet.setMargin(HSSFSheet.BottomMargin, (short) 1);// 页边距（下）
        sheet.setMargin(HSSFSheet.LeftMargin, (short) 1);// 页边距（左）
        sheet.setMargin(HSSFSheet.RightMargin, (short) 1);// 页边距（右）

        // 不显示网格
        sheet.setDisplayGridlines(false);
        // 设置水平居中
        sheet.setHorizontallyCenter(true);

        /**************** 数据行样式 start ****************/
        HSSFFont fontTitle = wb.createFont();
        fontTitle.setFontName("宋体");
        fontTitle.setFontHeightInPoints((short) 14);
        // 设置字体加粗
        fontTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        HSSFFont fontComp = wb.createFont();
        fontComp.setFontName("宋体");
        fontComp.setFontHeightInPoints((short) 16);
        // 设置字体加粗
        fontComp.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        HSSFFont fontDetail = wb.createFont();
        fontDetail.setFontName("宋体");
        fontDetail.setFontHeightInPoints((short) 12);

        HSSFFont fontDetail1 = wb.createFont();
        fontDetail1.setFontName("宋体");
        fontDetail1.setFontHeightInPoints((short) 12);
        fontDetail1.setColor(HSSFColor.RED.index);

        // title
        HSSFCellStyle styleTitle = wb.createCellStyle();
        // 指定单元格居中对齐
        styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        styleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleTitle.setWrapText(true);
        // 设置单元格字体
        styleTitle.setFont(fontTitle);
        // 右边框
        styleTitle.setBorderRight(HSSFCellStyle.BORDER_NONE);
        // 左边框
        styleTitle.setBorderLeft(HSSFCellStyle.BORDER_NONE);
        // 上边框
        styleTitle.setBorderTop(HSSFCellStyle.BORDER_NONE);
        // 下边框
        styleTitle.setBorderBottom(HSSFCellStyle.BORDER_NONE);

        // title
        HSSFCellStyle styleComp = wb.createCellStyle();
        // 指定单元格居中对齐
        styleComp.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        styleComp.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleComp.setWrapText(true);
        // 设置单元格字体
        styleComp.setFont(fontComp);
        // 右边框
        styleComp.setBorderRight(HSSFCellStyle.BORDER_NONE);
        // 左边框
        styleComp.setBorderLeft(HSSFCellStyle.BORDER_NONE);
        // 上边框
        styleComp.setBorderTop(HSSFCellStyle.BORDER_NONE);
        // 下边框
        styleComp.setBorderBottom(HSSFCellStyle.BORDER_NONE);

        HSSFCellStyle styleInfo = wb.createCellStyle();
        // 指定单元格居中对齐
        styleInfo.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        // 指定单元格垂直居中对齐
        styleInfo.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleInfo.setWrapText(true);
        // 设置单元格字体
        styleInfo.setFont(fontDetail);
        // 右边框
        styleInfo.setBorderRight(HSSFCellStyle.BORDER_NONE);
        // 左边框
        styleInfo.setBorderLeft(HSSFCellStyle.BORDER_NONE);
        // 上边框
        styleInfo.setBorderTop(HSSFCellStyle.BORDER_NONE);
        // 下边框
        styleInfo.setBorderBottom(HSSFCellStyle.BORDER_NONE);

        HSSFCellStyle styleDetail = wb.createCellStyle();
        // 指定单元格居中对齐
        styleDetail.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        styleDetail.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleDetail.setWrapText(true);
        // 设置单元格字体
        styleDetail.setFont(fontDetail);
        // 右边框
        styleDetail.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        styleDetail.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        styleDetail.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        styleDetail.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        HSSFCellStyle styleDetail1 = wb.createCellStyle();
        // 指定单元格居中对齐
        styleDetail1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        styleDetail1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleDetail1.setWrapText(true);
        // 设置单元格字体
        styleDetail1.setFont(fontDetail1);
        // 右边框
        styleDetail1.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        styleDetail1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        styleDetail1.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        styleDetail1.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        HSSFCellStyle contextstyle = wb.createCellStyle();
        // 指定单元格居中对齐
        contextstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        contextstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        contextstyle.setWrapText(true);
        // 设置单元格字体
        contextstyle.setFont(fontDetail);
        // 右边框
        contextstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        contextstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        contextstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        contextstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        HSSFCellStyle contextstyle1 = wb.createCellStyle();
        // 指定单元格居中对齐
        contextstyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        contextstyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        contextstyle1.setWrapText(true);
        // 设置单元格字体
        contextstyle1.setFont(fontDetail1);
        // 右边框
        contextstyle1.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        contextstyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        contextstyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        contextstyle1.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        HSSFCellStyle styleDetailYellow = wb.createCellStyle();
        // 指定单元格居中对齐
        styleDetailYellow.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        styleDetailYellow.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleDetailYellow.setWrapText(true);
        // 设置单元格字体
        styleDetailYellow.setFont(fontDetail);
        // 右边框
        styleDetailYellow.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        styleDetailYellow.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        styleDetailYellow.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        styleDetailYellow.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleDetailYellow.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        styleDetailYellow.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);


        HSSFCellStyle styleDetailLeft = wb.createCellStyle();
        // 指定单元格居中对齐
        styleDetailLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        // 指定单元格垂直居中对齐
        styleDetailLeft.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleDetailLeft.setWrapText(true);
        // 设置单元格字体
        styleDetailLeft.setFont(fontDetail);
        // 右边框
        styleDetailLeft.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        styleDetailLeft.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        styleDetailLeft.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        styleDetailLeft.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        /**************** 数据行样式 end ****************/

        // 设置列宽
        sheet.setColumnWidth(0, 1 * 256 + 184);
        sheet.setColumnWidth(1, "采购公司".getBytes().length*3*256);
        sheet.setColumnWidth(2, "收货仓库".getBytes().length*25/10*256);
        sheet.setColumnWidth(3, 12 * 256 + 184);
        sheet.setColumnWidth(4, "开票日期".getBytes().length*2*256);
        sheet.setColumnWidth(5, "业务编号".getBytes().length*2*256);
        sheet.setColumnWidth(6, 12 * 256 + 184);
        sheet.setColumnWidth(7, "客户身份证号".getBytes().length*2*256);
        sheet.setColumnWidth(8, 12 * 256 + 184);
        sheet.setColumnWidth(9, 12 * 256 + 184);
        sheet.setColumnWidth(10, 12 * 256 + 184);
        sheet.setColumnWidth(11, 12 * 256 + 184);
        sheet.setColumnWidth(12, "货名".getBytes().length*3*256);
        sheet.setColumnWidth(13, 12 * 256 + 184);
        sheet.setColumnWidth(14, 12 * 256 + 184);
        sheet.setColumnWidth(15, 12 * 256 + 184);
        sheet.setColumnWidth(16, 12 * 256 + 184);
        sheet.setColumnWidth(17, 12 * 256 + 184);
        sheet.setColumnWidth(18, 12 * 256 + 184);
        sheet.setColumnWidth(19, 12 * 256 + 184);
        sheet.setColumnWidth(20, 12 * 256 + 184);
        sheet.setColumnWidth(21, 12 * 256 + 184);
        sheet.setColumnWidth(22, 12 * 256 + 184);
        sheet.setColumnWidth(23, 12 * 256 + 184);
        sheet.setColumnWidth(24, 12 * 256 + 184);
        sheet.setColumnWidth(25, 12 * 256 + 184);
        sheet.setColumnWidth(26, 12 * 256 + 184);
        sheet.setColumnWidth(27, 12 * 256 + 184);
        sheet.setColumnWidth(28, 12 * 256 + 184);
        sheet.setColumnWidth(29, 12 * 256 + 184);
        sheet.setColumnWidth(30, 12 * 256 + 184);
        sheet.setColumnWidth(31, 12 * 256 + 184);
        sheet.setColumnWidth(32, 12 * 256 + 184);
        sheet.setColumnWidth(33, 12 * 256 + 184);
        sheet.setColumnWidth(34, "卡号".getBytes().length*4*256);
        sheet.setColumnWidth(35, "开户支行".getBytes().length*4*256);
        sheet.setColumnWidth(36, 1 * 256 + 184);

        // 创建单元格对象
        HSSFCell cell = null;

        // 创建打印设置对象
        HSSFPrintSetup ps = sheet.getPrintSetup();
        // 打印方向，true：横向，false：纵向(默认)
        ps.setLandscape(false);
        // // 打印质量
        // ps.setVResolution((short) 700)
        // 设置缩放比例
        ps.setScale((short) 80);
        // 纸张类型
        ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);


        int rownum = 1;
        HSSFRow row0 = sheet.createRow((int) rownum);
        row0.setHeightInPoints(25);
        cell = row0.createCell(1);
        cell.setCellStyle(styleTitle);
        cell.setCellValue("付款信息");
        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, (short) 1, (short) 22));
        cell = row0.createCell(2);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(3);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(4);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(5);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(6);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(7);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(8);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(9);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(10);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(11);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(12);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(13);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(14);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(15);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(16);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(17);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(18);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(19);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(20);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(21);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(22);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(23);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(24);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(25);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(26);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(27);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(28);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(29);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(30);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(31);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(32);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(33);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(34);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(35);
        cell.setCellStyle(styleTitle);
        rownum++;


        Double waterContentTotal = 0d;//水分合计
        Double impurityTotal = 0d;//杂质合计
        Double mildewGrainTotal = 0d;//霉变粒合计
        Double imperfectGrainTotal = 0d;//不完善粒合计
        Double bulkDensityTotal = 0d;//容重合计
        Double jiaorenliTotal = 0d;//热损伤合计
        Double netWeightTotal = 0d;//净重合计
        Double amountIngPayableTotal = 0d;//应收合计
        for (int i = 0; i < paymentManagement.getPaymentManagementList().size(); i++) {
            PaymentManagement paymentManagement1 = paymentManagement.getPaymentManagementList().get(i);
            paymentManagement1.setIdentityAuthenticationInfo(iIdentityAuthenticationInfoService.selectById(paymentManagement1.getIdentityId()));
            paymentManagement1.setQualityInspectionManagement(qualityInspectionManagementService.selectOne(
                    new EntityWrapper<QualityInspectionManagement>()
                            .eq("relation_id", paymentManagement1.getRelationId())
                            .eq("comp_id", paymentManagement1.getCompId())
            ));
            paymentManagement1.setWeighingManagement(weighingManagementService.selectOne(
                    new EntityWrapper<WeighingManagement>()
                            .eq("relation_id", paymentManagement1.getRelationId())
                            .eq("comp_id", paymentManagement1.getCompId())
            ));
            //水分累计和
            waterContentTotal = waterContentTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getQualityInspectionManagement().getWaterContent()!=null?paymentManagement1.getQualityInspectionManagement().getWaterContent():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            impurityTotal = impurityTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getQualityInspectionManagement().getImpurity()!=null?paymentManagement1.getQualityInspectionManagement().getImpurity():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            mildewGrainTotal = mildewGrainTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getQualityInspectionManagement().getMildewGrain()!=null?paymentManagement1.getQualityInspectionManagement().getMildewGrain():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            imperfectGrainTotal = imperfectGrainTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getQualityInspectionManagement().getImperfectGrain()!=null?paymentManagement1.getQualityInspectionManagement().getImperfectGrain():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            bulkDensityTotal = bulkDensityTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getQualityInspectionManagement().getBulkDensity()!=null?paymentManagement1.getQualityInspectionManagement().getBulkDensity():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            jiaorenliTotal = jiaorenliTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getQualityInspectionManagement().getJiaorenli()!=null?paymentManagement1.getQualityInspectionManagement().getJiaorenli():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            netWeightTotal = netWeightTotal + (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight())));
            amountIngPayableTotal = amountIngPayableTotal + paymentManagement1.getAmountIngPayable();
        }

        HSSFRow rowp = sheet.createRow((int)(rownum));
        rowp.setHeightInPoints(20);
        cell = rowp.createCell(1);
        cell.setCellValue("平均值/合计");
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(2);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(3);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(4);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(5);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(6);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(7);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(8);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(9);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(10);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(11);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(12);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(13);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(14);
        cell.setCellStyle(styleDetail);
        //水分平均值
        cell = rowp.createCell(15);
        cell.setCellValue(waterContentTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(16);
        cell.setCellValue(impurityTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(17);
        cell.setCellValue(mildewGrainTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(18);
        cell.setCellValue(imperfectGrainTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(19);
        cell.setCellValue(bulkDensityTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(20);
        cell.setCellValue(jiaorenliTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(21);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(22);
        cell.setCellValue(paymentManagement.getPaymentManagementList().size());
        cell.setCellStyle(styleDetail1);
        cell = rowp.createCell(23);
        cell.setCellValue(amountIngPayableTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(24);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(25);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(26);
        cell.setCellStyle(styleDetail);
        //净重合计
        cell = rowp.createCell(27);
        cell.setCellValue(netWeightTotal);
        cell.setCellStyle(styleDetail1);
        cell = rowp.createCell(28);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(29);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(30);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(31);
        cell.setCellValue(amountIngPayableTotal);
        cell.setCellStyle(styleDetail1);
        cell = rowp.createCell(32);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(33);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(34);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(35);
        cell.setCellStyle(styleDetail);
        rownum++;

        HSSFRow row1 = sheet.createRow((int) rownum);
        row1.setHeightInPoints(30);
        cell = row1.createCell(1);
        cell.setCellValue("采购公司");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(2);
        cell.setCellValue("收货仓库");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(3);
        cell.setCellValue("仓位号");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(4);
        cell.setCellValue("开票日期");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(5);
        cell.setCellValue("业务编号");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(6);
        cell.setCellValue("客户");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(7);
        cell.setCellValue("客户身份证号");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(8);
        cell.setCellValue("供应商");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(9);
        cell.setCellValue("初检员");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(10);
        cell.setCellValue("复检员");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(11);
        cell.setCellValue("检斤员");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(12);
        cell.setCellValue("货名");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(13);
        cell.setCellValue("类型");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(14);
        cell.setCellValue("等级");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(15);
        cell.setCellValue("水分");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(16);
        cell.setCellValue("杂质");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(17);
        cell.setCellValue("霉变粒");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(18);
        cell.setCellValue("不完善粒");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(19);
        cell.setCellValue("容重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(20);
        cell.setCellValue("热损伤");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(21);
        cell.setCellValue("车牌号");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(22);
        cell.setCellValue("车数");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(23);
        cell.setCellValue("净重单价");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(24);
        cell.setCellValue("毛重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(25);
        cell.setCellValue("皮重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(26);
        cell.setCellValue("扣重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(27);
        cell.setCellValue("净重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(28);
        cell.setCellValue("扣单价");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(29);
        cell.setCellValue("纯重单价");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(30);
        cell.setCellValue("纯重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(31);
        cell.setCellValue("合计应付");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(32);
        cell.setCellValue("实付金额");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(33);
        cell.setCellValue("姓名");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(34);
        cell.setCellValue("卡号");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(35);
        cell.setCellValue("开户支行");
        cell.setCellStyle(styleDetail);
        rownum++;

        DecimalFormat decimalFormat=new DecimalFormat(".00");
        String compName = commonCompanyService.selectById(AuthSecurityUtils.getCurrentUserInfo().getCompId()).getCompName();

        // 详情信息
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(paymentManagement.getPaymentManagementList())) {
            for (int i = 0; i < paymentManagement.getPaymentManagementList().size(); i++) {
                PaymentManagement paymentManagement1 = paymentManagement.getPaymentManagementList().get(i);
                HSSFRow rowx = sheet.createRow((int) rownum);
                rowx.setHeightInPoints(20);
                cell = rowx.createCell(1);
                cell.setCellValue(compName);
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(2);
                cell.setCellValue(editString(paymentManagement1.getWarehouseName()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(3);
                cell.setCellValue(editString(paymentManagement1.getQualityInspectionManagement().getBinNumber()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(4);
                String date = addDateOneDay(paymentManagement1.getCreateDate());
                cell.setCellValue(date);
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(5);
                cell.setCellValue(editString(paymentManagement1.getPaymentNo()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(6);
                cell.setCellValue(editString(paymentManagement1.getCustomerName()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(7);
                cell.setCellValue(editString(paymentManagement1.getCustomerNumberCard()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(8);
                cell.setCellValue(editString(paymentManagement1.getIdentityAuthenticationInfo().getSupplier()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(9);
                cell.setCellValue(editString(paymentManagement1.getQualityInspectionManagement().getQualityInspector()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(10);
                cell.setCellValue(editString(paymentManagement1.getQualityInspectionManagement().getReInspector()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(11);
                cell.setCellValue(editString(paymentManagement1.getWeighingManagement().getSkinInspector()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(12);
                cell.setCellValue(editString(paymentManagement1.getGoodsName()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(13);
                cell.setCellValue(editString(paymentManagement1.getType()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(14);
                cell.setCellValue(editString(paymentManagement1.getQualityInspectionManagement().getGrade()));
                cell.setCellStyle(styleDetail);
                HSSFDataFormat df = wb.createDataFormat(); // 此处设置数据格式
                contextstyle.setDataFormat(df.getBuiltinFormat("#,##0.00"));//保留两位小数点
                HSSFDataFormat df1 = wb.createDataFormat(); // 此处设置数据格式
                contextstyle1.setDataFormat(df1.getBuiltinFormat("#,##0.00"));//保留两位小数点
                cell = rowx.createCell(15);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getQualityInspectionManagement().getWaterContent()!=null?paymentManagement1.getQualityInspectionManagement().getWaterContent():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(16);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getQualityInspectionManagement().getImpurity()!=null?paymentManagement1.getQualityInspectionManagement().getImpurity():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(17);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getQualityInspectionManagement().getMildewGrain()!=null?paymentManagement1.getQualityInspectionManagement().getMildewGrain():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(18);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getQualityInspectionManagement().getImperfectGrain()!=null?paymentManagement1.getQualityInspectionManagement().getImperfectGrain():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(19);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getQualityInspectionManagement().getBulkDensity()!=null?paymentManagement1.getQualityInspectionManagement().getBulkDensity():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(20);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getQualityInspectionManagement().getJiaorenli()!=null?paymentManagement1.getQualityInspectionManagement().getJiaorenli():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(21);
                cell.setCellValue(editString(paymentManagement1.getCarNo()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(22);
                cell.setCellValue("1");
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(23);
                cell.setCellValue(new Formatter().format("%.3f", editFloat(paymentManagement1.getTidalGrainPrice())).toString());
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(24);
                cell.setCellValue(editFloat(paymentManagement1.getGrossWeight()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(25);
                cell.setCellValue(editFloat(paymentManagement1.getTare()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(26);
                cell.setCellValue(new Formatter().format("%.2f", editFloat(paymentManagement1.getWeightDeduction())).toString());
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(27);
                cell.setCellValue(editFloat(paymentManagement1.getNetWeight()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(28);
                cell.setCellValue(editDouble(paymentManagement1.getUnitDeduction()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(29);
                cell.setCellValue(new Formatter().format("%.2f", editFloat(paymentManagement1.getSolidGrainPrice())).toString());
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(30);
                cell.setCellValue(editFloat(paymentManagement1.getPureWeight()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(31);
                cell.setCellValue(editDouble(paymentManagement1.getAmountIngPayable()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(32);
                cell.setCellValue(editDouble(paymentManagement1.getAmountEdPayable()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(33);
                cell.setCellValue(editString(paymentManagement1.getCustomerName()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(34);
                cell.setCellValue(editString(paymentManagement1.getIdentityAuthenticationInfo().getBankCard()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(35);
                cell.setCellValue(editString(paymentManagement1.getIdentityAuthenticationInfo().getBankDeposit() + "-" +paymentManagement1.getIdentityAuthenticationInfo().getBankDepositBranch()));
                cell.setCellStyle(styleDetail);

                rownum++;
            }

        }


        String path = null;
        String tempPath = null;
        String excelPath = null;

        try {

            // 取得绝对路径
            tempPath = URLDecoder.decode((localPath), "utf-8");

            // EXCEL路径
            excelPath = "/web/temp/pay/" + DateUtil.formatDate(new Date(), DateUtils.DATE_FMT_YYYYMMDD_NS) + '/'
                    + UUID.randomUUID().toString().replaceAll("-", "");

            path = tempPath + excelPath + '/' + "合同信息"
                    + ".xls";

        } catch (Exception e) {
            log.debug(e.getMessage());
        }
        // 下载
        download(path, response, wb);

    }


    /**
     * 手机审核文件下载
     *
     * @return
     * @throws Exception
     */
    @Override
    public String exportPhone(PaymentManagement paymentManagement, HttpServletResponse response) throws Exception {


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 查询
//        Page<ContractManagementInfo> contractManagementInfoPage = this.selectInfo(example);
//        List<ContractManagementInfo> contractManagementInfos = contractManagementInfoPage.getRecords();


        // 1.Excel的头部信息
        String headerTitle = "";

        // 2.创建一个webbook 对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();

        // 3.在webbook中添加一个sheet，对应Excel文件中的sheet
        HSSFSheet sheet;

        sheet = wb.createSheet("付款管理信息");
        headerTitle = "付款管理信息";

        // 4.设置打印参数
        sheet.setMargin(HSSFSheet.TopMargin, (short) 1);// 页边距（上）
        sheet.setMargin(HSSFSheet.BottomMargin, (short) 1);// 页边距（下）
        sheet.setMargin(HSSFSheet.LeftMargin, (short) 1);// 页边距（左）
        sheet.setMargin(HSSFSheet.RightMargin, (short) 1);// 页边距（右）

        // 不显示网格
        sheet.setDisplayGridlines(false);
        // 设置水平居中
        sheet.setHorizontallyCenter(true);

        /**************** 数据行样式 start ****************/
        HSSFFont fontTitle = wb.createFont();
        fontTitle.setFontName("宋体");
        fontTitle.setFontHeightInPoints((short) 14);
        // 设置字体加粗
        fontTitle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        HSSFFont fontComp = wb.createFont();
        fontComp.setFontName("宋体");
        fontComp.setFontHeightInPoints((short) 16);
        // 设置字体加粗
        fontComp.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        HSSFFont fontDetail = wb.createFont();
        fontDetail.setFontName("宋体");
        fontDetail.setFontHeightInPoints((short) 12);

        HSSFFont fontDetail1 = wb.createFont();
        fontDetail1.setFontName("宋体");
        fontDetail1.setFontHeightInPoints((short) 12);
        fontDetail1.setColor(HSSFColor.RED.index);

        // title
        HSSFCellStyle styleTitle = wb.createCellStyle();
        // 指定单元格居中对齐
        styleTitle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        styleTitle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleTitle.setWrapText(true);
        // 设置单元格字体
        styleTitle.setFont(fontTitle);
        // 右边框
        styleTitle.setBorderRight(HSSFCellStyle.BORDER_NONE);
        // 左边框
        styleTitle.setBorderLeft(HSSFCellStyle.BORDER_NONE);
        // 上边框
        styleTitle.setBorderTop(HSSFCellStyle.BORDER_NONE);
        // 下边框
        styleTitle.setBorderBottom(HSSFCellStyle.BORDER_NONE);

        // title
        HSSFCellStyle styleComp = wb.createCellStyle();
        // 指定单元格居中对齐
        styleComp.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        styleComp.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleComp.setWrapText(true);
        // 设置单元格字体
        styleComp.setFont(fontComp);
        // 右边框
        styleComp.setBorderRight(HSSFCellStyle.BORDER_NONE);
        // 左边框
        styleComp.setBorderLeft(HSSFCellStyle.BORDER_NONE);
        // 上边框
        styleComp.setBorderTop(HSSFCellStyle.BORDER_NONE);
        // 下边框
        styleComp.setBorderBottom(HSSFCellStyle.BORDER_NONE);

        HSSFCellStyle styleInfo = wb.createCellStyle();
        // 指定单元格居中对齐
        styleInfo.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        // 指定单元格垂直居中对齐
        styleInfo.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleInfo.setWrapText(true);
        // 设置单元格字体
        styleInfo.setFont(fontDetail);
        // 右边框
        styleInfo.setBorderRight(HSSFCellStyle.BORDER_NONE);
        // 左边框
        styleInfo.setBorderLeft(HSSFCellStyle.BORDER_NONE);
        // 上边框
        styleInfo.setBorderTop(HSSFCellStyle.BORDER_NONE);
        // 下边框
        styleInfo.setBorderBottom(HSSFCellStyle.BORDER_NONE);

        HSSFCellStyle styleDetail = wb.createCellStyle();
        // 指定单元格居中对齐
        styleDetail.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        styleDetail.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleDetail.setWrapText(true);
        // 设置单元格字体
        styleDetail.setFont(fontDetail);
        // 右边框
        styleDetail.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        styleDetail.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        styleDetail.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        styleDetail.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        HSSFCellStyle styleDetail1 = wb.createCellStyle();
        // 指定单元格居中对齐
        styleDetail1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        styleDetail1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleDetail1.setWrapText(true);
        // 设置单元格字体
        styleDetail1.setFont(fontDetail1);
        // 右边框
        styleDetail1.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        styleDetail1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        styleDetail1.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        styleDetail1.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        HSSFCellStyle contextstyle = wb.createCellStyle();
        // 指定单元格居中对齐
        contextstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        contextstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        contextstyle.setWrapText(true);
        // 设置单元格字体
        contextstyle.setFont(fontDetail);
        // 右边框
        contextstyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        contextstyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        contextstyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        contextstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        HSSFCellStyle contextstyle1 = wb.createCellStyle();
        // 指定单元格居中对齐
        contextstyle1.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        contextstyle1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        contextstyle1.setWrapText(true);
        // 设置单元格字体
        contextstyle1.setFont(fontDetail1);
        // 右边框
        contextstyle1.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        contextstyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        contextstyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        contextstyle1.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        HSSFCellStyle styleDetailYellow = wb.createCellStyle();
        // 指定单元格居中对齐
        styleDetailYellow.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        // 指定单元格垂直居中对齐
        styleDetailYellow.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleDetailYellow.setWrapText(true);
        // 设置单元格字体
        styleDetailYellow.setFont(fontDetail);
        // 右边框
        styleDetailYellow.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        styleDetailYellow.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        styleDetailYellow.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        styleDetailYellow.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        styleDetailYellow.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        styleDetailYellow.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);


        HSSFCellStyle styleDetailLeft = wb.createCellStyle();
        // 指定单元格居中对齐
        styleDetailLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        // 指定单元格垂直居中对齐
        styleDetailLeft.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        // 指定单元格自动换行
        styleDetailLeft.setWrapText(true);
        // 设置单元格字体
        styleDetailLeft.setFont(fontDetail);
        // 右边框
        styleDetailLeft.setBorderRight(HSSFCellStyle.BORDER_THIN);
        // 左边框
        styleDetailLeft.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        // 上边框
        styleDetailLeft.setBorderTop(HSSFCellStyle.BORDER_THIN);
        // 下styleDetailTitle
        styleDetailLeft.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        /**************** 数据行样式 end ****************/

        // 设置列宽
        sheet.setColumnWidth(0, 1 * 256 + 184);
        sheet.setColumnWidth(1, "采购公司".getBytes().length*3*256);
        sheet.setColumnWidth(2, "收货仓库".getBytes().length*25/10*256);
        sheet.setColumnWidth(3, 12 * 256 + 184);
        sheet.setColumnWidth(4, "开票日期".getBytes().length*2*256);
        sheet.setColumnWidth(5, "业务编号".getBytes().length*2*256);
        sheet.setColumnWidth(6, 12 * 256 + 184);
        sheet.setColumnWidth(7, "客户身份证号".getBytes().length*2*256);
        sheet.setColumnWidth(8, 12 * 256 + 184);
        sheet.setColumnWidth(9, 12 * 256 + 184);
        sheet.setColumnWidth(10, 12 * 256 + 184);
        sheet.setColumnWidth(11, 12 * 256 + 184);
        sheet.setColumnWidth(12, "货名".getBytes().length*3*256);
        sheet.setColumnWidth(13, 12 * 256 + 184);
        sheet.setColumnWidth(14, 12 * 256 + 184);
        sheet.setColumnWidth(15, 12 * 256 + 184);
        sheet.setColumnWidth(16, 12 * 256 + 184);
        sheet.setColumnWidth(17, 12 * 256 + 184);
        sheet.setColumnWidth(18, 12 * 256 + 184);
        sheet.setColumnWidth(19, 12 * 256 + 184);
        sheet.setColumnWidth(20, 12 * 256 + 184);
        sheet.setColumnWidth(21, 12 * 256 + 184);
        sheet.setColumnWidth(22, 12 * 256 + 184);
        sheet.setColumnWidth(23, 12 * 256 + 184);
        sheet.setColumnWidth(24, 12 * 256 + 184);
        sheet.setColumnWidth(25, 12 * 256 + 184);
        sheet.setColumnWidth(26, 12 * 256 + 184);
        sheet.setColumnWidth(27, 12 * 256 + 184);
        sheet.setColumnWidth(28, 12 * 256 + 184);
        sheet.setColumnWidth(29, 12 * 256 + 184);
        sheet.setColumnWidth(30, 12 * 256 + 184);
        sheet.setColumnWidth(31, 12 * 256 + 184);
        sheet.setColumnWidth(32, 12 * 256 + 184);
        sheet.setColumnWidth(33, 12 * 256 + 184);
        sheet.setColumnWidth(34, "卡号".getBytes().length*4*256);
        sheet.setColumnWidth(35, "开户支行".getBytes().length*4*256);
        sheet.setColumnWidth(35, 1 * 256 + 184);

        // 创建单元格对象
        HSSFCell cell = null;

        // 创建打印设置对象
        HSSFPrintSetup ps = sheet.getPrintSetup();
        // 打印方向，true：横向，false：纵向(默认)
        ps.setLandscape(false);
        // // 打印质量
        // ps.setVResolution((short) 700)
        // 设置缩放比例
        ps.setScale((short) 80);
        // 纸张类型
        ps.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);


        int rownum = 1;
        HSSFRow row0 = sheet.createRow((int) rownum);
        row0.setHeightInPoints(25);
        cell = row0.createCell(1);
        cell.setCellStyle(styleTitle);
        cell.setCellValue("付款信息");
        sheet.addMergedRegion(new CellRangeAddress(rownum, rownum, (short) 1, (short) 22));
        cell = row0.createCell(2);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(3);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(4);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(5);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(6);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(7);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(8);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(9);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(10);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(11);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(12);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(13);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(14);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(15);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(16);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(17);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(18);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(19);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(20);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(21);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(22);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(23);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(24);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(25);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(26);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(27);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(28);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(29);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(30);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(31);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(32);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(33);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(34);
        cell.setCellStyle(styleTitle);
        cell = row0.createCell(35);
        cell.setCellStyle(styleTitle);
        rownum++;

        List<String> businessKeys = null;
        if ("1".equals(paymentManagement.getSearchType())) {
            businessKeys = workflowService.getTaskBusinessKeysByCode("PAYMENT-MANAGEMENT-APPROVE");
        }

        Map<String, Object> pageView = new HashMap<>();
        //  仓库名称
        pageView.put("warehouseName", paymentManagement.getWarehouseName());
        pageView.put("businessKeys", businessKeys);
        // 查询付款总数
        List<PaymentManagement> dataList = baseMapper.getListByConditionOut(pageView);

//        String para = "";
//        if (!CollectionUtils.isEmpty(businessKeys)){
//            for(int i=0;i<businessKeys.size();i++){
//                para +=businessKeys.get(i);
//
//            }
//        }
//
//        Wrapper<PaymentManagement> paymentManagementWrapper = new EntityWrapper<>();
//        paymentManagementWrapper.in("id",para);
//        paymentManagementWrapper.eq("warehouse_name",paymentManagement.getWarehouseName());
//        paymentManagementWrapper.eq("delete_flag","0");
//        List<PaymentManagement> paymentManagements = this.selectList(paymentManagementWrapper);

        Double waterContentTotal = 0d;//水分合计
        Double impurityTotal = 0d;//杂质合计
        Double mildewGrainTotal = 0d;//霉变粒合计
        Double imperfectGrainTotal = 0d;//不完善粒合计
        Double bulkDensityTotal = 0d;//容重合计
        Double jiaorenliTotal = 0d;//热损伤合计
        Double netWeightTotal = 0d;//净重合计
        Double amountIngPayableTotal = 0d;//应收合计
        for (int i = 0; i < dataList.size(); i++) {
            PaymentManagement paymentManagement1 = dataList.get(i);
//            paymentManagement1.setIdentityAuthenticationInfo(iIdentityAuthenticationInfoService.selectById(paymentManagement1.getIdentityId()));
//            Wrapper<IdentityAuthenticationInfo> identityAuthenticationInfoWrapper = new EntityWrapper<>();
//            identityAuthenticationInfoWrapper.eq("customer_name",paymentManagement1.getCustomerName());
//            identityAuthenticationInfoWrapper.eq("customer_phone",paymentManagement1.getCustomerPhone());
//            identityAuthenticationInfoWrapper.eq("customer_number_card",paymentManagement1.getCustomerNumberCard());
//            identityAuthenticationInfoWrapper.eq("delete_flag","0");
//            if (identityAuthenticationInfoWrapper != null){
//                paymentManagement1.setIdentityAuthenticationInfo(iIdentityAuthenticationInfoService.selectOne(identityAuthenticationInfoWrapper));
//            }
//            Wrapper<QualityInspectionManagement> qualityInspectionManagementWrapper = new EntityWrapper<>();
//            qualityInspectionManagementWrapper.eq("relation_id", paymentManagement1.getRelationId());
//            qualityInspectionManagementWrapper.eq("comp_id", paymentManagement1.getCompId());
//            if (qualityInspectionManagementWrapper != null){
//                paymentManagement1.setQualityInspectionManagement(qualityInspectionManagementService.selectOne(qualityInspectionManagementWrapper));
//            }
//            Wrapper<WeighingManagement> weighingManagementWrapper = new EntityWrapper<>();
//            weighingManagementWrapper.eq("relation_id", paymentManagement1.getRelationId());
//            weighingManagementWrapper.eq("comp_id", paymentManagement1.getCompId());
//            if (weighingManagementWrapper != null){
//                paymentManagement1.setWeighingManagement(weighingManagementService.selectOne(weighingManagementWrapper));
//            }
            //水分累计和
            waterContentTotal = waterContentTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getWaterContent()!=null?paymentManagement1.getWaterContent():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            impurityTotal = impurityTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getImpurity()!=null?paymentManagement1.getImpurity():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            mildewGrainTotal = mildewGrainTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getMildewGrain()!=null?paymentManagement1.getMildewGrain():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            imperfectGrainTotal = imperfectGrainTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getImperfectGrain()!=null?paymentManagement1.getImperfectGrain():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            bulkDensityTotal = bulkDensityTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getBulkDensity()!=null?paymentManagement1.getBulkDensity():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            jiaorenliTotal = jiaorenliTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getJiaorenli()!=null?paymentManagement1.getJiaorenli():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            netWeightTotal = netWeightTotal + (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight())));
            amountIngPayableTotal = amountIngPayableTotal + paymentManagement1.getAmountIngPayable();
        }

        HSSFRow rowp = sheet.createRow((int)(rownum));
        rowp.setHeightInPoints(20);
        cell = rowp.createCell(1);
        cell.setCellValue("平均值/合计");
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(2);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(3);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(4);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(5);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(6);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(7);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(8);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(9);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(10);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(11);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(12);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(13);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(14);
        cell.setCellStyle(styleDetail);
        //水分平均值
        cell = rowp.createCell(15);
        cell.setCellValue(waterContentTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(16);
        cell.setCellValue(impurityTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(17);
        cell.setCellValue(mildewGrainTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(18);
        cell.setCellValue(imperfectGrainTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(19);
        cell.setCellValue(bulkDensityTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(20);
        cell.setCellValue(jiaorenliTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(21);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(22);
        cell.setCellValue(dataList.size());
        cell.setCellStyle(styleDetail1);
        cell = rowp.createCell(23);
        cell.setCellValue(amountIngPayableTotal/netWeightTotal);
        cell.setCellStyle(contextstyle1);
        cell = rowp.createCell(24);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(25);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(26);
        cell.setCellStyle(styleDetail);
        //净重合计
        cell = rowp.createCell(27);
        cell.setCellValue(netWeightTotal);
        cell.setCellStyle(styleDetail1);
        cell = rowp.createCell(28);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(29);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(30);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(31);
        cell.setCellValue(amountIngPayableTotal);
        cell.setCellStyle(styleDetail1);
        cell = rowp.createCell(32);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(33);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(34);
        cell.setCellStyle(styleDetail);
        cell = rowp.createCell(35);
        cell.setCellStyle(styleDetail);
        rownum++;

        HSSFRow row1 = sheet.createRow((int) rownum);
        row1.setHeightInPoints(30);
        cell = row1.createCell(1);
        cell.setCellValue("采购公司");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(2);
        cell.setCellValue("收货仓库");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(3);
        cell.setCellValue("仓位号");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(4);
        cell.setCellValue("开票日期");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(5);
        cell.setCellValue("业务编号");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(6);
        cell.setCellValue("客户");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(7);
        cell.setCellValue("客户身份证号");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(8);
        cell.setCellValue("供应商");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(9);
        cell.setCellValue("初检员");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(10);
        cell.setCellValue("复检员");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(11);
        cell.setCellValue("检斤员");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(12);
        cell.setCellValue("货名");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(13);
        cell.setCellValue("类型");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(14);
        cell.setCellValue("等级");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(15);
        cell.setCellValue("水分");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(16);
        cell.setCellValue("杂质");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(17);
        cell.setCellValue("霉变粒");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(18);
        cell.setCellValue("不完善粒");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(19);
        cell.setCellValue("容重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(20);
        cell.setCellValue("热损伤");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(21);
        cell.setCellValue("车牌号");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(22);
        cell.setCellValue("车数");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(23);
        cell.setCellValue("净重单价");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(24);
        cell.setCellValue("毛重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(25);
        cell.setCellValue("皮重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(26);
        cell.setCellValue("扣重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(27);
        cell.setCellValue("净重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(28);
        cell.setCellValue("扣单价");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(29);
        cell.setCellValue("纯重单价");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(30);
        cell.setCellValue("纯重");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(31);
        cell.setCellValue("合计应付");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(32);
        cell.setCellValue("实付金额");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(33);
        cell.setCellValue("姓名");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(34);
        cell.setCellValue("卡号");
        cell.setCellStyle(styleDetail);
        cell = row1.createCell(35);
        cell.setCellValue("开户支行");
        cell.setCellStyle(styleDetail);
        rownum++;

        DecimalFormat decimalFormat=new DecimalFormat(".00");
        String compName = commonCompanyService.selectById(AuthSecurityUtils.getCurrentUserInfo().getCompId()).getCompName();

        // 详情信息
        if (org.apache.commons.collections.CollectionUtils.isNotEmpty(dataList)) {
            for (int i = 0; i < dataList.size(); i++) {
                PaymentManagement paymentManagement1 = dataList.get(i);

                HSSFRow rowx = sheet.createRow((int) rownum);
                rowx.setHeightInPoints(20);
                cell = rowx.createCell(1);
                cell.setCellValue(compName);
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(2);
                cell.setCellValue(editString(paymentManagement1.getWarehouseName()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(3);
                cell.setCellValue(editString(paymentManagement1.getBinNumber()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(4);
                String date = addDateOneDay(paymentManagement1.getCreateDate());
                cell.setCellValue(date);
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(5);
                cell.setCellValue(editString(paymentManagement1.getPaymentNo()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(6);
                cell.setCellValue(editString(paymentManagement1.getCustomerName()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(7);
                cell.setCellValue(editString(paymentManagement1.getCustomerNumberCard()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(8);
                if (paymentManagement1 != null){
                    cell.setCellValue(editString(paymentManagement1.getSupplier()));
                }
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(9);
                cell.setCellValue(editString(paymentManagement1.getQualityInspector()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(10);
                cell.setCellValue(editString(paymentManagement1.getReInspector()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(11);
                cell.setCellValue(editString(paymentManagement1.getSkinInspector()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(12);
                cell.setCellValue(editString(paymentManagement1.getGoodsName()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(13);
                cell.setCellValue(editString(paymentManagement1.getType()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(14);
                cell.setCellValue(editString(paymentManagement1.getGrade()));
                cell.setCellStyle(styleDetail);
                HSSFDataFormat df = wb.createDataFormat(); // 此处设置数据格式
                contextstyle.setDataFormat(df.getBuiltinFormat("#,##0.00"));//保留两位小数点
                HSSFDataFormat df1 = wb.createDataFormat(); // 此处设置数据格式
                contextstyle1.setDataFormat(df1.getBuiltinFormat("#,##0.00"));//保留两位小数点
                cell = rowx.createCell(15);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getWaterContent()!=null?paymentManagement1.getWaterContent():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(16);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getImpurity()!=null?paymentManagement1.getImpurity():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(17);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getMildewGrain()!=null?paymentManagement1.getMildewGrain():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(18);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getImperfectGrain()!=null?paymentManagement1.getImperfectGrain():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(19);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getBulkDensity()!=null?paymentManagement1.getBulkDensity():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(20);
                cell.setCellValue(Double.parseDouble(paymentManagement1.getJiaorenli()!=null?paymentManagement1.getJiaorenli():"0"));
                cell.setCellStyle(contextstyle);
                cell = rowx.createCell(21);
                cell.setCellValue(editString(paymentManagement1.getCarNo()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(22);
                cell.setCellValue("1");
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(23);
                cell.setCellValue(new Formatter().format("%.3f", editFloat(paymentManagement1.getTidalGrainPrice())).toString());
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(24);
                cell.setCellValue(editFloat(paymentManagement1.getGrossWeight()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(25);
                cell.setCellValue(editFloat(paymentManagement1.getTare()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(26);
                cell.setCellValue(new Formatter().format("%.2f", editFloat(paymentManagement1.getWeightDeduction())).toString());
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(27);
                cell.setCellValue(editFloat(paymentManagement1.getNetWeight()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(28);
                cell.setCellValue(editDouble(paymentManagement1.getUnitDeduction()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(29);
                cell.setCellValue(new Formatter().format("%.2f", editFloat(paymentManagement1.getSolidGrainPrice())).toString());
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(30);
                cell.setCellValue(editFloat(paymentManagement1.getPureWeight()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(31);
                cell.setCellValue(editDouble(paymentManagement1.getAmountIngPayable()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(32);
                cell.setCellValue(editDouble(paymentManagement1.getAmountEdPayable()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(33);
                cell.setCellValue(editString(paymentManagement1.getCustomerName()));
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(34);
                if(paymentManagement1 != null){
                    cell.setCellValue(editString(paymentManagement1.getBankCard()));
                }
                cell.setCellStyle(styleDetail);
                cell = rowx.createCell(35);
                if(paymentManagement1 != null){
                    cell.setCellValue(editString(paymentManagement1.getBankDeposit() + "-" +paymentManagement1.getBankDepositBranch()));
                }
                cell.setCellStyle(styleDetail);
                rownum++;
            }

        }


        String path = null;
        String tempPath = null;
        String excelPath = null;

        try {

            // 取得绝对路径
            tempPath = URLDecoder.decode((localPath), "utf-8");

            // EXCEL路径
            excelPath = "/web/temp/pay/" + DateUtil.formatDate(new Date(), DateUtils.DATE_FMT_YYYYMMDD_NS) + '/'
                    + UUID.randomUUID().toString().replaceAll("-", "");

            path = tempPath + excelPath + '/' + "仓库信息"
                    + ".xlsx";

        } catch (Exception e) {
            log.debug(e.getMessage());
        }
        // 下载
//        DecimalFormat df = new DecimalFormat("0.00");
//        df.setRoundingMode(RoundingMode.HALF_UP);
//        DecimalFormat df1 = new DecimalFormat("0.000");
//        df1.setRoundingMode(RoundingMode.HALF_UP);
//
//        PaymentView paymentView = new PaymentView();
//        paymentView.setAmountIngPayableTotal(Double.valueOf(df.format(amountIngPayableTotal)));
//        paymentView.setCarTotal(Double.valueOf(paymentManagements.size()));
//        paymentView.setNetWeightTotal(Double.valueOf(df1.format(netWeightTotal)));
//        paymentView.setAverageUnitPrice(Double.valueOf(df.format(amountIngPayableTotal/netWeightTotal)));
//        paymentView.setWaterContentTotal(Double.valueOf(df.format(waterContentTotal/netWeightTotal)));
//        paymentView.setImpurityTotal(Double.valueOf(df.format(impurityTotal/netWeightTotal)));
//        paymentView.setMildewGrainTotal(Double.valueOf(df.format(mildewGrainTotal/netWeightTotal)));
//        paymentView.setImperfectGrainTotal(Double.valueOf(df.format(imperfectGrainTotal/netWeightTotal)));
//        paymentView.setBulkDensityTotal(Double.valueOf(df.format(bulkDensityTotal/netWeightTotal)));
//        paymentView.setJiaorenliTotal(Double.valueOf(df.format(jiaorenliTotal/netWeightTotal)));
//        paymentView.setAddressUrl(a);

        return downloadPhone(path, response, wb);

    }

    /**
     * 手机审核数据
     *
     * @return
     * @throws Exception
     */
    @Override
    public PaymentView exportPhoneData(PaymentManagement paymentManagement){

        List<String> businessKeys = null;
        if ("1".equals(paymentManagement.getSearchType())) {
            businessKeys = workflowService.getTaskBusinessKeysByCode("PAYMENT-MANAGEMENT-APPROVE");
        }

        Map<String, Object> pageView = new HashMap<>();
        //  仓库名称
        pageView.put("warehouseName", paymentManagement.getWarehouseName());
        pageView.put("businessKeys", businessKeys);
        // 查询付款总数
        List<PaymentManagement> dataList = baseMapper.getListByConditionOut(pageView);

        Double waterContentTotal = 0d;//水分合计
        Double impurityTotal = 0d;//杂质合计
        Double mildewGrainTotal = 0d;//霉变粒合计
        Double imperfectGrainTotal = 0d;//不完善粒合计
        Double bulkDensityTotal = 0d;//容重合计
        Double jiaorenliTotal = 0d;//热损伤合计
        Double netWeightTotal = 0d;//净重合计
        Double amountIngPayableTotal = 0d;//应收合计
        for (int i = 0; i < dataList.size(); i++) {
            PaymentManagement paymentManagement1 = dataList.get(i);

            //水分累计和
            waterContentTotal = waterContentTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getWaterContent()!=null?paymentManagement1.getWaterContent():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            impurityTotal = impurityTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getImpurity()!=null?paymentManagement1.getImpurity():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            mildewGrainTotal = mildewGrainTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getMildewGrain()!=null?paymentManagement1.getMildewGrain():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            imperfectGrainTotal = imperfectGrainTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getImperfectGrain()!=null?paymentManagement1.getImperfectGrain():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            bulkDensityTotal = bulkDensityTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getBulkDensity()!=null?paymentManagement1.getBulkDensity():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            jiaorenliTotal = jiaorenliTotal + ((Double.valueOf(String.valueOf(paymentManagement1.getJiaorenli()!=null?paymentManagement1.getJiaorenli():"0"))) * (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight()))));
            netWeightTotal = netWeightTotal + (Double.valueOf(String.valueOf(paymentManagement1.getNetWeight())));
            amountIngPayableTotal = amountIngPayableTotal + paymentManagement1.getAmountIngPayable();
        }

        // 返回数据
        DecimalFormat df = new DecimalFormat("0.00");
        df.setRoundingMode(RoundingMode.HALF_UP);
        DecimalFormat df1 = new DecimalFormat("0.000");
        df1.setRoundingMode(RoundingMode.HALF_UP);

        PaymentView paymentView = new PaymentView();
        paymentView.setAmountIngPayableTotal(Double.valueOf(df.format(amountIngPayableTotal)));
        paymentView.setCarTotal(Double.valueOf(dataList.size()));
        paymentView.setNetWeightTotal(Double.valueOf(df1.format(netWeightTotal/1000)));
        paymentView.setAverageUnitPrice(Double.valueOf(df.format(amountIngPayableTotal/netWeightTotal*1000)));
        paymentView.setWaterContentTotal(Double.valueOf(df.format(waterContentTotal/netWeightTotal)));
        paymentView.setImpurityTotal(Double.valueOf(df.format(impurityTotal/netWeightTotal)));
        paymentView.setMildewGrainTotal(Double.valueOf(df.format(mildewGrainTotal/netWeightTotal)));
        paymentView.setImperfectGrainTotal(Double.valueOf(df.format(imperfectGrainTotal/netWeightTotal)));
        paymentView.setBulkDensityTotal(Double.valueOf(df.format(bulkDensityTotal/netWeightTotal)));
        paymentView.setJiaorenliTotal(Double.valueOf(df.format(jiaorenliTotal/netWeightTotal)));
        return paymentView;

    }



    /**
     * 下载
     *
     * @param path
     * @param response
     */
    private String downloadPhone(String path, HttpServletResponse response, Workbook wb) {

        try {

            // path是指欲下载的文件的路径。
//            File file = new File(path);
//            File file = new File( new String(("templates" + File.separator + "仓库信息"+".xlsx").getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
            // 取得文件名。
            String fileName = "excel";
            // 给文件名编码
            fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");

            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);

            // 定义byte输出流
            ByteArrayOutputStream out = new ByteArrayOutputStream();

            // 写导出文件
            wb.write(out);

            // 取得模板文件中的数据
            byte[] result = out.toByteArray();

            // 设置输出数据类型
            response.setContentType("application/vnd.ms-excel");
            // 设置输出数据长度
            response.setContentLength(result.length);

            // 设置文件名称
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            // 将文件流输出到画面
//            response.getOutputStream().write(result);

            out.flush();
            out.close();
            //生成的新文件路径
            String outputFile = "templates" + File.separator + fileName + ".xlsx";
            File file1 = new File(new String(outputFile.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
            try (FileOutputStream fos = new FileOutputStream(file1)) {
                wb.write(fos);
            }

            FileInputStream fileInputStream = new FileInputStream(file1.getAbsoluteFile());

            MultipartFile multipartFile = new MockMultipartFile(ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
            String result1 = upload(multipartFile, fileName);
            file1.delete();
            return result1;
        } catch (IOException ex) {
            log.debug(ex.getMessage());
        }
        return "ng";
    }

    /**
     * 后台通过服务器间接传文件
     *
     * @param file
     * @return
     * @throws IOException
     */
    public String upload(MultipartFile file, String name) throws IOException {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(file.getSize());
        objectMetadata.setContentType(file.getContentType());
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, "pcfiles/" + name + ".xlsx", file.getInputStream(), objectMetadata);
        ossClient.putObject(putObjectRequest);
        return baseUrl + "pcfiles/" + name + ".xlsx";
    }



    private String addDateOneDay(Date date) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
//        if (null == date) {
//            return date;
//        }
//        Calendar c = Calendar.getInstance();
//        c.setTime(date); // 设置当前日期
//        c.add(Calendar.DATE, 1); // 日期加1天
//        date = c.getTime();
        return sdf.format(date);
    }

    /**
     * 判断浮点数
     *
     * @param example
     * @return
     */
    private Float editFloat(Float example) {
        Float exampleOne = example != null ? example : 0f;
        return exampleOne;
    }
    /**
     * 判断浮点数
     *
     * @param example
     * @return
     */
    private Double editDouble(Double example) {
        Double exampleOne = example != null ? example : 0d;
        return exampleOne;
    }

    /**
     * 判断字符串
     *
     * @param example
     * @return
     */
    private String editString(String example) {
        String exampleOne = example != null ? example : "";
        return exampleOne;
    }

    /**
     * 下载
     *
     * @param path
     * @param response
     */
    private void download(String path, HttpServletResponse response, Workbook wb) {

        try {

            // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String fileName = file.getName();
            // 给文件名编码
            fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");

            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);

            // 定义byte输出流
            ByteArrayOutputStream out = new ByteArrayOutputStream();

            // 写导出文件
            wb.write(out);

            // 取得模板文件中的数据
            byte[] result = out.toByteArray();

            // 设置输出数据类型
            response.setContentType("application/vnd.ms-excel");
            // 设置输出数据长度
            response.setContentLength(result.length);

            // 设置文件名称
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            // 将文件流输出到画面
            response.getOutputStream().write(result);

            out.flush();
            out.close();

        } catch (IOException ex) {
            log.debug(ex.getMessage());
        }

    }
}
