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.mapper.SmartUserMapper;
import com.template.model.enumModel.eIdentityStatu;
import com.template.model.pojo.SmartUser;
import com.template.model.result.PageUtils;
import com.template.model.vo.*;
import com.template.services.SmartUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
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 2023-12-04
 */
@Service
public class SmartUserServiceImpl extends ServiceImpl<SmartUserMapper, SmartUser> implements SmartUserService {
    @Autowired
    private SmartUserMapper smartUserMapper;

    @Override
    public int insertSmartUser(SmartUser sa) {
        int result = smartUserMapper.insert(sa);
        return result;
    }

    @Override
    public int updateSmartUser(SmartUser sa) {
        int result = smartUserMapper.updateById(sa);
        return result;
    }

    @Override
    public PageUtils<SmartUser> queryPageSmartUsers(int currentPage, int pageCount, List<Integer> departmentIds) {
        Page<SmartUser> page = new Page<>(currentPage, pageCount);
//        QueryWrapper<SmartUser> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(departmentId > 0, "department_id", departmentId);
        IPage<SmartUser> result = smartUserMapper.selectPage(page, null);
        return new PageUtils<>(result);
    }

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

    @Override
    public PageUtils<GradeVo> querySmartSecordPage(int currentPage, int pageCount, String name) {
        Page<GradeVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<GradeVo> result = smartUserMapper.querySmartSecordPage(page, name);
        return new PageUtils(result);
    }

    @Override
    public List<SmartUser> querySmartUsers(List<Integer> departmentIds, String name) {
        List<SmartUser> result = smartUserMapper.querySmartUsers(departmentIds, name);
        return result;
    }


    @Override
    public List<SmartUser> getSmartUserByIds(List<Integer> ids) {
        QueryWrapper<SmartUser> queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        queryWrapper.eq("is_cancel", 0);
        List<SmartUser> result = smartUserMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<SmartUser> getSmartUserIds(List<String> ids) {
        QueryWrapper<SmartUser> queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        queryWrapper.eq("is_cancel", 0);
        List<SmartUser> result = smartUserMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public int getSmartUserCountByIds(List<Integer> ids) {
        QueryWrapper<SmartUser> queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        queryWrapper.eq("is_cancel", 0);

        int result = smartUserMapper.selectCount(queryWrapper);
        return result;
    }

    @Override
    public boolean updateUserBatchById(List<SmartUser> users) {
        boolean result = this.updateBatchById(users);
        return result;
    }

    @Override
    public Integer querySmartUserByCardNo(String cardNo) {
        QueryWrapper<SmartUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("card_no", cardNo);
        queryWrapper.eq("is_cancel", 0);

        int existCount = smartUserMapper.selectCount(queryWrapper);
        return existCount;
    }

    @Override
    public List<SmartUser> queryStudentBySchoolClass(Integer schoolClass) {
        QueryWrapper<SmartUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("school_class", schoolClass);
        queryWrapper.eq("identity_id", eIdentityStatu.Student.getValue());
        queryWrapper.eq("is_cancel", 0);

        List<SmartUser> result = smartUserMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public int deleteSmartUserById(int id) {
        int result = smartUserMapper.deleteById(id);
        return result;
    }

    @Override
    public int deleteSmartUserByIds(List<Integer> ids) {
        int result = smartUserMapper.deleteBatchIds(ids);
        return result;
    }

    @Override
    public SmartUser getSmartById(int id) {
        QueryWrapper<SmartUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_cancel", 0);
        queryWrapper.eq("id", id);
        SmartUser result = smartUserMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public List<AffiliateUserVo> queryAffiliateUserById(Integer id) {
        List<AffiliateUserVo> result = smartUserMapper.queryAffiliateUserById(id);
        return result;
    }

    @Override
    public List<SmartUser> queryStudentDatas(){
        QueryWrapper<SmartUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_cancel", 0);
        queryWrapper.eq("identity_id", eIdentityStatu.Student.getValue());
        List<SmartUser> result = smartUserMapper.selectList(queryWrapper);
        return result;
    }

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

    @Override
    public List<SmartUser> warningPushList() {
        return smartUserMapper.warningPushList();
    }

    @Override
    public List<AffiliateParentVo> queryAffiliateParents(Integer userId){
        return smartUserMapper.queryAffiliateParents(userId);
    }

    @Override
    public SmartUser queryUserInfo(String name, String cardNo, String idCard) {
        QueryWrapper<SmartUser> queryWrapper = new QueryWrapper();
        queryWrapper.eq(StringUtils.hasText(name),"name", name);
        queryWrapper.eq(StringUtils.hasText(cardNo),"card_no", cardNo);
        queryWrapper.eq(StringUtils.hasText(idCard),"id_card", idCard);
        queryWrapper.eq("is_cancel", 0);
        SmartUser result = smartUserMapper.selectOne(queryWrapper);
        return result;
    }

}
