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

import cn.hutool.core.date.DateUtil;
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.web.v2.v1.model.JsTransport;
import com.ruoyi.web.v2.v1.mapper.JsTransportMapper;
import com.ruoyi.web.v2.v1.service.IJsTransportService;
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 java.text.SimpleDateFormat;
import java.util.Date;

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 JsTransportServiceImpl extends ServiceImpl<JsTransportMapper, JsTransport> implements IJsTransportService {

    @Autowired
    private JsTransportMapper transportMapper;

    @Override
    public AjaxResult add(JsTransport transport) {
        if (ObjectUtil.isNotEmpty(transport.getPhone())) {
            if (!PhoneNumberValidator.isValidPhoneNumber(transport.getPhone())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return error("手机号码格式不正确！");
            }
        }
        JsTransport jsTransport = transportMapper.selectOne(new QueryWrapper<JsTransport>().orderByDesc("id").last(" limit 1").between("transport_time", DateUtil.beginOfDay(new Date()),DateUtil.endOfDay(new Date())));
        if (ObjectUtil.isNotEmpty(jsTransport)) {
            String materialBatch = jsTransport.getHarmlessTreatmentId();
            Integer integer = Integer.valueOf(materialBatch);
            String formattedNumber = String.format("%02d", integer+1);
            transport.setHarmlessTreatmentId(formattedNumber);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String format = sdf.format(new Date());
            transport.setHarmlessTreatmentId(format+String.format("%02d", 1));
        }
        transportMapper.insert(transport);
        return success();
    }

    @Override
    public AjaxResult edit(JsTransport transport) {
        if (ObjectUtil.isNotEmpty(transport.getPhone())) {
            if (!PhoneNumberValidator.isValidPhoneNumber(transport.getPhone())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return error("手机号码格式不正确！");
            }
        }
        transportMapper.updateById(transport);
        return success();
    }

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

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

        } else {
            queryWrapper.between("transport_time", startTime, endTime);
        }
        if (null != harmlessTreatmentId) {
            queryWrapper.eq("harmless_treatment_id", harmlessTreatmentId);
        }
        queryWrapper.orderByDesc("transport_time");
        return success(transportMapper.selectPage(page, queryWrapper));
    }

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