package org.jeecg.modules.warning.service.impl;

import antlr.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.warning.entity.AConfigure;
import org.jeecg.modules.warning.entity.ATemplate;
import org.jeecg.modules.warning.entity.ATrack;
import org.jeecg.modules.warning.mapper.AConfigureMapper;
import org.jeecg.modules.warning.mapper.ATemplateMapper;
import org.jeecg.modules.warning.mapper.ATrackMapper;
import org.jeecg.modules.warning.model.chartConfigureModel;
import org.jeecg.modules.warning.model.chartTemplateTreeModel;
import org.jeecg.modules.warning.model.chartTrackTreeModel;
import org.jeecg.modules.warning.service.IATemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ATemplateServiceImpl extends ServiceImpl<ATemplateMapper, ATemplate> implements IATemplateService {

    @Autowired
    ATemplateMapper aTemplateMapper;
    @Autowired
    ATrackMapper aTrackMapper;
    @Autowired
    AConfigureMapper aConfigureMapper;

    @Override
    public List<chartTemplateTreeModel> getMyChartTreeList(String id) {
        List<chartTemplateTreeModel> result=new ArrayList<>();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<ATemplate> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ID",id);
        queryWrapper.orderByDesc("create_time");
        List<ATemplate> templateList= this.list(queryWrapper);
        List<String> templateIds=new ArrayList<>();
        for(ATemplate item:templateList){
            templateIds.add(item.getId());
        }
        QueryWrapper<ATrack> trackQueryWrapper=new QueryWrapper<>();
        trackQueryWrapper.in("TEMPLATE_ID",templateIds);
        trackQueryWrapper.orderByAsc("SORT_CODE");
        List<ATrack> trackList=aTrackMapper.selectList(trackQueryWrapper);
        QueryWrapper<AConfigure> configureQueryWrapper=new QueryWrapper<>();
        configureQueryWrapper.in("TEMPLATE_ID",templateIds);
        configureQueryWrapper.orderByAsc("SORT_CODE");
        List<AConfigure> configureList=aConfigureMapper.selectList(configureQueryWrapper);
        for(ATemplate item:templateList){
            chartTemplateTreeModel chartTemplateTreeModel=new chartTemplateTreeModel(item);
            List<chartTrackTreeModel> trackTreeModelList=new ArrayList<>();
            for (ATrack trackItem:trackList){
                if (trackItem.getTemplateId().equals(item.getId())){
                    List<chartConfigureModel> configureModelList=new ArrayList<>();
                    chartTrackTreeModel chartTrackTreeModel=new chartTrackTreeModel(trackItem);
                    for (AConfigure configureItem:configureList){
                        if(configureItem.getUnit().equals("m3")){
                            configureItem.setUnit("m³");
                        }
                        if(configureItem.getUnit().equals("g/cm3")){
                            configureItem.setUnit("g/cm³");
                        }
                        if (configureItem.getTrackId().equals(trackItem.getId())){
                            configureModelList.add(new chartConfigureModel(configureItem));
                        }
                    }
                    chartTrackTreeModel.setChildren(configureModelList);
                    trackTreeModelList.add(chartTrackTreeModel);
                }
            }
            chartTemplateTreeModel.setChildren(trackTreeModelList);
            result.add(chartTemplateTreeModel);
        }
        return result;
    }

    @Override
    @Transactional
    public Boolean initChart(String userId) {
        QueryWrapper<ATemplate> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("CREATE_BY",userId);
        List<ATemplate> templateList= this.list(queryWrapper);
        if(templateList.size()>0){
            return true;
        }
        queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("IS_DEFAULT","Y");
        List<ATemplate> templateList2= this.list(queryWrapper);
        for (ATemplate tempItem:templateList2)
        {
            tempItem.setCreateBy(userId);
            tempItem.setCreateTime(new Date());
            tempItem.setIsDefault("N");
            QueryWrapper<ATrack> trackQueryWrapper=new QueryWrapper<>();
            trackQueryWrapper.eq("TEMPLATE_ID",tempItem.getId());
            trackQueryWrapper.orderByAsc("SORT_CODE");
            List<ATrack> trackList=aTrackMapper.selectList(trackQueryWrapper);
            tempItem.setId(null);
            this.save(tempItem);
            for (ATrack trackItem:trackList)
            {
                trackItem.setTemplateId(tempItem.getId());
                trackItem.setCreateBy(userId);
                trackItem.setCreateTime(new Date());
                QueryWrapper<AConfigure> configureQueryWrapper=new QueryWrapper<>();
                configureQueryWrapper.eq("TRACK_ID",trackItem.getId());
                configureQueryWrapper.orderByAsc("SORT_CODE");
                List<AConfigure> configureList=aConfigureMapper.selectList(configureQueryWrapper);
                trackItem.setId(null);
                aTrackMapper.insert(trackItem);
                for (AConfigure configItem:configureList)
                {
                    configItem.setTemplateId(tempItem.getId());
                    configItem.setTrackId(trackItem.getId());
                    configItem.setCreateBy(userId);
                    configItem.setCreateTime(new Date());
                    configItem.setId(null);
                    aConfigureMapper.insert(configItem);
                }
            }
        }
        return true;
    }
}
