package com.ningdatech.pmapi.organization.manage;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.pmapi.organization.entity.GovBusinessStrip;
import com.ningdatech.pmapi.organization.model.entity.ProvincialGovBusinessStrip;
import com.ningdatech.pmapi.organization.model.vo.GovBusinessStripTreeVO;
import com.ningdatech.pmapi.organization.model.vo.ProvincialGovBusinessStripVO;
import com.ningdatech.pmapi.organization.service.IGovBusinessStripService;
import com.ningdatech.pmapi.organization.service.IProvincialGovBusinessStripService;
import com.ningdatech.pmapi.provincial.service.IJoinReviewProvincialBureauService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

/**
 * @author liuxinxin
 * @date 2023/3/14 下午4:48
 */

@Component
@RequiredArgsConstructor
public class GovBusinessStripManage {

    private final IGovBusinessStripService iGovBusinessStripService;

    private final IProvincialGovBusinessStripService provincialGovBusinessStripService;

    private final IJoinReviewProvincialBureauService joinReviewProvincialBureauService;

    @Value("${irs.province-gov.can-search}")
    private Boolean canSearch;

    public List<GovBusinessStripTreeVO> getChildOrganizationList(String parentCode) {
        if (Objects.isNull(parentCode)) {
            parentCode = "-1";
        }
        List<GovBusinessStrip> govBusinessStripList = iGovBusinessStripService.list(Wrappers.lambdaQuery(GovBusinessStrip.class)
                .eq(GovBusinessStrip::getParentCode, parentCode));

        return govBusinessStripList.stream().map(r -> {
            GovBusinessStripTreeVO govBusinessStripTreeVO = new GovBusinessStripTreeVO();
            govBusinessStripTreeVO.setBusinessStripCode(r.getBusinessStripCode());
            govBusinessStripTreeVO.setBusinessStripName(r.getBusinessStripName());
            govBusinessStripTreeVO.setParentCode(r.getParentCode());
            govBusinessStripTreeVO.setParentName(r.getParentName());
            return govBusinessStripTreeVO;
        }).collect(Collectors.toList());
    }

    public List<ProvincialGovBusinessStripVO> getProvincialGovBusinessStripList(String businessStripName) {
        if(Objects.nonNull(canSearch) && canSearch){
            List<ProvincialGovBusinessStripVO> vos = joinReviewProvincialBureauService.searchGovUnits();
            return vos.stream()
                    .filter(b -> StringUtils.isBlank(businessStripName) || b.getBusinessStripName().contains(businessStripName))
                    .collect(Collectors.toList());
        }

        List<ProvincialGovBusinessStrip> strips = provincialGovBusinessStripService.list(Wrappers.lambdaQuery(ProvincialGovBusinessStrip.class)
                .orderByAsc(ProvincialGovBusinessStrip::getBusinessStripName));

        return strips.stream()
                .filter(b -> StringUtils.isBlank(businessStripName) || b.getBusinessStripName().contains(businessStripName))
                .map(p -> BeanUtil.copyProperties(p,ProvincialGovBusinessStripVO.class))
                .collect(Collectors.toList());
    }
}
