package com.inkasso.factoring.org.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.inkasso.adapter.tree.TreeUtils;
import com.inkasso.adapter.util.BeanUtilsProxy;
import com.inkasso.factoring.org.dao.OrgDAO;
import com.inkasso.factoring.org.entity.OrgDO;
import com.inkasso.factoring.org.model.dto.OrgDTO;
import com.inkasso.factoring.org.service.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author liutao
* @date  2023-09-07
*/
@Service
public class OrgServiceImpl extends ServiceImpl<OrgDAO, OrgDO> implements OrgService {

   @Autowired
    private TreeUtils treeUtils;

    @Override
    public List<OrgDTO> getProvince() {
        List<OrgDO> list = lambdaQuery().eq(OrgDO::getParentId, 1).list();
        List<OrgDTO> orgDTOS = BeanUtilsProxy.copyPropertiesList(list, OrgDTO.class);
        orgDTOS.forEach(r->{
            if(r.getName().endsWith("市")){
                r.setEnd(true);
            }
            r.setLevel(1);
        });
        return orgDTOS;
    }

    @Override
    public List<OrgDTO> getCityByParentId(Long parentId) {
        List<OrgDO> list = lambdaQuery().eq(OrgDO::getParentId, parentId).list();
        List<OrgDTO> orgDTOS = BeanUtilsProxy.copyPropertiesList(list, OrgDTO.class);
        orgDTOS.forEach(r->r.setLevel(2));
        return orgDTOS;
    }

    @Override
    public List<OrgDTO> getAll() {
        List<OrgDO> list = lambdaQuery().eq(OrgDO::getParentId, 1).list();
        List<OrgDTO> orgDTOS = BeanUtilsProxy.copyPropertiesList(list, OrgDTO.class);

        List returnList = treeUtils.buidTree(orgDTOS, 1L);
        return returnList;
    }
}

