package com.repair.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.repair.mapper.RepairUserMapper;
import com.repair.model.pojo.RepairPayRecord;
import com.repair.model.pojo.RepairUser;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.*;
import com.repair.services.RepairUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-07-11
 */
@Service
public class RepairUserServiceImpl extends ServiceImpl<RepairUserMapper, RepairUser> implements RepairUserService {

    @Autowired
    private RepairUserMapper repairUserMapper;

    @Override
    public int insertRepairUser(RepairUser ru) {
        int result = repairUserMapper.insert(ru);
        return ru.getId();
    }

    @Override
    public int updateRepairUser(RepairUser ru) {
        int result = repairUserMapper.updateById(ru);
        return result;
    }

    @Override
    public PageUtils<RepairUser> queryPageRepairUser(int currentPage, int pageCount) {
        //ru.user_zzid = 1 or ru.user_zzid = 2
        Page<RepairUser> page = new Page<>(currentPage, pageCount);
        QueryWrapper<RepairUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(e -> e.eq("user_zzid", 1)
                .or()
                .eq("user_zzid", 2));
        IPage<RepairUser> result = repairUserMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairUser> queryPageUserBySchoolId(int currentPage, int pageCount, int schoolId) {
        //(ru.user_zzid = 1 or ru.user_zzid = 2) and school_Id = X
        Page<RepairUser> page = new Page<>(currentPage, pageCount);
        QueryWrapper<RepairUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("school_id", schoolId);
        queryWrapper.and(e -> e.eq("user_zzid", 1)
                .or()
                .eq("user_zzid", 2));
        IPage<RepairUser> result = repairUserMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }


    @Override
    public List<RepairUser> queryRepairUsers() {
        QueryWrapper<RepairUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(e -> e.eq("user_zzid", 1)
                .or()
                .eq("user_zzid", 2));
        List<RepairUser> result = repairUserMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<RepairUser> queryRepairUserByArticle() {
        QueryWrapper<RepairUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("article_id");
        List<RepairUser> result = repairUserMapper.selectList(queryWrapper);
        return result;
    }


    @Override
    public PageUtils<RepairUserVo> queryPageRepairUser(int currentPage, int pageCount, String keyWork, Integer teamId, Integer schoolId) {
        Page<RepairUserVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairUserVo> datas = repairUserMapper.queryUserPageList(page, schoolId == null ? null : String.valueOf(schoolId), teamId == null ? null : String.valueOf(teamId), keyWork);
        return new PageUtils(datas);
    }

    @Override
    public int deleteRepairUserById(int id) {
        int result = repairUserMapper.deleteById(id);
        return result;
    }

    @Override
    public RepairUser getRepairById(int id) {
        RepairUser result = repairUserMapper.selectById(id);
        return result;
    }

    @Override
    public RepairUser getRepairByCardNumber(String CardNumber) {
        QueryWrapper<RepairUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("card_number", CardNumber);
        RepairUser result = repairUserMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public List<RepairUser> getRepairs(List<Integer> ids) {
        QueryWrapper<RepairUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids);
        List<RepairUser> result = repairUserMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<RepairUser> getRepairByUserZZ(List<Integer> userZZId, Integer schoolId) {
        QueryWrapper<RepairUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_zzid", userZZId);
        if (schoolId != null) {
            queryWrapper.eq("school_id", schoolId);
        }
        List<RepairUser> result = repairUserMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<RepairLogisticsVo> queryLogisticsList(Integer recordId) {
        List<RepairLogisticsVo> result = repairUserMapper.queryLogisticsList(recordId);
        return result;
    }

    @Override
    public UserDataVo queryUserData(Integer userId, String dateNow) {
        UserDataVo result = repairUserMapper.queryUserData(userId, dateNow);
        return result;
    }

    @Override
    public PageUtils<CollaboratorVo> queryCollaborator(int currentPage, int pageCount, Integer userId, Integer schoolId, String keyWord) {
        Page<CollaboratorVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<CollaboratorVo> result = repairUserMapper.queryCollaborator(page, userId, schoolId, keyWord);
        return new PageUtils(result);
    }

    @Override
    public List<AddressBookVo> queryAddressBook(Integer userId, Integer schoolId, String keyWord) {
        List<AddressBookVo> result = repairUserMapper.queryAddressBook(schoolId, keyWord);
        return result;
    }

    @Override
    public PageUtils<AddressBookVo> queryUserPageBySchoolId(int currentPage, int pageCount, Integer schoolId, Integer userId) {
        Page<AddressBookVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<AddressBookVo> result = repairUserMapper.queryUserPageBySchoolId(page, schoolId, userId);
        return new PageUtils(result);
    }

    @Override
    public List<AutoDispatchUserVo> autoDispatchUser(Integer schoolId, String dateNow, Integer articleId, Integer buildId) {
        List<AutoDispatchUserVo> result = repairUserMapper.autoDispatchUser(schoolId, dateNow, articleId, buildId, null);

        return result;
    }

    @Override
    public PageUtils<AutoDispatchUserVo> autoDispatchUser(int currentPage, int pageCount, Integer schoolId, String dateNow, Integer articleId, Integer buildId, String keyWord) {
        Page<AutoDispatchUserVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<AutoDispatchUserVo> result = repairUserMapper.autoDispatchUser(page, schoolId, dateNow, articleId, buildId, keyWord);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<AutoOfflineUserVo> autoOfflineRepairUser(int currentPage, int pageCount, Integer schoolId, String keyWord) {
        Page<AutoOfflineUserVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<AutoOfflineUserVo> result = repairUserMapper.autoOfflineRepairUser(page, schoolId, keyWord);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<RepairWorkTypeVo> queryCollaboratorUser(int currentPage, int pageCount, Integer schoolId, Integer userId, String dateNow, String keyWord) {
        Page<RepairWorkTypeVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<RepairWorkTypeVo> result = repairUserMapper.queryCollaboratorUser(page, schoolId, userId, dateNow, keyWord);
        return new PageUtils(result);
    }

    @Override
    public PageUtils<PositiveReviewVo> queryPositiveReviewPage(int currentPage, int pageCount, Integer schoolId) {
        Page<PositiveReviewVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<PositiveReviewVo> result = repairUserMapper.queryPositiveReviewPage(page, schoolId);
        return new PageUtils(result);
    }

    @Override
    public List<AutoDispatchUserVo> dropDown(Integer buildId, Integer articleId, Integer schoolId, Integer userId) {

        List<AutoDispatchUserVo> vos = repairUserMapper.dropDown(buildId, articleId, schoolId, userId);
        return vos;
    }

    @Override
    public RepairUser queryUserByOpenid(String openId) {
        QueryWrapper<RepairUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid", openId);
        RepairUser rus = repairUserMapper.selectOne(queryWrapper);
        return rus;
    }


    @Override
    public List<ArticleBuildVo> queryArticleBuilds(List<Integer> userIds) {

        List<ArticleBuildVo> articleBuilds = repairUserMapper.queryArticleBuilds(userIds);
        return articleBuilds;
    }

    @Override
    public PageUtils<RepairUser> queryUserPage(int currentPage, int pageCount,  Integer schoolId, String keyWord) {
        List<Integer> userZZs = new ArrayList<>();
        userZZs.add(1);
        userZZs.add(2);
        Page<RepairUser> page = new Page<>(currentPage, pageCount);
        QueryWrapper<RepairUser> userWrapper = new QueryWrapper<>();
        userWrapper.in("user_zzid", userZZs);
        userWrapper.eq("school_id", schoolId == null ? 1 : schoolId);
        userWrapper.like(keyWord != null, "user_name", keyWord);
        IPage<RepairUser> users = repairUserMapper.selectPage(page, userWrapper);
        return new PageUtils(users);
    }

}
