package com.zcloud.modules.custormerInfo.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.zcloud.common.utils.PageUtils;
 import com.zcloud.modules.custormerInfo.dao.BusTeacherAuditRecordDao;
 import com.zcloud.modules.custormerInfo.dao.SysUserTeacherDao;
 import com.zcloud.modules.custormerInfo.entity.BusTeacherAuditRecordEntity;
 import com.zcloud.modules.custormerInfo.entity.SysUserTeacherEntity;
 import com.zcloud.modules.custormerInfo.service.SysUserTeacherService;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

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

/**
 * @Author wangyitao
 * @Desc (SysUserTeacher)表服务实现类
 * @Date 2024-10-15 14:32:59
 */
@Service("sysUserTeacherService")
public class SysUserTeacherServiceImpl extends ServiceImpl<SysUserTeacherDao, SysUserTeacherEntity> implements SysUserTeacherService {

    @Autowired
    private BusTeacherAuditRecordDao busTeacherAuditRecordDao;

    @Override
	public SysUserTeacherEntity queryByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<SysUserTeacherEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectOne(queryWrapper);
	}

	@Override
	public List<SysUserTeacherEntity> queryListByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<SysUserTeacherEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectList(queryWrapper);
	}

    @Override
    public PageUtils listPage(Map<String, Object> params) {
        Page<Map<String,Object>> page = new Page<>(Integer.parseInt(params.get("curPage").toString()),Integer.parseInt(params.get("limit").toString()));
        params.put("isDelete",0);
        IPage<SysUserTeacherEntity> ipage = baseMapper.listPage(page,params);
        return new PageUtils(ipage);
    }


   @Override
	public void delete(String teacherId,String userId){
		UpdateWrapper<SysUserTeacherEntity> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("teacher_id",teacherId);

		SysUserTeacherEntity sysUserTeacher = new SysUserTeacherEntity();
		sysUserTeacher.setOperator(userId);
		sysUserTeacher.setIsDelete(1);

		this.update(sysUserTeacher, updateWrapper);
	}

    @Override
    public SysUserTeacherEntity getInfo(String teacherId) {
        SysUserTeacherEntity sysUserTeacherEntity = baseMapper.selectById(teacherId);
        if (sysUserTeacherEntity.getStatus() == -2) {
            BusTeacherAuditRecordEntity busTeacherAuditRecordEntity = busTeacherAuditRecordDao.selectOne(Wrappers.<BusTeacherAuditRecordEntity>lambdaQuery()
                    .eq(BusTeacherAuditRecordEntity::getTeacherId, sysUserTeacherEntity.getTeacherId())
                    .eq(BusTeacherAuditRecordEntity::getStatus, -2)
                    .eq(BusTeacherAuditRecordEntity::getIsDelete, 0)
                    .orderByDesc(BusTeacherAuditRecordEntity::getCreateTime)
                    .last("limit 1"));
            sysUserTeacherEntity.setStatusContent(busTeacherAuditRecordEntity.getStatusContent());
        }
        return sysUserTeacherEntity;
    }
}
