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.JsStore;
import com.ruoyi.web.v2.v1.model.JsTransport;
import com.ruoyi.web.v2.v1.mapper.JsStoreMapper;
import com.ruoyi.web.v2.v1.mapper.JsTransportMapper;
import com.ruoyi.web.v2.v1.service.IJsStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

/**
 * <p>
 * v2.0无害化暂存 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-26
 */
@Service
public class JsStoreServiceImpl extends ServiceImpl<JsStoreMapper, JsStore> implements IJsStoreService {

    @Autowired
    private JsStoreMapper jsStoreMapper;
    @Autowired
    private JsTransportMapper transportMapper;

    @Override
    public AjaxResult add(JsStore jsStore) {
        JsStore jsStore1 = jsStoreMapper.selectOne(new QueryWrapper<JsStore>().orderByDesc("id").last(" limit 1").between("store_time", DateUtil.beginOfDay(new Date()),DateUtil.endOfDay(new Date())));
        if (ObjectUtil.isNotEmpty(jsStore1)) {
            String materialBatch = jsStore1.getHarmlessTreatmentId();
            Integer integer = Integer.valueOf(materialBatch);
            String formattedNumber = String.format("%02d", integer+1);
            jsStore.setHarmlessTreatmentId(formattedNumber);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String format = sdf.format(new Date());
            jsStore.setHarmlessTreatmentId(format+String.format("%02d", 1));
        }
        jsStoreMapper.insert(jsStore);
        return success();
    }

    @Override
    public AjaxResult edit(JsStore jsStore) {
        jsStoreMapper.updateById(jsStore);
        return success();
    }

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

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

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

    @Override
    public AjaxResult listAll() {
        List<JsStore> jsStores = jsStoreMapper.selectList(null);
        List<JsStore> list = new ArrayList<>();
        if (jsStores.size() != 0) {
            for (JsStore jsStore : jsStores) {
                QueryWrapper<JsTransport> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("harmless_treatment_id", jsStore.getHarmlessTreatmentId());
                Integer count = transportMapper.selectCount(queryWrapper);
                if (count == 0) {
                    list.add(jsStore);
                }
            }
        }
        return success(list);
    }
}
