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.RepairConsumablesMapper;
import com.repair.model.pojo.RepairConsumables;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.ChangeConsumesVo;
import com.repair.model.vo.ConsumableExcelVo;
import com.repair.model.vo.ConsumablePageVo;
import com.repair.services.RepairConsumablesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-07-20
 */
@Service
public class RepairConsumablesServiceImpl extends ServiceImpl<RepairConsumablesMapper, RepairConsumables> implements RepairConsumablesService {

    @Autowired
    private RepairConsumablesMapper repairConsumablesMapper;

    @Override
    public int insertRepairConsumables(RepairConsumables rc) {
        int result = repairConsumablesMapper.insert(rc);
        return result;
    }

    @Override
    public int updateRepairConsumables(RepairConsumables rc) {
        int result = repairConsumablesMapper.updateById(rc);
        return result;
    }

    @Override
    public PageUtils<RepairConsumables> queryPageRepairConsumabless(int currentPage, int pageCount, String name) {
        Page<RepairConsumables> page = new Page<>(currentPage, pageCount);
        QueryWrapper<RepairConsumables> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("update_time");
        IPage<RepairConsumables> result = repairConsumablesMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

    @Override
    public int deleteRepairConsumablesById(int id) {
        int result = repairConsumablesMapper.deleteById(id);
        return result;
    }

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

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

    @Override
    public List<RepairConsumables> getRepairsByRecordIds(List<Integer> recordIds) {
        QueryWrapper<RepairConsumables> queryWrapper = new QueryWrapper();
        queryWrapper.in("record_id", recordIds);
        List<RepairConsumables> result = repairConsumablesMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<RepairConsumables> getRepairsByRecordId(Integer recordIds) {
        QueryWrapper<RepairConsumables> queryWrapper = new QueryWrapper();
        queryWrapper.eq("record_id", recordIds);
        List<RepairConsumables> result = repairConsumablesMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public PageUtils<ConsumablePageVo> queryConsumablePageList(int currentPage, int pageCount, String keyWord, String startTime, String endTime) {
        Page<ConsumablePageVo> page = new Page<>();
        page.setCurrent(currentPage);
        page.setSize(pageCount);
        IPage<ConsumablePageVo> datas = repairConsumablesMapper.queryConsumablePageList(page, keyWord, startTime, endTime);
        return new PageUtils(datas);
    }

    @Override
    public List<ConsumableExcelVo> queryConsumablePageList(String keyWord, String startTime, String endTime) {
        List<ConsumableExcelVo> result = repairConsumablesMapper.queryConsumableExcelList(keyWord, startTime, endTime);
        return result;
    }

    @Override
    public boolean insertConsumableBatch(List<RepairConsumables> rns) {
        boolean result = this.saveBatch(rns, rns.size());
        return result;
    }

    @Override
    public boolean saveorUpdateConsumableBatch(List<RepairConsumables> rns) {
        boolean result = this.saveOrUpdateBatch(rns);
        return result;
    }

    @Override
    public List<ChangeConsumesVo> repairChangeConsumes(Integer recordId) {
        List<ChangeConsumesVo> result = repairConsumablesMapper.repairChangeConsumes(recordId);
        return result;
    }

    @Override
    public int deletedConsumableByIds(List<Integer> ids) {
        QueryWrapper<RepairConsumables> queryWrapper = new QueryWrapper();
        queryWrapper.in("id", ids);
        int result = repairConsumablesMapper.delete(queryWrapper);
        return result;
    }
}
