package com.xhpc.order.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.xhpc.common.api.SmsService;
import com.xhpc.common.api.UserTypeService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.utils.SecurityUtils;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.domain.XhpcPlaceholderTime;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.common.util.UserTypeUtil;
import com.xhpc.evcs.domain.PlaceholderRules;
import com.xhpc.evcs.domain.PriceInfo;
import com.xhpc.order.domain.XhpcPlaceholderOrder;
import com.xhpc.order.dto.FeeDifferenceDto;
import com.xhpc.order.dto.PositionFeeDto;
import com.xhpc.order.dto.XhpcPlaceholderTimeOrderDto;
import com.xhpc.order.mapper.XhpcPlaceholderOrderMapper;
import com.xhpc.order.service.IXhpcChargeOrderService;
import com.xhpc.order.service.IXhpcPlaceholderOrderService;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.api.model.LoginUser;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;

import static com.xhpc.common.data.redis.StaticBeanUtil.REDIS;

/**
 * @author yuyang
 * @Date 2025-05-06 10:07
 */
@Service
public class XhpcPlaceholderOrderServiceImpl extends BaseService implements IXhpcPlaceholderOrderService {

    @Autowired
    private XhpcPlaceholderOrderMapper xhpcPlaceholderOrderMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserTypeService userTypeService;
    @Autowired
    private IXhpcChargeOrderService xhpcChargeOrderService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private RedisService redisService;

    @Override
    public int insertXhpcPlaceholderOrder(XhpcPlaceholderOrder placeholderOrder) {
        return xhpcPlaceholderOrderMapper.insertXhpcPlaceholderOrder(placeholderOrder);
    }

    @Override
    public void getTimingPriceTime(String gunNumber) {
        Map<String, Object> cacheGun = REDIS.getCacheMap("gun:"+gunNumber);
        if (cacheGun != null) {
            try {
                if (cacheGun.get("previousOrder") !=null &&  !"".equals(cacheGun.get("previousOrder").toString())) {
                    String serialNumber = cacheGun.get("previousOrder").toString();
                    //查询订单
                    XhpcPlaceholderOrder xhpcPlaceholderOrder = xhpcPlaceholderOrderMapper.getXhpcPlaceholderOrderSerialNumber(serialNumber);
                    int xhpcChargeOrderStatus = xhpcPlaceholderOrderMapper.getXhpcChargeOrderStatus(serialNumber);

                    if(xhpcPlaceholderOrder !=null && xhpcChargeOrderStatus==1){
                        if (xhpcPlaceholderOrder.getType() == 0 ) {
                            //删除占位费时间段收费表
                            // xhpcPlaceholderOrderMapper.deleteByPlaceholderOrderIdInt(xhpcPlaceholderOrder.getPlaceholderOrderId());
                            //结算订单,查询该时间段价格
                            String startTimeYu = DateUtil.format(xhpcPlaceholderOrder.getStartTime(), "yyyy-MM-dd HH:mm:ss");
                            DateTime startTime = DateUtil.parse(startTimeYu,"yyyy-MM-dd HH:mm:ss");
                            DateTime endTime = DateUtil.parse(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd HH:mm:ss");

                            //查看是否有历史订单，历史订单为空，不收取占位费
                            int count = xhpcPlaceholderOrderMapper.getXhpcHistoryOrderTotalPrice(serialNumber, 2);
                            if(count==0){
                                endTime = startTime;
                            }else{
                                int number = xhpcPlaceholderOrderMapper.getXhpcHistoryOrderNextOrderTimeYu(xhpcPlaceholderOrder.getHistoryOrderId(), xhpcPlaceholderOrder.getTerminalId(), startTimeYu);
                                //有下一单返回1 没有下一单返回0
                                if(number>0){
                                    //查看下一单的时间是否超过30分钟
                                    int result = xhpcPlaceholderOrderMapper.getXhpcHistoryOrderNextOrderTime(xhpcPlaceholderOrder.getHistoryOrderId(), xhpcPlaceholderOrder.getTerminalId(), startTimeYu);
                                    if(result==0){
                                        endTime = startTime;
                                    }
                                }
                            }
                            String start = DateUtil.formatTime(startTime);
                            String end = DateUtil.formatTime(endTime);
                            DateTime parse = DateUtil.parse(DateUtil.format(startTime, "yyyy-MM-dd"), "yyyy-MM-dd");
                            DateTime parse1 = DateUtil.parse(DateUtil.format(endTime, "yyyy-MM-dd"), "yyyy-MM-dd");
                            long betweenDay = DateUtil.between(parse,parse1, DateUnit.DAY);
                            long between = DateUtil.between(endTime, startTime, DateUnit.MINUTE);
                            xhpcPlaceholderOrder.setEndTime(endTime);
                            xhpcPlaceholderOrder.setTotalTimeSpan((int) between);
                            BigDecimal totalMoney = new BigDecimal("0.0");
                            int freeTime = (int) xhpcPlaceholderOrder.getFreeTime();
                            if (freeTime - between > 0) {
                                between = 0L;
                            } else {
                                between = between - freeTime;
                                if (betweenDay > 0) {
                                    for (int m = 0; m <= betweenDay; m++) {
                                        if(m==0){
                                            List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime(start, "23:59:59", xhpcPlaceholderOrder.getPlaceholderId());
                                            for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                                                XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                                                BigDecimal price = xhpcPlaceholderTime.getPrice();
                                                String pattern = "HH:mm:ss";
                                                Long startOne = DateUtil.parse(start, pattern).getTime();
                                                Long endOne = DateUtil.parse("23:59:59", pattern).getTime();

                                                String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                                String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                                Long time1 = DateUtil.parse(format1, pattern).getTime();
                                                Long time2 = DateUtil.parse(format2, pattern).getTime();
                                                XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                                                if (startOne < time1) {
                                                    startOne = time1;
                                                    placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                                                }else{
                                                    placeholderTimeOrderDto.setStartTime(DateUtil.parse(start, pattern));
                                                }
                                                if (endOne > time2) {
                                                    endOne = time2;
                                                    placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                                                }else{
                                                    placeholderTimeOrderDto.setEndTime(DateUtil.parse("23:59:59", pattern));
                                                }
                                                long betweOne = (endOne - startOne) / 60000;
                                                int i1 = (int) (betweOne);

                                                if (freeTime - i1 > 0) {
                                                    freeTime = freeTime - i1;
                                                    i1 = 0;
                                                } else {
                                                    i1 = i1 - freeTime;
                                                    freeTime = 0;
                                                }
                                                BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                                totalMoney = totalMoney.add(multiply);

                                                placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                                                placeholderTimeOrderDto.setPrice(price);
                                                placeholderTimeOrderDto.setTotalMoney(multiply);
                                                placeholderTimeOrderDto.setTotalTimeSpan(i1);
                                                xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);

                                            }
                                        }else if(m>0 && m<betweenDay){
                                            List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime("00:00:00", "23:59:59", xhpcPlaceholderOrder.getPlaceholderId());
                                            for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                                                XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                                                BigDecimal price = xhpcPlaceholderTime.getPrice();
                                                String pattern = "HH:mm:ss";
                                                Long startOne = DateUtil.parse("00:00:00", pattern).getTime();
                                                Long endOne = DateUtil.parse("23:59:59", pattern).getTime();

                                                String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                                String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                                Long time1 = DateUtil.parse(format1, pattern).getTime();
                                                Long time2 = DateUtil.parse(format2, pattern).getTime();
                                                XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                                                if (startOne < time1) {
                                                    startOne = time1;
                                                    placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                                                }else{
                                                    placeholderTimeOrderDto.setStartTime(DateUtil.parse("00:00:00", pattern));
                                                }
                                                if (endOne > time2) {
                                                    endOne = time2;
                                                    placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                                                }else{
                                                    placeholderTimeOrderDto.setEndTime(DateUtil.parse("23:59:59", pattern));
                                                }
                                                long betweOne = (endOne - startOne) / 60000;
                                                int i1 = (int) (betweOne);

                                                if (freeTime - i1 > 0) {
                                                    freeTime = freeTime - i1;
                                                    i1 = 0;
                                                } else {
                                                    i1 = i1 - freeTime;
                                                    freeTime = 0;
                                                }

                                                BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                                totalMoney = totalMoney.add(multiply);

                                                placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                                                placeholderTimeOrderDto.setPrice(price);
                                                placeholderTimeOrderDto.setTotalMoney(multiply);
                                                placeholderTimeOrderDto.setTotalTimeSpan(i1);
                                                xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);

                                            }
                                        }else{
                                            List<XhpcPlaceholderTime> xhpcPlaceholderTimeTwo = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime("00:00:00", end, xhpcPlaceholderOrder.getPlaceholderId());
                                            for (int i = 0; i < xhpcPlaceholderTimeTwo.size(); i++) {
                                                XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeTwo.get(i);
                                                BigDecimal price = xhpcPlaceholderTime.getPrice();
                                                String pattern = "HH:mm:ss";
                                                Long startOne = DateUtil.parse("00:00:00", pattern).getTime();
                                                Long endOne = DateUtil.parse(end, pattern).getTime();

                                                String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                                String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                                Long time1 = DateUtil.parse(format1, pattern).getTime();
                                                Long time2 = DateUtil.parse(format2, pattern).getTime();

                                                XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                                                if (startOne < time1) {
                                                    startOne = time1;
                                                    placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                                                }else{
                                                    placeholderTimeOrderDto.setStartTime(DateUtil.parse("00:00:00", pattern));
                                                }
                                                if (endOne > time2) {
                                                    endOne = time2;
                                                    placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                                                }else{
                                                    placeholderTimeOrderDto.setEndTime(DateUtil.parse(end, pattern));
                                                }
                                                long betweOne = (endOne - startOne) / 60000;
                                                int i1 = (int) (betweOne);
                                                if (freeTime - i1 > 0) {
                                                    freeTime = freeTime - i1;
                                                    i1 = 0;
                                                } else {
                                                    i1 = i1 - freeTime;
                                                    freeTime = 0;
                                                }
                                                BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                                totalMoney = totalMoney.add(multiply);


                                                placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                                                placeholderTimeOrderDto.setPrice(price);
                                                placeholderTimeOrderDto.setTotalMoney(multiply);
                                                placeholderTimeOrderDto.setTotalTimeSpan(i1);
                                                xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);
                                            }
                                        }
                                    }
                                }else {
                                    List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime(start, end, xhpcPlaceholderOrder.getPlaceholderId());
                                    for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                                        XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                                        BigDecimal price = xhpcPlaceholderTime.getPrice();
                                        String pattern = "HH:mm:ss";
                                        Long startOne = DateUtil.parse(start, pattern).getTime();
                                        Long endOne = DateUtil.parse(end, pattern).getTime();

                                        String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                        String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                        Long time1 = DateUtil.parse(format1, pattern).getTime();
                                        Long time2 = DateUtil.parse(format2, pattern).getTime();

                                        XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                                        if (startOne < time1) {
                                            startOne = time1;
                                            placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                                        }else{
                                            placeholderTimeOrderDto.setStartTime(DateUtil.parse(start, pattern));
                                        }
                                        if (endOne > time2) {
                                            endOne = time2;
                                            placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                                        }else{
                                            placeholderTimeOrderDto.setEndTime(DateUtil.parse(end, pattern));
                                        }
                                        long betweOne = (endOne - startOne) / 60000;
                                        int i1 = (int) (betweOne);

                                        if (freeTime - i1 > 0) {
                                            freeTime = freeTime - i1;
                                            i1 = 0;
                                        } else {
                                            i1 = i1 - freeTime;
                                            freeTime = 0;
                                        }
                                        BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                        totalMoney = totalMoney.add(multiply);

                                        placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                                        placeholderTimeOrderDto.setPrice(price);
                                        placeholderTimeOrderDto.setTotalMoney(multiply);
                                        placeholderTimeOrderDto.setTotalTimeSpan(i1);
                                        xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);
                                    }
                                }
                            }

                            if(totalMoney.compareTo(new BigDecimal(50)) == 1){
                                totalMoney = new BigDecimal(50);
                            }
                            xhpcPlaceholderOrder.setTotalMoney(totalMoney);
                            if (new BigDecimal(0).compareTo(new BigDecimal(between)) == -1) {
                                xhpcPlaceholderOrder.setOvertimePrice(totalMoney.divide(new BigDecimal(between), 2, BigDecimal.ROUND_HALF_UP));
                            } else {
                                xhpcPlaceholderOrder.setOvertimePrice(new BigDecimal("0.0"));
                            }

