package com.repair.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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.repair.mapper.RepairArticleTypeMapper;
import com.repair.model.pojo.RepairArticleType;
import com.repair.model.pojo.RepairLeaveMessage;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.RepairArticleChildVo;
import com.repair.model.vo.RepairWorkTypeVo;
import com.repair.services.RepairArticleTypeService;
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-07-18
 */
@Service
public class RepairArticleTypeServiceImpl extends ServiceImpl<RepairArticleTypeMapper, RepairArticleType> implements RepairArticleTypeService {

    @Autowired
    private RepairArticleTypeMapper repairArticleTypeMapper;

    @Override
    public int insertRepairArticleType(RepairArticleType rat) {
        int result = repairArticleTypeMapper.insert(rat);
        return result;
    }

    @Override
    public int updateRepairArticleType(RepairArticleType rat) {
        int result = repairArticleTypeMapper.updateById(rat);
        return result;
    }

    @Override
    public PageUtils<RepairArticleType> queryPageRepairArticleTypes(int currentPage, int pageCount, String name) {
        Page<RepairArticleType> page = new Page<>(currentPage, pageCount);
        QueryWrapper<RepairArticleType> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name), "name", name);
        IPage<RepairArticleType> result = repairArticleTypeMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

    @Override
    public int deleteRepairArticleTypeById(int id) {
        QueryWrapper<RepairArticleType> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(e -> e.eq("id", id)
                .or()
                .eq("parent_id", id));
        int result = repairArticleTypeMapper.delete(queryWrapper);
        return result;
    }

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

    @Override
    public List<RepairArticleType> getRepairArticleTypes() {
        List<RepairArticleType> datas = repairArticleTypeMapper.selectList(null);
        return datas;
    }

    @Override
    public List<RepairArticleChildVo> getRepairParentArticleTypes(int schoolId) {
        List<RepairArticleChildVo> datas = repairArticleTypeMapper.queryArticleChildList(schoolId);
        return datas;
    }

    @Override
    public RepairArticleType getRepairArticleTypeByName(String name, Integer schoolId) {
        QueryWrapper<RepairArticleType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(name), "name", name);
        queryWrapper.eq(schoolId != null, "school_id", schoolId);
        RepairArticleType datas = repairArticleTypeMapper.selectOne(queryWrapper);
        return datas;
    }

    @Override
    public List<RepairArticleType> getRepairArticleTypesBySchoolId(int schoolId) {
        QueryWrapper<RepairArticleType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("school_id", schoolId);
        List<RepairArticleType> datas = repairArticleTypeMapper.selectList(queryWrapper);
        return datas;
    }

    @Override
    public List<RepairArticleType> getRepairArticlesBySchoolId(int schoolId) {
        QueryWrapper<RepairArticleType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("school_id", schoolId);
        queryWrapper.eq("parent_id", 0);
        List<RepairArticleType> datas = repairArticleTypeMapper.selectList(queryWrapper);
        return datas;
    }

    @Override
    public List<RepairWorkTypeVo> getRepairArticleTypes(int schoolId) {
        List<RepairWorkTypeVo> datas = repairArticleTypeMapper.queryArticleTypeList(schoolId);
        return datas;
    }

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

    @Override
    public boolean updateBatchArticle(List<RepairArticleType> datas) {
        boolean result = this.updateBatchById(datas, datas.size());
        return result;
    }
}
