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.JsOtherProduce;
import com.ruoyi.web.v2.v1.mapper.JsOtherProduceMapper;
import com.ruoyi.web.v2.v1.service.IJsOtherProduceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.web.v2.v1.utils.PhoneNumberValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

/**
 * <p>
 * v2.0其他产品记录 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-26
 */
@Service
public class JsOtherProduceServiceImpl extends ServiceImpl<JsOtherProduceMapper, JsOtherProduce> implements IJsOtherProduceService {

    @Autowired
    private JsOtherProduceMapper otherProduceMapper;

    @Override
    public AjaxResult add(JsOtherProduce otherProduce) {
        if (ObjectUtil.isNotEmpty(otherProduce.getPhone())) {
            if (!PhoneNumberValidator.isValidContact(otherProduce.getPhone())) {

                return error("联系方式格式不正确！");
            }
        }
        otherProduceMapper.insert(otherProduce);
        return success();
    }

    @Override
    public AjaxResult edit(JsOtherProduce otherProduce) {
        if (ObjectUtil.isNotEmpty(otherProduce.getPhone())) {
            if (!PhoneNumberValidator.isValidContact(otherProduce.getPhone())) {

                return error("联系方式格式不正确！");
            }
        }
        otherProduceMapper.updateById(otherProduce);
        return success();
    }

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

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

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

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