                            String orderKey = "notificationPlaceholderOrderInfo:" + xhpcPlaceholderOrder.getSerialNumber();
                            Map<String, Object> cacheOrder = new HashMap<>();
                            cacheOrder.put("OvertimeBillCode", xhpcPlaceholderOrder.getPlaceholderOrderNumber());
                            cacheOrder.put("StartChargeSeq", xhpcPlaceholderOrder.getInternetSerialNumber());
                            cacheOrder.put("OvertimeBillStatus", 3);
                            cacheOrder.put("ConnectorId", xhpcPlaceholderOrder.getTerminalNumber());
                            cacheOrder.put("BillEquipment", 1);
                            cacheOrder.put("StartTime", DateUtil.format(xhpcPlaceholderOrder.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
                            cacheOrder.put("EndTime", DateUtil.format(xhpcPlaceholderOrder.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
                            cacheOrder.put("TotalTimeSpan", xhpcPlaceholderOrder.getTotalTimeSpan());
                            cacheOrder.put("OvertimePrice", xhpcPlaceholderOrder.getOvertimePrice());
                            cacheOrder.put("TotalMoney", xhpcPlaceholderOrder.getTotalMoney());

                            Map<String, Object> overtimeDetails = new HashMap<>();
                            overtimeDetails.put("DetailStartTime", DateUtil.format(xhpcPlaceholderOrder.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
                            overtimeDetails.put("DetailEndTime", DateUtil.format(xhpcPlaceholderOrder.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
                            overtimeDetails.put("DetailMoney", xhpcPlaceholderOrder.getTotalMoney());
                            overtimeDetails.put("OvertimePrice", xhpcPlaceholderOrder.getOvertimePrice());
                            cacheOrder.put("OvertimeDetails", overtimeDetails);
                            REDIS.setCacheMap(orderKey, cacheOrder);
                            xhpcPlaceholderOrder.setType(1);
                            REDIS.setCacheMap(gunNumber, cacheGun);

                            if(xhpcPlaceholderOrder.getSource() !=UserTypeUtil.INTERNET_TYPE){
                                Long userId = xhpcPlaceholderOrder.getUserId();
                                Integer source = xhpcPlaceholderOrder.getSource();
                                String tenantId = xhpcPlaceholderOrder.getTenantId();

                                if(UserTypeUtil.USER_TYPE == xhpcPlaceholderOrder.getSource()){
                                    xhpcPlaceholderOrder.setChargingMode("C端用户");
                                }else if(UserTypeUtil.COMMUNIT_TYPE == xhpcPlaceholderOrder.getSource()){
                                    xhpcPlaceholderOrder.setChargingMode("社区用户");
                                }else if(UserTypeUtil.MOTORCADE_TYPE == xhpcPlaceholderOrder.getSource()){
                                    xhpcPlaceholderOrder.setChargingMode("车队用户");
                                }else if(UserTypeUtil.MOTORCADE_USER_TYPE == xhpcPlaceholderOrder.getSource()){
                                    xhpcPlaceholderOrder.setChargingMode("车队用户");
                                }else{
                                    xhpcPlaceholderOrder.setChargingMode("B端用户");
                                }
                                BigDecimal totalMoney1 = xhpcPlaceholderOrder.getTotalMoney();
                                xhpcPlaceholderOrder.setStatus(3);
                                if(totalMoney1.compareTo(new BigDecimal(0))>0){
                                    System.out.println("==========11111============");
                                    String key ="placeholderOrder:" +source+ tenantId+userId;
                                    int number =0;
                                    if(redisService.getCacheObject(key) !=null){
                                        String numberYu = redisService.getCacheObject(key).toString();
                                        int numbers =Integer.parseInt(numberYu);
                                        numbers++;
                                        number=numbers;
                                        redisService.setCacheObject(key, numbers);
                                    }else{
                                        redisService.setCacheObject(key, 1);
                                        number=1;
                                    }
                                    System.out.println("==========2222============");
                                    R user = userTypeService.getUser(null, userId, source, null, tenantId);
                                    if(number>3){
                                        if(user !=null && user.getData() !=null ){
                                            Map<String, Object> userMessage = (Map<String, Object>)user.getData();
                                            String phone =userMessage.get("phone").toString();
                                            BigDecimal balance1 =new BigDecimal(userMessage.get("balance").toString());
                                            BigDecimal subtract = balance1.subtract(totalMoney1);

                                            int i = xhpcChargeOrderService.updateUserBalance(userId, subtract,source,tenantId);
                                            if(i==0){
                                                //扣钱失败
                                                System.out.println("<<<<<<<<<<<<<<<<扣钱失败>>>>userId>>>>>>>>>>>>>"+userId+">>subtract>>>>>>>>"+subtract+">>balance1>>>>>>>>"+balance1+">>source>>>>>>>>"+source);
                                            }else{
                                                //添加流水
                                                xhpcChargeOrderService.addUserAccountStatement(userId,totalMoney1, subtract, xhpcPlaceholderOrder.getPlaceholderOrderId(), 7, new Date(),source);
                                            }
                                            HashMap<String, String> paramMap = new HashMap<>();
                                            paramMap.put("time", xhpcPlaceholderOrder.getTotalTimeSpan()+"");
                                            paramMap.put("sumMoney",totalMoney1.toString());
                                            paramMap.put("phone", phone);
                                            paramMap.put("content", "尊敬的用户你好，你的爱车已停止充电"+xhpcPlaceholderOrder.getTotalTimeSpan()+"分钟，前30分钟免费，平台收取本次设备占用费："+totalMoney1+"元，占位费费用将从余额扣除，占位费明细请查询翔桦充电小程序我的历史订单，充电结束请及时挪车。");
                                            smsService.sendNotice(paramMap);
                                            //平台用户直接余额扣钱
                                            xhpcPlaceholderOrder.setActPrice(xhpcPlaceholderOrder.getTotalMoney());
                                            xhpcPlaceholderOrder.setActStatus(1);
                                        }
                                    }else{
                                        //发送短信
                                        //平台用户直接余额扣钱
                                        if(user !=null && user.getData() !=null ){
                                            Map<String, Object> userMessage = (Map<String, Object>)user.getData();
                                            String phone =userMessage.get("phone").toString();
                                            HashMap<String, String> paramMap = new HashMap<>();
                                            paramMap.put("time", xhpcPlaceholderOrder.getTotalTimeSpan()+"");
                                            paramMap.put("sumMoney",totalMoney1.toString());
                                            paramMap.put("number", number+"");
                                            paramMap.put("phone", phone);
                                            paramMap.put("content", "尊敬的用户，你的爱车已停止充电"+xhpcPlaceholderOrder.getTotalTimeSpan()+"分钟，前30分钟免费，平台收取本次设备占用费："+totalMoney1+"元，占位费费用将从余额扣除，占位费明细请查询翔桦充电小程序我的历史订单，试运行期间前三次免费，当前免费次数为"+number+"，充电结束请及时挪车。");
                                            smsService.sendNotice(paramMap);

                                        }
                                        xhpcPlaceholderOrder.setActPrice(new BigDecimal(0));
                                        xhpcPlaceholderOrder.setActStatus(1);
                                    }
                                }else{
                                    xhpcPlaceholderOrder.setActPrice(new BigDecimal(0));
                                    xhpcPlaceholderOrder.setActStatus(1);
                                }
                            }
                            xhpcPlaceholderOrderMapper.updateXhpcPlaceholderOrder(xhpcPlaceholderOrder);
                        }
                    }
                }
            }catch (Exception e){

            }
        }
    }

    @Override
    public void getReturnGunRecord(String serialNumber, String startTimeYu, String endTimeYu) {

        //查询订单
        XhpcPlaceholderOrder xhpcPlaceholderOrder = xhpcPlaceholderOrderMapper.getXhpcPlaceholderOrderSerialNumber(serialNumber);
        int xhpcChargeOrderStatus = xhpcPlaceholderOrderMapper.getXhpcChargeOrderStatus(serialNumber);

        if(xhpcPlaceholderOrder !=null && xhpcChargeOrderStatus==1){
            {
                if (xhpcPlaceholderOrder.getType() == 0 ) {
                    //结算订单,查询该时间段价格
                    DateTime startTime = DateUtil.parse(startTimeYu,"yyyy-MM-dd HH:mm:ss");
                    DateTime endTime = DateUtil.parse(endTimeYu,"yyyy-MM-dd HH:mm:ss");
                    Boolean noMoney =true;
                    //查看是否有历史订单，历史订单为空，不收取占位费
                    int count = xhpcPlaceholderOrderMapper.getXhpcHistoryOrderTotalPrice(serialNumber, 2);
                    if(count>0){
                        int number = xhpcPlaceholderOrderMapper.getXhpcHistoryOrderTotalPrice(serialNumber, 1);
                        if(number==0){
                            endTime = startTime;
                            noMoney =false;
                        }
                    }
                    if(noMoney){
                        //查看下一单的时间是否超过30分钟
                        int result = xhpcPlaceholderOrderMapper.getXhpcHistoryOrderNextOrderTime(xhpcPlaceholderOrder.getHistoryOrderId(), xhpcPlaceholderOrder.getTerminalId(), startTimeYu);
                        if(result==0){
                            endTime = startTime;
                        }
                    }
                    String start = DateUtil.formatTime(startTime);
                    String end = DateUtil.formatTime(endTime);
                    DateTime parse = DateUtil.parse(DateUtil.format(startTime, "yyyy-MM-dd"), "yyyy-MM-dd");
                    DateTime parse1 = DateUtil.parse(DateUtil.format(endTime, "yyyy-MM-dd"), "yyyy-MM-dd");
                    long betweenDay = DateUtil.between(parse,parse1, DateUnit.DAY);
                    long between = DateUtil.between(endTime, startTime, DateUnit.MINUTE);
                    xhpcPlaceholderOrder.setEndTime(endTime);
                    xhpcPlaceholderOrder.setTotalTimeSpan((int) between);
                    BigDecimal totalMoney = new BigDecimal("0.0");
                    int freeTime = (int) xhpcPlaceholderOrder.getFreeTime();
                    if (freeTime - between > 0) {
                        between = 0L;
                    } else {
                        between = between - freeTime;
                        if (betweenDay > 0) {
                            for (int m = 0; m <= betweenDay; m++) {
                                if(m==0){
                                    List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime(start, "23:59:59", xhpcPlaceholderOrder.getPlaceholderId());
                                    for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                                        XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                                        BigDecimal price = xhpcPlaceholderTime.getPrice();
                                        String pattern = "HH:mm:ss";
                                        Long startOne = DateUtil.parse(start, pattern).getTime();
                                        Long endOne = DateUtil.parse("23:59:59", pattern).getTime();

                                        String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                        String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                        Long time1 = DateUtil.parse(format1, pattern).getTime();
                                        Long time2 = DateUtil.parse(format2, pattern).getTime();
                                        XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                                        if (startOne < time1) {
                                            startOne = time1;
                                            placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                                        }else{
                                            placeholderTimeOrderDto.setStartTime(DateUtil.parse(start, pattern));
                                        }
                                        if (endOne > time2) {
                                            endOne = time2;
                                            placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                                        }else{
                                            placeholderTimeOrderDto.setEndTime(DateUtil.parse("23:59:59", pattern));
                                        }
                                        long betweOne = (endOne - startOne) / 60000;
                                        int i1 = (int) (betweOne);

                                        if (freeTime - i1 > 0) {
                                            freeTime = freeTime - i1;
                                            i1 = 0;
                                        } else {
                                            i1 = i1 - freeTime;
                                            freeTime = 0;
                                        }
                                        BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                        totalMoney = totalMoney.add(multiply);

                                        placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                                        placeholderTimeOrderDto.setPrice(price);
                                        placeholderTimeOrderDto.setTotalMoney(multiply);
                                        placeholderTimeOrderDto.setTotalTimeSpan(i1);
                                        xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);

                                    }
                                }else if(m>0 && m<betweenDay){
                                    List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime("00:00:00", "23:59:59", xhpcPlaceholderOrder.getPlaceholderId());
                                    for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                                        XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                                        BigDecimal price = xhpcPlaceholderTime.getPrice();
                                        String pattern = "HH:mm:ss";
                                        Long startOne = DateUtil.parse("00:00:00", pattern).getTime();
                                        Long endOne = DateUtil.parse("23:59:59", pattern).getTime();

                                        String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                        String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                        Long time1 = DateUtil.parse(format1, pattern).getTime();
                                        Long time2 = DateUtil.parse(format2, pattern).getTime();
                                        XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                                        if (startOne < time1) {
                                            startOne = time1;
                                            placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                                        }else{
                                            placeholderTimeOrderDto.setStartTime(DateUtil.parse("00:00:00", pattern));
                                        }
                                        if (endOne > time2) {
                                            endOne = time2;
                                            placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                                        }else{
                                            placeholderTimeOrderDto.setEndTime(DateUtil.parse("23:59:59", pattern));
                                        }
                                        long betweOne = (endOne - startOne) / 60000;
                                        int i1 = (int) (betweOne);

                                        if (freeTime - i1 > 0) {
                                            freeTime = freeTime - i1;
                                            i1 = 0;
                                        } else {
                                            i1 = i1 - freeTime;
                                            freeTime = 0;
                                        }

                                        BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                        totalMoney = totalMoney.add(multiply);

                                        placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                                        placeholderTimeOrderDto.setPrice(price);
                                        placeholderTimeOrderDto.setTotalMoney(multiply);
                                        placeholderTimeOrderDto.setTotalTimeSpan(i1);
                                        xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);

                                    }
                                }else{
                                    List<XhpcPlaceholderTime> xhpcPlaceholderTimeTwo = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime("00:00:00", end, xhpcPlaceholderOrder.getPlaceholderId());
                                    for (int i = 0; i < xhpcPlaceholderTimeTwo.size(); i++) {
                                        XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeTwo.get(i);
                                        BigDecimal price = xhpcPlaceholderTime.getPrice();
                                        String pattern = "HH:mm:ss";
                                        Long startOne = DateUtil.parse("00:00:00", pattern).getTime();
                                        Long endOne = DateUtil.parse(end, pattern).getTime();

                                        String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                        String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                        Long time1 = DateUtil.parse(format1, pattern).getTime();
                                        Long time2 = DateUtil.parse(format2, pattern).getTime();

                                        XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                                        if (startOne < time1) {
                                            startOne = time1;
                                            placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                                        }else{
                                            placeholderTimeOrderDto.setStartTime(DateUtil.parse("00:00:00", pattern));
                                        }
                                        if (endOne > time2) {
                                            endOne = time2;
                                            placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                                        }else{
                                            placeholderTimeOrderDto.setEndTime(DateUtil.parse(end, pattern));
                                        }
                                        long betweOne = (endOne - startOne) / 60000;
                                        int i1 = (int) (betweOne);
                                        if (freeTime - i1 > 0) {
                                            freeTime = freeTime - i1;
                                            i1 = 0;
                                        } else {
                                            i1 = i1 - freeTime;
                                            freeTime = 0;
                                        }
                                        BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                        totalMoney = totalMoney.add(multiply);


                                        placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                                        placeholderTimeOrderDto.setPrice(price);
                                        placeholderTimeOrderDto.setTotalMoney(multiply);
                                        placeholderTimeOrderDto.setTotalTimeSpan(i1);
                                        xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);
                                    }
                                }
                            }
                        }else {
                            List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime(start, end, xhpcPlaceholderOrder.getPlaceholderId());
                            for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                                XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                                BigDecimal price = xhpcPlaceholderTime.getPrice();
                                String pattern = "HH:mm:ss";
                                Long startOne = DateUtil.parse(start, pattern).getTime();
                                Long endOne = DateUtil.parse(end, pattern).getTime();

                                String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                Long time1 = DateUtil.parse(format1, pattern).getTime();
                                Long time2 = DateUtil.parse(format2, pattern).getTime();

                                XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                                if (startOne < time1) {
                                    startOne = time1;
                                    placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                                }else{
                                    placeholderTimeOrderDto.setStartTime(DateUtil.parse(start, pattern));
                                }
                                if (endOne > time2) {
                                    endOne = time2;
                                    placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                                }else{
                                    placeholderTimeOrderDto.setEndTime(DateUtil.parse(end, pattern));
                                }
                                long betweOne = (endOne - startOne) / 60000;
                                int i1 = (int) (betweOne);

                                if (freeTime - i1 > 0) {
                                    freeTime = freeTime - i1;
                                    i1 = 0;
                                } else {
                                    i1 = i1 - freeTime;
                                    freeTime = 0;
                                }
                                BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                totalMoney = totalMoney.add(multiply);

                                placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                                placeholderTimeOrderDto.setPrice(price);
                                placeholderTimeOrderDto.setTotalMoney(multiply);
                                placeholderTimeOrderDto.setTotalTimeSpan(i1);
                                xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);
                            }
                        }
                    }

                    if(totalMoney.compareTo(new BigDecimal(50)) == 1){
                        totalMoney = new BigDecimal(50);
                    }
                    xhpcPlaceholderOrder.setTotalMoney(totalMoney);
                    if (new BigDecimal(0).compareTo(new BigDecimal(between)) == -1) {
                        xhpcPlaceholderOrder.setOvertimePrice(totalMoney.divide(new BigDecimal(between), 2, BigDecimal.ROUND_HALF_UP));
                    } else {
                        xhpcPlaceholderOrder.setOvertimePrice(new BigDecimal("0.0"));
                    }

                    String orderKey = "notificationPlaceholderOrderInfo:" + xhpcPlaceholderOrder.getSerialNumber();
                    Map<String, Object> cacheOrder = new HashMap<>();
                    cacheOrder.put("OvertimeBillCode", xhpcPlaceholderOrder.getPlaceholderOrderNumber());
                    cacheOrder.put("StartChargeSeq", xhpcPlaceholderOrder.getInternetSerialNumber());
                    cacheOrder.put("OvertimeBillStatus", 3);
                    cacheOrder.put("ConnectorId", xhpcPlaceholderOrder.getTerminalNumber());
                    cacheOrder.put("BillEquipment", 1);
                    cacheOrder.put("StartTime", DateUtil.format(xhpcPlaceholderOrder.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
                    cacheOrder.put("EndTime", DateUtil.format(xhpcPlaceholderOrder.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
                    cacheOrder.put("TotalTimeSpan", xhpcPlaceholderOrder.getTotalTimeSpan());
                    cacheOrder.put("OvertimePrice", xhpcPlaceholderOrder.getOvertimePrice());
                    cacheOrder.put("TotalMoney", xhpcPlaceholderOrder.getTotalMoney());

                    Map<String, Object> overtimeDetails = new HashMap<>();
                    overtimeDetails.put("DetailStartTime", DateUtil.format(xhpcPlaceholderOrder.getStartTime(), "yyyy-MM-dd HH:mm:ss"));
                    overtimeDetails.put("DetailEndTime", DateUtil.format(xhpcPlaceholderOrder.getEndTime(), "yyyy-MM-dd HH:mm:ss"));
                    overtimeDetails.put("DetailMoney", xhpcPlaceholderOrder.getTotalMoney());
                    overtimeDetails.put("OvertimePrice", xhpcPlaceholderOrder.getOvertimePrice());
                    cacheOrder.put("OvertimeDetails", overtimeDetails);
                    REDIS.setCacheMap(orderKey, cacheOrder);
                    xhpcPlaceholderOrder.setType(1);

                    if(xhpcPlaceholderOrder.getSource() !=UserTypeUtil.INTERNET_TYPE){
                        Long userId = xhpcPlaceholderOrder.getUserId();
                        Integer source = xhpcPlaceholderOrder.getSource();
                        String tenantId = xhpcPlaceholderOrder.getTenantId();

                        if(UserTypeUtil.USER_TYPE == xhpcPlaceholderOrder.getSource()){
                            xhpcPlaceholderOrder.setChargingMode("C端用户");
                        }else if(UserTypeUtil.COMMUNIT_TYPE == xhpcPlaceholderOrder.getSource()){
                            xhpcPlaceholderOrder.setChargingMode("社区用户");
                        }else if(UserTypeUtil.COMMUNIT_TYPE == xhpcPlaceholderOrder.getSource()){
                            xhpcPlaceholderOrder.setChargingMode("社区用户");
                        }else if(UserTypeUtil.COMMUNIT_TYPE == xhpcPlaceholderOrder.getSource()){
                            xhpcPlaceholderOrder.setChargingMode("社区用户");
                        }else{
                            xhpcPlaceholderOrder.setChargingMode("B端用户");
                        }
                        BigDecimal totalMoney1 = xhpcPlaceholderOrder.getTotalMoney();
                        xhpcPlaceholderOrder.setStatus(3);
                        if(totalMoney1.compareTo(new BigDecimal(0))>0){
                            String key ="placeholderOrder:" +source+ tenantId+userId;
                            int number =0;
                            if(redisService.getCacheObject(key) !=null){
                                String numberYu = redisService.getCacheObject(key).toString();
                                int numbers =Integer.parseInt(numberYu);
                                numbers++;
                                number=numbers;
                                redisService.setCacheObject(key, numbers);
                            }else{
                                redisService.setCacheObject(key, 1);
                                number=1;
                            }
                            R user = userTypeService.getUser(null, userId, source, null, tenantId);
                            if(number>3){
                                if(user !=null && user.getData() !=null ){
                                    Map<String, Object> userMessage = (Map<String, Object>)user.getData();
                                    String phone =userMessage.get("phone").toString();
                                    BigDecimal balance1 =new BigDecimal(userMessage.get("balance").toString());
                                    BigDecimal subtract = balance1.subtract(totalMoney1);

                                    int i = xhpcChargeOrderService.updateUserBalance(userId, subtract,source,tenantId);
                                    if(i==0){
                                        //扣钱失败
                                        System.out.println("<<<<<<<<<<<<<<<<扣钱失败>>>>userId>>>>>>>>>>>>>"+userId+">>subtract>>>>>>>>"+subtract+">>balance1>>>>>>>>"+balance1+">>source>>>>>>>>"+source);
                                    }else{
                                        //添加流水
                                        xhpcChargeOrderService.addUserAccountStatement(userId,totalMoney1, subtract, xhpcPlaceholderOrder.getPlaceholderOrderId(), 7, new Date(),source);
                                    }
                                    HashMap<String, String> paramMap = new HashMap<>();
                                    paramMap.put("time", xhpcPlaceholderOrder.getTotalTimeSpan()+"");
                                    paramMap.put("sumMoney",totalMoney1.toString());
                                    paramMap.put("phone", phone);
                                    paramMap.put("content", "尊敬的用户你好，你的爱车已停止充电"+xhpcPlaceholderOrder.getTotalTimeSpan()+"分钟，前30分钟免费，平台收取本次设备占用费："+totalMoney1+"元，占位费费用将从余额扣除，占位费明细请查询翔桦充电小程序我的历史订单，充电结束请及时挪车。");
                                    smsService.sendNotice(paramMap);
                                    //平台用户直接余额扣钱
                                    xhpcPlaceholderOrder.setActPrice(xhpcPlaceholderOrder.getTotalMoney());
                                    xhpcPlaceholderOrder.setActStatus(1);
                                }
                            }else{
                                //发送短信
                                //平台用户直接余额扣钱
                                if(user !=null && user.getData() !=null ){
                                    Map<String, Object> userMessage = (Map<String, Object>)user.getData();
                                    String phone =userMessage.get("phone").toString();
                                    HashMap<String, String> paramMap = new HashMap<>();
                                    paramMap.put("time", xhpcPlaceholderOrder.getTotalTimeSpan()+"");
                                    paramMap.put("sumMoney",totalMoney1.toString());
                                    paramMap.put("number", number+"");
                                    paramMap.put("phone", phone);
                                    paramMap.put("content", "尊敬的用户，你的爱车已停止充电"+xhpcPlaceholderOrder.getTotalTimeSpan()+"分钟，前30分钟免费，平台收取本次设备占用费："+totalMoney1+"元，占位费费用将从余额扣除，占位费明细请查询翔桦充电小程序我的历史订单，试运行期间前三次免费，当前免费次数为"+number+"，充电结束请及时挪车。");
                                    smsService.sendNotice(paramMap);

                                }
                                xhpcPlaceholderOrder.setActPrice(new BigDecimal(0));
                                xhpcPlaceholderOrder.setActStatus(1);
                            }
                        }else{
                            xhpcPlaceholderOrder.setActPrice(new BigDecimal(0));
                            xhpcPlaceholderOrder.setActStatus(1);
                        }
                    }
                    xhpcPlaceholderOrderMapper.updateXhpcPlaceholderOrder(xhpcPlaceholderOrder);
                }
            }
        }
    }

    @Override
    public List<Map<String, Object>> getListPage(String tenantId, Long chargingStationId, Long chargingPileId, Long terminalId, String phone, String transactionNumber, Integer status, String chargingStationName, Long operatorId, Integer source, String beginStartTime, String beginEndTime, Long userId, Integer type, String affiliationOrganization, String evcsOrderNo, String plateNum, Integer internetId, String internetSerialNumber, String terminalName, String vinCode, String overStartTime, String overEndTime, Long personnelId, Integer confirmResult, Double totalMoneyMin, Double totalMoneyMax,String placeholderOrderNumber) {
        Integer number = 0;
        //电话和用户类型都不为null  number =3
        //电话不为null，用户类型为null  number =1
        //电话为null，用户类型不为null  number=2
        //电话为null，用户类型为null  number=0
        if (!"".equals(phone) && phone != null && !"".equals(source) && source != null) {
            number = 3;
        } else if (!"".equals(phone) && phone != null && source == null) {
            number = 1;
        } else if (phone == null && source != null) {
            number = 2;
        }
        //获取登陆用户
        Long logUserId = SecurityUtils.getUserId();
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        //桩的统计、该时段金额
        List<Map<String, Object>> list = new ArrayList<>();
        if (!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())) {
            startPage();
            if (UserTypeUtil.SYS_SUPER_TYPE_ONE.equals(loginUser.getSuperType())) {
                tenantId = loginUser.getTenantId();
                Long logOperatorId = sysUser.getOperatorId();
                list = xhpcPlaceholderOrderMapper.getListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 1, chargingStationName, operatorId, source, beginStartTime, beginEndTime, logOperatorId, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0,  totalMoneyMin,  totalMoneyMax,placeholderOrderNumber);
            }
            if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())) {
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcPlaceholderOrderMapper.getListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 1, chargingStationName, operatorId, source, beginStartTime, beginEndTime, logOperatorId, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0, totalMoneyMin, totalMoneyMax,placeholderOrderNumber);
            } else {
                list = xhpcPlaceholderOrderMapper.getListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 2, chargingStationName, operatorId, source, beginStartTime, beginEndTime, logUserId, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0, totalMoneyMin, totalMoneyMax,placeholderOrderNumber);
            }
        } else {
            startPage();
            list = xhpcPlaceholderOrderMapper.getListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 0, chargingStationName, operatorId, source, beginStartTime, beginEndTime, logUserId, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0, totalMoneyMin, totalMoneyMax,placeholderOrderNumber);

        }
        return list;
    }

    @Override
    public List<Map<String, Object>> getAbnormalListPage(String tenantId, Long chargingStationId, Long chargingPileId, Long terminalId, String phone, String transactionNumber, Integer status, String chargingStationName, Long operatorId, Integer source, String beginStartTime, String beginEndTime, Long userId, Integer type, String affiliationOrganization, String evcsOrderNo, String plateNum, Integer internetId, String internetSerialNumber, String terminalName, String vinCode, String overStartTime, String overEndTime, Long personnelId, Integer confirmResult) {
        Integer number = 0;
        if (!"".equals(phone) && phone != null && !"".equals(source) && source != null) {
            number = 3;
        } else if (!"".equals(phone) && phone != null && source == null) {
            number = 1;
        } else if (phone == null && source != null) {
            number = 2;
        }
        //获取登陆用户
        Long logUserId = SecurityUtils.getUserId();
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        //桩的统计、该时段金额
        List<Map<String, Object>> list = new ArrayList<>();
        if (!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())) {
            startPage();
            if (UserTypeUtil.SYS_SUPER_TYPE_ONE.equals(loginUser.getSuperType())) {
                tenantId = loginUser.getTenantId();
                Long logOperatorId = sysUser.getOperatorId();
                list = xhpcPlaceholderOrderMapper.getAbnormalListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 1, chargingStationName, operatorId, source, beginStartTime, beginEndTime, logOperatorId, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0);
            }
            if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())) {
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcPlaceholderOrderMapper.getAbnormalListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 1, chargingStationName, operatorId, source, beginStartTime, beginEndTime, logOperatorId, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0);
            } else {
                list = xhpcPlaceholderOrderMapper.getAbnormalListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 2, chargingStationName, operatorId, source, beginStartTime, beginEndTime, logUserId, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0);
            }
        } else {
            startPage();
            list = xhpcPlaceholderOrderMapper.getAbnormalListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 0, chargingStationName, operatorId, source, beginStartTime, beginEndTime, null, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0);

        }
        return list;
    }

    @Override
    public void export(HttpServletResponse response, String tenantId, Long chargingStationId, Long chargingPileId, Long terminalId, String phone, String transactionNumber, Integer status, String chargingStationName, Long operatorId, Integer source, String beginStartTime, String beginEndTime, Long userId, Integer type, String affiliationOrganization, String evcsOrderNo, String plateNum, Integer internetId, String internetSerialNumber, String terminalName, String vinCode, String overStartTime, String overEndTime, Long personnelId, Integer confirmResult, Double totalMoneyMin, Double totalMoneyMax,String placeholderOrderNumber) throws IOException {

        Integer number = 0;
        //电话和用户类型都不为null  number =3
        //电话不为null，用户类型为null  number =1
        //电话为null，用户类型不为null  number=2
        //电话为null，用户类型为null  number=0
        if (!"".equals(phone) && phone != null && !"".equals(source) && source != null) {
            number = 3;
        } else if (!"".equals(phone) && phone != null && source == null) {
            number = 1;
        } else if (phone == null && source != null) {
            number = 2;
        }
        //获取登陆用户
        Long logUserId = SecurityUtils.getUserId();
        LoginUser loginUser = tokenService.getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        //桩的统计、该时段金额
        List<Map<String, Object>> list = new ArrayList<>();
        if (!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())) {
            if (UserTypeUtil.SYS_SUPER_TYPE_ONE.equals(loginUser.getSuperType())) {
                tenantId = loginUser.getTenantId();
                Long logOperatorId = sysUser.getOperatorId();
                list = xhpcPlaceholderOrderMapper.getListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 1, chargingStationName, operatorId, source, beginStartTime, beginEndTime, logOperatorId, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0,  totalMoneyMin,  totalMoneyMax,placeholderOrderNumber);
            }
            if (UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())) {
                Long logOperatorId = sysUser.getOperatorId();
                //运营商看自己的场站
                list = xhpcPlaceholderOrderMapper.getListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 1, chargingStationName, operatorId, source, beginStartTime, beginEndTime, logOperatorId, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0,  totalMoneyMin,  totalMoneyMax,placeholderOrderNumber);
            } else {
                list = xhpcPlaceholderOrderMapper.getListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 2, chargingStationName, operatorId, source, beginStartTime, beginEndTime, logUserId, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0,  totalMoneyMin,  totalMoneyMax,placeholderOrderNumber);
            }
        } else {
            list = xhpcPlaceholderOrderMapper.getListPage(chargingStationId, chargingPileId, terminalId, phone, transactionNumber, 0, chargingStationName, operatorId, source, beginStartTime, beginEndTime, null, type, number, affiliationOrganization, evcsOrderNo, plateNum, internetId, internetSerialNumber, terminalName, vinCode, overStartTime, overEndTime, personnelId, confirmResult, tenantId, 0,  totalMoneyMin,  totalMoneyMax,placeholderOrderNumber);
        }

        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("placeholderOrder_" + System.currentTimeMillis() + ".xlsx");
