package com.sptg.permissions.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.constant.SymbolConstant;
import com.sptg.permissions.api.dto.RegionAuthorizeDto;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.entity.Region;
import com.sptg.permissions.api.entity.RegionAuthorize;
import com.sptg.permissions.biz.mapper.RegionAuthorizeMapper;
import com.sptg.permissions.biz.service.RegionAuthorizeServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * title: RegionAuthorizeServiceImpl
 * package: com.sptg.permissions.biz.security.impl
 * description: 区域授权-服务层实现类
 *
 *                  
 *                 
 */
@Service
@RequiredArgsConstructor
public class RegionAuthorizeServiceImpl extends ServiceImpl<RegionAuthorizeMapper, RegionAuthorize> implements RegionAuthorizeServiceInterface {

    @Override
    public void deleteByOrgPositionOrResidentialId(String position, Long residentialId) {
        Wrapper<RegionAuthorize> wrapper = new EntityWrapper<>();
        wrapper.like("s_org_position",position, SqlLike.RIGHT);
        wrapper.or().eq("l_xq_id",residentialId);
        this.delete(wrapper);
    }

    @Override
    public List<Region> selectAuthorizeRegionList(String position) {
        List<Region> regionList = this.baseMapper.selectAuthorizeRegionList(position);
        if (null == regionList){
            regionList = new ArrayList<>(0);
        }
        return regionList;
    }

    @Override
    public List<Region> selectAuthorizeRegion(RegionAuthorizeDto dto) {
        List<Region> regionList = this.baseMapper.selectAuthorizeRegion(dto);
        if (null == regionList){
            regionList = new ArrayList<>(0);
        }
        return regionList;
    }

    @Override
    public Page<RegionAuthorize> pagingQuery(RegionAuthorizeDto dto,Page<RegionAuthorize> page) {
        //  获取需要过滤的小区Id
        Wrapper<RegionAuthorize> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect("l_xq_id");
        String position = dto.getUserOrgPosition() + SymbolConstant.UNDERLINE + SymbolConstant.PER_CENT;
        wrapper.like("s_org_position",position ,SqlLike.CUSTOM);
        Optional.ofNullable(dto.getAreaIdList())
                .ifPresent(bean -> {
                    if (dto.getAreaIdList().size() > 0){
                        String sql = "(s_region_province IN ({0}) OR s_region_city IN ({0}) OR s_region_area IN ({0}))";
                        StringBuilder builder = new StringBuilder();
                        dto.getAreaIdList().forEach(id -> builder.append(id).append(","));
                        wrapper.and(sql,builder.toString());
                    }
                });
        List<Object> excludeList = this.selectObjs(wrapper);
        List<RegionAuthorize> regionAuthorizeList = this.baseMapper.selectAuthorizeList(dto,excludeList,page);
        //  判断是否需要获取授权状态
        if (null == dto.getIsAuthorized()){
            regionAuthorizeList.forEach(authorize -> {
                if (authorize.getOrgPosition().equals(dto.getOrgPosition())){
                    authorize.setIsAuthorized(OperationConstant.ONE);
                }else {
                    authorize.setIsAuthorized(OperationConstant.TWO);
                }
            });
        }
        page.setRecords(regionAuthorizeList);
        return page;
    }

    @Override
    public List<Organization> selectAuthorizeOrgList(RegionAuthorizeDto dto) {
        if (null == dto.getPid()) {
            dto.setGroupBy(" o.l_pid ");
        }else {
            dto.setGroupBy(" o.l_id ");
        }
        List<Organization> orgList = this.baseMapper.selectAuthorizeOrgList(dto);
        if (null == orgList){
            orgList = new ArrayList<>(0);
        }
        return orgList;
    }

    @Override
    public List<Organization> selectAuthorizeOrgListTwo(RegionAuthorizeDto dto) {
        dto.setGroupBy(" o.l_id ");
        List<Organization> orgList = this.baseMapper.selectAuthorizeOrgListTwo(dto);
        if (null == orgList){
            orgList = new ArrayList<>(0);
        }
        return orgList;
    }

    @Override
    public Page<Organization> pagingQueryAuthorizeOrg(RegionAuthorizeDto dto,Page<RegionAuthorize> page) {
        dto.setGroupBy(" da.l_xq_id ");
        List<Organization> orgList = this.baseMapper.selectAuthorizeOrgList(dto, page);
        if (null == orgList){
            orgList = new ArrayList<>();
        }
        Page<Organization> orgPage = new Page<>(page.getCurrent(),page.getSize());
        orgPage.setTotal(page.getTotal());
        orgPage.setRecords(orgList);
        return orgPage;
    }

    @Override
    public List<RegionAuthorize> selectListByXqId(Long xqId) {
        Wrapper<RegionAuthorize> wrapper = new EntityWrapper<>();
        wrapper.eq("l_xq_id",xqId);
        return this.selectList(wrapper);
    }
}
