package com.hz.pm.api.organization.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hz.pm.api.organization.entity.GovBusinessStrip;
import com.hz.pm.api.organization.model.entity.ProvincialGovBusinessStrip;
import com.hz.pm.api.organization.model.vo.GovBusinessStripTreeVO;
import com.hz.pm.api.organization.model.vo.ProvincialGovBusinessStripVO;
import com.hz.pm.api.organization.service.IGovBusinessStripService;
import com.hz.pm.api.organization.service.IProvincialGovBusinessStripService;
import com.hz.pm.api.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.Collections;
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) {
        List<ProvincialGovBusinessStrip> strips = provincialGovBusinessStripService.list(Wrappers.lambdaQuery(ProvincialGovBusinessStrip.class)
                .like(StringUtils.isNotBlank(businessStripName),ProvincialGovBusinessStrip::getBusinessStripName,businessStripName)
                .orderByAsc(ProvincialGovBusinessStrip::getBusinessStripName));

        if(CollUtil.isEmpty(strips)){
            return Collections.emptyList();
        }

        return strips.stream()
                .map(p -> BeanUtil.copyProperties(p,ProvincialGovBusinessStripVO.class))
                .collect(Collectors.toList());
    }
}
