package com.mortals.xhx.module.base.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.mortals.framework.common.Rest;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.common.utils.AreaMatchCodeUtil;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.area.model.AreaEntity;
import com.mortals.xhx.module.area.service.AreaService;
import com.mortals.xhx.module.base.dao.BaseAreaDao;
import com.mortals.xhx.module.base.model.BaseAreaEntity;
import com.mortals.xhx.module.base.model.BaseAreaQuery;
import com.mortals.xhx.module.base.service.BaseAreaService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * BaseAreaService
 * 区域 service实现
 *
 * @author zxfei
 * @date 2022-11-22
 */
@Service("baseAreaService")
public class BaseAreaServiceImpl extends AbstractCRUDServiceImpl<BaseAreaDao, BaseAreaEntity, Long> implements BaseAreaService {

    @Autowired
    private AreaService areaService;
    @Override
    public Rest<String> genSubAreaByRootName(BaseAreaEntity baseAreaEntity, Context context) {

        String areaCode = baseAreaEntity.getAreaCode();
        String matchCode = areaCode.replaceAll("(0)+$", "");
        matchCode = AreaMatchCodeUtil.getMatchCode(matchCode, baseAreaEntity.getAreaLevel());
        BaseAreaQuery baseAreaQuery = new BaseAreaQuery();
        baseAreaQuery.setAreaCode(matchCode + "%");
        List<BaseAreaEntity> baseAreaEntityList =this.find(baseAreaQuery);

        //清除子区域数据
        Rest<String> rest = areaService.removeAll();
        List<AreaEntity> collect = baseAreaEntityList.stream().map(item -> {
            AreaEntity areaEntity = new AreaEntity();
            areaEntity.initAttrValue();
            BeanUtils.copyProperties(item, areaEntity,BeanUtil.getNullPropertyNames(item));
            if(item.getAreaCode().equalsIgnoreCase(areaCode)){
                areaEntity.setPid("0");
            }
            return areaEntity;
        }).collect(Collectors.toList());
        log.info("更新数据条数:"+collect.size());
        List<List<AreaEntity>> partition = ListUtil.partition(collect, 100);
        for (List<AreaEntity> areaEntities : partition) {
            areaService.save(areaEntities, context);
        }
        areaService.loadCache();
        return Rest.ok("初始化区域数据成功！");
    }

    /**
     * @param baseAreaEntity
     * @param context
     * @return
     */
    @Override
    public Rest<List<String>> getSubAreaByRootName(BaseAreaEntity baseAreaEntity, Context context) {

        String areaCode = baseAreaEntity.getAreaCode();
        String matchCode = areaCode.replaceAll("(0)+$", "");
        matchCode = AreaMatchCodeUtil.getMatchCode(matchCode, baseAreaEntity.getAreaLevel());
        BaseAreaQuery baseAreaQuery = new BaseAreaQuery();
        baseAreaQuery.setAreaCode(matchCode + "%");
        List<BaseAreaEntity> baseAreaEntityList =this.find(baseAreaQuery);
        List<String> areaCodeList = baseAreaEntityList.stream().map(i -> i.getAreaCode()).collect(Collectors.toList());


        return Rest.ok(areaCodeList);
    }

}