package com.jpsoft.shinestar.modules.base.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jpsoft.shinestar.modules.base.dao.RegionInfoDAO;
import com.jpsoft.shinestar.modules.base.dto.RegionInfoDTO;
import com.jpsoft.shinestar.modules.base.entity.DistrictInfo;
import com.jpsoft.shinestar.modules.base.entity.RegionInfo;
import com.jpsoft.shinestar.modules.base.service.DistrictInfoService;
import com.jpsoft.shinestar.modules.base.service.RegionInfoService;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 墨鱼_mo
 * @date 2020-10-12 16:18
 */
@Transactional
@Component(value="regionInfoService")
public class RegionInfoServiceImpl implements RegionInfoService {

    @Resource(name="regionInfoDAO")
    private RegionInfoDAO regionInfoDAO;

    @Autowired
    private DistrictInfoService districtInfoService;

    @Override
    public RegionInfo get(String id) {
        // TODO Auto-generated method stub
        return regionInfoDAO.get(id);
    }

    @Override
    public int insert(RegionInfo model) {
        // TODO Auto-generated method stub
        //model.setId(UUID.randomUUID().toString());

        return regionInfoDAO.insert(model);
    }

    @Override
    public int update(RegionInfo model) {
        // TODO Auto-generated method stub
        return regionInfoDAO.update(model);
    }

    @Override
    public int delete(String id) {
        // TODO Auto-generated method stub
        return regionInfoDAO.delete(id);
    }

    @Override
    public boolean exist(String id) {
        // TODO Auto-generated method stub
        int count = regionInfoDAO.exist(id);

        return count > 0 ? true : false;
    }

    @Override
    public List<RegionInfo> list() {
        // TODO Auto-generated method stub
        return regionInfoDAO.list();
    }

    @Override
    public Page<RegionInfo> pageSearch(Map<String, Object> searchParams, int pageNumber, int pageSize, boolean count, List<Sort> sortList) {
        Page<RegionInfo> page = PageHelper.startPage(pageNumber,pageSize,count).doSelectPage(()->{
            regionInfoDAO.search(searchParams,sortList);
        });

        return page;
    }

    @Override
    public List<RegionInfo> findByParentId(String parentId) {
        return regionInfoDAO.findByParentId(parentId);
    }

    @Override
    public List<RegionInfo> findTopRegion() {
        return regionInfoDAO.findTopRegion();
    }

    @Override
    public List<RegionInfoDTO> findByType(String type) {

        List<RegionInfoDTO> list = new ArrayList<>();
        if ("1".equals(type)) {
            List<RegionInfo> regionInfoList0 = findByParentId("1");
            if (regionInfoList0.size() > 0) {
                for (RegionInfo regionInfo : regionInfoList0) {
                    RegionInfoDTO regionInfoDTO = new RegionInfoDTO();
                    regionInfoDTO.setValue(regionInfo.getId());
                    regionInfoDTO.setText(regionInfo.getName());
                    List<RegionInfoDTO> regionInfoDTOList = new ArrayList<>();
                    List<RegionInfo> regionInfoList = findByParentId(regionInfo.getId());
                    if (regionInfoList.size() > 0) {
                        for (RegionInfo regionInfo1 : regionInfoList) {
                            RegionInfoDTO regionInfoDTO1 = new RegionInfoDTO();
                            regionInfoDTO1.setValue(regionInfo1.getId());
                            regionInfoDTO1.setText(regionInfo1.getName());
                            List<RegionInfoDTO> regionInfoDTOList1 = new ArrayList<>();
                            List<RegionInfo> regionInfoList1 = findByParentId(regionInfo1.getId());
                            if (regionInfoList1.size() > 0) {
                                for (RegionInfo regionInfo2 : regionInfoList1) {
                                    RegionInfoDTO regionInfoDTO2 = new RegionInfoDTO();
                                    regionInfoDTO2.setValue(regionInfo2.getId());
                                    regionInfoDTO2.setText(regionInfo2.getName());
                                    List<RegionInfoDTO> regionInfoDTOList2 = new ArrayList<>();
                                    List<RegionInfo> regionInfoList2 = findByParentId(regionInfo.getId());

                                    regionInfoDTO2.setChildren(regionInfoDTOList2);
                                    regionInfoDTOList1.add(regionInfoDTO2);
                                }

                            }
                            regionInfoDTO1.setChildren(regionInfoDTOList1);
                            regionInfoDTOList.add(regionInfoDTO1);
                        }
                    }
                    regionInfoDTO.setChildren(regionInfoDTOList);
                    list.add(regionInfoDTO);

                }
            }

        }

        if ("2".equals(type)) {
            List<DistrictInfo> districtInfoList0 = districtInfoService.findTopRegion();
            if (districtInfoList0.size() > 0) {
                for (DistrictInfo districtInfo : districtInfoList0) {
                    RegionInfoDTO regionInfoDTO = new RegionInfoDTO();
                    regionInfoDTO.setValue(districtInfo.getId());
                    regionInfoDTO.setText(districtInfo.getName());
                    List<RegionInfoDTO> regionInfoDTOList = new ArrayList<>();
                    List<DistrictInfo> districtInfoList = districtInfoService.findByParentId(districtInfo.getId());
                    if (districtInfoList.size() > 0) {
                        for (DistrictInfo districtInfo1 : districtInfoList) {
                            RegionInfoDTO regionInfoDTO1 = new RegionInfoDTO();
                            regionInfoDTO1.setValue(districtInfo1.getId());
                            regionInfoDTO1.setText(districtInfo1.getName());
                            List<RegionInfoDTO> regionInfoDTOList1 = new ArrayList<>();
                            List<DistrictInfo> districtInfoList1 = districtInfoService.findByParentId(districtInfo1.getId());
                            if (districtInfoList1.size() > 0) {
                                for (DistrictInfo districtInfo2 : districtInfoList1) {
                                    RegionInfoDTO regionInfoDTO2 = new RegionInfoDTO();
                                    regionInfoDTO2.setValue(districtInfo2.getId());
                                    regionInfoDTO2.setText(districtInfo2.getName());
                                    List<RegionInfoDTO> regionInfoDTOList2 = new ArrayList<>();
                                    regionInfoDTO2.setChildren(regionInfoDTOList2);
                                    regionInfoDTOList1.add(regionInfoDTO2);
                                }

                            }
                            regionInfoDTO1.setChildren(regionInfoDTOList1);
                            regionInfoDTOList.add(regionInfoDTO1);
                        }
                    }
                    regionInfoDTO.setChildren(regionInfoDTOList);
                    list.add(regionInfoDTO);

                }
            }

        }

        return list;
    }

    @Override
    public RegionInfo findByName(String name){
        return regionInfoDAO.findByName(name);
    }
}
