package com.ssm.manage.dao.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.impl.BaseDaoImpl;
import com.ssm.manage.dao.SsmRoutingDao;
import com.ssm.manage.entity.Routing;
import com.ssm.manage.entity.SysMenuPower;
import com.ssm.manage.entity.SysRolePower;
import com.ssm.manage.mapper.RoutingMapper;
import com.ssm.manage.query.RoutingListRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class SsmRoutingDaoImpl extends BaseDaoImpl<Routing> implements SsmRoutingDao {
    @Autowired
    private RoutingMapper mapper;

    @Override
    protected RoutingMapper getMapper() {
        return mapper;
    }

    int num=1;
    Integer  grade_id=10;

    @Override
    public PageInfo<Routing> selectPage(RoutingListRequest routingListRequest) {
        if(routingListRequest.getIsall()!=null){
            List<Routing> list = mapper.selectPage(routingListRequest);
            PageInfo<Routing> pageInfo = new PageInfo<>(list);
            return pageInfo;
        }
        PageHelper.startPage(routingListRequest.getPageNum(),routingListRequest.getPageSize(),"id");
        List<Routing> list = mapper.selectPage(routingListRequest);
        PageInfo<Routing> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }



    @Override
    public int add(RoutingListRequest routingListRequest) {
        int a=0;


        //1级生产10,2级1010，权限101010
        //权限
        if(routingListRequest.getType()==1){
            grade_id=101010;
            Long id=Long.valueOf(grade_id);
            this.distribution1(id,routingListRequest.getType());
        }
        else if(routingListRequest.getSuperiorId() !=null ){
            grade_id=10;
            Long id1=routingListRequest.getSuperiorId();
            Long id2=Long.valueOf(grade_id);
            this.distribution2(id1,id2);
        }else {
            grade_id=10;
            Long id=Long.valueOf(grade_id);
            this.distribution1(id,routingListRequest.getType());
        }
        Long grade_id1=Long.valueOf(grade_id);
        routingListRequest.setId(grade_id1);

        Routing routing=new Routing();
        routingListRequest.setPathIndex(routingListRequest.getPath());
        //查询路径是否重复
        List<Routing> list=mapper.selectPath(routingListRequest);
        if(list.size()>0){
            routingListRequest.setPath(routingListRequest.getPath()+"X");
        }
        routingListRequest.setFounderDatetime(new Date());
        BeanUtils.copyProperties(routingListRequest,routing);
        //判断操作为1是权限，需要向sys_menu_power、sys_role_power表添加数据，0为菜单直接插入
        if(routingListRequest.getType()==1){
            mapper.add1(routingListRequest);
            mapper.add2(routingListRequest);
        }
        return this.saveNotNull(routing);




    }

    @Override
    public int delete(RoutingListRequest routingListRequest) {
        if(routingListRequest.getSuperiorId() == null){
            List<Routing> delList=mapper.selectSuperid(routingListRequest);
            if (delList.size()>0)
                mapper.deleteID(delList);
        }
        if(routingListRequest.getType()==1){
            mapper.deleteID1(routingListRequest.getId());
            mapper.deleteID2(routingListRequest.getId());
        }
        return this.deleteByPrimaryKey(routingListRequest.getId());

    }




    @Override
    public int updateId(RoutingListRequest routingListRequest) {
        Routing routing=new Routing();
        routingListRequest.setModifyDatetime(new Date());
        List<Routing> delList=mapper.selectSuperid(routingListRequest);
        if(delList.size() != 0){
            mapper.updateId(delList,routingListRequest.getMemberName());
        }
        if(routingListRequest.getType()==1){
            mapper.updateId1(routingListRequest.getMemberName(),routingListRequest.getId());
        }
        BeanUtils.copyProperties(routingListRequest,routing);
        return this.updateByPrimaryKey(routing);
    }




    //递归分配1级和权限ID
    public void distribution1(Long id,Long type){
        RoutingListRequest routingListRequest1 = new RoutingListRequest();
        routingListRequest1.setId(id);
        List<Routing> dvalue=mapper.selectDvalue(routingListRequest1);
        if(type==1){
            SysRolePower sysRolePower=new SysRolePower();
            SysMenuPower sysMenuPower=new SysMenuPower();
            sysRolePower.setPowerId(Math.toIntExact(id));
            sysMenuPower.setId(Math.toIntExact(id));
            List<SysRolePower> dvalue1=mapper.selectDvalue1(sysRolePower);
            List<SysMenuPower> dvalue2=mapper.selectDvalue2(sysMenuPower);
            if(dvalue.size() >=1 && dvalue1.size() >=1 && dvalue2.size() >=1){//不等于空说明有重复值，不能添加重复id
                id += grade_id;
                this.distribution1(id,type);
            }else if(dvalue.size() ==0 && dvalue1.size() ==0 && dvalue2.size() ==0)
                grade_id = Math.toIntExact(id);
        }else if(type ==0){
            if(dvalue.size() >=1 ){//不等于空说明有重复值，不能添加重复id
                id += grade_id;
                this.distribution1(id,type);
            }else if(dvalue.size()==0)
                grade_id = Math.toIntExact(id);
        }
    }
    //递归分配2级
    public void distribution2(Long id1,Long id2){
        String a=String.valueOf(id1);
        String b=String.valueOf(id2);
        Long id=Long.valueOf(a+b);
        RoutingListRequest routingListRequest1 = new RoutingListRequest();
        routingListRequest1.setId(id);
        List<Routing> dvalue=mapper.selectDvalue(routingListRequest1);
        if(dvalue.size() >=1){//不等于空说明有重复值，不能添加重复id
            id2 += grade_id;
            this.distribution2(id1,id2);
        }else {
            grade_id = Math.toIntExact(id);
        }
    }


}
