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

import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.mortals.framework.ap.GlobalSysInfo;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.service.impl.AbstractCRUDCacheServiceImpl;
import com.mortals.framework.util.StringUtils;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.code.AreaLevelEnum;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.utils.AreaMatchCodeUtil;
import com.mortals.xhx.feign.user.IUserFeign;
import com.mortals.xhx.module.area.model.AreaEntity;
import com.mortals.xhx.module.area.model.AreaQuery;
import com.mortals.xhx.module.area.service.AreaService;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.matter.service.MatterService;
import com.mortals.xhx.module.site.dao.SiteDao;
import com.mortals.xhx.module.site.model.SiteEntity;
import com.mortals.xhx.module.site.model.SiteQuery;
import com.mortals.xhx.module.site.model.SiteTreeSelect;
import com.mortals.xhx.module.site.model.vo.SiteAreaVo;
import com.mortals.xhx.module.site.service.SiteMatterService;
import com.mortals.xhx.module.site.service.SiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.*;
import java.util.stream.Collectors;

import static com.mortals.xhx.common.key.Constant.PARAM_SERVER_HTTP_URL;
import static com.mortals.xhx.common.key.Constant.USER_SITE_TREE;

/**
 * SiteService
 * 站点 service实现
 *
 * @author zxfei
 * @date 2022-01-12
 */
@Service("siteService")
@Slf4j
public class SiteServiceImpl extends AbstractCRUDCacheServiceImpl<SiteDao, SiteEntity, Long> implements SiteService {
    // private List<SiteTreeSelect> siteTreeList;
    /**
     * 根据用户id 暂存对应站点树 默认0为全站点树
     */
    private Map<Long, List<SiteTreeSelect>> siteTreeMap = new HashMap<>();
    @Autowired
    @Lazy
    private AreaService areaService;

    @Autowired
    @Lazy
    private IUserFeign userFeign;
    @Autowired
    @Lazy
    private UserService userService;
    @Autowired
    private DeptService deptService;

    @Autowired
    private MatterService matterService;

    @Autowired
    private SiteMatterService siteMatterService;



    private volatile Boolean refresh = false;

    @Override
    protected String getExtKey(SiteEntity data) {
        return data.getSiteCode();
    }

    @Override
    protected void updateBefore(SiteEntity entity, Context context) throws AppException {
        super.updateBefore(entity, context);
    }


    @Override
    protected void saveBefore(SiteEntity entity, Context context) throws AppException {
       super.saveBefore(entity, context);
    }


    @Override
    protected void saveAfter(SiteEntity entity, Context context) throws AppException {
        super.saveAfter(entity, context);

    }


    @Override
    protected void updateAfter(SiteEntity entity, Context context) throws AppException {
    }


    public static void main(String[] args) {

        String matchCode = "511530000000".replaceAll("(0)+$", "");
        System.out.println(matchCode);

        matchCode = StrUtil.padAfter(matchCode, 7, "0");

        System.out.println(matchCode);

    }


}