package com.yida.data.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.system.enums.RoleEnum;
import com.yida.data.common.core.entity.user.EduParent;
import com.yida.data.common.core.entity.user.EduStudent;
import com.yida.data.common.core.entity.user.EduStudentApplyFor;
import com.yida.data.common.core.exception.FebsException;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.common.service.CommonService;
import com.yida.data.system.feign.RemoteDeptService;
import com.yida.data.user.dto.ListStudentApplyForDTO;
import com.yida.data.user.mapper.EduStudentApplyForMapper;
import com.yida.data.user.service.EduStudentApplyForService;
import com.yida.data.user.service.EduStudentService;
import com.yida.data.user.service.EduUserDeptService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class EduStudentApplyServiceForImpl extends ServiceImpl<EduStudentApplyForMapper, EduStudentApplyFor> implements
    EduStudentApplyForService {

    private final CommonService commonService;

    private final RemoteDeptService remoteDeptService;

    private final EduUserDeptService eduUserDeptService;


    private final EduStudentService eduStudentService;


    /**
     * 分页查询学生会员申请记录
     *
     * @param dto
     * @param page
     * @return
     */
    @Override
    public IPage<EduStudentApplyFor> listStudentApplyFor(ListStudentApplyForDTO dto, Page<EduStudentApplyFor> page) {
        List<Long> schoolIds = new ArrayList<>();
        CurrentUser currentUser = FebsUtil.getCurrentUser();
        if (currentUser.getRolePerms().contains(RoleEnum.ROLE_SCHOOL_PRINCIPAL.getValue())) {
            schoolIds = Collections.singletonList(currentUser.getDeptId());
        } else {
            schoolIds = remoteDeptService.getSchoolByArea(null, null).getData().stream().map(Dept::getDeptId)
                .collect(Collectors.toList());
        }
        dto.setSchoolIdList(schoolIds);
        return this.baseMapper.listStudentApplyFor(dto, page);
    }

    /**
     * 保存会员申请
     *
     * @param eduStudentApplyFor
     */
    @Override
    public void saveStudentApplyFor(EduStudentApplyFor eduStudentApplyFor) {
        //判断是否已申请
        EduStudentApplyFor applyFor = getOne(
            Wrappers.lambdaQuery(new EduStudentApplyFor()).eq(EduStudentApplyFor::getApplyCode, eduStudentApplyFor.getApplyCode())
                .eq(EduStudentApplyFor::getStudentId, eduStudentApplyFor.getStudentId())
                .eq(EduStudentApplyFor::getStatus, 0));
        if (ObjectUtil.isNotNull(applyFor)) {
            throw new FebsException("用户已申请，请等待管理联系处理！");
        }
        eduStudentApplyFor.setApplyName(
            commonService.getDictByTypeAndValueOrLabel("apply_type", eduStudentApplyFor.getApplyCode(), null).getLabel());
        EduStudent student = eduStudentService.getStudent(eduStudentApplyFor.getStudentId());
        log.info("学生信息：【{}】", student);
        eduStudentApplyFor.setStudentName(student.getStuName());
        eduStudentApplyFor.setStudentNumber(student.getStuNumber());
        eduStudentApplyFor.setStudentMobile(
            StringUtils.join(student.getParents().stream().map(EduParent::getMobile).collect(Collectors.toList()), ","));
        eduStudentApplyFor.setSchoolName(remoteDeptService.getInfoById(student.getSchoolId()).getData().getDeptName());
        eduStudentApplyFor.setSchoolId(student.getSchoolId());
        eduStudentApplyFor.setCampusId(student.getCampusId());
        eduStudentApplyFor.setCampusName(student.getCampusName());
        eduStudentApplyFor.setSectionId(student.getSectionId());
        eduStudentApplyFor.setSectionName(student.getSectionName());
        eduStudentApplyFor.setGradeId(student.getGradeId());
        eduStudentApplyFor.setGradeName(student.getGradeName());
        eduStudentApplyFor.setClassId(student.getClassId());
        eduStudentApplyFor.setClassName(student.getClassName());
        this.save(eduStudentApplyFor);
    }

    /**
     * 处理会员申请
     *
     * @param id
     * @param status
     */
    @Override
    public void dealApplyFor(Long id, Integer status) {
        CurrentUser currentUser = FebsUtil.getCurrentUser();

        update(Wrappers.lambdaUpdate(new EduStudentApplyFor()).eq(EduStudentApplyFor::getId, id)
            .set(EduStudentApplyFor::getStatus, status)
            .set(EduStudentApplyFor::getDealStaffId, currentUser.getUserId())
            .set(EduStudentApplyFor::getDealStaffName, currentUser.getUsername()));
    }

    /**
     * 根据应用编码，用户ID，查询申请详情
     *
     * @param studentId
     * @param appCode
     * @return
     */
    @Override
    public EduStudentApplyFor getByStudentId(Long studentId, String appCode) {
        return getOne(
            Wrappers.lambdaQuery(new EduStudentApplyFor()).eq(EduStudentApplyFor::getApplyCode, appCode)
                .eq(EduStudentApplyFor::getStudentId, studentId)
                .eq(EduStudentApplyFor::getStatus, 0));
    }


}
