package com.ningdatech.pmapi.organization.manage;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.pmapi.common.config.DingOrganizationProperties;
import com.ningdatech.pmapi.common.util.StrUtils;
import com.ningdatech.pmapi.organization.model.entity.DingOrganization;
import com.ningdatech.pmapi.organization.model.po.ReqOrganizationListPO;
import com.ningdatech.pmapi.organization.model.vo.OrganizationTreeVO;
import com.ningdatech.pmapi.organization.model.vo.ResOrganizationListVO;
import com.ningdatech.pmapi.organization.service.IDingOrganizationService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liuxinxin
 * @date 2023/1/13 下午2:55
 */
@Component
@RequiredArgsConstructor
public class OrganizationManage {

    private final IDingOrganizationService iDingOrganizationService;

    private final DingOrganizationProperties dingOrganizationProperties;

    public PageVo<ResOrganizationListVO> organizationList(ReqOrganizationListPO reqOrganizationListPO) {
        String orgName = reqOrganizationListPO.getOrgName();

        Page<DingOrganization> page = iDingOrganizationService
                .page(new Page<>(reqOrganizationListPO.getPageNumber(), reqOrganizationListPO.getPageSize())
                        , Wrappers.lambdaQuery(DingOrganization.class)
                                .like(StringUtils.isNotBlank(orgName), DingOrganization::getOrganizationName, orgName));

        long total = page.getTotal();
        List<DingOrganization> records = page.getRecords();

        List<ResOrganizationListVO> resVOList = new ArrayList<>();
        if (CollUtil.isNotEmpty(records)) {
            resVOList = records.stream().map(r -> {
                ResOrganizationListVO resOrganizationListVO = new ResOrganizationListVO();
                resOrganizationListVO.setOrgCode(r.getOrganizationCode());
                resOrganizationListVO.setOrgName(r.getOrganizationName());
                // TODO 补充其他字段
                return resOrganizationListVO;
            }).collect(Collectors.toList());
        }

        PageVo<ResOrganizationListVO> pageVo = new PageVo<>();
        pageVo.setTotal(total);
        pageVo.setRecords(resVOList);
        return pageVo;
    }

    public List<OrganizationTreeVO> getChildOrganizationList(String parentCode) {
        List<String> parentCodeList = new ArrayList<>();
        List<DingOrganization> dingOrganizationList = new ArrayList<>();
        if (StrUtils.isBlank(parentCode)) {
            parentCodeList = dingOrganizationProperties.getDeptVisibleScopes();
            dingOrganizationList = iDingOrganizationService.list(Wrappers.lambdaQuery(DingOrganization.class)
                    .in(DingOrganization::getOrganizationCode, parentCodeList));
        } else {
            parentCodeList = CollectionUtil.toList(parentCode);
            dingOrganizationList = iDingOrganizationService
                    .list(Wrappers.lambdaQuery(DingOrganization.class).in(DingOrganization::getParentCode, parentCodeList));
        }

        if (CollectionUtils.isEmpty(dingOrganizationList)) {
            return new ArrayList<>();
        }

        dingOrganizationList.sort(Comparator.comparing(DingOrganization::getDisplayOrder));
        return dingOrganizationList.stream().map(r -> {
            OrganizationTreeVO treeVO = new OrganizationTreeVO();
            treeVO.setId(r.getId());
            treeVO.setName(r.getOrganizationName());
//            Boolean enabled = "1".equals(r.get());
//            treeVO.setEnabled(enabled);
            treeVO.setOrganizationCode(r.getOrganizationCode());
            treeVO.setParentCode(r.getParentCode());
//            treeVO.setSubCount(r.get());
//            Boolean isLeaf = true;
//            Long subCount = r.getSubCount();
//            if (Objects.nonNull(subCount) && subCount > 0) {
//                isLeaf = false;
//            }
//            treeVO.setIsLeaf(isLeaf);
//            treeVO.setDeptSort(r.getDeptSort());
//            treeVO.setUnionCode(r.getName() + "##" + r.getOrganizationCode());
            return treeVO;
        }).collect(Collectors.toList());
    }

}
