package com.xhpc.order.service.impl;

import cn.hutool.core.date.DateUtil;
import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.data.down.StartChargingData;
import com.xhpc.common.data.redis.CacheRealtimeData;
import com.xhpc.common.data.redis.StaticBeanUtil;
import com.xhpc.common.domain.XhpcRate;
import com.xhpc.common.domain.XhpcTerminal;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.order.domain.XhpcChargeOrder;
import com.xhpc.order.domain.XhpcOrderRedisRecord;
import com.xhpc.order.mapper.XhpcChargeOrderMapper;
import com.xhpc.order.mapper.XhpcInternetUserMapper;
import com.xhpc.order.service.IXhpcChargeOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yuyang
 * @date 2021/8/4 9:54
 */
@Service
public class XhpcChargeOrderServiceImpl implements IXhpcChargeOrderService {

    public static RedisService REDIS;

    @Autowired
    private XhpcChargeOrderMapper xhpcChargeOrderMapper;
    @Autowired
    private PowerPileService powerPileService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private XhpcInternetUserMapper xhpcInternetUserMapper;

    private static final Logger logger = LoggerFactory.getLogger(XhpcChargeOrderServiceImpl.class);
    //队列名称
    private final static String NAME = "webSocket";

    @PostConstruct
    public  void init(){
        REDIS =redisService;
    }

    @Override
    public AjaxResult getHistotyChargeOrderMessage(Long userId) {

        int count = xhpcChargeOrderMapper.getHistotyChargeOrderMessage(userId);
        if(count>0){
          return  AjaxResult.success();
        }
        return AjaxResult.error(1201,"无实时数据");
        //return AjaxResult.success(xhpcChargeOrderMapper.getMessage(userId));
    }

    @Override
    public Map<String, Object> getOrderMessage(Long userId) {
        return xhpcChargeOrderMapper.getMessage(userId);
    }

    @Override
    public List<Map<String, Object>> getHistotyChargeOrderStatusList(Long userId) {

        return xhpcChargeOrderMapper.getHistotyChargeOrderStatusList(userId);
    }

