package com.xhpc.system.service.impl;

import com.xhpc.common.core.utils.StringUtils;
import com.xhpc.system.api.domain.SysUser;
import com.xhpc.system.mapper.SysUserMapper;
import com.xhpc.system.mapper.XhpcUserPrivilegeMapper;
import com.xhpc.system.service.IXhpcUserPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据权限 服务层
 *
 * @author ruoyi
 */
@Service
public class XhpcUserPrivilegeServiceImpl implements IXhpcUserPrivilegeService {

    @Autowired
    private XhpcUserPrivilegeMapper XhpcUserPrivilege;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 新增数据权限
     *
     * @param xhpcUserPrivilege 流量用户设置流量方黑名单信息
     */
    @Override
    public int insert(com.xhpc.system.domain.XhpcUserPrivilege xhpcUserPrivilege) {

        return XhpcUserPrivilege.insert(xhpcUserPrivilege);
    }

    /**
     * 通过用户ID删除用户数据权限
     *
     * @param userId 需要删除的数据ID
     * @return 结果
     */
    @Override
    public void deleteByIds(Long userId) {
        XhpcUserPrivilege.delete(userId);
    }

    /**
     * 用户拥有数据权限
     *
     * @param userId 用户id
     * @return 结果
     */
    @Override
    public List<Long> dataCheckedKeys(Long userId) {
        return XhpcUserPrivilege.dataCheckedKeys(userId);
    }

    /**
     * 用户拥有数据权限
     *
     * @param userId 用户id
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> dataList(Long userId) {
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        List<Map<String, Object>> listOperator = new ArrayList<>();
        if (0 == sysUser.getDataPowerType()) {
            listOperator = XhpcUserPrivilege.getOperatorId(null);

        } else if (1 == sysUser.getDataPowerType()) {
            String[] strings = new String[1];
            strings[0] = StringUtils.valueOf(sysUser.getOperatorId());
            listOperator = XhpcUserPrivilege.getOperatorId(strings);
        } else {
            listOperator = XhpcUserPrivilege.getOperatorIdByUserId(userId);
        }
        if (2 != sysUser.getDataPowerType()) {
            return allDataPower(listOperator);
        } else {
            return dataPower(listOperator, userId);
        }
    }

    public List<Map<String, Object>> allDataPower(List<Map<String, Object>> listOperator) {
        if (null != listOperator && listOperator.size() > 0) {
            for (Map<String, Object> map : listOperator) {
                String operatorId = StringUtils.valueOf(map.get("operatorId"));
                List<Map<String, Object>> groupProvince = XhpcUserPrivilege.groupProvinceByOperatorId(null, Long.parseLong(operatorId));
                if (null != groupProvince && groupProvince.size() > 0) {
                    for (Map<String, Object> province : groupProvince) {
                        String provinceCode = StringUtils.valueOf(province.get("code"));
                        List<Map<String, Object>> groupCity = XhpcUserPrivilege.groupCityByOperatorId(null, provinceCode, Long.parseLong(operatorId));
                        if (null != groupCity && groupCity.size() > 0) {
                            for (Map<String, Object> city : groupCity) {
                                String cityCode = StringUtils.valueOf(city.get("code"));
                                List<Map<String, Object>> groupArea = XhpcUserPrivilege.groupAreaByOperatorId(null, cityCode, Long.parseLong(operatorId));
                                if (null != groupArea && groupArea.size() > 0) {
                                    for (Map<String, Object> area : groupArea) {
                                        String code = StringUtils.valueOf(area.get("code"));
                                        area.put("dataPower", XhpcUserPrivilege.dataPowerByOperatorId(null, code, Long.parseLong(operatorId)));
                                    }
                                }
                                city.put("areaList", groupArea);
                            }
                        }
                        province.put("cityList", groupCity);
                    }
                }
                map.put("provinceList", groupProvince);
            }
        }
        return listOperator;
    }

    public List<Map<String, Object>> dataPower(List<Map<String, Object>> listOperator, Long userId) {
        if (null != listOperator && listOperator.size() > 0) {
            for (Map<String, Object> map : listOperator) {
                String operatorId = StringUtils.valueOf(map.get("operatorId"));
                List<Map<String, Object>> groupProvince = XhpcUserPrivilege.groupProvinceByOperatorId(userId, Long.parseLong(operatorId));
                if (null != groupProvince && groupProvince.size() > 0) {
                    for (Map<String, Object> province : groupProvince) {
                        String provinceCode = StringUtils.valueOf(province.get("code"));
                        List<Map<String, Object>> groupCity = XhpcUserPrivilege.groupCityByOperatorId(userId, provinceCode, Long.parseLong(operatorId));
                        if (null != groupCity && groupCity.size() > 0) {
                            for (Map<String, Object> city : groupCity) {
                                String cityCode = StringUtils.valueOf(city.get("code"));
                                List<Map<String, Object>> groupArea = XhpcUserPrivilege.groupAreaByOperatorId(userId, cityCode, Long.parseLong(operatorId));
                                if (null != groupArea && groupArea.size() > 0) {
                                    for (Map<String, Object> area : groupArea) {
                                        String code = StringUtils.valueOf(area.get("code"));
                                        area.put("dataPower", XhpcUserPrivilege.dataPowerByOperatorId(userId, code, Long.parseLong(operatorId)));
                                    }
                                }
                                city.put("areaList", groupArea);
                            }
                        }
                        province.put("cityList", groupCity);
                    }
                }
                map.put("provinceList", groupProvince);
            }
        }
        return listOperator;
    }
}
