package com.casic.missiles.modular.sync.service.impl;

import com.casic.missiles.invoke.sync.ReceiveParamsBean;
import com.casic.missiles.invoke.sync.handler.ReceiveHandler;
import com.casic.missiles.modular.sync.service.ICommonReceiveSyncDataService;
import com.casic.missiles.modular.sync.service.ISyncDataService;
import com.casic.missiles.modular.system.model.DeptSync;
import com.casic.missiles.modular.system.model.UserSync;
import com.casic.missiles.modular.system.service.IDeptSyncService;
import com.casic.missiles.modular.system.service.IUserSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class SyncDataServiceImpl implements ISyncDataService {
    private static final Logger logger = LoggerFactory.getLogger(SyncDataServiceImpl.class);
    @Autowired
    private IUserSyncService userSyncService;
    @Autowired
    private IDeptSyncService deptSyncService;
    @Autowired(required = false)
    private ICommonReceiveSyncDataService receiveSyncDataService;
    @Autowired
    private ReceiveHandler handler;

    public void addUsers(List<UserSync> persons) {
        userSyncService.delete(null);
        userSyncService.insertBatch(persons);
    }

    public void addDepts(List<DeptSync> orgs) {
        deptSyncService.delete(null);
        deptSyncService.insertBatch(orgs);
    }

    @Override
    public void saveSyncData(ReceiveParamsBean params) {
        this.addDepts(params.getOrgaList());
        this.addUsers(params.getPersonOrDeviceList());
    }

    @Override
    public void processIncreData(ReceiveParamsBean params) {
        String msg = "";
        switch (params.getDataType()) {
            case "1"://初始化接收数据及强制推送数据
                logger.info("接收数据:{}", params);
                receiveSyncDataService.addDepts(params.getOrgaList(), handler);
                receiveSyncDataService.addUsers(params.getPersonOrDeviceList(), handler);
                msg = "初始化数据";
                break;

            case "3"://人员设备推送数据
                logger.info("接收数据:{}", params.getPersonOrDeviceList());
                receiveSyncDataService.addUsers(params.getPersonOrDeviceList(), handler);
                msg = "增量用户数据";
                break;
            case "4"://组织机构数据推送
                logger.info("接收数据:{}", params.getOrgaList());
                receiveSyncDataService.addDepts(params.getOrgaList(), handler);
                msg = "增量组织数据";
                break;

        }
        logger.info("{}处理完成", msg);
    }
}
