package com.vctgo.system.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Joiner;
import com.vctgo.common.core.constant.HttpStatus;
import com.vctgo.common.core.domain.R;
import com.vctgo.common.core.exception.ServiceException;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.api.domain.SysReLocationArea;
import com.vctgo.system.api.domain.SysReLocationClass;
import com.vctgo.system.api.domain.SysUser;
import com.vctgo.system.domain.SysAreaSetting;
import com.vctgo.system.mapper.SysAreaSettingMapper;
import com.vctgo.system.mapper.SysClassSettingMapper;
import com.vctgo.system.mapper.SysReLocationAreaMapper;
import com.vctgo.system.mapper.SysUserMapper;
import com.vctgo.system.service.ISysAreaSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 区域设置Service业务层处理
 *
 * @author caesar
 * @date 2023-05-30
 */
@Slf4j
@Service
public class SysAreaSettingServiceImpl implements ISysAreaSettingService
{
    @Autowired
    private SysAreaSettingMapper sysAreaSettingMapper;

    @Autowired
    private SysReLocationAreaMapper sysReLocationAreaMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


    @Autowired
    private SysClassSettingMapper sysClassSettingMapper;

    /**
     * 查询区域设置
     *
     * @param areaId 区域设置主键
     * @return 区域设置
     */
    @Override
    public SysAreaSetting selectSysAreaSettingByAreaId(Long areaId)
    {
        return sysAreaSettingMapper.selectById(areaId);
    }

    /**
     * 查询区域设置列表-分页
     *
     * @param sysAreaSetting 区域设置
     * @return 区域设置
     */
    @Override
    public IPage<SysAreaSetting> selectSysAreaSettingPage(SysAreaSetting sysAreaSetting)
    {
        Page mpPage =new Page(Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM),1L)
                ,Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE),10L));
        IPage<SysAreaSetting> pageData = sysAreaSettingMapper.selectTenantAreaPageList(mpPage,sysAreaSetting);

        Map<String, Object> params = new HashMap<>();
        params.put("tenantType", SecurityUtils.getTenantType());
        params.put("tenantId", SecurityUtils.getTenantId());
        List<SysUser> sysUserList = sysUserMapper.selectUserListByParams(params);
        Map<Long, String> sysUserMap = sysUserList.stream().filter(element -> element.getUserId() != null).collect(Collectors.toMap(SysUser::getUserId, SysUser::getNickName));
        pageData.getRecords().stream().forEach(obj-> {
            if(sysUserMap.containsKey(obj.getCreateById())){
                obj.setCreateBy(sysUserMap.get(obj.getCreateById()));
            }
        });
        return pageData;
    }
    /**
     * 获取全系统所有的区域列表
     *
     * @return 区域设置
     */
    @Override
    public List<SysAreaSetting> selectSysAreaSettingList(SysAreaSetting sysAreaSetting)
    {
        return sysAreaSettingMapper.selectSysAreaSettingList(sysAreaSetting);
    }

    @Override
    public List<SysAreaSetting> selectTenantAreaSettingList(SysAreaSetting sysAreaSetting) {
        return sysAreaSettingMapper.selectTenantAreaSettingList(sysAreaSetting);
    }

    @Override
    public List<SysAreaSetting> selectAreaListByParams(Map<String, Object> params) {
        return sysAreaSettingMapper.selectAreaListByParams(params);
    }

    /**
     * 新增区域设置
     *
     * @param sysAreaSetting 区域设置
     * @return 结果
     */

    @Override
    public int insertSysAreaSetting(SysAreaSetting sysAreaSetting)
    {
        return sysAreaSettingMapper.insert(sysAreaSetting);
    }

    /**
     * 修改区域设置
     *
     * @param sysAreaSetting 区域设置
     * @return 结果
     */
    @Override
    public int updateSysAreaSetting(SysAreaSetting sysAreaSetting)
    {
        return sysAreaSettingMapper.updateById(sysAreaSetting);
    }

    @Override
    public int logicalDeleteByAreaIds(Long[] ids) {
        return 0;
    }


    /**
     * 批量删除区域设置
     *
     * @param areaIds 需要删除的区域设置主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaSettingByAreaIds(Long[] areaIds)
    {
        return sysAreaSettingMapper.deleteSysAreaSettingByAreaIds(areaIds);
    }

    /**
     * 删除区域设置信息
     *
     * @param areaId 区域设置主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaSettingByAreaId(Long areaId)
    {
        return sysAreaSettingMapper.deleteById(areaId);
    }

    @Override
    public List<SysAreaSetting> getAreaSettingInfo(String locationIds,String attribute) {
        List<SysAreaSetting> areaSettingInfo = new ArrayList<>();
        if(attribute!=null&&locationIds==null){
            return areaSettingInfo;
        }
        if("1".equals(attribute)){
            //根据班级查询位置信息
            List<SysReLocationClass> list = sysClassSettingMapper.getSysReLocationClassByClassId(locationIds);
            if(CollectionUtils.isNotEmpty(list)){
                list = list.stream().filter(obj->obj.getLocationId()!=null).collect(Collectors.toList());
                String locationIdStr =list.stream().map(obj->String.valueOf(obj.getLocationId())).collect(Collectors.joining(","));
                areaSettingInfo = sysAreaSettingMapper.getAreaSettingInfo(locationIdStr);
                areaSettingInfo = areaSettingInfo.stream()
                        .collect(Collectors.toMap(
                                SysAreaSetting::getAreaId,
                                Function.identity(),
                                (existing, replacement) -> existing
                        )).values().stream()
                        .collect(Collectors.toList());
            }
        } else {
            areaSettingInfo = sysAreaSettingMapper.getAreaSettingInfo(locationIds);
            areaSettingInfo = areaSettingInfo.stream()
                    .collect(Collectors.toMap(
                            SysAreaSetting::getAreaId,
                            Function.identity(),
                            (existing, replacement) -> existing
                    )).values().stream()
                    .collect(Collectors.toList());
        }
        return areaSettingInfo;
    }

    @Override
    public List<String> getAreaNameByIds(List<Long> areaIds) {
        List<SysAreaSetting> list = sysAreaSettingMapper.getAreaNameByIds(areaIds);
        if(CollectionUtils.isNotEmpty(list)){
            return list.stream().map(SysAreaSetting::getAreaName).collect(Collectors.toList());
        }
        return null;
    }

    public List<SysAreaSetting> selectAreaNameByIds(List<Long> areaIds) {
        List<SysAreaSetting> list = sysAreaSettingMapper.selectAreaNameByIds(areaIds);
        return list;
    }
}