//        writer.addHeaderAlias("placeholderOrderId", "占位费订单ID");
        writer.addHeaderAlias("serialNumber", "充电订单号");
        writer.addHeaderAlias("internetSerialNumber", "流量方订单号");
        writer.addHeaderAlias("placeholderOrderNumber", "占位费订单号");
        writer.addHeaderAlias("operatorName", "运营商名称");
        writer.addHeaderAlias("chargingStationName", "电站名称");
        writer.addHeaderAlias("pileSerialNumber", "桩编号");
        writer.addHeaderAlias("terminalName", "终端名称");
        writer.addHeaderAlias("chargingModeName", "订单来源");
        writer.addHeaderAlias("freeTime", "免费时长（分钟）");
        writer.addHeaderAlias("maxFee", "最大占位费（元）");
        writer.addHeaderAlias("totalTimeSpan", "占位总时长（分钟）");
        writer.addHeaderAlias("totalTimeSpan", "占位总时长（分钟）");
        writer.addHeaderAlias("totalMoney", "流量方占位费（元）");
        writer.addHeaderAlias("rechargeMoney", "流量方占位已支付（元）");
        writer.addHeaderAlias("refundMoney", "流量方占位未支付（元）");
        writer.addHeaderAlias("confirmResultName", "流量方推送状态");
