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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.bytebuddy.implementation.bytecode.Throw;
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.ATrack;
import org.jeecg.modules.warning.mapper.AConfigureMapper;
import org.jeecg.modules.warning.mapper.ATrackMapper;
import org.jeecg.modules.warning.model.addTrackDto;
import org.jeecg.modules.warning.model.chartConfigureModel;
import org.jeecg.modules.warning.model.chartTrackTreeModel;
import org.jeecg.modules.warning.model.lineMoveDto;
import org.jeecg.modules.warning.service.IATrackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ATrackServiceImpl extends ServiceImpl<ATrackMapper, ATrack> implements IATrackService{
    @Autowired
    AConfigureMapper aConfigureMapper;

    @Override
    @Transactional
    public void moveLine(lineMoveDto lineMoveDto) {
        List<chartConfigureModel> oldLine=lineMoveDto.getOldTrack().getChildren();
        List<chartConfigureModel> newLine=lineMoveDto.getNewTrack().getChildren();
        String oldTempId=lineMoveDto.getOldTrack().getKey().split("-")[0];
        String oldTrackID=lineMoveDto.getOldTrack().getId();
        String newTempId=lineMoveDto.getNewTrack().getKey().split("-")[0];
        String newTrackId=lineMoveDto.getNewTrack().getId();
        List<AConfigure> oldList=getLine(oldTempId,oldTrackID,oldLine);
        List<AConfigure> newList=getLine(newTempId,newTrackId,newLine);
        QueryWrapper<AConfigure> delWrapper=new QueryWrapper<>();
        delWrapper.eq("TRACK_ID",oldTrackID).or().eq("TRACK_ID",newTrackId);
        aConfigureMapper.delete(delWrapper);
        if(!oldTrackID.equals(newTrackId)){
            for(AConfigure item:oldList){
                aConfigureMapper.insert(item);
            }
        }
        for(AConfigure item:newList){
            aConfigureMapper.insert(item);
        }

    }
    private List<AConfigure> getLine(String tempId,String trackId, List<chartConfigureModel> model){
        List<AConfigure> lineList=new ArrayList<>();
        BigDecimal sortCode=new BigDecimal("2");
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        for(chartConfigureModel itemModel:model){
            AConfigure lineItem=new AConfigure();
            lineItem.setTitle(itemModel.getTitle());
            lineItem.setBackground(itemModel.getBackground());
            lineItem.setColor(itemModel.getColor());
            lineItem.setCreateBy(user.getId());
            lineItem.setCreateTime(new Date());
            lineItem.setDataSource(itemModel.getDataSource());
            lineItem.setLineStyle(itemModel.getLineStyle());
            lineItem.setMaxNum(itemModel.getMaxNum());
            lineItem.setMaxSpread(itemModel.getMaxSpread());
            lineItem.setMinNum(itemModel.getMinNum());
            lineItem.setMinSpread(itemModel.getMinSpread());
            lineItem.setSortCode(sortCode);
            sortCode=sortCode.add(new BigDecimal("2"));
            lineItem.setSpread(itemModel.getSpread());
            lineItem.setUnit(itemModel.getUnit());
            lineItem.setTemplateId(tempId);
            lineItem.setTrackId(trackId);
            lineList.add(lineItem);
        }
        return lineList;
    }

    private ATrack getTrack(String tempId,BigDecimal sortCode, chartTrackTreeModel trackModel){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        ATrack aTrack=new ATrack();
        aTrack.setTitle(trackModel.getTitle());
        aTrack.setWidth(trackModel.getWidth());
        aTrack.setFix(trackModel.getFix());
        aTrack.setCreateBy(user.getId());
        aTrack.setCreateTime(new Date());
        aTrack.setTemplateId(tempId);
        aTrack.setSortCode(sortCode);
        return aTrack;
    }
    @Override
    @Transactional
    public void addByDirection(addTrackDto addTrackDto){
       String direction=addTrackDto.getDirection();
       String tempId=addTrackDto.getTempId();
       String trackId=addTrackDto.getTrackId().split("-")[1];
       BigDecimal trackSortCode=new BigDecimal("2");
       QueryWrapper<ATrack> trackQueryWrapper=new QueryWrapper<>();
       trackQueryWrapper.eq("TEMPLATE_ID",tempId);
       trackQueryWrapper.orderByAsc("SORT_CODE");
       List<ATrack> trackList=this.list(trackQueryWrapper);
       int listCount=trackList.size();
       int trackIndex=0;
       for (int i = 0; i < listCount; i++) {
          if(trackList.get(i).getId().equals(trackId)) {
              trackIndex=i;
              break;
          }
       }
       if (direction.equals("r")){
           if((trackIndex+1)==listCount){
               trackSortCode=trackSortCode.add(trackList.get(trackIndex).getSortCode());
           }else{
             BigDecimal total=trackList.get(trackIndex).getSortCode().add(trackList.get((trackIndex+1)).getSortCode());
             trackSortCode=total.divide(BigDecimal.valueOf(2.0));
           }
       }
       if(direction.equals("l")){
           if(trackIndex==0){
               trackSortCode=trackList.get(trackIndex).getSortCode().divide(BigDecimal.valueOf(2.0));
           }else{
               BigDecimal total=trackList.get(trackIndex).getSortCode().add(trackList.get((trackIndex-1)).getSortCode());
               trackSortCode=total.divide(BigDecimal.valueOf(2.0));
           }
       }
        ATrack aTrack=getTrack(tempId,trackSortCode,addTrackDto.getNewTrack());
        this.save(aTrack);
        List<AConfigure> lineList=getLine(tempId,aTrack.getId(),addTrackDto.getNewTrack().getChildren());
        for(AConfigure item:lineList){
            aConfigureMapper.insert(item);
        }
    }
}
