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.RepairDispatchRecordMapper;
import com.repair.model.enumModel.eDispatchResult;
import com.repair.model.pojo.RepairDispatchRecord;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.DisReceCountVo;
import com.repair.model.vo.RepairCardNumberVo;
import com.repair.services.RepairDispatchRecordService;
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 RepairDispatchRecordServiceImpl extends ServiceImpl<RepairDispatchRecordMapper, RepairDispatchRecord> implements RepairDispatchRecordService {

    @Autowired
    private RepairDispatchRecordMapper repairDispatchRecordMapper;

    @Override
    public int insertRepairDispatchRecord(RepairDispatchRecord rdr) {
        int result = repairDispatchRecordMapper.insert(rdr);
        return rdr.getId();
    }

    @Override
    public int updateRepairDispatchRecord(RepairDispatchRecord rdr) {
        int result = repairDispatchRecordMapper.updateById(rdr);
        return result;
    }

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

    @Override
    public int deleteRepairDispatchRecordById(int id) {
        int result = repairDispatchRecordMapper.deleteById(id);
        return result;
    }

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

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

    @Override
    public List<RepairCardNumberVo> queryRepairCardNumber(Integer recordId) {
        List<RepairCardNumberVo> result = repairDispatchRecordMapper.queryRepairCardNumber(recordId);
        return result;
    }

    @Override
    public List<RepairDispatchRecord> queryRepairDispatch(List<Integer> recordIds) {
        QueryWrapper<RepairDispatchRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("record_id", recordIds);
        queryWrapper.eq("is_lose_efficacy", 0);
        List<RepairDispatchRecord> result = repairDispatchRecordMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public int queryDispatchCount(Integer recordId) {
        QueryWrapper<RepairDispatchRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("record_id", recordId);
        queryWrapper.eq("is_lose_efficacy", 0);
        Integer result = repairDispatchRecordMapper.selectCount(queryWrapper);
        return result;
    }

    @Override
    public RepairDispatchRecord queryOwnRepairDispatch(Integer recordId, Integer userId) {
        QueryWrapper<RepairDispatchRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("record_id", recordId);
        queryWrapper.eq("users_id", userId);
        queryWrapper.eq("is_lose_efficacy", 0);
        RepairDispatchRecord result = repairDispatchRecordMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public boolean insertDispatchBatch(List<RepairDispatchRecord> rdr) {
        boolean result = this.saveBatch(rdr,rdr.size());
        return result;
    }

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

    @Override
    public List<RepairDispatchRecord> queryDispatchByRecordId(Integer recordId) {
        LambdaQueryWrapper<RepairDispatchRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairDispatchRecord::getRecordId, recordId)
                .eq(RepairDispatchRecord::getIsLoseEfficacy, eDispatchResult.NotExpired.getValue());
        List<RepairDispatchRecord> list = repairDispatchRecordMapper.selectList(wrapper);

        return list;
    }

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

    @Override
    public DisReceCountVo queryDisReceCount(Integer userId) {
        DisReceCountVo result = repairDispatchRecordMapper.queryDisReceCount(userId);
        return result;
    }

}
