package com.ruoyi.web.v2.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.model.JsProducePlan;
import com.ruoyi.web.v2.model.JsSample;
import com.ruoyi.web.v2.mapper.JsSampleMapper;
import com.ruoyi.web.v2.service.IJsSampleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

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

/**
 * <p>
 * v2.0样品留存登记 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-23
 */
@Service
public class JsSampleServiceImpl extends ServiceImpl<JsSampleMapper, JsSample> implements IJsSampleService {

    @Autowired
    private JsSampleMapper sampleMapper;

    @Transactional
    @Override
    public AjaxResult add(JsSample sample) {
        JsSample jsSample = sampleMapper.selectLatestForUpdate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String format = sdf.format(new Date());
        if (ObjectUtil.isNotEmpty(jsSample) && ObjectUtil.isNotEmpty(jsSample.getSampleNum()) && jsSample.getSampleNum().substring(0, 8).equals(format)) {
            String formattedNumber = StringUtils.incrementString(jsSample.getSampleNum(),8);
            sample.setSampleNum(formattedNumber);
        } else {
            sample.setSampleNum(format+String.format("%04d", 1));
        }
        sampleMapper.insert(sample);
        return success();
    }

    @Override
    public AjaxResult edit(JsSample sample) {
        sampleMapper.updateById(sample);
        return success();
    }

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

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

        } else {
            queryWrapper.between("keep_time", startTime+" 00:00:00", endTime+" 23:59:59");
        }
        queryWrapper.orderByDesc("keep_time");
        queryWrapper.eq(StringUtils.isNotEmpty(animalType),"animal_type",animalType);
        queryWrapper.like(StringUtils.isNotEmpty(sampleNum),"sample_num", sampleNum);
        return success(sampleMapper.selectPage(page, queryWrapper));
    }

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


    @Override
    public JsSample getDetail(JsSample jsSample) {
        return sampleMapper.selectForOne(jsSample);
    }
}
