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.RepairShiftSettingsMapper;
import com.repair.model.pojo.RepairShiftSettings;
import com.repair.model.result.PageUtils;
import com.repair.services.RepairShiftSettingsService;
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-13
 */
@Service
public class RepairShiftSettingsServiceImpl extends ServiceImpl<RepairShiftSettingsMapper, RepairShiftSettings> implements RepairShiftSettingsService {

    @Autowired
    private RepairShiftSettingsMapper repairShiftSettingsMapper;

    @Override
    public int insertRepairShiftSettings(RepairShiftSettings rss) {
        int result = repairShiftSettingsMapper.insert(rss);
        return result;
    }

    @Override
    public int updateRepairShiftSettings(RepairShiftSettings rss) {
        int result = repairShiftSettingsMapper.updateById(rss);
        return result;
    }

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

    @Override
    public int deleteRepairShiftSettingsById(int id) {
        int result = repairShiftSettingsMapper.deleteById(id);
        return result;
    }


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

    @Override
    public List<RepairShiftSettings> getRepairs() {
        List<RepairShiftSettings> result = repairShiftSettingsMapper.selectList(null);
        return result;
    }

    @Override
    public List<RepairShiftSettings> getRepairsShiftIdByHour(List<String> shiftIds, String dateNow) {
        QueryWrapper<RepairShiftSettings> queryWrapper = new QueryWrapper();
        queryWrapper.in("id", shiftIds);
        queryWrapper.gt("end_time", dateNow);
        queryWrapper.orderByDesc("end_time");
        List<RepairShiftSettings> result = repairShiftSettingsMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<RepairShiftSettings> getRepairsByShiftIds(List<String> shiftIds, String dateNow) {
        QueryWrapper<RepairShiftSettings> queryWrapper = new QueryWrapper();
        queryWrapper.in("id", shiftIds);
        queryWrapper.gt("end_time", dateNow);
        queryWrapper.orderByDesc("end_time");
        List<RepairShiftSettings> result = repairShiftSettingsMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<RepairShiftSettings> getRepairsByShiftIds(List<String> shiftIds) {
        QueryWrapper<RepairShiftSettings> queryWrapper = new QueryWrapper();
        queryWrapper.in("id", shiftIds);
        queryWrapper.orderByDesc("end_time");
        List<RepairShiftSettings> result = repairShiftSettingsMapper.selectList(queryWrapper);
        return result;
    }
}