    @Override
    @Transactional
    public AjaxResult startUp(Long userId, String serialNumber,Integer type) {
        String pattern = "^([0-9]{16})";
        Pattern compile = Pattern.compile(pattern);
        Matcher m = compile.matcher(serialNumber);
        if (serialNumber.length()!=16 || !m.matches()) {
            return AjaxResult.error(1104, "无效的终端编号");
        }
        //查看充电用户金额是否大于5元
        Map<String, Object> userMessage = xhpcChargeOrderMapper.getUserMessage(userId);
        BigDecimal a = new BigDecimal(5);
        if (userMessage == null || userMessage.get("balance") == null || a.compareTo(new BigDecimal(userMessage.get("balance").toString())) == 1) {
            return AjaxResult.error(1100, "金额小于5元,不能充电，请充值后再进行充电");
        }
        //查看充电用户是否有申请退款的订单，还未处理
        if (Integer.parseInt(userMessage.get("isRefundApplication").toString()) != 0) {
            return AjaxResult.error(1101, "你有申请退款订单在审核中,需要充电请取消申请退款");
        }
        //充电用户是否在充电中
        String i = xhpcChargeOrderMapper.countXhpcRealTimeOrder(userId);

        if (!"".equals(i) && i!=null) {
            return AjaxResult.error(1102, "车辆正在充电,请查询车辆充电信息");
        }
        //充电用户是否存在异常的订单
        int j = xhpcChargeOrderMapper.countXhpcChargeOrder(userId);
        if (j > 0) {
            return AjaxResult.error(1103, "你有异常订单未解决，请拨打客服电话进行解决");
        }
        //终端状态是否空闲
        //是否插枪
        Map<String, Object> cacheMap = REDIS.getCacheMap("gun:" + serialNumber);
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<cacheMap>>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<cacheMap>>>>>>>>>>>>>>>>>"+serialNumber);
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<cacheMap>>>>>>>>>>>>>>>>>"+cacheMap.toString());
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<cacheMap>>>>>>>>>>>>>>>>>");

        if(cacheMap==null){
            return AjaxResult.error(1105, "未注册的终端,请选择其他终端充电");
        }else{
            if(cacheMap.get("status") ==null){
                return AjaxResult.error(1106, "未知的终端状态,请选择其他终端充电");
            }else{
                String status = cacheMap.get("status").toString();
                //不同的状态
                if("离线".equals(status)||"故障".equals(status) ||"充电".equals(status)){
                    return AjaxResult.error(1107, "此终端"+status+"中,请选择其他终端充电");
                }
            }
            if(cacheMap.get("vehicleGunStatus") ==null){
                return AjaxResult.error(1108, "未知的枪状态,请选择其他终端充电");
            }else{
                String vehicleGunStatus = cacheMap.get("vehicleGunStatus").toString();
                if(!"是".equals(vehicleGunStatus)){
                    return AjaxResult.error(1109, "请插好充电枪");
                }
            }
        }

        //终端信息
        XhpcTerminal xhpcTerminal = xhpcChargeOrderMapper.getXhpcTerminalSerialNumber(serialNumber);
        if (xhpcTerminal == null || xhpcTerminal.getTerminalId() == null || xhpcTerminal.getChargingPileId() == null || xhpcTerminal.getPileSerialNumber() == null) {
            return AjaxResult.error(1104, "无效的终端编号");
        }
        //余额
        String balance = new BigDecimal(userMessage.get("balance").toString()).multiply(new BigDecimal(100)).toString();


        //获取桩信息
        Map<String, Object> xhpcChargingPileById =
                xhpcChargeOrderMapper.getXhpcChargingPileById(xhpcTerminal.getChargingPileId());

        //启动充电
        StartChargingData startChargingData = new StartChargingData();
        //订单流水号 终端号+年月日时分秒+自增4位 共32位
        Date date = Calendar.getInstance().getTime();
        String format = DateUtil.format(date, "yyMMddHHmmss");
        //自增
        String orderNo = serialNumber + format + StaticBeanUtil.seqDec("gun:" + serialNumber + ".seqdec");

        startChargingData.setOrderNo(orderNo);
        if(userMessage.get("phone") !=null){
            startChargingData.setTel(userMessage.get("phone").toString());
        }
        startChargingData.setPileNo(xhpcTerminal.getPileSerialNumber());
        startChargingData.setGunId(xhpcTerminal.getSerialNumber().substring(14));
        startChargingData.setBalance(Double.valueOf(balance).intValue());
        if(xhpcChargingPileById.get("communicationProtocolVersion")!=null && !"".equals(xhpcChargingPileById.get("communicationProtocolVersion").toString())){
            startChargingData.setVersion(xhpcChargingPileById.get("communicationProtocolVersion").toString());
        }else{
            startChargingData.setVersion("0A");
        }

        //用户、桩、平台（最小的）
        int number =0;
        if(!"".equals(userMessage.get("soc")) && userMessage.get("soc") !=null && !"".equals(userMessage.get("soc"))){
            number =Integer.parseInt(userMessage.get("soc").toString());
        }
        //平台
        String soc = redisService.getCacheObject("global:SOC");
        if(!"".equals(soc) && soc!=null){
            if(number!=0){
                if(Integer.parseInt(soc)-number<0){
                    number=Integer.parseInt(soc);
                }
            }else{
                number=Integer.parseInt(soc);
            }
        }
        Map<String, Object> operatorMessage = xhpcChargeOrderMapper.getOperatorMessage(xhpcTerminal.getChargingStationId());
        if(operatorMessage !=null && operatorMessage.get("soc") !=null && !"".equals(operatorMessage.get("soc"))){
            if(number!=0){
                if(Integer.parseInt(operatorMessage.get("soc").toString())-number<0){
                    number=Integer.parseInt(operatorMessage.get("soc").toString());
                }
            }else{
                number=Integer.parseInt(operatorMessage.get("soc").toString());
            }

        }
        if(number !=0){
            startChargingData.setSoc(number);
        }
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<number>>>>>>>>>>>>>>>>>:"+number);
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<Soc>>>>>>>>>>>>>>>>>:"+startChargingData.getSoc());

        R r1 = powerPileService.startCharging(startChargingData);
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<充电返回>>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getCode() + ">>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getMsg() + ">>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getData() + ">>>>>>>>>>>>>>>>>");
        if (r1.getCode() != 200) {
            return AjaxResult.error(r1.getMsg());
        }


