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.BusTeacherAuditRecordService;
import com.zcloud.modules.sys.dao.SysUserDao;
import com.zcloud.modules.sys.dao.SysUserRoleDao;
import com.zcloud.modules.sys.entity.SysUserEntity;
import com.zcloud.modules.sys.entity.SysUserRoleEntity;
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 org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

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

/**
 * @Author wangyitao
 * @Desc 教师认证审核记录表(BusTeacherAuditRecord)表服务实现类
 * @Date 2024-10-15 14:33:52
 */
@Service("busTeacherAuditRecordService")
public class BusTeacherAuditRecordServiceImpl extends ServiceImpl<BusTeacherAuditRecordDao, BusTeacherAuditRecordEntity> implements BusTeacherAuditRecordService {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserTeacherDao sysUserTeacherDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;

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

        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<BusTeacherAuditRecordEntity> queryListByFlexibleParams(String paramName, Object paramValue) {
        QueryWrapper<BusTeacherAuditRecordEntity> 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<BusTeacherAuditRecordEntity> ipage = baseMapper.listPage(page, params);
        return new PageUtils(ipage);
    }


    @Override
    public void delete(String teacherAuditRecordId, String userId) {
        UpdateWrapper<BusTeacherAuditRecordEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("teacher_audit_record_id", teacherAuditRecordId);

        BusTeacherAuditRecordEntity busTeacherAuditRecord = new BusTeacherAuditRecordEntity();
        busTeacherAuditRecord.setOperator(userId);
        busTeacherAuditRecord.setIsDelete(1);

        this.update(busTeacherAuditRecord, updateWrapper);
    }

    @Override
    @Transactional
    public void insert(BusTeacherAuditRecordEntity busTeacherAuditRecordEntity) {
        if (1 == busTeacherAuditRecordEntity.getStatus()) {
            SysUserEntity sysUserEntity = sysUserDao.selectOne(Wrappers.<SysUserEntity>lambdaQuery()
                    .eq(SysUserEntity::getTeacherId, busTeacherAuditRecordEntity.getTeacherId())
                    .eq(SysUserEntity::getIsDelete, 0)
                    .last("limit 1"));

            //获取用户是否已绑定教师角色
            SysUserRoleEntity sysUserRoleEntity = sysUserRoleDao.selectOne(Wrappers.<SysUserRoleEntity>lambdaQuery()
                    .eq(SysUserRoleEntity::getUserId, sysUserEntity.getUserId())
                    .eq(SysUserRoleEntity::getRoleId, "b24693165403046fae909e091f96b3ef"));

            if (ObjectUtils.isEmpty(sysUserRoleEntity)) {// 未绑定过 绑定教师角色
                SysUserRoleEntity userRole = new SysUserRoleEntity();
                userRole.setUserId(sysUserEntity.getUserId());
                userRole.setRoleId("b24693165403046fae909e091f96b3ef");
                sysUserRoleDao.insert(userRole);
            }
        }

        SysUserTeacherEntity sysUserTeacherEntity = new SysUserTeacherEntity();
        sysUserTeacherEntity.setTeacherId(busTeacherAuditRecordEntity.getTeacherId());
        sysUserTeacherEntity.setStatus(busTeacherAuditRecordEntity.getStatus());
        sysUserTeacherEntity.setOperator(busTeacherAuditRecordEntity.getOperator());
        sysUserTeacherDao.updateById(sysUserTeacherEntity);

        this.saveOrUpdate(busTeacherAuditRecordEntity);
    }
}
