package com.usoftchina.bi.server.service.common;

import com.usoftchina.bi.core.jdbc.DynamicDataSourceContextHolder;
import com.usoftchina.bi.server.aspect.JwtTokenAspect;
import com.usoftchina.bi.server.dao.common.UASMapper;
import com.usoftchina.bi.server.dao.user.UserMapper;
import com.usoftchina.bi.server.model.po.*;
import com.usoftchina.bi.server.model.vo.configVo.DatabasesInfo;
import com.usoftchina.bi.server.model.vo.dataVo.DashboardCopyInfo;
import com.usoftchina.bi.server.service.dashboard.DashboardsService;
import com.usoftchina.bi.server.service.dataSource.DataBasesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Author hy
 * @Date 2019-04-26
 */
@Service
public class UASService {

    @Autowired
    private UASMapper uasMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    JwtTokenAspect jwtTokenUtil;

    @Autowired
    DashboardsService dashboardsService;

    @Autowired
    DataBasesService dataBasesService;

    public String refreshData(RefreshData refreshData){
        uasMapper.sp_refreshdata(refreshData);
        String BI_DATABASES_ID = refreshData.getRes();
        //添加数据源可切换
        DatabasesInfo databasesInfo = uasMapper.getDatabasesInfo(BI_DATABASES_ID);
        dataBasesService.addDataSource(databasesInfo);
        //把人员信息重新插入一遍
        //切换到主账套
        DynamicDataSourceContextHolder.setDataSourceType(Integer.valueOf(BI_DATABASES_ID));
        List<User> userList = userMapper.getUASEmployee();
        DynamicDataSourceContextHolder.clearDataSourceType();
        userMapper.batchInsert(userList);
        return "true";
    }

    public String sync(SyncData syncData){
        //校验from 和 to  是否存在
        Integer id = userMapper.getMasterId();
        if(ObjectUtils.isEmpty(id)){
            return "主账套还未初始化，请初始化后再使用同步功能";
        }
        int hasFrom = uasMapper.checkDataBase(syncData.getFrom());
        int hasTo = uasMapper.checkDataBase(syncData.getTo());
        if(hasFrom==0){
            //插入数据源
            return "来源账套还未初始化，请初始化后再使用同步功能";
        }
        if(hasTo==0){
            //插入数据源
            uasMapper.insertDataBase(syncData.getToName(),syncData.getTo(),id);
        }
        //目标账套数据源ID
        String to_basedata_id = userMapper.getMasterIdByName(syncData.getTo());
        //添加数据源可切换
        DatabasesInfo databasesInfo = uasMapper.getDatabasesInfo(to_basedata_id);
        dataBasesService.addDataSource(databasesInfo);
        //删除目标数据源所有标准报表
        uasMapper.sp_clean_sob(syncData.getTo(),"");
        //模拟登录
        User user = userMapper.getLogin(syncData.getEmName());
        String token = jwtTokenUtil.createToke(user).getToken();
        //查找所有符合条件的Dashboard
        List<DashboardCopyInfo> list = uasMapper.getFromSobDashboard(syncData.getFrom(),syncData.getTo());
        for (  DashboardCopyInfo dashboardCopyInfo : list) {
            dashboardCopyInfo.setLastName("_" + syncData.getToName());
            dashboardCopyInfo.setDataSourceId(Integer.valueOf(to_basedata_id));
            //复制报表
            dashboardsService.copy(token,dashboardCopyInfo);
        }
        //更新标准的旧报表编号账套后缀
        //uasMapper.sp_sync(syncData.getTo(),"");
        return "true";
    }
}
