package com.xhpc.order.service.impl;

import cn.hutool.core.date.DateUtil;
import com.xhpc.common.api.PowerPileService;
import com.xhpc.common.api.WorkOrderYuService;
import com.xhpc.common.core.domain.R;
import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.core.web.service.BaseService;
import com.xhpc.common.redis.service.RedisService;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.order.domain.XhpcPileRegularInspect;
import com.xhpc.order.mapper.XhpcPileRegularInspectMapper;
import com.xhpc.order.service.IXhpcPileRegularInspectService;
import com.xhpc.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class XhpcPileRegularInspectServiceImpl extends BaseService implements IXhpcPileRegularInspectService {

    @Resource
    private TokenService tokenService;
    @Resource
    private RedisService redisService;
    @Resource
    private WorkOrderYuService workOrderService;
    @Resource
    private PowerPileService powerPileService;

    @Resource
    private XhpcPileRegularInspectMapper xhpcPileRegularInspectMapper;

    @Override
    public AjaxResult getPileRegularInspectTenantId() {
        try {
            LoginUser loginUser = tokenService.getLoginUser();
            String tenantId =loginUser.getTenantId();
            if(tenantId==null || "".equals(tenantId)){
                return AjaxResult.error(500, "请重新登录");
            }
            Map<String, Object> map = xhpcPileRegularInspectMapper.getPileRegularInspectTenantId(tenantId);
            return AjaxResult.success(map);
        }catch (Exception e){
            return AjaxResult.error(500, "请重新登录!");
        }
    }

    @Override
    public AjaxResult addPileRegularInspect(HttpServletRequest request, XhpcPileRegularInspect xhpcPileRegularInspect) {

        LoginUser loginUser = tokenService.getLoginUser(request);
        String tenantId =loginUser.getTenantId();
        if(tenantId==null || "".equals(tenantId)){
            return AjaxResult.error(500, "请重新登录");
        }

//        if(xhpcPileRegularInspect.getVersionNumberTime()==null || "".equals(xhpcPileRegularInspect.getVersionNumberTime())){
//            return AjaxResult.error(500, "版本号检查时间必填");
//        }
        if(xhpcPileRegularInspect.getTimingPriceTime()==null || "".equals(xhpcPileRegularInspect.getTimingPriceTime())){
            return AjaxResult.error(500, "校时校价时间必填");
        }
//        if(xhpcPileRegularInspect.getRateTime()==null || "".equals(xhpcPileRegularInspect.getRateTime())){
//            return AjaxResult.error(500, "费率时间必填");
//        }
//        if(xhpcPileRegularInspect.getCopyingAdministratorTime()==null || "".equals(xhpcPileRegularInspect.getCopyingAdministratorTime())){
//            return AjaxResult.error(500, "定期抄发给管理员时间必填");
//        }
//        try{
//            String[] split = xhpcPileRegularInspect.getVersionNumberTime().split(",");
//            for (int i = 0; i <split.length ; i++) {
//                int s = Integer.parseInt(split[i]);
//                if(s<0 ||s>24){
//                    return AjaxResult.error(500, "版本号检查时间格式不对");
//                }
//            }
//        }catch (Exception e){
//            return AjaxResult.error(500, "版本号检查时间格式不对");
//        }
        try{
            String[] split = xhpcPileRegularInspect.getTimingPriceTime().split(",");
            for (int i = 0; i <split.length ; i++) {
                int s = Integer.parseInt(split[i]);
                if(s<0 ||s>24){
                    return AjaxResult.error(500, "校时校价时格式不对");
                }
            }
        }catch (Exception e){
            return AjaxResult.error(500, "校时校价时格式不对");
        }

//        try{
//            String[] split = xhpcPileRegularInspect.getRateTime().split(",");
//            for (int i = 0; i <split.length ; i++) {
//                int s = Integer.parseInt(split[i]);
//                if(s<0 ||s>24){
//                    return AjaxResult.error(500, "定期抄发给管理员时间格式不对");
//                }
//            }
//        }catch (Exception e){
//            return AjaxResult.error(500, "定期抄发给管理员时间格式不对");
//        }

        try{
//            String[] split = xhpcPileRegularInspect.getCopyingAdministratorTime().split(",");
//            for (int i = 0; i <split.length ; i++) {
//                int s = Integer.parseInt(split[i]);
//                if(s<0 ||s>24){
//                    return AjaxResult.error(500, "费率式不对");
//                }
//            }
            if(xhpcPileRegularInspect.getPileRegularInspectId()==null){
                Map<String, Object> map = xhpcPileRegularInspectMapper.getPileRegularInspectTenantId(tenantId);
                if(map !=null){
                    return AjaxResult.error(500, "请重新刷新页面上传数据");
                }
                //xhpcPileRegularInspect.setTenantId(tenantId);
                int i = xhpcPileRegularInspectMapper.insertPileRegularInspect(xhpcPileRegularInspect);
                if(i>0){
                    return AjaxResult.success();
                }
                return AjaxResult.error(500, "请重新登录");
            }else{
                int i = xhpcPileRegularInspectMapper.updatePileRegularInspect(xhpcPileRegularInspect);
                if(i>0){
                    return AjaxResult.success();
                }
                return AjaxResult.error(500, "请重新登录");
            }
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(500, "服务器开小差了，请联系管理员");
        }
    }

    @Override
    public void getVersionTimer() {
        try{
            System.out.println("============定时任务版本检查====================");
            //时间整点是否超过3分钟，超过整点3分钟不进行
            String times = DateUtil.formatTime(new Date());
            String [] time =times.split(":");
            if("00".equals(time[1]) ||"01".equals(time[1])||"02".equals(time[1])||"03".equals(time[1])){
                int number = xhpcPileRegularInspectMapper.getPileRegularInspect(time[0],1);
                if(number>0){
                    //获取所有桩需要核对的版本号
                    List<Map<String, Object>> list = xhpcPileRegularInspectMapper.verificationPileRepeat();
                    for (int i = 0; i <list.size() ; i++) {
                        Map<String, Object> map1=list.get(i);
                        String serialNumber = map1.get("serialNumber").toString();
                        String directNumber = map1.get("directNumber").toString();
                        String communicationNumber = map1.get("communicationNumber").toString();
                        Integer type =Integer.parseInt(map1.get("type").toString());
                        Map<String, Object> cachePile = redisService.getCacheMap("pile:" + serialNumber);
                        String chargingStationId = map1.get("chargingStationId").toString();
                        if(cachePile==null || cachePile.get("firmwareVersion")==null){
                            R r = workOrderService.addNewOrder("12","版本号不一致","该桩版本号和系统设置的版本号不一致","",chargingStationId,"PILE",serialNumber);
                        }else{
                            String firmwareVersion = String.valueOf(cachePile.get("firmwareVersion"));
                            String replace1 = firmwareVersion.replace("\\u0000", "").trim();
                            String replace = directNumber.replace("\\u0000", "").trim();
                            String replace2 = communicationNumber.replace("\\u0000", "").trim();
                            if(type==1){
                                boolean inspect =false;
                                if(replace.length() ==replace1.length()){
                                    if(!replace1.equals(replace)){
                                        inspect =true;
                                    }
                                }else{
                                    inspect =true;
                                }
                                if(inspect){
                                    //添加一条工单
                                    R r = workOrderService.addNewOrder("12","版本号不一致","该桩版本号和系统设置的版本号不一致","",chargingStationId,"PILE",serialNumber);
                                }
                            }else{
                                if(!replace2.equals(replace1)){
                                    //添加一条工单
                                    R r = workOrderService.addNewOrder("12","版本号不一致","该桩版本号和系统设置的版本号不一致","",chargingStationId,"PILE",serialNumber);
                                }
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void getTimingPriceTime() {
        try{
            System.out.println("============校时效价定时任务====================");
            //时间整点是否超过3分钟，超过整点3分钟不进行
            String times = DateUtil.formatTime(new Date());
            String [] time =times.split(":");
            if("00".equals(time[1]) ||"01".equals(time[1])||"02".equals(time[1])||"03".equals(time[1])||"04".equals(time[1])){
                int number = xhpcPileRegularInspectMapper.getPileRegularInspect(time[0],2);
                if(number>0){
                    //获取所有桩需要核对的版本号
                    List<Map<String, Object>> pileSerialNumber = xhpcPileRegularInspectMapper.getPileSerialNumber();
                    for (int i = 0; i < pileSerialNumber.size(); i++) {
                        Map<String, Object> map = pileSerialNumber.get(i);
                        System.out.println("============校时效价定时任务执行===================="+map.get("serialNumber").toString());
                        powerPileService.configTimeNRateModel(map.get("serialNumber").toString());
                    }
                    System.out.println("============校时效价定时任务成功====================");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void getRateTime() {
        try{
            System.out.println("============费率定时任务====================");
            //时间整点是否超过3分钟，超过整点3分钟不进行
            String times = DateUtil.formatTime(new Date());
            String [] time =times.split(":");
            if("00".equals(time[1]) ||"01".equals(time[1])||"02".equals(time[1])||"03".equals(time[1])||"04".equals(time[1])||"05".equals(time[1])){
                int number = xhpcPileRegularInspectMapper.getPileRegularInspect(time[0],3);
                if(number>0){
                    //获取所有桩需要核对的版本号
                    List<Map<String, Object>> pileSerialNumber = xhpcPileRegularInspectMapper.getPileSerialNumber();
                    for (int i = 0; i < pileSerialNumber.size(); i++) {
                        Map<String, Object> map = pileSerialNumber.get(i);
                        String serialNumber = map.get("serialNumber").toString();
                        String modelId = map.get("rateModelId").toString();
                        String chargingStationId = map.get("chargingStationId").toString();
                        Map<String, Object> cachePile = redisService.getCacheMap("pile:" + serialNumber);
                        if(cachePile==null || cachePile.get("rateModelId") ==null){
                            R r = workOrderService.addNewOrder("13","费率不一致","该桩费率和场站设置的费率不一致","",chargingStationId,"PILE",serialNumber);
                        }else{
                            String rateModelId = cachePile.get("rateModelId").toString();

                            if(!modelId.equals(rateModelId)){
                                R r = workOrderService.addNewOrder("13","费率不一致","该桩费率和场站设置的费率不一致","",chargingStationId,"PILE",serialNumber);
                            }
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String times = DateUtil.formatTime(new Date());
        String [] time =times.split(":");
        System.out.println("==777===:"+time[0]);
        System.out.println("==888===:"+time[1]);
        if("00".equals(time[1]) ||"11".equals(time[1])||"12".equals(time[1])||"13".equals(time[1])||"14".equals(time[1])){
            System.out.println("==222===:"+time[1]);
        }
        for (int i = 0; i <time.length ; i++) {
            System.out.println("=====:"+time[i]);
        }

    }
}
