package com.yida.data.school.transaction.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.apply.EduProductModel;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.system.SystemUser;
import com.yida.data.common.core.entity.system.enums.RoleEnum;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.common.core.utils.RoleUtil;
import com.yida.data.school.transaction.mapper.EduProductModelMapper;
import com.yida.data.school.transaction.service.EduProductModelService;
import com.yida.data.system.feign.RemoteDeptService;
import com.yida.data.system.feign.RemoteUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class EduProductModelServiceImpl implements EduProductModelService {
    @Resource
    private EduProductModelMapper eduProductModelMapper;
    @Resource
    private RemoteDeptService remoteDeptService;

    @Resource
    private RemoteUserService remoteUserService;

    /**
     * 查询模板
     *
     * @return
     */
    @Override
    public IPage<EduProductModel> selectEduProductModelService(Page<EduProductModel> page, String modelName) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        // 获取登陆用户的最大角色
        RoleEnum roleEnum = RoleUtil.getMaxRole(currentUser);
        // 查询下级部门信息
        List<Long> childIdList = this.remoteDeptService.deptList(currentUser.getDeptId()).getData()
                .stream().map(Dept::getDeptId).collect(Collectors.toList());
        if (RoleEnum.ROLE_EDUCATION_PRINCIPAL.equals(roleEnum) ||
                RoleEnum.ROLE_AGENT_ADMIN.equals(roleEnum)) {
            childIdList.add(currentUser.getDeptId());
        }
        IPage<EduProductModel> iPage = eduProductModelMapper.selectEduProductModelPage(page, modelName, currentUser, childIdList);
        // 查询创建人名称
        iPage.getRecords().forEach(eduProductModel -> {
            SystemUser systemUser = remoteUserService.getInfoById(eduProductModel.getCreateId()).getData();
            if (Objects.nonNull(systemUser)) {
                eduProductModel.setCreateName(systemUser.getNickname());
            }
        });
        return iPage;
    }

    /**
     * 新增模板
     */

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertEduProductModelService(EduProductModel eduProductModel) {
        eduProductModel.setCreateDeptId(FebsUtil.getCurrentUser().getDeptId());
        eduProductModelMapper.insert(eduProductModel);

    }

    /**
     * 删除模板
     *
     * @param modelId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeEduProductModelService(Integer modelId) {
        eduProductModelMapper.deleteById(modelId);
    }

    /**
     * 更新模板
     *
     * @param eduProductModel
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateEduProductModelService(EduProductModel eduProductModel) {
        eduProductModelMapper.updateById(eduProductModel);
    }
}