//        writer.addHeaderAlias("sourceName", "用户类型");
        writer.addHeaderAlias("startTime", "开始占位时间");
        writer.addHeaderAlias("endTime", "结束占位时间");
        writer.addHeaderAlias("actPrice", "平台用户占位费（元）");
        writer.addHeaderAlias("appealRechargeMoney", "平台用户已支付（元）");
        writer.addHeaderAlias("appealRefundMoney", "平台用户未支付（元）");
//        writer.addHeaderAlias("plateNum", "电站名称");
//        writer.addHeaderAlias("disputeOrderStatus", "桩编号");

        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
//        //response为HttpServletResponse对象
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");
//        //test.xls是弹出下载对话框的文件名，不能为中文，中文请自行编码
//        response.setHeader("Content-Disposition", "attachment;filename=历史订单列表.xls");
        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    @Override
    public R getById(Long placeholderOrderId) {
        try {
            Map<String, Object> byId = xhpcPlaceholderOrderMapper.getById(placeholderOrderId);
            if (byId != null) {
                Long placeholderId = Long.valueOf(byId.get("placeholderId").toString());
                List<XhpcPlaceholderTime> list = xhpcPlaceholderOrderMapper.getXhpcPlaceholderId(placeholderId);
                byId.put("xhpcPlaceholderTime",list);

                List<Map<String, Object>> xhpcPlaceholderTimeOrder = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTimeOrder(placeholderOrderId);
                List<Map<String, Object>> mapList =new ArrayList<>();
                if(xhpcPlaceholderTimeOrder!=null&& xhpcPlaceholderTimeOrder.size()>0){
                    byId.put("xhpcPlaceholderTimeOrder",xhpcPlaceholderTimeOrder);
                }else{

                    DateTime startTime = DateUtil.parse(byId.get("startTime").toString(),"yyyy-MM-dd HH:mm:ss");
                    DateTime endTime = DateUtil.parse(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd HH:mm:ss");
                    String start = DateUtil.formatTime(startTime);
                    String end = DateUtil.formatTime(endTime);
                    DateTime parse = DateUtil.parse(DateUtil.format(startTime, "yyyy-MM-dd"), "yyyy-MM-dd");
                    DateTime parse1 = DateUtil.parse(DateUtil.format(endTime, "yyyy-MM-dd"), "yyyy-MM-dd");
                    long betweenDay = DateUtil.between(parse,parse1, DateUnit.DAY);
                    long between = DateUtil.between(endTime, startTime, DateUnit.MINUTE);

                    BigDecimal totalMoney = new BigDecimal("0.0");
                    int freeTime = Integer.parseInt(byId.get("freeTime").toString());
                    if (freeTime - between > 0) {
                        between = 0L;
                    } else {
                        between = between - freeTime;
                        if (betweenDay > 0) {
                            for (int m = 0; m <= betweenDay; m++) {
                                if(m==0){
                                    List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime(start, "23:59:59", placeholderId);
                                    for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                                        XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                                        BigDecimal price = xhpcPlaceholderTime.getPrice();
                                        String pattern = "HH:mm:ss";
                                        Long startOne = DateUtil.parse(start, pattern).getTime();
                                        Long endOne = DateUtil.parse("23:59:59", pattern).getTime();

                                        String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                        String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                        Long time1 = DateUtil.parse(format1, pattern).getTime();
                                        Long time2 = DateUtil.parse(format2, pattern).getTime();

                                        String startTimeYu="";
                                        String endTimeYu="";
                                        if (startOne < time1) {
                                            startOne = time1;
                                            startTimeYu = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                        }else{
                                            startTimeYu = start;
                                        }
                                        if (endOne > time2) {
                                            endOne = time2;
                                            endTimeYu=DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);
                                        }else{
                                            endTimeYu = "23:59:59";
                                        }
                                        long betweOne = (endOne - startOne) / 60000;
                                        int i1 = (int) (betweOne);
                                        if (freeTime - i1 > 0) {
                                            freeTime = freeTime - i1;
                                            i1 = 0;
                                        } else {
                                            i1 = i1 - freeTime;
                                            freeTime = 0;
                                        }
                                        BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                        Map<String, Object> map =new HashMap<>();
                                        map.put("time",startTimeYu+"-"+endTimeYu);
                                        map.put("price",price);
                                        map.put("totalMoney",multiply);
                                        map.put("totalTimeSpan",betweOne);
                                        mapList.add(map);
                                    }
                                }else if(m>0 && m<betweenDay){
                                    List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime("00:00:00", "23:59:59", placeholderId);
                                    for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                                        XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                                        BigDecimal price = xhpcPlaceholderTime.getPrice();
                                        String pattern = "HH:mm:ss";
                                        Long startOne = DateUtil.parse("00:00:00", pattern).getTime();
                                        Long endOne = DateUtil.parse("23:59:59", pattern).getTime();

                                        String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                        String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                        Long time1 = DateUtil.parse(format1, pattern).getTime();
                                        Long time2 = DateUtil.parse(format2, pattern).getTime();

                                        String startTimeYu="";
                                        String endTimeYu="";

                                        if (startOne < time1) {
                                            startOne = time1;
                                            startTimeYu = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                        }else{
                                            startTimeYu="00:00:00";
                                        }
                                        if (endOne > time2) {
                                            endOne = time2;
                                            endTimeYu = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);
                                        }else{
                                            endTimeYu="23:59:59";
                                        }
                                        long betweOne = (endOne - startOne) / 60000;
                                        int i1 = (int) (betweOne);

                                        if (freeTime - i1 > 0) {
                                            freeTime = freeTime - i1;
                                            i1 = 0;
                                        } else {
                                            i1 = i1 - freeTime;
                                            freeTime = 0;
                                        }

                                        BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                        totalMoney = totalMoney.add(multiply);
                                        Map<String, Object> map =new HashMap<>();
                                        map.put("time",startTimeYu+"-"+endTimeYu);
                                        map.put("price",price);
                                        map.put("totalMoney",multiply);
                                        map.put("totalTimeSpan",betweOne);
                                        mapList.add(map);
                                    }
                                }else{
                                    List<XhpcPlaceholderTime> xhpcPlaceholderTimeTwo = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime("00:00:00", end, placeholderId);
                                    for (int i = 0; i < xhpcPlaceholderTimeTwo.size(); i++) {
                                        XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeTwo.get(i);
                                        BigDecimal price = xhpcPlaceholderTime.getPrice();
                                        String pattern = "HH:mm:ss";
                                        Long startOne = DateUtil.parse("00:00:00", pattern).getTime();
                                        Long endOne = DateUtil.parse(end, pattern).getTime();

                                        String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                        String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                        Long time1 = DateUtil.parse(format1, pattern).getTime();
                                        Long time2 = DateUtil.parse(format2, pattern).getTime();

                                        String startTimeYu="";
                                        String endTimeYu="";

                                        if (startOne < time1) {
                                            startOne = time1;
                                            startTimeYu=DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                        }else{
                                            startTimeYu="00:00:00";
                                        }
                                        if (endOne > time2) {
                                            endOne = time2;
                                            endTimeYu=DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);
                                        }else{
                                            endTimeYu=end;
                                        }
                                        long betweOne = (endOne - startOne) / 60000;
                                        int i1 = (int) (betweOne);

                                        if (freeTime - i1 > 0) {
                                            freeTime = freeTime - i1;
                                            i1 = 0;
                                        } else {
                                            i1 = i1 - freeTime;
                                            freeTime = 0;
                                        }
                                        BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                        totalMoney = totalMoney.add(multiply);
                                        Map<String, Object> map =new HashMap<>();
                                        map.put("time",startTimeYu+"-"+endTimeYu);
                                        map.put("price",price);
                                        map.put("totalMoney",multiply);
                                        map.put("totalTimeSpan",betweOne);
                                        mapList.add(map);
                                    }
                                }
                            }
                        }else {
                            List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime(start, end, placeholderId);
                            for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                                XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                                BigDecimal price = xhpcPlaceholderTime.getPrice();
                                String pattern = "HH:mm:ss";
                                Long startOne = DateUtil.parse(start, pattern).getTime();
                                Long endOne = DateUtil.parse(end, pattern).getTime();

                                String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                                Long time1 = DateUtil.parse(format1, pattern).getTime();
                                Long time2 = DateUtil.parse(format2, pattern).getTime();

                                String startTimeYu="";
                                String endTimeYu="";
                                if (startOne < time1) {
                                    startOne = time1;
                                    startTimeYu=DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                                }else{
                                    startTimeYu=start;
                                }
                                if (endOne > time2) {
                                    endOne = time2;
                                    endTimeYu=DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);
                                }else{
                                    endTimeYu=end;
                                }
                                long betweOne = (endOne - startOne) / 60000;
                                int i1 = (int) (betweOne);

                                if (freeTime - i1 > 0) {
                                    freeTime = freeTime - i1;
                                    i1 = 0;
                                } else {
                                    i1 = i1 - freeTime;
                                    freeTime = 0;
                                }
                                BigDecimal multiply = new BigDecimal(i1).multiply(price);
                                totalMoney = totalMoney.add(multiply);
                                Map<String, Object> map =new HashMap<>();
                                map.put("time",startTimeYu+"-"+endTimeYu);
                                map.put("price",price);
                                map.put("totalMoney",multiply);
                                map.put("totalTimeSpan",betweOne);
                                mapList.add(map);
                            }
                        }
                    }
                    byId.put("xhpcPlaceholderTimeOrder",mapList);
                }
            }

            return R.ok(byId);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail(e.getMessage());
        }
    }

    @Override
    public void getxhpcPlaceholderOrderTasks() {
        //定时任务相同桩号，前一个订单已结束，本次订单响应结束


    }

    @Override
    public void getAbnormal() {
        xhpcPlaceholderOrderMapper.getAbnormal();
    }

    @Override
    public void getType() {
        //查询type=3的数据
        List<Map<String, Object>> mapList = xhpcPlaceholderOrderMapper.getType();
        for (int i = 0; i < mapList.size(); i++) {
            try{
                Map<String, Object> map = mapList.get(i);
                String serialNumber = map.get("serialNumber").toString();
                Long placeholderOrderId = Long.parseLong(map.get("placeholderOrderId").toString());
                //查询历史订单是否为0
                int count = xhpcPlaceholderOrderMapper.getXhpcHistoryOrderTotalPrice(serialNumber, 2);
                if(count==0){
                    getExamine(placeholderOrderId,new BigDecimal(0),map.get("startTime").toString(),map.get("startTime").toString());
                }else{
                    String startTime = map.get("startTime").toString();
                    Long historyOrderId =Long.valueOf(map.get("historyOrderId").toString());
                    Long terminalId =Long.valueOf(map.get("terminalId").toString());
                    //查看是否有下一单
                    int number = xhpcPlaceholderOrderMapper.getXhpcHistoryOrderNextOrderTimeYu(historyOrderId, terminalId, startTime);
                    //有下一单返回1 没有下一单返回0
                    if(number==0){
                        //查询最后拔枪的时间
                        Map<String, Object> mapTime = xhpcPlaceholderOrderMapper.getXhpcPlaceholderOrderTypeYu(serialNumber, serialNumber.substring(0, 16));
                        if (mapTime != null && mapTime.get("createTime") != null) {
                            System.out.println("==111=========createTime==============="+mapTime.get("createTime").toString());
                            getExamine(placeholderOrderId, new BigDecimal(0), map.get("startTime").toString(),mapTime.get("createTime").toString());
                        }
                    }else{
                        //查看下一单的时间是否超过30分钟
                        int result = xhpcPlaceholderOrderMapper.getXhpcHistoryOrderNextOrderTime(historyOrderId, terminalId, startTime);
                        if(result==0){
                            getExamine(placeholderOrderId,new BigDecimal(0),map.get("startTime").toString(),map.get("startTime").toString());
                        }else{
                            //查询最后拔枪的时间
                            System.out.println("===33333========createTime===============");
                            //查询出最大一条数据
                            Long deviceMessageIdMax = xhpcPlaceholderOrderMapper.getDeviceMessageIdMax(serialNumber, serialNumber.substring(0, 16));
                            if (deviceMessageIdMax !=null) {
                                Map<String, Object> mapTime = xhpcPlaceholderOrderMapper.getXhpcPlaceholderOrderType(serialNumber, serialNumber.substring(0, 16),deviceMessageIdMax);
                                if (mapTime!=null &&  mapTime.get("createTime") !=null){
                                    System.out.println("===222========createTime==============="+mapTime.get("createTime").toString()+"==========serialNumber========"+serialNumber);
                                    getExamine(placeholderOrderId,new BigDecimal(0),map.get("startTime").toString(),mapTime.get("createTime").toString());
                                }
                            }

                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


    @Override
    public void getOrderNum() {
        //当前实时数据的终端有新的订单异常处理
        List<XhpcPlaceholderOrder> abnormalTimeList = xhpcPlaceholderOrderMapper.getAbnormalTimeList();

        for (int i = 0; i <abnormalTimeList.size() ; i++) {
            XhpcPlaceholderOrder xhpcPlaceholderOrder = abnormalTimeList.get(i);
            //查看是否有下一单
            int number = xhpcPlaceholderOrderMapper.getXhpcHistoryOrderNextOrderTimeYu(xhpcPlaceholderOrder.getHistoryOrderId(), xhpcPlaceholderOrder.getTerminalId(), null);
            if(number==1){
                xhpcPlaceholderOrder.setType(2);
                xhpcPlaceholderOrderMapper.updateXhpcPlaceholderOrder(xhpcPlaceholderOrder);
            }else{
                //0离线 1故障 2空闲 3充电 4 已插枪 5未知
                //查询当前桩的状态是否空闲
                String serialNumber = xhpcPlaceholderOrder.getSerialNumber().substring(0, 16);
                Map<String, Object> cacheMap =redisService.getCacheMap("gun:"+serialNumber);
                boolean fian =false;
                if(!cacheMap.isEmpty()){
                    if ("离线".equals(cacheMap.get("status").toString())){
                        fian =true;
                    }else if("故障".equals(cacheMap.get("status").toString())){
                        fian =true;
                    }else if("空闲".equals(cacheMap.get("status").toString()) || "插枪".equals(cacheMap.get("status").toString())){
                        if("是".equals(cacheMap.get("vehicleGunStatus").toString())){

                        }else{
                            fian =true;
                        }
                    }
                }
                if(fian){
                    xhpcPlaceholderOrder.setType(2);
                    xhpcPlaceholderOrderMapper.updateXhpcPlaceholderOrder(xhpcPlaceholderOrder);
                }
            }
        }




    }

    @Override
    public R getPlaceholderIDs(String[] stationIDs) {

        List<Long> stations =new ArrayList<>();
        for (int i = 0; i < stationIDs.length; i++) {
            String stationID = stationIDs[i];
            stations.add(Long.valueOf(stationID));
        }
        List<PlaceholderRules> placeholderIDs = xhpcPlaceholderOrderMapper.getPlaceholderIDs(stations);

        for (int i = 0; i <placeholderIDs.size() ; i++) {
            PlaceholderRules placeholderRules = placeholderIDs.get(i);
            List<PriceInfo> priceInfo = xhpcPlaceholderOrderMapper.getPriceInfo(placeholderRules.getPlaceholderId());
            placeholderRules.setPriceInfos(priceInfo);
        }
        return R.ok(placeholderIDs);
    }

    @Override
    public AjaxResult getExamine(Long placeholderOrderId, BigDecimal totalMoneyYu, String startTimeYu, String endTimeYu) {

        XhpcPlaceholderOrder xhpcPlaceholderOrder=xhpcPlaceholderOrderMapper.getByPlaceholderOrderId(placeholderOrderId);

        xhpcPlaceholderOrder.setPlaceholderOrderId(placeholderOrderId);

        xhpcPlaceholderOrder.setStartTime(DateUtil.parse(startTimeYu,"yyyy-MM-dd HH:mm:ss"));
        xhpcPlaceholderOrder.setEndTime(DateUtil.parse(endTimeYu,"yyyy-MM-dd HH:mm:ss"));

        xhpcPlaceholderOrder.setTotalTimeSpan((int) DateUtil.between(xhpcPlaceholderOrder.getStartTime(), xhpcPlaceholderOrder.getEndTime(), DateUnit.MINUTE));
        xhpcPlaceholderOrder.setType(1);


        DateTime startTime = DateUtil.parse(DateUtil.format(xhpcPlaceholderOrder.getStartTime(), "yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd HH:mm:ss");
        DateTime endTime = DateUtil.parse(DateUtil.format(xhpcPlaceholderOrder.getEndTime(), "yyyy-MM-dd HH:mm:ss"),"yyyy-MM-dd HH:mm:ss");
        String start = DateUtil.formatTime(startTime);
        String end = DateUtil.formatTime(endTime);
        DateTime parse = DateUtil.parse(DateUtil.format(startTime, "yyyy-MM-dd"), "yyyy-MM-dd");
        DateTime parse1 = DateUtil.parse(DateUtil.format(endTime, "yyyy-MM-dd"), "yyyy-MM-dd");
        long betweenDay = DateUtil.between(parse,parse1, DateUnit.DAY);
        long between = DateUtil.between(endTime, startTime, DateUnit.MINUTE);
        xhpcPlaceholderOrder.setEndTime(endTime);
        xhpcPlaceholderOrder.setTotalTimeSpan((int) between);
        BigDecimal totalMoney = new BigDecimal("0.0");
        int freeTime = (int) xhpcPlaceholderOrder.getFreeTime();
        if (freeTime - between > 0) {
            between = 0L;
        } else{
            between = between - freeTime;
            if (betweenDay > 0) {
                for (int m = 0; m <= betweenDay; m++) {
                    if(m==0){
                        List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime(start, "23:59:59", xhpcPlaceholderOrder.getPlaceholderId());
                        for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                            XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                            BigDecimal price = xhpcPlaceholderTime.getPrice();
                            String pattern = "HH:mm:ss";
                            Long startOne = DateUtil.parse(start, pattern).getTime();
                            Long endOne = DateUtil.parse("23:59:59", pattern).getTime();

                            String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                            String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                            Long time1 = DateUtil.parse(format1, pattern).getTime();
                            Long time2 = DateUtil.parse(format2, pattern).getTime();
                            XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                            if (startOne < time1) {
                                startOne = time1;
                                placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                            }else{
                                placeholderTimeOrderDto.setStartTime(DateUtil.parse(start, pattern));
                            }
                            if (endOne > time2) {
                                endOne = time2;
                                placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                            }else{
                                placeholderTimeOrderDto.setEndTime(DateUtil.parse("23:59:59", pattern));
                            }
                            long betweOne = (endOne - startOne) / 60000;
                            int i1 = (int) (betweOne);

                            if (freeTime - i1 > 0) {
                                freeTime = freeTime - i1;
                                i1 = 0;
                            } else {
                                i1 = i1 - freeTime;
                                freeTime = 0;
                            }
                            BigDecimal multiply = new BigDecimal(i1).multiply(price);
                            totalMoney = totalMoney.add(multiply);

                            placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                            placeholderTimeOrderDto.setPrice(price);
                            placeholderTimeOrderDto.setTotalMoney(multiply);
                            placeholderTimeOrderDto.setTotalTimeSpan(i1);
                            xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);

                        }
                    }else if(m>0 && m<betweenDay){
                        List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime("00:00:00", "23:59:59", xhpcPlaceholderOrder.getPlaceholderId());
                        for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                            XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                            BigDecimal price = xhpcPlaceholderTime.getPrice();
                            String pattern = "HH:mm:ss";
                            Long startOne = DateUtil.parse("00:00:00", pattern).getTime();
                            Long endOne = DateUtil.parse("23:59:59", pattern).getTime();

                            String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                            String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                            Long time1 = DateUtil.parse(format1, pattern).getTime();
                            Long time2 = DateUtil.parse(format2, pattern).getTime();
                            XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                            if (startOne < time1) {
                                startOne = time1;
                                placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                            }else{
                                placeholderTimeOrderDto.setStartTime(DateUtil.parse("00:00:00", pattern));
                            }
                            if (endOne > time2) {
                                endOne = time2;
                                placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                            }else{
                                placeholderTimeOrderDto.setEndTime(DateUtil.parse("23:59:59", pattern));
                            }
                            long betweOne = (endOne - startOne) / 60000;
                            int i1 = (int) (betweOne);

                            if (freeTime - i1 > 0) {
                                freeTime = freeTime - i1;
                                i1 = 0;
                            } else {
                                i1 = i1 - freeTime;
                                freeTime = 0;
                            }

                            BigDecimal multiply = new BigDecimal(i1).multiply(price);
                            totalMoney = totalMoney.add(multiply);

                            placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                            placeholderTimeOrderDto.setPrice(price);
                            placeholderTimeOrderDto.setTotalMoney(multiply);
                            placeholderTimeOrderDto.setTotalTimeSpan(i1);
                            xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);

                        }
                    }else{
                        List<XhpcPlaceholderTime> xhpcPlaceholderTimeTwo = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime("00:00:00", end, xhpcPlaceholderOrder.getPlaceholderId());
                        for (int i = 0; i < xhpcPlaceholderTimeTwo.size(); i++) {
                            XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeTwo.get(i);
                            BigDecimal price = xhpcPlaceholderTime.getPrice();
                            String pattern = "HH:mm:ss";
                            Long startOne = DateUtil.parse("00:00:00", pattern).getTime();
                            Long endOne = DateUtil.parse(end, pattern).getTime();

                            String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                            String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                            Long time1 = DateUtil.parse(format1, pattern).getTime();
                            Long time2 = DateUtil.parse(format2, pattern).getTime();

                            XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                            if (startOne < time1) {
                                startOne = time1;
                                placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                            }else{
                                placeholderTimeOrderDto.setStartTime(DateUtil.parse("00:00:00", pattern));
                            }
                            if (endOne > time2) {
                                endOne = time2;
                                placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                            }else{
                                placeholderTimeOrderDto.setEndTime(DateUtil.parse(end, pattern));
                            }
                            long betweOne = (endOne - startOne) / 60000;
                            int i1 = (int) (betweOne);
                            if (freeTime - i1 > 0) {
                                freeTime = freeTime - i1;
                                i1 = 0;
                            } else {
                                i1 = i1 - freeTime;
                                freeTime = 0;
                            }
                            BigDecimal multiply = new BigDecimal(i1).multiply(price);
                            totalMoney = totalMoney.add(multiply);


                            placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                            placeholderTimeOrderDto.setPrice(price);
                            placeholderTimeOrderDto.setTotalMoney(multiply);
                            placeholderTimeOrderDto.setTotalTimeSpan(i1);
                            xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);
                        }
                    }
                }
            }else {
                List<XhpcPlaceholderTime> xhpcPlaceholderTimeOne = xhpcPlaceholderOrderMapper.getXhpcPlaceholderTime(start, end, xhpcPlaceholderOrder.getPlaceholderId());
                for (int i = 0; i < xhpcPlaceholderTimeOne.size(); i++) {
                    XhpcPlaceholderTime xhpcPlaceholderTime = xhpcPlaceholderTimeOne.get(i);
                    BigDecimal price = xhpcPlaceholderTime.getPrice();
                    String pattern = "HH:mm:ss";
                    Long startOne = DateUtil.parse(start, pattern).getTime();
                    Long endOne = DateUtil.parse(end, pattern).getTime();

                    String format1 = DateUtil.format(xhpcPlaceholderTime.getStartTime(), pattern);
                    String format2 = DateUtil.format(xhpcPlaceholderTime.getEndTime(), pattern);

                    Long time1 = DateUtil.parse(format1, pattern).getTime();
                    Long time2 = DateUtil.parse(format2, pattern).getTime();

                    XhpcPlaceholderTimeOrderDto placeholderTimeOrderDto = new XhpcPlaceholderTimeOrderDto();
                    if (startOne < time1) {
                        startOne = time1;
                        placeholderTimeOrderDto.setStartTime(xhpcPlaceholderTime.getStartTime());
                    }else{
                        placeholderTimeOrderDto.setStartTime(DateUtil.parse(start, pattern));
                    }
                    if (endOne > time2) {
                        endOne = time2;
                        placeholderTimeOrderDto.setEndTime(xhpcPlaceholderTime.getEndTime());
                    }else{
                        placeholderTimeOrderDto.setEndTime(DateUtil.parse(end, pattern));
                    }
                    long betweOne = (endOne - startOne) / 60000;
                    int i1 = (int) (betweOne);

                    if (freeTime - i1 > 0) {
                        freeTime = freeTime - i1;
                        i1 = 0;
                    } else {
                        i1 = i1 - freeTime;
                        freeTime = 0;
                    }
                    BigDecimal multiply = new BigDecimal(i1).multiply(price);
                    totalMoney = totalMoney.add(multiply);

                    placeholderTimeOrderDto.setPlaceholderOrderId(xhpcPlaceholderOrder.getPlaceholderOrderId());
                    placeholderTimeOrderDto.setPrice(price);
                    placeholderTimeOrderDto.setTotalMoney(multiply);
                    placeholderTimeOrderDto.setTotalTimeSpan(i1);
                    xhpcPlaceholderOrderMapper.insertXhpcPlaceholderTimeOrder(placeholderTimeOrderDto);
                }
            }
        }
        if(totalMoney.compareTo(new BigDecimal(50)) == 1){
            totalMoney = new BigDecimal(50);
        }
        xhpcPlaceholderOrder.setTotalMoney(totalMoney);
        if (new BigDecimal(0).compareTo(new BigDecimal(between)) == -1) {
            xhpcPlaceholderOrder.setOvertimePrice(totalMoney.divide(new BigDecimal(between), 2, BigDecimal.ROUND_HALF_UP));
        } else {
            xhpcPlaceholderOrder.setOvertimePrice(new BigDecimal("0.0"));
        }
        //xhpcPlaceholderOrder.setTotalMoney(totalMoneyYu);
        if(xhpcPlaceholderOrder.getSource() !=UserTypeUtil.INTERNET_TYPE){
            Long userId = xhpcPlaceholderOrder.getUserId();
            Integer source = xhpcPlaceholderOrder.getSource();
            String tenantId = xhpcPlaceholderOrder.getTenantId();
            BigDecimal totalMoney1 = xhpcPlaceholderOrder.getTotalMoney();
            if(0 == xhpcPlaceholderOrder.getSource()){
                xhpcPlaceholderOrder.setChargingMode("C端用户");
            }else if(2 == xhpcPlaceholderOrder.getSource()){
                xhpcPlaceholderOrder.setChargingMode("社区用户");
            }else{
                xhpcPlaceholderOrder.setChargingMode("B端用户");
            }
            if(totalMoney1.compareTo(new BigDecimal(0))>0){
                String key ="placeholderOrder:" +source+ tenantId+userId;
                int number =0;
                if(redisService.getCacheObject(key) !=null){
                    String numberYu = redisService.getCacheObject(key).toString();
                    int numbers =Integer.parseInt(numberYu);
                    numbers++;
                    number=numbers;
                    redisService.setCacheObject(key, numbers);
                }else{
                    redisService.setCacheObject(key, 1);
                    number=1;
                }
                R user = userTypeService.getUser(null, userId, source, null, tenantId);
                if(number>3){
                    if(user !=null && user.getData() !=null ){
                        Map<String, Object> userMessage = (Map<String, Object>)user.getData();
                        String phone =userMessage.get("phone").toString();
                        BigDecimal balance1 =new BigDecimal(userMessage.get("balance").toString());
                        BigDecimal subtract = balance1.subtract(totalMoney1);

                        int i = xhpcChargeOrderService.updateUserBalance(userId, subtract,source,tenantId);
                        if(i==0){
                            //扣钱失败
                            System.out.println("<<<<<<<<<<<<<<<<扣钱失败>>>>userId>>>>>>>>>>>>>"+userId+">>subtract>>>>>>>>"+subtract+">>balance1>>>>>>>>"+balance1+">>source>>>>>>>>"+source);
                        }else{
                            //添加流水
                            xhpcChargeOrderService.addUserAccountStatement(userId,totalMoney1, subtract, xhpcPlaceholderOrder.getPlaceholderOrderId(), 7, new Date(),source);
                        }
                        HashMap<String, String> paramMap = new HashMap<>();
                        paramMap.put("time", xhpcPlaceholderOrder.getTotalTimeSpan()+"");
                        paramMap.put("sumMoney",totalMoney1.toString());
                        paramMap.put("phone", phone);
                        paramMap.put("content", "尊敬的用户你好，你的爱车已停止充电"+xhpcPlaceholderOrder.getTotalTimeSpan()+"分钟，前30分钟免费，平台收取本次设备占用费："+totalMoney1+"元，占位费费用将从余额扣除，占位费明细请查询翔桦充电小程序我的历史订单，充电结束请及时挪车。");
                        smsService.sendNotice(paramMap);
                        //平台用户直接余额扣钱
                        xhpcPlaceholderOrder.setActPrice(xhpcPlaceholderOrder.getTotalMoney());
                        xhpcPlaceholderOrder.setActStatus(1);
                    }
                }else{
                    //发送短信
                    //平台用户直接余额扣钱
                    if(user !=null && user.getData() !=null ){
                        Map<String, Object> userMessage = (Map<String, Object>)user.getData();
                        String phone =userMessage.get("phone").toString();
                        HashMap<String, String> paramMap = new HashMap<>();
                        paramMap.put("time", xhpcPlaceholderOrder.getTotalTimeSpan()+"");
                        paramMap.put("sumMoney",totalMoney1.toString());
                        paramMap.put("number", number+"");
                        paramMap.put("phone", phone);
                        paramMap.put("content", "尊敬的用户，你的爱车已停止充电"+xhpcPlaceholderOrder.getTotalTimeSpan()+"分钟，前30分钟免费，平台收取本次设备占用费："+totalMoney1+"元，占位费费用将从余额扣除，占位费明细请查询翔桦充电小程序我的历史订单，试运行期间前三次免费，当前免费次数为"+number+"，充电结束请及时挪车。");
                        smsService.sendNotice(paramMap);

                    }
                    xhpcPlaceholderOrder.setActPrice(new BigDecimal(0));
                    xhpcPlaceholderOrder.setActStatus(1);
                }
            }else{
                xhpcPlaceholderOrder.setActPrice(new BigDecimal(0));
                xhpcPlaceholderOrder.setActStatus(1);
            }
        }
        xhpcPlaceholderOrderMapper.updateXhpcPlaceholderOrder(xhpcPlaceholderOrder);
        return AjaxResult.success();
    }

    @Override
    public List<Map<String, Object>> getSumSourceList(HttpServletRequest request, String tenantId, Long operatorId, Long chargingStationId, Long chargingPileId, Long terminalId, String startTime, String endTime, String sourceIds, String chargingStationIds) {
        List<String> sourceList =new ArrayList<>();
        if(!"".equals(sourceIds) && sourceIds !=null){
            sourceList= Arrays.asList(sourceIds.split(","));
        }
        List<String> chargingStationIdList =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationIdList= Arrays.asList(chargingStationIds.split(","));
        }
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();

        List<Map<String, Object>> list =new ArrayList<>();
        if(UserTypeUtil.SYS_USER_TYPE_FOUR.equals(sysUser.getUserType())){
            //运维管理人员
            startPage();
            list = xhpcPlaceholderOrderMapper.getSumSourceList(operatorId,chargingStationId,chargingPileId,terminalId,startTime, endTime, sourceList, chargingStationIdList,tenantId,sysUserId,2);
        }else{
            if(UserTypeUtil.SYS_SUPER_TYPE_ONE == loginUser.getSuperType()){
                list = xhpcPlaceholderOrderMapper.getSumSourceList(operatorId,chargingStationId,chargingPileId,terminalId,startTime, endTime, sourceList, chargingStationIdList,loginUser.getTenantId(),sysUserId,0);
            }else{
                if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
                    startPage();
                    if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                        Long logOperatorId = sysUser.getOperatorId();
                        //运营商看自己的场站
                        list = xhpcPlaceholderOrderMapper.getSumSourceList(operatorId,chargingStationId,chargingPileId,terminalId,startTime, endTime, sourceList,chargingStationIdList,tenantId,logOperatorId,1);
                    }else{
                        //查询赋值的场站
                        list = xhpcPlaceholderOrderMapper.getSumSourceList(operatorId,chargingStationId,chargingPileId,terminalId,startTime, endTime, sourceList, chargingStationIdList,tenantId,sysUserId,2);
                    }
                }else{
                    startPage();
                    list = xhpcPlaceholderOrderMapper.getSumSourceList(operatorId,chargingStationId,chargingPileId,terminalId,startTime, endTime, sourceList, chargingStationIdList,tenantId,sysUserId,0);
                }
            }
        }
        return list;
    }

    @Override
    public void getSumSourceListExport(HttpServletRequest request,HttpServletResponse response,String tenantId,Long operatorId,Long chargingStationId,Long chargingPileId,Long terminalId, String startTime, String endTime, String sourceIds, String chargingStationIds) throws IOException {

        List<Map<String, Object>> list = getSumSourceList(request,tenantId,operatorId,chargingStationId,chargingPileId,terminalId,startTime, endTime, sourceIds,chargingStationIds);

        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("kudaiDianMoney", "快电总费用");
        writer.addHeaderAlias("kudaiDianRefundMoney", "快电退款费用");
        writer.addHeaderAlias("kudaiDianRechargeMoney", "快电用户已支付费用");
        writer.addHeaderAlias("henDaMoney", "恒大总费用");
        writer.addHeaderAlias("xinDianTuMoney", "新电途总费用");
        writer.addHeaderAlias("refundMoney", "新电途退款费用");
        writer.addHeaderAlias("refundMoney", "新电途用户已支付费用");
        writer.addHeaderAlias("yunKuaiChongMoney", "云快充总费用");
        writer.addHeaderAlias("yunKuaiChongRefundMoney", "云快充退款费用");
        writer.addHeaderAlias("yunKuaiChongRechargeMoney", "云快充用户已支付费用");
        writer.addHeaderAlias("xiaoJuMoney", "小桔总费用");
        writer.addHeaderAlias("cheWeiJiaMoney", "车为家总费用");
        writer.addHeaderAlias("actPrice", "平台用户总费用");
        writer.addHeaderAlias("appealRechargeMoney", "平台用户退款总费用");
        writer.addHeaderAlias("appealRefundMoney", "平台用户支付总费用");
        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    @Override
    public List<Map<String, Object>> getDateIntervalPage(HttpServletRequest request,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId,String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) {

        List<String> chargingStationIdList =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationIdList= Arrays.asList(chargingStationIds.split(","));
        }
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        //获取登陆用户
        List<Map<String, Object>> list =new ArrayList<>();
        if(UserTypeUtil.SYS_USER_TYPE_FOUR.equals(sysUser.getUserType())){
            //运维管理人员
            startPage();
            list = xhpcPlaceholderOrderMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId);
        }else{
            if(UserTypeUtil.SYS_SUPER_TYPE_ONE == loginUser.getSuperType()){
                list =xhpcPlaceholderOrderMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 0,sysUserId,loginUser.getTenantId());
            }else{
                if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
                    startPage();
                    if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                        Long logOperatorId = sysUser.getOperatorId();
                        //运营商看自己的场站
                        list = xhpcPlaceholderOrderMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId, chargingStationIdList,internetUserId, operatorId, startTime, endTime, 1,logOperatorId,tenantId);
                    }else{
                        //查询赋值的场站
                        list = xhpcPlaceholderOrderMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 2,sysUserId,tenantId);
                    }
                }else{
                    startPage();
                    list =xhpcPlaceholderOrderMapper.getDateIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList, internetUserId, operatorId, startTime, endTime, 0,sysUserId,tenantId);
                }
            }
        }
        return list;
    }


    @Override
    public void dateIntervalExport(HttpServletRequest request,HttpServletResponse response,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId, String chargingStationIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) throws IOException {
        List<Map<String, Object>> list =new ArrayList<>();
        //获取登陆用户
        list = getDateIntervalPage(request,tenantId,chargingStationId,chargingPileId,terminalId,chargingStationIds,internetUserId,operatorId,startTime,endTime,userId,type);

        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("createTime", "日期");
        writer.addHeaderAlias("kudaiDianMoney", "快电总费用");
        writer.addHeaderAlias("kudaiDianRefundMoney", "快电退款费用");
        writer.addHeaderAlias("kudaiDianRechargeMoney", "快电用户已支付费用");
        writer.addHeaderAlias("henDaMoney", "恒大总费用");
        writer.addHeaderAlias("xinDianTuMoney", "新电途总费用");
        writer.addHeaderAlias("refundMoney", "新电途退款费用");
        writer.addHeaderAlias("refundMoney", "新电途用户已支付费用");
        writer.addHeaderAlias("ykcMoney", "云快充总费用");
        writer.addHeaderAlias("ykcRefundMoney", "云快充退款费用");
        writer.addHeaderAlias("ykcRechargeMoney", "云快充用户已支付费用");
        writer.addHeaderAlias("xiaoJuMoney", "小桔总费用");
        writer.addHeaderAlias("cheWeiJiaMoney", "车为家总费用");
        writer.addHeaderAlias("actPrice", "平台用户总费用");
        writer.addHeaderAlias("appealRechargeMoney", "平台用户退款总费用");
        writer.addHeaderAlias("appealRefundMoney", "平台用户支付总费用");
        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    @Override
    public List<Map<String, Object>> getStationIntervalPage(HttpServletRequest request,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId,String chargingStationIds, String operatorIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) {

        List<String> chargingStationIdList =new ArrayList<>();
        if(!"".equals(chargingStationIds) && chargingStationIds !=null){
            chargingStationIdList= Arrays.asList(chargingStationIds.split(","));
        }
        List<String> operatorUserId =new ArrayList<>();
        if(!"".equals(operatorIds) && operatorIds !=null){
            operatorUserId= Arrays.asList(operatorIds.split(","));
        }
        LoginUser loginUser = tokenService.getLoginUser(request);
        SysUser sysUser = loginUser.getSysUser();
        Long sysUserId = sysUser.getUserId();
        List<Map<String, Object>> list =new ArrayList<>();

        if(UserTypeUtil.SYS_USER_TYPE_FOUR.equals(sysUser.getUserType())){
            //运维管理人员
            startPage();
            list = xhpcPlaceholderOrderMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,2,sysUserId,tenantId);
        }else{
            if(UserTypeUtil.SYS_SUPER_TYPE_ONE == loginUser.getSuperType()){
                list =xhpcPlaceholderOrderMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,0,sysUserId,loginUser.getTenantId());
            }else{
                if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
                    startPage();
                    if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                        Long logOperatorId = sysUser.getOperatorId();
                        //运营商看自己的场站
                        list = xhpcPlaceholderOrderMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,1,logOperatorId,tenantId);
                    }else{
                        //查询赋值的场站
                        list = xhpcPlaceholderOrderMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,2,sysUserId,tenantId);
                    }
                }else{
                    startPage();
                    list =xhpcPlaceholderOrderMapper.getStationIntervalPage(chargingStationId,chargingPileId,terminalId,chargingStationIdList,operatorUserId,internetUserId,operatorId,startTime,endTime,0,sysUserId,tenantId);
                }
            }
        }
        return list;
    }
    @Override
    public void stationIntervalExport(HttpServletRequest request,HttpServletResponse response,String tenantId,Long chargingStationId,Long chargingPileId,Long terminalId, String chargingStationIds,String operatorIds, Long internetUserId, Long operatorId, String startTime, String endTime, Long userId, Integer type) throws IOException {
        List<Map<String, Object>> list =new ArrayList<>();

        list =getStationIntervalPage(request,tenantId,chargingStationId,chargingPileId,terminalId,chargingStationIds,operatorIds,internetUserId,operatorId,startTime,endTime,userId,type);

        // 通过工具类创建writer，默认创建xls格式
        BigExcelWriter writer = ExcelUtil.getBigWriter("temp_" + System.currentTimeMillis() + ".xlsx");
        writer.addHeaderAlias("chargingStationName", "电站名称");
        writer.addHeaderAlias("operatorName", "运营商名称");
        writer.addHeaderAlias("kudaiDianMoney", "快电总费用");
        writer.addHeaderAlias("kudaiDianRefundMoney", "快电退款费用");
        writer.addHeaderAlias("kudaiDianRechargeMoney", "快电用户已支付费用");
        writer.addHeaderAlias("henDaMoney", "恒大总费用");
        writer.addHeaderAlias("xinDianTuMoney", "新电途总费用");
        writer.addHeaderAlias("refundMoney", "新电途退款费用");
        writer.addHeaderAlias("refundMoney", "新电途用户已支付费用");
        writer.addHeaderAlias("henDaMoney", "恒大总费用");
        writer.addHeaderAlias("ykcMoney", "云快充总费用");
        writer.addHeaderAlias("ykcRefundMoney", "云快充退款费用");
        writer.addHeaderAlias("ykcRechargeMoney", "云快充用户已支付费用");
        writer.addHeaderAlias("xiaoJuMoney", "小桔总费用");
        writer.addHeaderAlias("cheWeiJiaMoney", "车为家总费用");
        writer.addHeaderAlias("actPrice", "平台用户总费用");
        writer.addHeaderAlias("appealRechargeMoney", "平台用户退款总费用");
        writer.addHeaderAlias("appealRefundMoney", "平台用户支付总费用");
        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(list, true);
        //out为OutputStream，需要写出到的目标流

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");

        ServletOutputStream out = response.getOutputStream();

        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
        //此处记得关闭输出Servlet流
        IoUtil.close(out);
    }

    @Override
    public List<Map<String, Object>> appealPlaceholderOrderList(HttpServletRequest request) {
        //获取登陆用户
        LoginUser loginUser = tokenService.getLoginUser(request);
        String tenantId = loginUser.getTenantId();
        return xhpcPlaceholderOrderMapper.appealPlaceholderOrderList(loginUser.getUserid(),loginUser.getUserType(),tenantId);

    }

    @Override
    public R applyPlaceholderOrder(HttpServletRequest request, Long placeholderOrderId, String appealUserResult) {

        XhpcPlaceholderOrder placeholderOrder = xhpcPlaceholderOrderMapper.getByPlaceholderOrderId(placeholderOrderId);

        if(placeholderOrder.getAppealStatus() !=0){
            return R.fail("该占位费订单已申述过");
        }
        try{
            Date createTime = placeholderOrder.getCreateTime();
            LocalDate targetDate  = createTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();

            LocalDate currentDate  = LocalDate.now();
            long days = ChronoUnit.DAYS.between(targetDate, currentDate);
            if(days>14){
                return R.fail("该占位费订单已超过14天不能申述，请联系客服");
            }
        }catch (Exception e){

        }

        XhpcPlaceholderOrder xhpcPlaceholderOrder = new XhpcPlaceholderOrder();
        xhpcPlaceholderOrder.setPlaceholderOrderId(placeholderOrderId);
        xhpcPlaceholderOrder.setAppealUserResult(appealUserResult);
        xhpcPlaceholderOrder.setAppealStatus(1);
        xhpcPlaceholderOrder.setAppealTime(new Date());
        int i = xhpcPlaceholderOrderMapper.updateXhpcPlaceholderOrder(xhpcPlaceholderOrder);
        if(i>0){
            return R.ok();
        }
        return R.fail("申述失败,请稍后再申述");
    }

    @Override
    public R applyPlaceholderOrderProcess(HttpServletRequest request, Long placeholderOrderId, BigDecimal actPrice,String refundType,String appealResult,String imgId) {
        XhpcPlaceholderOrder placeholderOrder = xhpcPlaceholderOrderMapper.getByPlaceholderOrderId(placeholderOrderId);

        XhpcPlaceholderOrder xhpcPlaceholderOrder = new XhpcPlaceholderOrder();
        xhpcPlaceholderOrder.setPlaceholderOrderId(placeholderOrderId);
        xhpcPlaceholderOrder.setAppealResult(appealResult);
        xhpcPlaceholderOrder.setAppealProcessTime(new Date());
        xhpcPlaceholderOrder.setImgId(imgId);
        BigDecimal subtractYu=new BigDecimal(0);
        if(!"reject".equals(refundType)){
            //申述成功用户进行充值金额
            xhpcPlaceholderOrder.setAppealStatus(2);
            xhpcPlaceholderOrder.setActStatus(2);
            if(placeholderOrder.getActPrice().compareTo(actPrice)==1){
                //剩余金额
                subtractYu = actPrice;
                xhpcPlaceholderOrder.setActPrice(placeholderOrder.getActPrice().subtract(actPrice));
            }else {
                subtractYu = placeholderOrder.getActPrice();
                xhpcPlaceholderOrder.setActPrice(new BigDecimal(0));
            }
            xhpcPlaceholderOrder.setAppealRefundMoney(actPrice);
            int i = xhpcPlaceholderOrderMapper.updateXhpcPlaceholderOrder(xhpcPlaceholderOrder);
            if(i>0){
                Long userId = placeholderOrder.getUserId();
                Integer source = placeholderOrder.getSource();
                String tenantId = placeholderOrder.getTenantId();
                //用户进行发送红包
                R user = userTypeService.getUser(null, userId, source, null, tenantId);

                if(user !=null && user.getData() !=null ){
                    Map<String, Object> userMessage = (Map<String, Object>)user.getData();
                    BigDecimal balance1 =new BigDecimal(userMessage.get("balance").toString());
                    BigDecimal subtract = balance1.add(subtractYu);
                    int y = xhpcChargeOrderService.updateUserBalance(userId, subtract, source,tenantId);
                    if(y==0){
                        //增加金额失败
                        System.out.println("<<<<<<<<<<<<<<<<增加金额失败>>>>placeholderOrder.getSernumber>>>>>>>>>>>>>"+placeholderOrder.getSerialNumber());

                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    }else{
                        //添加流水
                        xhpcChargeOrderService.addUserAccountStatement(userId,subtractYu, subtract, xhpcPlaceholderOrder.getPlaceholderOrderId(), 7, new Date(),source);
                    }
                }
                return R.ok();
            }
        }else{

            //申述失败
            xhpcPlaceholderOrder.setAppealStatus(3);
            int i = xhpcPlaceholderOrderMapper.updateXhpcPlaceholderOrder(xhpcPlaceholderOrder);
            if(i>0){
                return R.ok();
            }
        }
        return R.fail("申述失败,请稍后再申述");
    }

    @Override
    public R applyPlaceholderOrderInternetProcess(HttpServletRequest request, Long placeholderOrderId, BigDecimal actPrice,String refundType,String appealResult,String imgId) {
        XhpcPlaceholderOrder placeholderOrder = xhpcPlaceholderOrderMapper.getByPlaceholderOrderId(placeholderOrderId);

        XhpcPlaceholderOrder xhpcPlaceholderOrder = new XhpcPlaceholderOrder();
        xhpcPlaceholderOrder.setPlaceholderOrderId(placeholderOrderId);
        xhpcPlaceholderOrder.setAppealResult(appealResult);
        xhpcPlaceholderOrder.setAppealProcessTime(new Date());
        if(!"reject".equals(refundType)){
            //第三方处理退款
            xhpcPlaceholderOrder.setUserStatus(1);
            if(placeholderOrder.getTotalMoney().compareTo(actPrice)==1){
                xhpcPlaceholderOrder.setRechargeMoney(placeholderOrder.getTotalMoney().subtract(actPrice));
            }else {
                xhpcPlaceholderOrder.setRechargeMoney(new BigDecimal(0));
            }
            xhpcPlaceholderOrder.setRefundMoney(actPrice);
            xhpcPlaceholderOrder.setImgId(imgId);
            int i = xhpcPlaceholderOrderMapper.updateXhpcPlaceholderOrder(xhpcPlaceholderOrder);
            if(i>0){
                return R.ok();
            }
        }else{
            return R.ok();
        }
        return R.fail("申述失败,请稍后再申述");
    }

    @Override
    public List<Map<String, Object>> appealPlaceholderOrderListPC(HttpServletRequest request, String tenantId, Long operatorId, String name, Integer type, String serialNumber, Long chargingStationId) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        //获取登陆用户
        SysUser sysUser = loginUser.getSysUser();
        Long userId = sysUser.getUserId();
        //桩的统计、该时段金额
        List<Map<String, Object>> list =new ArrayList<>();

        if(UserTypeUtil.SYS_USER_TYPE_FOUR.equals(sysUser.getUserType())){
            //运维管理人员
            startPage();
            return xhpcPlaceholderOrderMapper.appealPlaceholderOrderListPC(operatorId,name, type, serialNumber, chargingStationId,userId,2,tenantId);
        }else{
            startPage();
            if(UserTypeUtil.SYS_SUPER_TYPE_ONE == loginUser.getSuperType()){
                list= xhpcPlaceholderOrderMapper.appealPlaceholderOrderListPC(operatorId,name, type, serialNumber, chargingStationId,userId,0,loginUser.getTenantId());
            }else{
                if(!UserTypeUtil.SYS_USER_TYPE_ADMIN.equals(sysUser.getUserId())){
                    if(UserTypeUtil.SYS_USER_TYPE_ONE.equals(sysUser.getUserType())){
                        Long logOperatorId = sysUser.getOperatorId();
                        //运营商看自己的场站
                        list= xhpcPlaceholderOrderMapper.appealPlaceholderOrderListPC(operatorId,name, type, serialNumber, chargingStationId,logOperatorId,1,tenantId);

                    }else{
                        //查询赋值的场站
                        list= xhpcPlaceholderOrderMapper.appealPlaceholderOrderListPC(operatorId,name, type, serialNumber, chargingStationId,userId,2,tenantId);

                    }
                }else{
                        list= xhpcPlaceholderOrderMapper.appealPlaceholderOrderListPC(operatorId,name, type, serialNumber, chargingStationId,userId,0,tenantId);
                }
            }
        }

        return list;
    }

    @Override
    public R fileUpload(HttpServletRequest request, HttpServletResponse response, String tenantId, MultipartFile file, int type, String startTime, String endTime) throws Exception {
        // 创建OSSClient实例
        List<PositionFeeDto> positionFees = new ArrayList<>();
        try (InputStream inputStream = file.getInputStream();
             Workbook workbook = new XSSFWorkbook(inputStream)) {

            Sheet sheet = workbook.getSheetAt(0); // 获取第一个sheet

            // 跳过表头，从第二行开始读取
            for (int i = 2; i <= sheet.getLastRowNum()-1; i++) {
                Row row = sheet.getRow(i);
                if (row == null) continue;

                // 读取订单ID（A列）
                Cell orderIdCell = row.getCell(0);
                // 读取金额（B列）
                Cell feeCell = row.getCell(2);

                if (orderIdCell != null && feeCell != null) {
                    String orderId = getCellStringValue(orderIdCell);
                    BigDecimal feeAmount = getCellNumericValue(feeCell);

                    if (orderId != null && !orderId.trim().isEmpty() && feeAmount != null) {
                        positionFees.add(new PositionFeeDto(orderId.trim(), feeAmount));
                    }
                }
            }
        }

        if(positionFees.size()>0){
            List<PositionFeeDto> placeholderOrderExcelList =new ArrayList<>();
            if(type==0){
                placeholderOrderExcelList = xhpcPlaceholderOrderMapper.getPlaceholderOrderUserExcelList(positionFees);
            }else{
               placeholderOrderExcelList = xhpcPlaceholderOrderMapper.getPlaceholderOrderExcelList(positionFees, Long.valueOf(type));
            }

            List<FeeDifferenceDto> feeDifferenceDtos = compareWithDatabase(positionFees, placeholderOrderExcelList,type);
            if (feeDifferenceDtos.isEmpty()) {
                return R.ok(new ArrayList<>(),"对账完成，无差异");
            } else {
                return R.ok(feeDifferenceDtos, "发现" + feeDifferenceDtos.size() + "条差异记录");
            }
        }else{
            return R.fail("Excel 为空");
        }
    }

    @Override
    public List<Map<String, Object>> deviceMessageList(String chargeOrderNo) {

        startPage();
        return xhpcPlaceholderOrderMapper.deviceMessageList(chargeOrderNo,chargeOrderNo.substring(0,16));
    }


    public List<FeeDifferenceDto> compareWithDatabase(List<PositionFeeDto> excelData,
                                                      List<PositionFeeDto> dbData,Integer type) {
        List<FeeDifferenceDto> differences = new ArrayList<>();

        // 将数据库数据转换为Map便于查找
        Map<String, BigDecimal> dbFeeMap = new HashMap<>();
        for (PositionFeeDto dbFee : dbData) {
            dbFeeMap.put(dbFee.getOrderId(), dbFee.getFeeAmount());
        }
        Map<String, Integer> dbFeeMapStatus = new HashMap<>();
        for (PositionFeeDto dbFee : dbData) {
            dbFeeMapStatus.put(dbFee.getOrderId(), dbFee.getUserStatus());
        }


        // 比较每个Excel中的数据
        for (PositionFeeDto excelFee : excelData) {
            String orderId = excelFee.getOrderId();
            BigDecimal excelAmount = excelFee.getFeeAmount();
            BigDecimal dbAmount = dbFeeMap.get(orderId);

            if (dbAmount == null) {
                // 数据库中没有该订单
                differences.add(new FeeDifferenceDto(orderId, excelAmount, null, "数据库中没有该订单"));
            } else if (excelAmount.compareTo(dbAmount) != 0) {
                // 金额不一致
                differences.add(new FeeDifferenceDto(orderId, excelAmount, dbAmount,
                        "金额不一致: Excel=" + excelAmount + ", DB=" + dbAmount));
            }else if(excelAmount.compareTo(dbAmount) == 0){
                //是否已对账
                if(dbFeeMapStatus.get(orderId)!=null){
                    Integer userStatus = dbFeeMapStatus.get(orderId);
                    if(userStatus==0){
                        if(type==0){
                            xhpcPlaceholderOrderMapper.updatePlaceholderOrderRechargeUserMoney(excelAmount,orderId);
                        }else{
                            xhpcPlaceholderOrderMapper.updatePlaceholderOrderRechargeMoney(excelAmount,orderId);
                        }
                    }else if(userStatus==1){
                        differences.add(new FeeDifferenceDto(orderId, excelAmount, null,"该订单已退款=" + excelFee.getRefundMoney() ));
                    }else{
                        differences.add(new FeeDifferenceDto(orderId, excelAmount, null,"该订单已对账"));
                    }
                }else{
                    differences.add(new FeeDifferenceDto(orderId, excelAmount, null,"该订单需要技术人员进行核对"));
                }

            }
        }

        return differences;
    }

    /**
     * 获取单元格字符串值
     */
    private String getCellStringValue(Cell cell) {
        if (cell == null) return null;

        return cell.getStringCellValue();
    }

    /**
     * 获取单元格数值
     */
    private BigDecimal getCellNumericValue(Cell cell) {
        if (cell == null) return null;

        try {
            return BigDecimal.valueOf(cell.getNumericCellValue());
        } catch (Exception e) {
            return null;
        }
    }
    public static void main(String[] args) {
        System.out.println("MA25CNM38251212065315246240".substring(0,9));



    }
}
