package com.chinaweal.aiccs.aiccs.abnormal.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.entity.Gzaiccode;
import com.chinaweal.aiccs.aiccs.abnormal.mapper.GzaiccodeMapper;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.abnormal.service.IGzaiccodeService;
import com.chinaweal.aiccs.aiccs.dict.entity.CascadeTreeNode;
import com.chinaweal.aiccs.org.entity.OrgUnits;
import com.chinaweal.aiccs.org.service.TUsersService;
import com.chinaweal.aicorg.model.AICOrg;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.aicorg.services.OrgUM;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chinaweal
 * @since 2021-10-21
 */
@Slf4j
@Service
@Transactional
public class GzaiccodeServiceImpl extends ServiceImpl<GzaiccodeMapper, Gzaiccode> implements IGzaiccodeService {
    private static final Map<String, List<CascadeTreeNode>> CACHE = new ConcurrentHashMap<>();

    @Resource
    private EBaseinfoService eBaseinfoService;
    @Resource
    private OrgUM orgUM;
    @Resource
    private TUsersService tUsersService;

    @Override
    public List<CascadeTreeNode> getOrgTreeList() {
        return CACHE.get("orgTreeList");
    }

    @Override
    public void flushCache() {
        List<CascadeTreeNode> list = getList(null);
        List<CascadeTreeNode> resultList = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)){
            resultList = addChild(list);
        }
        CACHE.put("orgTreeList", resultList);
    }

    private List<CascadeTreeNode> addChild(List<CascadeTreeNode> parentList){
        for (CascadeTreeNode cascadeTreeNode : parentList) {
            List<CascadeTreeNode> childList = getChildList(cascadeTreeNode.getId());
            if(CollectionUtils.isNotEmpty(childList)) {
                cascadeTreeNode.setChildren(childList);
            }
            addChild(childList);
        }
        return parentList;
    }

    @Override
    public Gzaiccode saveGzaiccode(Gzaiccode gzaiccode) {
        save(gzaiccode);
        return gzaiccode;
    }

    @Override
    public void updateGzaiccode(Gzaiccode gzaiccode) {
        updateById(gzaiccode);
    }

    @Override
    public IPage<Gzaiccode> listGzaiccode(BaseQuery<Gzaiccode> query) {
        Gzaiccode entity = query.getEntity(Gzaiccode.class);
        Page<Gzaiccode> page = query.getPage();

        LambdaQueryWrapper<Gzaiccode> lambdaQueryWrapper = Wrappers.lambdaQuery();

        return page(page, lambdaQueryWrapper);
    }

    @Override
    public List<AICUser> byPermissionListHandleUserList(String pripid, String permission, String decorg, String decorgCn) {
        List<AICUser> userList = new ArrayList<>();
        EBaseinfo eBaseinfo = eBaseinfoService.getById(pripid);
        String acceptRegorg = eBaseinfo.getAcceptRegorg() == null?"":eBaseinfo.getAcceptRegorg();   //受理机关
        String regulatory = eBaseinfo.getSuporg();  //属地监管所
        String unitCode = decorg.substring(0, 6);
        if (unitCode != null) {
            //谁列入谁移出
            AICOrg orgByUnitCode = orgUM.findOrgByRegion(unitCode);
            if(orgByUnitCode != null && StringUtils.isNotBlank(orgByUnitCode.getPrimaryKey())){
                List<AICUser> unitCodeUserList = orgUM.findUsersByOrgAndPermission(orgByUnitCode.getPrimaryKey(), permission);
                if(unitCodeUserList != null)
                    userList.addAll(unitCodeUserList);
            }
            //属地监管所有经办权限人员
            if(StringUtils.isNotBlank(regulatory)) {
                AICOrg orgByRegulatory = orgUM.findOrgByRegion(regulatory);
                if (orgByRegulatory != null && StringUtils.isNotBlank(orgByRegulatory.getPrimaryKey())) {
                    List<AICUser> regulatoryUserList = orgUM.findUsersByOrgAndPermission(orgByRegulatory.getPrimaryKey(), permission);
                    if (regulatoryUserList != null) userList.addAll(regulatoryUserList);
                }
            }
        }
        if(userList != null)
            userList.forEach(each -> each.setPassword(null));
        return userList == null ? new ArrayList<>() : userList;
    }

    @Override
    public String getNameByCode(String code) {
        if(StringUtils.isNoneEmpty(code)) {
            AICOrg aicOrg = orgUM.findOrgByRegion(code);
            if (aicOrg != null) {
                return aicOrg.getName();
            }
        }
        return null;
    }

    @Override
    public List<CascadeTreeNode> getList(String slevel) {
        return baseMapper.getNodeList(slevel,null);
    }

    @Override
    public List<CascadeTreeNode> getChildList(String parentid) {
        return baseMapper.getNodeList(null,parentid);
    }

    @Override
    public List<CascadeTreeNode> getNodeByOrg(String org) {
        return baseMapper.getNodeByOrg(org);
    }

    @Override
    public List<CascadeTreeNode> getChildStationList(String parentid) {
        return baseMapper.getChildStationList(null,parentid);
    }
}
