package com.mortals.xhx.daemon.task;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.service.ITask;
import com.mortals.framework.service.ITaskExcuteService;
import lombok.extern.slf4j.Slf4j;

/**
 * 同步用户
 */
@Slf4j
//@Service("SyncUserTask")
public class SyncUserTaskImpl implements ITaskExcuteService {


    @Override
    public void excuteTask(ITask task) throws AppException {
        log.info("同步用户");
        /*SitePdu sitePdu = new SitePdu();
        sitePdu.setId(1L);
        Rest<List<SitePdu>> siteRest = siteFeign.getFlatSitesBySiteId(sitePdu);
        if (siteRest.getCode() == YesNoEnum.YES.getValue()) {
            log.info("站点总数量：{}", siteRest.getData().size());
            siteRest.getData().forEach(site -> {
                log.info("站点名称:{}",site.getSiteName());
                DevicePdu devicePdu = new DevicePdu();
                devicePdu.setProductCode("ybj");
                devicePdu.setSize(-1);
                Rest<RespData<List<DevicePdu>>> deviceRest = deviceFeign.list(devicePdu);
                log.info("deviceRest：{}", JSON.toJSONString(deviceRest));
                if (YesNoEnum.YES.getValue() == deviceRest.getCode()) {
                    List<DevicePdu> devicePduList = deviceRest.getData().getData();
                    log.info("样表机总数量：{}", devicePduList.size());
                    if (!ObjectUtils.isEmpty(devicePduList)) {
                        List<DeviceEntity> newDeviceList = devicePduList.stream().map(newDevice -> {
                            DeviceEntity deviceEntity = new DeviceEntity();
                            deviceEntity.initAttrValue();
                            BeanUtils.copyProperties(newDevice, deviceEntity, BeanUtil.getNullPropertyNames(newDevice));
                            return deviceEntity;
                        }).collect(Collectors.toList());

                        List<DeviceEntity> oldDeviceList = deviceService.find(new DeviceQuery().siteId(site.getId()));
                        Map<String, DeviceEntity> oldDeviceMap = oldDeviceList.stream().collect(Collectors.toMap(x -> x.getDeviceCode(), y -> y, (o, n) -> n));
                        Map<String, DeviceEntity> newDeviceMap = newDeviceList.stream().collect(Collectors.toMap(x -> x.getDeviceCode(), y -> y, (o, n) -> n));

                        List<DeviceEntity> updateDeviceLsit = newDeviceList.stream().map(item -> {
                            if (oldDeviceMap.containsKey(item.getDeviceCode())) {
                                item.setId(oldDeviceMap.get(item.getDeviceCode()).getId());
                                item.setDeviceId(item.getId());
                                item.setProductCode(oldDeviceMap.get(item.getDeviceCode()).getProductCode());
                                item.setProductName(oldDeviceMap.get(item.getDeviceCode()).getProductName());
                                item.setUpdateTime(new Date());
                                return item;
                            }
                            return null;
                        }).filter(f -> f != null).collect(Collectors.toList());

                        List<DeviceEntity> saveDeviceList = newDeviceList.stream().map(item -> {
                            if (!oldDeviceMap.containsKey(item.getDeviceCode())) {
                                item.setDeviceId(item.getId());
                                item.setCreateUserId(1L);
                                item.setCreateUserName("系统管理员");
                                item.setCreateTime(new Date());
                                return item;
                            }
                            return null;
                        }).filter(f -> f != null).collect(Collectors.toList());

                        //做差集
                        List<Long> delDeviceList = oldDeviceList.stream().map(item -> {
                            if (!newDeviceMap.containsKey(item.getDeviceCode())) {
                                return item.getId();
                            }
                            return null;
                        }).filter(f -> f != null).collect(Collectors.toList());

                        if (!ObjectUtils.isEmpty(updateDeviceLsit)) {
                            log.info("设备更新，size:{}", updateDeviceLsit.size());
                            deviceService.update(updateDeviceLsit);
                        }

                        if (!ObjectUtils.isEmpty(saveDeviceList)) {
                            log.info("设备新增，size:{}", saveDeviceList.size());
                            deviceService.save(saveDeviceList);
                        }

                        if (!ObjectUtils.isEmpty(delDeviceList)) {
                            log.info("设备删除，size:{}", delDeviceList.size());
                            deviceService.remove(delDeviceList,null);
                        }
                    }
                } else {
                    log.info("设备列表查询异常，{}", JSON.toJSONString(deviceRest));
                }
            });
        } else {
            log.info("站点列表查询异常，{}", JSON.toJSONString(siteRest));
        }*/

    }


    @Override
    public void stopTask(ITask task) throws AppException {

    }


}
