package com.nantian.cloud.jljc.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jljc.entity.TStandardsRegulations;
import com.nantian.cloud.jljc.entity.TSupervisionOrganization;
import com.nantian.cloud.jljc.mapper.TStandardsRegulationsMapper;
import com.nantian.cloud.jljc.mapper.TSupervisionOrganizationMapper;
import com.nantian.cloud.jljc.service.TStandardsRegulationsService;
import com.nantian.cloud.jljc.service.TSupervisionOrganizationService;
import com.nantian.cloud.jljc.vo.RegionOrganizationTreeVo;
import com.nantian.cloud.jljc.vo.TreeVo;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.nantian.cloud.jljc.vo.util.TreeVoUtil.findTree;

@Service("TSupervisionOrganizationService")
public class TSupervisionOrganizationServiceImpl extends ServiceImpl<TSupervisionOrganizationMapper, TSupervisionOrganization> implements TSupervisionOrganizationService {


    public IPage<TSupervisionOrganization> selectall(Page page, TSupervisionOrganization tSupervisionOrganization) {
        return baseMapper.selectall(page, tSupervisionOrganization, new DataScope());
    }

    @Override
    public boolean updateall(TSupervisionOrganization tSupervisionOrganization) {
        if (baseMapper.updateall(tSupervisionOrganization) > 0) {
            return true;
        } else {
            return false;
        }

    }

    @Override
    public boolean deleteall(TSupervisionOrganization tSupervisionOrganization) {
        if (baseMapper.deleteall(tSupervisionOrganization) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean insertall(TSupervisionOrganization tSupervisionOrganization) {
        if (baseMapper.insertall(tSupervisionOrganization) > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<TSupervisionOrganization> getList(TSupervisionOrganization tSupervisionOrganization) {
        return baseMapper.getList(tSupervisionOrganization, new DataScope());
    }

    @Override
    public List<TreeVo> getTree(String deptId) {
        if ("".equals(deptId) || deptId == null) {
            deptId = SecurityUtils.getUser().getDeptId().toString();
        }
        String regionCode = "";
        if (deptId.contains("0000")) { //省
            regionCode = StrUtil.subWithLength(deptId, 0, 2);
        } else if (deptId.contains("00")) {  //州市
            regionCode =  StrUtil.subWithLength(deptId, 0, 4);
        }else{
            regionCode = deptId;
        }
        List<TreeVo> organizationTreee = baseMapper.getTree(deptId,regionCode);
        if(organizationTreee.size()>1){
            return findTree(organizationTreee);
        }else{
            return organizationTreee;
        }
    }

    @Override
    public List<RegionOrganizationTreeVo> regionOrganizationTree(TSupervisionOrganization tSupervisionOrganization) {
        List<RegionOrganizationTreeVo> regionTree = baseMapper.regionTree();
        List<RegionOrganizationTreeVo> organizationTree = baseMapper.organizationTree(tSupervisionOrganization);

        String regionStr = baseMapper.getRegionStr(tSupervisionOrganization);
        List<RegionOrganizationTreeVo> newregionTree = new ArrayList<RegionOrganizationTreeVo>();
        for (RegionOrganizationTreeVo treeVo : regionTree) {
            if (regionStr != null && regionStr.contains(treeVo.getValue())) {
                newregionTree.add(treeVo);
            }
        }
        if (newregionTree.size() > 0 && !regionStr.contains("530000")) {
            RegionOrganizationTreeVo vo = new RegionOrganizationTreeVo();
            vo.setValue("530000");
            vo.setLabel("云南省");
            newregionTree.add(vo);
        }

        List<RegionOrganizationTreeVo> treeVos = new ArrayList<>();
        treeVos.addAll(newregionTree.size() > 0 ? newregionTree : regionTree);
        treeVos.addAll(organizationTree);
        return findTree(treeVos);
    }


}
