package com.rongwei.bscommon.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.EquMaintenanceTaskDao;
import com.rongwei.bscommon.sys.service.EquMaintenanceTaskService;
import com.rongwei.bsentity.domain.EquMaintenanceTaskDo;
import com.rongwei.bsentity.dto.TaskCreatorDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author shangmi
 * @title EquMaintenanceTaskServiceImpl
 * @date 2023/11/27 15:24
 * @description 保养检修任务业务实现
 */
@Service
public class EquMaintenanceTaskServiceImpl extends ServiceImpl<EquMaintenanceTaskDao,EquMaintenanceTaskDo> implements EquMaintenanceTaskService {

    @Autowired
    private EquMaintenanceTaskDao maintenanceTaskDao;

    /**
     * 查询任务中是否存在该条记录
     *
     * @param taskCreator
     * @return {@link EquMaintenanceTaskDo}
     * @date 2023/11/27 15:37
     * @author shangmi
     */

    @Override
    public List<EquMaintenanceTaskDo> getMaintenanceSite(TaskCreatorDTO taskCreator) {
        LambdaQueryWrapper<EquMaintenanceTaskDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquMaintenanceTaskDo::getEquipmentid, taskCreator.getEquipmentId())
                .eq(EquMaintenanceTaskDo::getPlanyear, taskCreator.getYear())
                .eq(EquMaintenanceTaskDo::getMonth, taskCreator.getMonth())
                .eq(EquMaintenanceTaskDo::getPlantype, taskCreator.getPlanType())
                .eq(EquMaintenanceTaskDo::getDeleted,0);
        return maintenanceTaskDao.selectList(queryWrapper);
    }



    @Override
    public int insertBatch(List<EquMaintenanceTaskDo> equMaintenanceTaskDos) {
        int total = 0;
        for(EquMaintenanceTaskDo info :equMaintenanceTaskDos){
            total +=maintenanceTaskDao.insert(info);
        };
        return total;
    }

    @Override
    public List<EquMaintenanceTaskDo> getListByYearAndMonth(int year, int month) {
        LambdaQueryWrapper<EquMaintenanceTaskDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquMaintenanceTaskDo::getPlanyear,year)
                .eq(EquMaintenanceTaskDo::getMonth,month)
                .eq(EquMaintenanceTaskDo::getDeleted,0);
        return maintenanceTaskDao.selectList(queryWrapper);
    }

    @Override
    public List<EquMaintenanceTaskDo> getListByTimeQuantum(int year, int month, boolean stride) {
        LambdaQueryWrapper<EquMaintenanceTaskDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(EquMaintenanceTaskDo::getTaskstate, Arrays.asList("10","30","70"))
                .eq(EquMaintenanceTaskDo::getDeleted,0);
        //跨月
        queryWrapper.and(stride,query ->query
                        .eq(EquMaintenanceTaskDo::getPlanyear,year)
                        .lt(EquMaintenanceTaskDo::getMonth,month)
                        .or()
                        .eq(EquMaintenanceTaskDo::getPlanyear,year-1)
                        .ge(EquMaintenanceTaskDo::getMonth,month));
        //月末
        queryWrapper.and(!stride,query -> query
                .eq(EquMaintenanceTaskDo::getPlanyear,year)
                .eq(EquMaintenanceTaskDo::getMonth,month));
        return maintenanceTaskDao.selectList(queryWrapper);
    }
}
