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.JsProduceRecall;
import com.ruoyi.web.v2.v1.mapper.JsProduceRecallMapper;
import com.ruoyi.web.v2.v1.service.IJsProduceRecallService;
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 JsProduceRecallServiceImpl extends ServiceImpl<JsProduceRecallMapper, JsProduceRecall> implements IJsProduceRecallService {

    @Autowired
    private JsProduceRecallMapper produceRecallMapper;

    @Override
    public AjaxResult add(JsProduceRecall produceRecall) {
        produceRecallMapper.insert(produceRecall);
        return success();
    }

    @Override
    public AjaxResult edit(JsProduceRecall produceRecall) {
        produceRecallMapper.updateById(produceRecall);
        return success();
    }

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

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

        } else {
            queryWrapper.between("recall_time",startTime+" 00:00:00", endTime+" 23:59:59");
        }
        queryWrapper.orderByDesc("recall_time");
        queryWrapper.like(StringUtils.isNotEmpty(produceName),"produce_name", produceName);
        queryWrapper.eq(StringUtils.isNotEmpty(animalType),"animal_type", animalType);
        return success(produceRecallMapper.selectPage(page, queryWrapper));
    }

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