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.RepairReceivingMapper;
import com.repair.model.enumModel.eDispatchResult;
import com.repair.model.pojo.RepairReceiving;
import com.repair.model.result.PageUtils;
import com.repair.services.RepairReceivingService;
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 RepairReceivingServiceImpl extends ServiceImpl<RepairReceivingMapper, RepairReceiving> implements RepairReceivingService {

    @Autowired
    private RepairReceivingMapper repairReceivingMapper;

    @Override
    public int insertRepairReceiving(RepairReceiving rr) {
        int result = repairReceivingMapper.insert(rr);
        return result;
    }

    @Override
    public int updateRepairReceiving(RepairReceiving rr) {
        int result = repairReceivingMapper.updateById(rr);
        return result;
    }

    @Override
    public PageUtils<RepairReceiving> queryPageRepairReceivings(int currentPage, int pageCount, String name) {
        Page<RepairReceiving> page = new Page<>(currentPage, pageCount);
        QueryWrapper<RepairReceiving> queryWrapper = new QueryWrapper<>();
        IPage<RepairReceiving> result = repairReceivingMapper.selectPage(page, queryWrapper);
        return new PageUtils(result);
    }

    @Override
    public int deleteRepairReceivingById(int id) {
        int result = repairReceivingMapper.deleteById(id);
        return result;
    }

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

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

    @Override
    public int updateRepairByRecordId(Integer recordId,RepairReceiving rr) {
        QueryWrapper<RepairReceiving> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id",recordId);
        queryWrapper.eq("is_lose_efficacy",0);
        int result = repairReceivingMapper.update(rr,queryWrapper);
        return result;
    }

    @Override
    public boolean updateReceivingBatch(List<RepairReceiving> rdr) {
        boolean result = this.updateBatchById(rdr);
        return result;
    }

    @Override
    public List<RepairReceiving> queryReceivingByRecordId(Integer recordId) {
        LambdaQueryWrapper<RepairReceiving> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairReceiving::getRecordId, recordId)
                .eq(RepairReceiving::getIsLoseEfficacy, eDispatchResult.NotExpired.getValue());

        List<RepairReceiving> list = repairReceivingMapper.selectList(wrapper);

        return list;
    }

    @Override
    public int updateReceiving(Integer recordId, RepairReceiving rdr) {
        QueryWrapper<RepairReceiving> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("record_id",recordId);
        int result = repairReceivingMapper.update(rdr,queryWrapper);
        return result;
    }
}
