package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.model.enumModel.eAccountType;
import com.template.model.enumModel.eRoleType;
import com.template.model.pojo.AlumniUser;
import com.template.mapper.AlumniUserMapper;
import com.template.model.result.CommonResult;
import com.template.model.result.PageUtils;
import com.template.model.vo.AccountVo;
import com.template.model.vo.UserVo;
import com.template.services.AlumniUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2024-11-20
 */
@Service
public class AlumniUserServiceImpl extends ServiceImpl<AlumniUserMapper, AlumniUser> implements AlumniUserService {

    @Autowired
    private AlumniUserMapper alumniUserMapper;

    @Override
    public int insertAlumniUser(AlumniUser au) {
        int result = alumniUserMapper.insert(au);
        return au.getId();
    }

    @Override
    public int updateAlumniUser(AlumniUser au) {
        int result = alumniUserMapper.updateById(au);
        return result;
    }

    @Override
    public AlumniUser queryDataById(Integer id) {
        AlumniUser result = alumniUserMapper.selectById(id);
        return result;
    }

    @Override
    public List<AlumniUser> querySuperAdmins() {
        QueryWrapper<AlumniUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id", eRoleType.Super.getValue());
        List<AlumniUser> result = alumniUserMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public AlumniUser queryDataByCardNumber(String cardNumber) {
        QueryWrapper<AlumniUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(cardNumber), "card_number", cardNumber);
        AlumniUser result = alumniUserMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public AlumniUser queryDataByAccount(String account) {
        QueryWrapper<AlumniUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(account), "account", account);
        AlumniUser result = alumniUserMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public PageUtils<AlumniUser> queryPageList(int currentPage, int pageCount, String name, String orgName, Integer collegeId, Integer periodId, Integer majorId, Integer classId) {
        Page<AlumniUser> page = new Page<>(currentPage, pageCount);
        QueryWrapper<AlumniUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name), "name", name);//姓名
        queryWrapper.like(StringUtils.hasText(orgName), "org_name", orgName);//组织名称
        queryWrapper.eq(collegeId != null && collegeId.intValue() > 0, "college_id", collegeId);//学院
        queryWrapper.eq(periodId != null && periodId.intValue() > 0, "period_id", periodId);//学段
        queryWrapper.eq(majorId != null && majorId.intValue() > 0, "major_id", majorId);//专业
        queryWrapper.eq(classId != null && classId.intValue() > 0, "class_id", classId);//班级
        queryWrapper.eq("type_id", eAccountType.User.getValue());
        IPage<AlumniUser> result = alumniUserMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

    @Override
    public List<AlumniUser> queryUsers(String keyword) {
        List<AlumniUser> result = alumniUserMapper.queryUsers(keyword);
        return result;
    }

    @Override
    public PageUtils<UserVo> queryUserPages(int currentPage, int pageCount, List<Integer> departmentIds) {
        Page<UserVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<UserVo> result = alumniUserMapper.queryUserPages(page, departmentIds);
        return new PageUtils(result);
    }

    @Override
    public List<AlumniUser> queryDataByIds(List<String> ids) {
        QueryWrapper<AlumniUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(ids != null && ids.size() > 0, "id", ids);//id
        List<AlumniUser> result = alumniUserMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public PageUtils<AlumniUser> queryAccountPage(int currentPage, int pageCount, String account, String startTime, String endTime) {
        Page<AlumniUser> page = new Page<>(currentPage, pageCount);
        QueryWrapper<AlumniUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(account), "account", account);//账号
        if(StringUtils.hasText(startTime) && StringUtils.hasText(endTime)){
            queryWrapper.between("create_account_time",startTime,endTime);
        }
        queryWrapper.isNotNull("account");
        IPage<AlumniUser> result = alumniUserMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<AlumniUser> queryUserPage(int currentPage, int pageCount, String name) {
        Page<AlumniUser> page = new Page<>(currentPage, pageCount);
        QueryWrapper<AlumniUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name), "name", name);//名称
        IPage<AlumniUser> result = alumniUserMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<AlumniUser> queryUserPageByClassId(int currentPage, int pageCount, Integer classId, String name) {
        Page<AlumniUser> page = new Page<>(currentPage, pageCount);
        QueryWrapper<AlumniUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name), "name", name);//名称
        queryWrapper.eq(classId != null && classId.intValue() > 0, "class_id", classId);//班级
        IPage<AlumniUser> result = alumniUserMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

    @Override
    public AlumniUser queryUserByCnp(String cardNumber, String name, String phone) {
        QueryWrapper<AlumniUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(name), "name", name);//名称
        queryWrapper.eq(StringUtils.hasText(cardNumber), "card_number", cardNumber);//微校卡号
        queryWrapper.eq(StringUtils.hasText(phone), "phone", phone);//手机号
        AlumniUser au = alumniUserMapper.selectOne(queryWrapper);
        return au;
    }
}
