package com.xhpc.activity.task;


import com.xhpc.activity.domain.XhpcStationDeviceDomain;
import com.xhpc.activity.domain.XhpcWorkStationDomain;
import com.xhpc.activity.mapper.*;
import com.xhpc.common.core.utils.bean.BeanUtils;
import com.xhpc.common.domain.XhpcChargingPile;
import com.xhpc.common.domain.XhpcChargingStation;
import com.xhpc.common.domain.XhpcTerminal;
import com.xhpc.common.enums.StationDeviceEnum;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class SyncDeviceDataTask {

    @Resource
    XhpcWorkStationMapper stationMapper;
    @Resource
    XhpcStationDeviceMapper deviceMapper;

    @Resource
    XhpcChargingStationMapper chargingStationMapper;
    @Resource
    XhpcChargingPileMapper chargingPileMapper;
    @Resource
    XhpcTerminalMapper terminalMapper;


    /**
     * 自动同步场站设备信息
     */
    @Scheduled(cron = "0 0 18 * * ?")
    private void autoClearDeviceMessageData(){
        List<XhpcChargingStation> chargingStationList = chargingStationMapper.selectAll();
        for (XhpcChargingStation chargingStation: chargingStationList){
            XhpcWorkStationDomain workStationDomain = stationMapper.selectBySerialNumber(chargingStation.getSerialNumber());
            if(workStationDomain == null){
                workStationDomain = new XhpcWorkStationDomain();
                BeanUtils.copyProperties(chargingStation, workStationDomain);
                workStationDomain.setWorkStationId(chargingStation.getChargingStationId());
                stationMapper.insert(workStationDomain);
            }
            List<XhpcChargingPile> chargingPileList = chargingPileMapper.selectByStationId(chargingStation.getChargingStationId().toString());
            int sort=1;
            for(XhpcChargingPile chargingPile: chargingPileList){
                XhpcStationDeviceDomain pileDeviceDomain = deviceMapper.selectBySerialNumber(chargingPile.getSerialNumber());
                if(pileDeviceDomain == null){
                    pileDeviceDomain = new XhpcStationDeviceDomain();
                    pileDeviceDomain.setDeviceName(chargingPile.getName() + "号桩");
                    pileDeviceDomain.setDeviceType(StationDeviceEnum.PILE.getCode());
                    pileDeviceDomain.setCurrentType(chargingPile.getType() == 1? "直流(快)": "交流(慢)"); ////
                    pileDeviceDomain.setStationId(workStationDomain.getWorkStationId());
                    pileDeviceDomain.setBrandModel(chargingPile.getBrandModel());
                    pileDeviceDomain.setParentDeviceId(null);
                    pileDeviceDomain.setSerialNumber(chargingPile.getSerialNumber());
                    pileDeviceDomain.setSorted(sort); sort ++;
                    pileDeviceDomain.setStatus(Short.parseShort(chargingPile.getStatus().toString()));
                    pileDeviceDomain.setDelFlag(Short.parseShort(chargingPile.getDelFlag().toString()));
                    pileDeviceDomain.setCreateBy(chargingPile.getCreateBy());
                    pileDeviceDomain.setUpdateBy(chargingPile.getUpdateBy());
                    deviceMapper.insert(pileDeviceDomain);
                }


                List<XhpcTerminal> terminalList = terminalMapper.selectByPileId(chargingPile.getChargingPileId().toString());
                for(XhpcTerminal terminal: terminalList){
                    XhpcStationDeviceDomain gunDeviceDomain = deviceMapper.selectBySerialNumber(terminal.getSerialNumber());
                    if(gunDeviceDomain == null){
                        gunDeviceDomain = new XhpcStationDeviceDomain();
                        gunDeviceDomain.setDeviceName(terminal.getName());
                        gunDeviceDomain.setDeviceType(StationDeviceEnum.TERMINAL.getCode());
                        gunDeviceDomain.setCurrentType(chargingPile.getType() == 1? "直流(快)": "交流(慢)"); ////
                        gunDeviceDomain.setStationId(workStationDomain.getWorkStationId());
                        gunDeviceDomain.setBrandModel(chargingPile.getBrandModel());
                        gunDeviceDomain.setParentDeviceId(pileDeviceDomain.getDeviceId());
                        gunDeviceDomain.setSerialNumber(terminal.getSerialNumber());
                        gunDeviceDomain.setSorted(sort); sort ++;
                        gunDeviceDomain.setStatus(Short.parseShort(terminal.getStatus().toString()));
                        gunDeviceDomain.setDelFlag(Short.parseShort(terminal.getDelFlag().toString()));
                        gunDeviceDomain.setCreateBy(terminal.getCreateBy());
                        gunDeviceDomain.setUpdateBy(terminal.getUpdateBy());
                        deviceMapper.insert(gunDeviceDomain);
                    }
                }
            }
        }
    }
}
