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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.EquMaintenanceScheduleDao;
import com.rongwei.bscommon.sys.service.EquMaintenanceScheduleService;
import com.rongwei.bsentity.domain.EquMaintenanceScheduleDo;
import com.rongwei.bsentity.dto.BatchCreateDTO;
import com.rongwei.bsentity.dto.TaskCreatorDTO;
import com.rongwei.bsentity.enums.PlannedStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shangmi
 * @title EquMaintenanceScheduleServiceImpl
 * @date 2023/11/27 15:56
 * @description 保养/维护计划业务实现
 */
@Service
public class EquMaintenanceScheduleServiceImpl extends ServiceImpl<EquMaintenanceScheduleDao,EquMaintenanceScheduleDo> implements EquMaintenanceScheduleService {

    @Autowired
    private EquMaintenanceScheduleDao maintenanceScheduleDao;

    /**
     * 通过年、设备ID、保养分类获得保养计划
     *
     * @param taskCreator
     * @return {@link List< EquMaintenanceScheduleDo>}
     * @date 2023/11/27 16:06
     * @author shangmi
     */

    @Override
    public EquMaintenanceScheduleDo getMaintenanceSiteByList(TaskCreatorDTO taskCreator) {
        LambdaQueryWrapper<EquMaintenanceScheduleDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquMaintenanceScheduleDo::getEquipmentid,taskCreator.getEquipmentId())
                .eq(EquMaintenanceScheduleDo::getPlanyear,taskCreator.getYear())
                .eq(EquMaintenanceScheduleDo::getPlantype,taskCreator.getPlanType())
                .last("limit 1");
        return maintenanceScheduleDao.selectOne(queryWrapper);
    }

    /**
     * 批量保存
     *
     * @param maintenanceScheduleList 保存数据
     * @return {@link int}
     * @date 2023/11/30 18:24
     * @author shangmi
     *
     */

    @Override
    public int saveBatchList(List<EquMaintenanceScheduleDo> maintenanceScheduleList) {
        return maintenanceScheduleDao.saveBatchList(maintenanceScheduleList);
    }

    /**
     * 通过年和设备
     *
     * @param split
     * @param batchCreate
     * @return {@link List< EquMaintenanceScheduleDo>}
     * @date 2023/11/30 18:25
     * @author shangmi
     *
     */

    @Override
    public List<EquMaintenanceScheduleDo> getYearAndEquipmentByList(List<String> split, BatchCreateDTO batchCreate) {
        LambdaQueryWrapper<EquMaintenanceScheduleDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(EquMaintenanceScheduleDo::getEquipmentid,split)
                .eq(EquMaintenanceScheduleDo::getPlanyear,batchCreate.getYear())
                .eq(EquMaintenanceScheduleDo::getTenantid,batchCreate.getTenantId());
        return maintenanceScheduleDao.selectList(queryWrapper);
    }

    /**
     * 查询该年下当前工厂的计划
     *
     * @param oldYear
     * @param tenantId
     * @return {@link List<EquMaintenanceScheduleDo>}
     * @date 2023/12/13 10:06
     * @author shangmi
     *
     */
    @Override
    public List<EquMaintenanceScheduleDo> getYearAndTenantIdByList(int oldYear, String tenantId) {
        LambdaQueryWrapper<EquMaintenanceScheduleDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquMaintenanceScheduleDo::getPlanyear,oldYear)
                .eq(EquMaintenanceScheduleDo::getTenantid,tenantId)
                .eq(EquMaintenanceScheduleDo::getDeleted,0);
        return maintenanceScheduleDao.selectList(queryWrapper);
    }

    /**
     * 通过ID查询记录表
     *
     * @param masterIds
     * @return {@link List< EquMaintenanceScheduleDo>}
     * @date 2023/12/13 10:06
     * @author shangmi
     *
     */
    @Override
    public List<EquMaintenanceScheduleDo> getListByIds(List<String> masterIds) {
        LambdaQueryWrapper<EquMaintenanceScheduleDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(EquMaintenanceScheduleDo::getId,masterIds)
                .eq(EquMaintenanceScheduleDo::getDeleted,0);
        return maintenanceScheduleDao.selectList(queryWrapper);
    }
    /**
     * 根据用户ID修改计划状态
     *
     * @param strings
     * @param plannedStatusEnum
     * @return
     * @date 2023/12/13 10:07
     * @author shangmi
     *
     */
    @Override
    public void updatePlannedstatusByIds(List<String> strings, PlannedStatusEnum plannedStatusEnum) {
        LambdaUpdateWrapper<EquMaintenanceScheduleDo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(EquMaintenanceScheduleDo::getId,strings)
                .set(EquMaintenanceScheduleDo::getPlannedstatus,plannedStatusEnum.getValue());
        maintenanceScheduleDao.update(null,updateWrapper);
    }

    @Override
    public List<EquMaintenanceScheduleDo> getYearByList(int year) {
        LambdaQueryWrapper<EquMaintenanceScheduleDo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquMaintenanceScheduleDo::getPlanyear,year)
                .eq(EquMaintenanceScheduleDo::getDeleted,0);
        return maintenanceScheduleDao.selectList(queryWrapper);
    }

    @Override
    public void updateStateByEquId(String equipmentId) {
        LambdaUpdateWrapper<EquMaintenanceScheduleDo> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(EquMaintenanceScheduleDo::getEquipmentid,equipmentId)
                .set(EquMaintenanceScheduleDo::getPlannedstatus,"scrap");
        maintenanceScheduleDao.update(null,updateWrapper);
    }
}
