package com.vctgo.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.SysUser;
import com.vctgo.system.domain.SysChildhoodManage;
import com.vctgo.system.domain.dto.SysClassInfoDto;
import com.vctgo.system.mapper.SysChildhoodManageMapper;
import com.vctgo.system.mapper.SysClassSettingMapper;
import com.vctgo.system.mapper.SysUserMapper;
import com.vctgo.system.service.ISysChildhoodManageService;
import com.vctgo.system.util.ConstantUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 幼儿管理对象Service业务层处理
 *
 * @author wanglonghua
 * @date 2023-10-08
 */
@Slf4j
@Service
public class SysChildhoodManageServiceImpl implements ISysChildhoodManageService
{
    @Autowired
    private SysChildhoodManageMapper sysChildhoodManageMapper;

    @Autowired
    private SysClassSettingMapper sysClassSettingMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询幼儿管理对象
     *
     * @param id 幼儿管理对象主键
     * @return 幼儿管理对象
     */
    @Override
    public SysChildhoodManage selectSysChildhoodManageById(Long id) {
        return sysChildhoodManageMapper.selectSysChildhoodManageById(id);
    }

    /**
     * 查询幼儿管理对象列表-分页
     *
     * @param sysChildhoodManage 幼儿管理对象
     * @return 幼儿管理对象
     */
    @Override
    public IPage<SysChildhoodManage> selectSysChildhoodManagePage(SysChildhoodManage sysChildhoodManage)
    {
        Page mpPage =new Page(Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM),1L)
                ,Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE),10L));
        IPage<SysChildhoodManage> pageData =sysChildhoodManageMapper.selectSysChildhoodManageList(mpPage,sysChildhoodManage);
        Map<String, Object> params = new HashMap<>();
        params.put("tenantId", SecurityUtils.getTenantId());
        params.put("bindType", 1);
        List<SysClassInfoDto> sysClassInfoDtoList = sysClassSettingMapper.selectSysReClassInfoListByParams(params);
        Map<Long, SysClassInfoDto> sysClassInfoMap = sysClassInfoDtoList.stream().collect(Collectors.toMap(SysClassInfoDto::getId, Function.identity()));
        //获取当前租户类型，判断当前租户是集团租户还是独立幼儿园租户
        params.clear();
        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(obj.getClassId() != null && sysClassInfoMap.containsKey(obj.getClassId())){
                obj.getParams().put("className", sysClassInfoMap.get(obj.getClassId()).getClassName()+"("+sysClassInfoMap.get(obj.getClassId()).getEnrollmentYear()+")");
                obj.getParams().put("mainTeacherName", sysClassInfoMap.get(obj.getClassId()).getMainTeacherName());
            }
            if(sysUserMap.containsKey(obj.getCreateById())){
                obj.setCreateBy(sysUserMap.get(obj.getCreateById()));
            }
        });
        return pageData;
    }

    /**
     * 查询幼儿管理对象列表
     *
     * @param sysChildhoodManage 幼儿管理对象
     * @return 幼儿管理对象
     */
    @Override
    public List<SysChildhoodManage> selectSysChildhoodManageList(SysChildhoodManage sysChildhoodManage)
    {
        sysChildhoodManage.setTenantId(SecurityUtils.getTenantId());
        return sysChildhoodManageMapper.selectSysChildhoodManageList(sysChildhoodManage);
    }

    /**
     * 新增幼儿管理对象
     *
     * @param sysChildhoodManage 幼儿管理对象
     * @return 结果
     */

    @Override
    public int insertSysChildhoodManage(SysChildhoodManage sysChildhoodManage)
    {
        return sysChildhoodManageMapper.insert(sysChildhoodManage);
    }

    /**
     * 修改幼儿管理对象
     *
     * @param sysChildhoodManage 幼儿管理对象
     * @return 结果
     */
    @Override
    public int updateSysChildhoodManage(SysChildhoodManage sysChildhoodManage)
    {
        return sysChildhoodManageMapper.updateById(sysChildhoodManage);
    }

    /**
     * 逻辑删除幼儿管理对象
     *
     * @param ids 需要删除的幼儿管理对象主键
     * @return 结果
     */
    @Override
    public int logicalDeleteSysChildhoodManageByIds(Long[] ids)
    {
        SysChildhoodManage sysChildhoodManage = new SysChildhoodManage();
        sysChildhoodManage.setUpdateById(SecurityUtils.getUserId());
        sysChildhoodManage.setUpdateBy(SecurityUtils.getUsername());
        sysChildhoodManage.setUpdateTime(new Date());
        sysChildhoodManage.setIsDeleted(ConstantUtils.IS_DELETED_1);
        return sysChildhoodManageMapper.logicalDeleteSysChildhoodManageByIds(ids, sysChildhoodManage);
    }

    /**
     * 批量删除幼儿管理对象
     *
     * @param ids 需要删除的幼儿管理对象主键
     * @return 结果
     */
    @Override
    public int deleteSysChildhoodManageByIds(Long[] ids)
    {
        return sysChildhoodManageMapper.deleteSysChildhoodManageByIds(ids);
    }

    /**
     * 删除幼儿管理对象信息
     *
     * @param id 幼儿管理对象主键
     * @return 结果
     */
    @Override
    public int deleteSysChildhoodManageById(Long id)
    {
        return sysChildhoodManageMapper.deleteById(id);
    }
}
