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.RepairAreaMapper;
import com.repair.model.pojo.RepairArea;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.RepairSchoolVo;
import com.repair.services.RepairAreaService;
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-12
 */
@Service
public class RepairAreaServiceImpl extends ServiceImpl<RepairAreaMapper, RepairArea> implements RepairAreaService {

    @Autowired
    private RepairAreaMapper repairAreaMapper;

    @Override
    public int insertRepairArea(RepairArea ra) {
        int result = repairAreaMapper.insert(ra);
        return result;
    }

    @Override
    public int updateRepairArea(RepairArea ra) {
        int result = repairAreaMapper.updateById(ra);
        return result;
    }

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

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

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

    @Override
    public List<RepairSchoolVo> queryRepairSchools() {
        List<RepairSchoolVo> result = repairAreaMapper.queryRepairSchools();
        return result;
    }

    @Override
    public List<RepairArea> getRepairAreas() {
        List<RepairArea> datas = repairAreaMapper.selectList(null);
        return datas;
    }

    @Override
    public RepairArea queryAreaByName(String name, Integer parentId) {
        QueryWrapper<RepairArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("parent_id", parentId);
        RepairArea result = repairAreaMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public RepairArea getRepairByName(String name) {
        QueryWrapper<RepairArea> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        RepairArea result = repairAreaMapper.selectOne(queryWrapper);
        return result;
    }
}