        //创建充电订单(充电启动soc初始值，结束是获取,并修改状态)
        XhpcChargeOrder xhpcChargeOrder = new XhpcChargeOrder();
        xhpcChargeOrder.setChargingStationId(xhpcTerminal.getChargingStationId());
        xhpcChargeOrder.setUserId(userId);
        xhpcChargeOrder.setTerminalId(xhpcTerminal.getTerminalId());
        xhpcChargeOrder.setGunId(xhpcTerminal.getSerialNumber());
        xhpcChargeOrder.setSerialNumber(orderNo);
        xhpcChargeOrder.setSource(0);
        xhpcChargeOrder.setStatus(-1);
        if (xhpcChargingPileById != null && xhpcChargingPileById.get("power") != null) {
            xhpcChargeOrder.setPower(xhpcChargingPileById.get("power").toString());
        }
        xhpcChargeOrder.setRateModelId(Long.valueOf(r1.getData().toString()));
        if (type == 1) {
            xhpcChargeOrder.setChargingMode("微信");
        } else {
            xhpcChargeOrder.setChargingMode("支付宝");
        }
        xhpcChargeOrder.setCreateTime(Calendar.getInstance().getTime());
        xhpcChargeOrder.setType(40);
        xhpcChargeOrderMapper.addXhpcChargeOrder(xhpcChargeOrder);

