package com.dayou.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.dayou.bo.MenuTreeBO;
import com.dayou.entity.Districts;
import com.dayou.mapper.DistrictsMapper;
import com.dayou.service.IDistrictsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.utils.ConvertUtil;
import com.dayou.utils.TreeUtil;
import com.dayou.vo.DistrictsTreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 地区 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-01-10
 */
@Service
public class DistrictsServiceImpl extends ServiceImpl<DistrictsMapper, Districts> implements IDistrictsService {


    @Override
    @SuppressWarnings("unchecked")
    public Page<Districts> selectPage(Page page,Districts districts){
        return this.page(page, new QueryWrapper<Districts>(districts));
    }


    @Override
    public Districts detail(Long id){
        return this.getById(id);
    }

    @Override
    public List<DistrictsTreeVO> getDistrictsTree() {
        List<DistrictsTreeVO> results = null;
        List<Districts> list = this.list();
        if (CollectionUtils.isNotEmpty(list)) {
            results = ConvertUtil.copyList(list, x -> {
                DistrictsTreeVO districtsTreeVO = new DistrictsTreeVO();
                districtsTreeVO.setValue(x.getId());
                districtsTreeVO.setLabel(x.getExtName());
                districtsTreeVO.setParentId(x.getPid());
                districtsTreeVO.setOrder(x.getWeight());
                return districtsTreeVO;
            });

            results = (List<DistrictsTreeVO>) TreeUtil.buildTree(results);
        }
        return results;
    }

    @Override
    public String getDistIds(String city, String county) {
        Districts countyDis = null;
        Districts cityDis = null;
        if (StrUtil.isNotBlank(county)){
            countyDis = this.getOne(new LambdaQueryWrapper<Districts>().eq(Districts::getDeep, 2).eq(Districts::getExtName, county));
        }
        if (StrUtil.isNotBlank(city)){
            cityDis = this.getOne(new LambdaQueryWrapper<Districts>().eq(Districts::getDeep, 1).eq(Districts::getExtName, city));
        }

        if (countyDis!=null && cityDis!=null){
            Long countyId = countyDis.getId();
            Long cityId = countyDis.getPid();
            Long xCityId = cityDis.getId();
            Long pId = cityDis.getPid();

            if (cityId.equals(xCityId)){
                return JSON.toJSONString(Arrays.asList(pId, cityId, countyId));
            }
        }
        return null;
    }

}
