package com.ruoyi.web.v2.v1.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.v2.v1.model.JsInstrumentMaintenance;
import com.ruoyi.web.v2.v1.model.JsMaintenanceWarning;
import com.ruoyi.web.v2.v1.mapper.JsInstrumentMaintenanceMapper;
import com.ruoyi.web.v2.v1.mapper.JsMaintenanceWarningMapper;
import com.ruoyi.web.v2.v1.service.IJsInstrumentMaintenanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * <p>
 * v2.0检验仪器维护登记 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-23
 */
@Service
public class JsInstrumentMaintenanceServiceImpl extends ServiceImpl<JsInstrumentMaintenanceMapper, JsInstrumentMaintenance> implements IJsInstrumentMaintenanceService {

    @Autowired
    private JsInstrumentMaintenanceMapper maintenanceMapper;
    @Autowired
    private JsMaintenanceWarningMapper warningMapper;

    @Override
    public AjaxResult add(JsInstrumentMaintenance deviceMaintenance) {
        maintenanceMapper.insert(deviceMaintenance);
        String deviceNum = deviceMaintenance.getDeviceNum();
        QueryWrapper<JsMaintenanceWarning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("device_num", deviceNum).orderByDesc("maintenance_time").last(" limit 1");
        JsMaintenanceWarning warning = warningMapper.selectOne(queryWrapper);
        if (ObjectUtil.isNotEmpty(warning)){
            warning.setIsHandle(1);
            warningMapper.updateById(warning);
        }
        return success();
    }

    @Override
    public AjaxResult edit(JsInstrumentMaintenance deviceMaintenance) {
        maintenanceMapper.updateById(deviceMaintenance);
        return success();
    }

    @Override
    public AjaxResult delete(String ids) {
        String[] split = ids.split(",");
        for (String s : split) {
            maintenanceMapper.deleteById(s);
        }
        return success();
    }

    @Override
    public AjaxResult page(Integer pageNum, Integer pageSize, String startTime, String endTime, String deviceNum, String deviceName) {
        Page<JsInstrumentMaintenance> page = new Page<>(pageNum, pageSize);
        QueryWrapper<JsInstrumentMaintenance> queryWrapper = new QueryWrapper<>();
        if ("".equals(startTime) || null == startTime) {

        } else {
            queryWrapper.between("maintenance_time", startTime+" 00:00:00", endTime+" 23:59:59");
        }
        queryWrapper.orderByDesc("maintenance_time");
        queryWrapper.like(StringUtils.isNotEmpty(deviceNum),"device_num", deviceNum)
                .or().like(StringUtils.isNotEmpty(deviceName),"device_name", deviceName);
        return success(maintenanceMapper.selectPage(page, queryWrapper));
    }

    @Override
    public AjaxResult listAll() {
        return success(maintenanceMapper.selectList(null));
    }
}