        return AjaxResult.success();
    }

    @Override
    public AjaxResult stopUp(Long userId, String serialNumber,Long chargingOrderId) {

        String pattern = "^([0-9]{16})";
        Pattern compile = Pattern.compile(pattern);
        Matcher m = compile.matcher(serialNumber);
        if (serialNumber.length()!=16 || !m.matches()) {
            return AjaxResult.error(1104, "无效的终端编号");
        }
        //终端信息
        XhpcTerminal xhpcTerminal = xhpcChargeOrderMapper.getXhpcTerminalSerialNumber(serialNumber);
        if (xhpcTerminal == null || xhpcTerminal.getTerminalId() == null || xhpcTerminal.getChargingPileId() == null || xhpcTerminal.getPileSerialNumber() == null) {
            return AjaxResult.error(1104, "无效的终端编号");
        }

        //获取桩信息
        Map<String, Object> xhpcChargingPileById =
                xhpcChargeOrderMapper.getXhpcChargingPileById(xhpcTerminal.getChargingPileId());
        String version ="0A";
        if(xhpcChargingPileById.get("communicationProtocolVersion")!=null && !"".equals(xhpcChargingPileById.get("communicationProtocolVersion").toString())){
            version=xhpcChargingPileById.get("communicationProtocolVersion").toString();
        }
        //获取充电订单号
        XhpcChargeOrder order = xhpcChargeOrderMapper.getChargingOrderId(chargingOrderId);
        if(order ==null){
            return AjaxResult.error(1104, "无效的终端编号");
        }else{
            if(order.getSource()==1){
                return AjaxResult.error(1104, "请在启动方停止充电");
            }
            if(!order.getUserId().equals(userId)){
                return AjaxResult.error(1104, "请登陆相应账号停止充电");
            }
        }

        R oa = powerPileService.stopCharging(order.getSerialNumber(), xhpcTerminal.getPileSerialNumber(),
                xhpcTerminal.getSerialNumber(), version);
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<停止充电返回>>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<"+oa.getCode()+">>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<"+oa.getMsg()+">>>>>>>>>>>>>>>>>");
        if(oa.getCode() !=200){
            return AjaxResult.error(oa.getMsg());
        }
        return AjaxResult.success();
    }

    @Override
    public XhpcChargeOrder getSerialNumberMessage(String serialNumber) {

        return xhpcChargeOrderMapper.getSerialNumberMessage(serialNumber);
    }

    @Override
    public int getCount(Long userId,Long chargeOrderId) {

        return xhpcChargeOrderMapper.getCount(userId,chargeOrderId);
    }

    @Override
    public Map<String, Object> getPromotion() {

        return xhpcChargeOrderMapper.getPromotion();
    }

    @Override
    public void updateXhpcChargeOrder(XhpcChargeOrder xhpcChargeOrder) {

        xhpcChargeOrderMapper.updateXhpcChargeOrder(xhpcChargeOrder);
    }

    @Override
    public Map<String, Object> getUserMessage(Long userId){

        return xhpcChargeOrderMapper.getUserMessage(userId);
    }

    @Override
    public int updateUserBalance(Long userId, BigDecimal balance) {

        return xhpcChargeOrderMapper.updateUserBalance(userId, balance);
    }

    @Override
    public Map<String, Object> getOperatorMessage(Long chargingStationId) {

        return xhpcChargeOrderMapper.getOperatorMessage(chargingStationId);
    }

    @Override
    public void addXhpcOrderRedisRecord(XhpcOrderRedisRecord xhpcOrderRedisRecord) {

        xhpcChargeOrderMapper.addXhpcOrderRedisRecord(xhpcOrderRedisRecord);
    }

    @Override
    public int addUserAccountStatement(Long userId, BigDecimal amount, BigDecimal remainingSum, Long chargeOrderId,
                                       Integer type, Date date) {

        return xhpcChargeOrderMapper.addUserAccountStatement(userId, amount, remainingSum, chargeOrderId, type, date);
    }

    @Override
    public XhpcChargeOrder getChargingOrderId(Long chargingOrderId) {

        return xhpcChargeOrderMapper.getChargingOrderId(chargingOrderId);
    }

    @Override
    public List<XhpcRate> getRateModelId(Long rateModelId) {
        return xhpcChargeOrderMapper.getRateModelId(rateModelId);
    }

    @Override
    public Map<String, Object> getXhpcChargingPile(Long terminalId) {
        return xhpcChargeOrderMapper.getXhpcChargingPile(terminalId);
    }

    @Override
    public List<XhpcChargeOrder> getXhpcChargeOrderStatus(Integer status, Integer source) {
        return xhpcChargeOrderMapper.getXhpcChargeOrderStatus(status, source);
    }



    @Override
    @Transactional
    public R startUpBy3rd(String internetSerialNumber, String driverId, Integer chargingAmt, String plateNum, Integer status, String connectorId) {

        R r = new R();
//        String day = internetSerialNumber.substring(9, internetSerialNumber.length() - 4);
//        if (!isValidDate(day)) {
//            r.setCode(500);
//            return r;
//        }

        int resTime = xhpcInternetUserMapper.selectByOperatorIdEvcs(internetSerialNumber.substring(0, 9));
        if (resTime == 0) {
            r.setCode(500);
            r.setMsg("运营商无权限对此桩进行操作");
            return r;
        }

        StartChargingData startChargingData = new StartChargingData();
        //500000 equals 500 yuan
        startChargingData.setBalance(chargingAmt * 1000);

        startChargingData.setGunId(connectorId);

        startChargingData.setTel(driverId);

        startChargingData.setPileNo(connectorId.substring(0, connectorId.length() - 3));

        //终端状态是否空闲
        //是否插枪
        Map<String, Object> cacheMap = REDIS.getCacheMap("gun:" + connectorId);
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<cacheMap>>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<cacheMap>>>>>>>>>>>>>>>>>" + connectorId);
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<cacheMap>>>>>>>>>>>>>>>>>" + cacheMap.toString());
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<cacheMap>>>>>>>>>>>>>>>>>");

        if (cacheMap == null) {
            r.setCode(500);
            r.setMsg("未注册的终端,请选择其他终端充电");
            return r;
        } else {
            if (cacheMap.get("status") == null) {
                r.setCode(500);
                r.setMsg("未知的终端状态");
                return r;
            } else {
                String statusCache = cacheMap.get("status").toString();
                //不同的状态
                if ("离线".equals(statusCache) || "故障".equals(statusCache) || "充电".equals(statusCache)) {
                    if ("离线".equals(statusCache)) {
                        r.setCode(2);
                    } else {
                        r.setCode(500);
                    }
                    r.setMsg("此终端" + statusCache + "中,请选择其他终端充电");
                    return r;
                }
            }
            if (cacheMap.get("vehicleGunStatus") == null) {
                r.setCode(500);
                r.setMsg("未知的枪状态");
                return r;
            } else {
                String vehicleGunStatus = cacheMap.get("vehicleGunStatus").toString();
                if (!"是".equals(vehicleGunStatus)) {
                    r.setCode(500);
                    r.setMsg("未插枪");
                    return r;
                }
            }
        }

        //终端信息
        XhpcTerminal xhpcTerminal = xhpcChargeOrderMapper.getXhpcTerminalSerialNumber(connectorId);
        if (xhpcTerminal == null || xhpcTerminal.getTerminalId() == null || xhpcTerminal.getChargingPileId() == null || xhpcTerminal.getPileSerialNumber() == null) {
            r.setCode(1);
            r.setMsg("无效的终端编号");
            return r;
        }

        //获取桩信息
        Map<String, Object> xhpcChargingPileById =
                xhpcChargeOrderMapper.getXhpcChargingPileById(xhpcTerminal.getChargingPileId());

        //启动充电
        //订单流水号 终端号+年月日时分秒+自增4位 共32位
        Date date = Calendar.getInstance().getTime();
        String format = DateUtil.format(date, "yyMMddHHmmss");
        //自增
        String orderNo = connectorId + format + StaticBeanUtil.seqDec("gun:" + connectorId + ".seqdec");

        startChargingData.setOrderNo(orderNo);
        startChargingData.setPileNo(xhpcTerminal.getPileSerialNumber());
        startChargingData.setGunId(xhpcTerminal.getSerialNumber().substring(14));
        if (xhpcChargingPileById.get("communicationProtocolVersion") != null && !"".equals(xhpcChargingPileById.get("communicationProtocolVersion").toString())) {
            startChargingData.setVersion(xhpcChargingPileById.get("communicationProtocolVersion").toString());
        } else {
            startChargingData.setVersion("0A");
        }


        //平台
        String soc = redisService.getCacheObject("global:SOC");


        startChargingData.setSoc(Integer.parseInt(soc));
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<number>>>>>>>>>>>>>>>>>:" + soc);
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<Soc>>>>>>>>>>>>>>>>>:" + startChargingData.getSoc());

        R r1 = powerPileService.startCharging(startChargingData);
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<第三方充电返回>>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getCode() + ">>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getMsg() + ">>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<" + r1.getData() + ">>>>>>>>>>>>>>>>>");
        logger.info("<<<<<<<<<<<<<<<<<<<<<<<<第三方充电返回>>>>>>>>>>>>>>>>>");
        if (r1.getCode() != 200) {
            r.setCode(500);
            r.setMsg(r1.getMsg());
            return r;
        }
        String startTime = DateUtil.format(date, "yyyy-MM-dd HH: mm: ss");
        Map<String, Object> extraData1 = xhpcChargeOrderMapper.selectDate3rdNeedBy(connectorId).get(0);
        Long rateModelId = Long.valueOf(r1.getData().toString());
        Long terminalId = (Long) extraData1.get("terminalId");
        Double power = (Double) extraData1.get("power");
        Long chargingStationId = (Long) extraData1.get("chargingStationId");
        Map<String, Object> extraData2 = xhpcChargeOrderMapper.select3rdNameBy(internetSerialNumber.substring(0, 9)).get(0);
        String chargingMode = (String) extraData2.get("name");
        Long userId = (Long) extraData2.get("userId");
        int res = xhpcChargeOrderMapper.addBy3rd(internetSerialNumber, orderNo, driverId, chargingAmt, plateNum, status, date, rateModelId, terminalId, power, chargingStationId, chargingMode, 1, userId);
        Map<String, Object> data = new HashMap<>();
        System.out.println(res);
        if (res != 0) {
            r.setCode(200);
            data.put("orderNo", orderNo);
            data.put("startTime", startTime);
            r.setMsg("成功");
            r.setData(data);
        } else {
            r.setCode(500);
            r.setMsg("内部服务调用错误");
        }
        return r;

    }

    @Override
    public void updateStatus() {
        //标记异常大于创建4小时，标记为异常 且没有实时数据的订单
        //查询是否有实时数据，没有实时数据查看该桩是否有成功的数据，有成功的数据表示该订单是无效的
        List<XhpcChargeOrder> list = xhpcChargeOrderMapper.getFourTimsStatus();
        if(list !=null && list.size()>0){
            for (XhpcChargeOrder xhpcChargeOrder:list) {
                CacheRealtimeData cacheRealtimeData = redisService.getCacheObject("order:" + xhpcChargeOrder.getSerialNumber() + ".lord");
                Long chargeOrderId = xhpcChargeOrder.getChargeOrderId();
                if(cacheRealtimeData ==null){
                    int count = xhpcChargeOrderMapper.getSerialNumberLike(xhpcChargeOrder.getSerialNumber().substring(0, 16), chargeOrderId);
                    if(count>0){
                        //有充电改为 由充电中变为启动充电中
                        xhpcChargeOrderMapper.updateStatus(-1,chargeOrderId);
                    }else{
                        //标记异常
                        xhpcChargeOrderMapper.updateStatus(2,chargeOrderId);
                    }
                }else{
                    xhpcChargeOrderMapper.updateStatus(2,chargeOrderId);
                }
            }
        }
    }

    @Override
    public Map<String, Object> getXhpcChargingPileById(Long chargingPileId) {
        return xhpcChargeOrderMapper.getXhpcChargingPileById(chargingPileId);
    }

    public static boolean isValidDate(String str) {
        try {
            if (0 != str.length()) {
                if (str.length() == 14) {
                    // 闰年标志
                    boolean isLeapYear = false;
                    String year = str.substring(0, 4);
                    String month = str.substring(4, 6);
                    String day = str.substring(6, 8);
                    String hour = str.substring(8, 10);
                    String minute = str.substring(10, 12);
                    String second = str.substring(12, 14);
                    int intYear = Integer.parseInt(year);
                    // 判断年份是否合法
                    if (intYear < 1900 || intYear > 2200) {
                        return false;
                    }
                    // 判断是否为闰年
                    if (intYear % 4 == 0 && intYear % 100 != 0 || intYear % 400 == 0) {
                        isLeapYear = true;
                    }
                    // 判断月份
                    // 1.判断月份
                    if (month.startsWith("0")) {
                        String unitMonth = month.substring(1, 2);
                        int intUnitMonth = Integer.parseInt(unitMonth);
                        if (intUnitMonth == 0) {
                            return false;
                        }
                        if (intUnitMonth == 2) {
                            // 获取2月的天数
                            int intDay4February = Integer.parseInt(day);
                            if (isLeapYear) {
                                if (intDay4February > 29) {
                                    return false;
                                }
                            } else {
                                if (intDay4February > 28) {
                                    return false;
                                }
                            }
                        }
                    } else {
                        // 2.判断非0打头的月份是否合法
                        int intMonth = Integer.parseInt(month);
                        if (intMonth != 10 && intMonth != 11 && intMonth != 12) {
                            return false;
                        }
                    }
                    // 判断日期
                    // 1.判断日期
                    if (day.startsWith("0")) {
                        String unit4Day = day.substring(1, 2);
                        int intUnit4Day = Integer.parseInt(unit4Day);
                        if (intUnit4Day == 0) {
                            return false;
                        }
                    } else {
                        // 2.判断非0打头的日期是否合法
                        int intDay = Integer.parseInt(day);
                        if (intDay < 10 || intDay > 31) {
                            return false;
                        }
                    }
                    // 判断时间
                    int intHour = Integer.parseInt(hour);
                    int intMinute = Integer.parseInt(minute);
                    int intSecond = Integer.parseInt(second);
                    return intHour >= 0 && intHour <= 23 && intMinute >= 0 && intMinute <= 59 && intSecond >= 0 && intSecond <= 59;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
