package org.dromara.system.service.impl;

import org.dromara.common.core.domain.dto.RoleDTO;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.CourseChange;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.FormalParentsStudentBo;
import org.dromara.system.domain.vo.FormalParentsStudentVo;
import org.dromara.system.domain.FormalParentsStudent;
import org.dromara.system.mapper.FormalParentsStudentMapper;
import org.dromara.system.service.IFormalParentsStudentService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 家长-学生(审核通过之后数据)Service业务层处理
 *
 * @author Lion Li
 * @date 2023-08-16
 */
@RequiredArgsConstructor
@Service
public class FormalParentsStudentServiceImpl implements IFormalParentsStudentService {

    private final FormalParentsStudentMapper baseMapper;

    /**
     * 查询家长-学生(审核通过之后数据)
     */
    @Override
    public FormalParentsStudentVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询家长-学生(审核通过之后数据)列表
     */
    @Override
    public TableDataInfo<FormalParentsStudentVo> queryPageList(FormalParentsStudentBo bo, PageQuery pageQuery) {
        if(StringUtils.isEmpty(bo.getIsDel())){
            bo.setIsDel("0");
        }
        LambdaQueryWrapper<FormalParentsStudent> lqw = buildQueryWrapper(bo);
        Page<FormalParentsStudentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询家长-学生(审核通过之后数据)列表
     */
    @Override
    public List<FormalParentsStudentVo> queryList(FormalParentsStudentBo bo) {
        if(StringUtils.isEmpty(bo.getIsDel())){
            bo.setIsDel("0");
        }
        LambdaQueryWrapper<FormalParentsStudent> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<FormalParentsStudent> buildQueryWrapper(FormalParentsStudentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<FormalParentsStudent> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getParentsId() != null, FormalParentsStudent::getParentsId, bo.getParentsId());
        lqw.eq(bo.getSchoolId() != null, FormalParentsStudent::getSchoolId, bo.getSchoolId());
        lqw.like(StringUtils.isNotBlank(bo.getSchoolName()), FormalParentsStudent::getSchoolName, bo.getSchoolName());
        lqw.eq(bo.getClassId() != null, FormalParentsStudent::getClassId, bo.getClassId());
        lqw.like(StringUtils.isNotBlank(bo.getClassName()), FormalParentsStudent::getClassName, bo.getClassName());
        lqw.like(StringUtils.isNotBlank(bo.getStudentName()), FormalParentsStudent::getStudentName, bo.getStudentName());
        lqw.eq(StringUtils.isNotBlank(bo.getStudentNumber()), FormalParentsStudent::getStudentNumber, bo.getStudentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDel()), FormalParentsStudent::getIsDel, bo.getIsDel());
        lqw.in((bo.getClassIds()!=null && bo.getClassIds().size()>0), FormalParentsStudent::getClassId,bo.getClassIds());
        return lqw;
    }

    /**
     * 新增家长-学生(审核通过之后数据)
     */
    @Override
    public Boolean insertByBo(FormalParentsStudentBo bo) {
        FormalParentsStudent add = MapstructUtils.convert(bo, FormalParentsStudent.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改家长-学生(审核通过之后数据)
     */
    @Override
    public Boolean updateByBo(FormalParentsStudentBo bo) {
        FormalParentsStudent update = MapstructUtils.convert(bo, FormalParentsStudent.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(FormalParentsStudent entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除家长-学生(审核通过之后数据)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TableDataInfo<FormalParentsStudentVo> selectFormalParentsStudentListNoPage(FormalParentsStudentBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();

        if(!"admin".equals(loginUser.getUsername())){
            Map<String, Object> params = bo.getParams();
            String role = "";
            if (params != null && params.size() > 0) {
                role = (String) params.get("role");
            }

            if("parents".equals(role)){
                bo.setParentsId(loginUser.getUserId());
            }
        }

        LambdaQueryWrapper<FormalParentsStudent> lqw = buildQueryWrapper(bo);
        List<FormalParentsStudentVo> list = baseMapper.selectVoList(lqw);
        return TableDataInfo.build(list);
    }
}
