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

import com.usoftchina.bi.server.dao.dashboard.DashboardsMapper;
import com.usoftchina.bi.server.dao.strategy.StrategysBdMapper;
import com.usoftchina.bi.server.dao.strategy.StrategysChartMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.usoftchina.bi.server.model.bo.DashOrder;
import com.usoftchina.bi.server.model.bo.Obj;
import com.usoftchina.bi.server.model.po.Strategys;
import com.usoftchina.bi.server.model.po.StrategysData;
import com.usoftchina.bi.core.base.RepCode;
import com.usoftchina.bi.core.base.RepEntity;
import com.usoftchina.bi.server.model.vo.configVo.ObjectInfo;
import com.usoftchina.bi.server.model.vo.configVo.StrategysInfo;
import com.usoftchina.bi.server.utils.ScreenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StrategysService {
    @Autowired
    ScreenUtil screenUtil;
    @Autowired
    StrategysBdMapper strategysBdMapper;
    @Autowired
    StrategysChartMapper strategysChartMapper;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    DashboardsMapper dashboardsMapper;
    /*
   保存数据源策略
    */
    public RepEntity addStrategys(StrategysInfo strategysInfo){
        Strategys strategys = new Strategys();
        BeanUtils.copyProperties(strategysInfo, strategys);
        String scr = screenUtil.screensUtil(strategysInfo.getRule(), "", "").getRet();
        strategys.setRule(scr);
        strategys.setRuleStr(String.valueOf(strategysInfo.getRule()));

        String type = strategysInfo.getType();
        if ("base".equals(type)){
            strategysBdMapper.addStrategys(strategys);
        }else {
            strategysChartMapper.addStrategys(strategys);
        }

        return new RepEntity(RepCode.success, strategys.getId());
    }

    /*
    更新数据源策略
     */
    public RepEntity updateStrategys(StrategysInfo strategysInfo){
        Strategys strategys = new Strategys();
        BeanUtils.copyProperties(strategysInfo, strategys);
        String scr = screenUtil.screensUtil(strategysInfo.getRule(), "", "").getRet();
        strategys.setRule(scr);
        String ruleStr = null;
        try {
            ruleStr = objectMapper.writeValueAsString(strategysInfo.getRule());
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        strategys.setRuleStr(ruleStr);
        String type = strategysInfo.getType();
        if ("base".equals(type)) {
            strategysBdMapper.updataStrategys(strategys);
        }else {
            strategysChartMapper.updataStrategys(strategys);
        }
        return new RepEntity(RepCode.success);
    }

    /*
    删除数据源策略
     */
    public RepEntity delDbStrategys(Long id){
        strategysBdMapper.delStrategys(id);
        strategysBdMapper.delObject(id.intValue());
        return new RepEntity(RepCode.success);
    }

    /*
    删除图表策略
     */
    public RepEntity delChartStrategys(Long id){
        strategysChartMapper.delStrategys(id);
        strategysChartMapper.delObject(id.intValue());
        return new RepEntity(RepCode.success);
    }

    /*
    查询数据源策略列表
     */
    public RepEntity getDbStrategys(int DbId){

        List<StrategysData> strategysDataList = new ArrayList<>();
        /*
        获取id列表
         */
        List<Integer> idList = strategysBdMapper.getStrIdList(DbId);
        Iterator isList = idList.iterator();
        while (isList.hasNext()) {
            int id = (int) isList.next();
            List<DashOrder> userGroupNameList = new ArrayList<>();
            List<DashOrder> userNameList = new ArrayList<>();
            StrategysData strategysDatas = new StrategysData();

            Strategys strategys = strategysBdMapper.getStrategys(id);  //策略的基本信息
            List<String> types = strategysBdMapper.getStrategysType(id);
            Iterator isTypeList = types.iterator();
            while (isTypeList.hasNext()) {
                String type = (String) isTypeList.next();
                if ("0".equals(type)) {
                    userGroupNameList = strategysBdMapper.getUserGroupIdAndName(id);
                } else {
                    userNameList = strategysBdMapper.getUserIdAndName(id);
                }
            }
            strategysDatas.setStrategies(strategys);
            strategysDatas.setUserGroupName(userGroupNameList);
            strategysDatas.setUserName(userNameList);
            strategysDataList.add(strategysDatas);
        }
        return new RepEntity(RepCode.success, strategysDataList);
    }

    /*
    添加用户或用户组
     */
    public RepEntity addObject(ObjectInfo objectInfo){
        String type = objectInfo.getType();
        int stId = objectInfo.getStId();
        if ("base".equals(type)){
            strategysBdMapper.delObject(stId);
        }else if ("chart".equals(type)){
            strategysChartMapper.delObject(stId);
        }else {
            dashboardsMapper.delObject(stId);
        }
        List objs = objectInfo.getObj();
        Iterator isObj = objs.iterator();
        while (isObj.hasNext()) {
            Obj obj = (Obj) isObj.next();
            String obType = obj.getObjectType();
            int obId = obj.getObId();
            if ("base".equals(type)) {
                strategysBdMapper.addObject(stId, obType, obId);
            } else if ("chart".equals(type)){
                strategysChartMapper.addObject(stId, obType, obId);
            }else {
                dashboardsMapper.addObject(stId, obType, obId);
            }
        }
        return new RepEntity(RepCode.success);
    }


    /*
    查询图表策略列表
     */
    public RepEntity getChartStrategys(int chartId){
        List<StrategysData> strategysDataList = new ArrayList<>();
        /*
        获取id列表
         */
        List<Integer> idList = strategysChartMapper.getStrIdList(chartId);
        Iterator isList = idList.iterator();
        while (isList.hasNext()) {
            int id = (int) isList.next();
            List<DashOrder> userGroupNameList = new ArrayList<>();
            List<DashOrder> userNameList = new ArrayList<>();
            StrategysData strategysDatas = new StrategysData();

            Strategys strategys = strategysChartMapper.getStrategys(id);  //策略的基本信息
            List<String> types = strategysChartMapper.getStrategysType(id);
            Iterator isTypeList = types.iterator();
            while (isTypeList.hasNext()) {
                String type = (String) isTypeList.next();
                if ("0".equals(type)) {
                    userGroupNameList = strategysChartMapper.getUserGroupIdAndName(id);
                } else {
                    userNameList = strategysChartMapper.getUserIdAndName(id);
                }
            }
            strategysDatas.setStrategies(strategys);
            strategysDatas.setUserGroupName(userGroupNameList);
            strategysDatas.setUserName(userNameList);
            strategysDataList.add(strategysDatas);
        }
        return new RepEntity(RepCode.success, strategysDataList);
    }
}
