/*
 * Copyright (c) 2024 codvision.com All Rights Reserved.
 */

package com.codvision.basic.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codvision.auth.entity.vo.OrgVO;
import com.codvision.auth.mapper.OrgMapper;
import com.codvision.authcore.bean.SysUserBean;
import com.codvision.authcore.util.AuthUtil;
import com.codvision.basic.entity.SocialOrg;
import com.codvision.basic.entity.vo.SocialOrgVO;
import com.codvision.basic.mapper.SocialOrgMapper;
import com.codvision.basic.service.SocialOrgService;
import com.codvision.commoncore.exception.BusinessException;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.util.QueryPageUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author KX
 */
@Service
@RequiredArgsConstructor
public class SocialOrgServiceImpl extends ServiceImpl<SocialOrgMapper, SocialOrg> implements SocialOrgService {

    private final OrgMapper orgMapper;

    @Override
    @SuppressWarnings("all")
    public IPage<SocialOrgVO> getPageResult(QueryPage<Map<String, Object>> queryPage) {
        SysUserBean currentUser = AuthUtil.getCurrentUser();
        Map<String, Object> paraMap = QueryPageUtil.queryMap(queryPage);
        if (!paraMap.containsKey("oid") || paraMap.get("oid") == null) {
            paraMap.put("oid", currentUser.getActiveOid());
        }
        return getBaseMapper().getPageResult(queryPage.getPage(), paraMap);
    }

    @Override
    public void addSocialOrg(SocialOrg socialOrg) {
        if (getBaseMapper().countEnterpriseByCreditCode(socialOrg) > 0) {
            throw new BusinessException("该统一社会信用代码已存在，请勿重复录入！");
        }
        updateDistrict(socialOrg);
        getBaseMapper().insert(socialOrg);
    }

    @Override
    public void updateSocialOrg(SocialOrg socialOrg) {
        if (getBaseMapper().countEnterpriseByCreditCode(socialOrg) > 0) {
            throw new BusinessException("该统一社会信用代码已存在，请勿重复录入！");
        }
        updateDistrict(socialOrg);
        getBaseMapper().updateById(socialOrg);
    }

    /**
     * 根据所选组织查询行政区划编码，更新行政地区信息
     *
     * @param socialOrg 社会组织信息
     */
    public void updateDistrict(SocialOrg socialOrg) {
        OrgVO orgVO = orgMapper.getByOid(socialOrg.getOid());
        if (orgVO == null || StringUtils.isEmpty(orgVO.getDistrictCode())) {
            return;
        }
        String[] split = orgVO.getPath().split("/");
        if (orgVO.getDistrictCode().length() == 15) {
            // 网格级别
            socialOrg.setGridCode(orgVO.getDistrictCode());
            for (int i = split.length - 1; i >= 0; i--) {
                if (i == split.length - 1) {
                    socialOrg.setGrid(split[i]);
                } else if (i == split.length - 2) {
                    socialOrg.setCommunity(split[i]);
                } else if (i == split.length - 3) {
                    socialOrg.setTown(split[i]);
                } else if (i == split.length - 4) {
                    socialOrg.setCounty(split[i]);
                } else if (i == split.length - 5) {
                    socialOrg.setCity(split[i]);
                }
            }
        } else if (orgVO.getDistrictCode().length() == 12) {
            // 社区
            for (int i = split.length - 1; i >= 0; i--) {
                if (i == split.length - 1) {
                    socialOrg.setCommunity(split[i]);
                } else if (i == split.length - 2) {
                    socialOrg.setTown(split[i]);
                } else if (i == split.length - 3) {
                    socialOrg.setCounty(split[i]);
                } else if (i == split.length - 4) {
                    socialOrg.setCity(split[i]);
                }
            }
        } else if (orgVO.getDistrictCode().length() == 9) {
            // 镇街
            for (int i = split.length - 1; i >= 0; i--) {
                if (i == split.length - 1) {
                    socialOrg.setTown(split[i]);
                } else if (i == split.length - 2) {
                    socialOrg.setCounty(split[i]);
                } else if (i == split.length - 3) {
                    socialOrg.setCity(split[i]);
                }
            }
        } else if (orgVO.getDistrictCode().length() == 6) {
            // 区县
            for (int i = split.length - 1; i >= 0; i--) {
                if (i == split.length - 1) {
                    socialOrg.setCounty(split[i]);
                } else if (i == split.length - 2) {
                    socialOrg.setCity(split[i]);
                }
            }
        }
    }


}
