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

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.JsPatrol;
import com.ruoyi.web.v2.v1.mapper.JsPatrolMapper;
import com.ruoyi.web.v2.v1.service.IJsPatrolService;
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-26
 */
@Service
public class JsPatrolServiceImpl extends ServiceImpl<JsPatrolMapper, JsPatrol> implements IJsPatrolService {

    @Autowired
    private JsPatrolMapper jsPatrolMapper;

    @Override
    public AjaxResult add(JsPatrol jsPatrol) {
        jsPatrolMapper.insert(jsPatrol);
        return success();
    }

    @Override
    public AjaxResult edit(JsPatrol jsPatrol) {
        jsPatrolMapper.updateById(jsPatrol);
        return success();
    }

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

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

        } else {
            endTime = endTime+" 23:59:59";
            queryWrapper.between("patrol_time", startTime, endTime);
        }
        queryWrapper.orderByDesc("patrol_time");
        queryWrapper.like(StringUtils.isNotEmpty(patrolLocation), "patrol_location", patrolLocation);


        return success(jsPatrolMapper.selectPage(page, queryWrapper));
    }

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