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

import cn.hutool.core.date.DateTime;
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.common.utils.StringUtils;
import com.ruoyi.web.v2.v1.entity.JsStoreTransportInfo;
import com.ruoyi.web.v2.v1.mapper.JsStoreTransportInfoMapper;
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.aspectj.weaver.ast.Var;
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.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;
    @Autowired
    private JsStoreTransportInfoMapper storeTransportInfoMapper;

    @Override
    @Transactional
    public AjaxResult add(JsStore jsStore) {
        DateTime parse = DateUtil.parse(jsStore.getStoreTime(), "yyyy-MM-dd");
        JsStore jsStore1 = jsStoreMapper.selectOne(new QueryWrapper<JsStore>().orderByDesc("id").last(" limit 1")
                .ge("store_time", DateUtil.beginOfDay(parse)).le("store_time",DateUtil.endOfDay(parse)));
        if (ObjectUtil.isNotEmpty(jsStore1)) {
            String materialBatch = jsStore1.getHarmlessTreatmentId();
            Integer integer = Integer.valueOf(materialBatch);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            String format = sdf.format(parse);
            String formattedNumber = String.format("%02d", integer+1);
            jsStore.setHarmlessTreatmentId(formattedNumber);
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String format = sdf.format(parse);
            jsStore.setHarmlessTreatmentId(format+String.format("%02d", 1));
        }
        jsStoreMapper.insert(jsStore);
        saveStoreTransport(jsStore);
        return success();
    }

    private void saveStoreTransport(JsStore jsStore) {
        Double unqualifiedWeight = Double.parseDouble(jsStore.getUnqualifiedWeight());
        JsStoreTransportInfo id = storeTransportInfoMapper.selectOne(new QueryWrapper<JsStoreTransportInfo>().eq("instock_date",DateUtil.format(new Date(),"yyyy-MM-dd")));
        id.setStockNum(String.format("%.2f",unqualifiedWeight +Double.parseDouble(id.getStockNum())));
        id.setInstockNum(String.format("%.2f",unqualifiedWeight +Double.parseDouble(id.getInstockNum())));
        id.setGuanliyuan(jsStore.getUserName());
        storeTransportInfoMapper.updateById(id);
    }


    @Override
    @Transactional
    public AjaxResult edit(JsStore jsStore) {
        JsStore jsStore1 = jsStoreMapper.selectById(jsStore.getId());
        jsStoreMapper.updateById(jsStore);
        jsStore.setUnqualifiedWeight(String.format("%.2f",Double.parseDouble(jsStore.getUnqualifiedWeight())-Double.parseDouble(jsStore1.getUnqualifiedWeight())));
        saveStoreTransport(jsStore);
        return success();
    }

    @Override
    @Transactional
    public AjaxResult delete(String ids) {
        String[] split = ids.split(",");
        for (String s : split) {
            JsStore jsStore1 = jsStoreMapper.selectById(s);
            jsStore1.setUnqualifiedWeight(String.format("%.2f",Double.parseDouble(jsStore1.getUnqualifiedWeight())*-1));
            saveStoreTransport(jsStore1);
            jsStoreMapper.deleteById(s);

        }
        return success();
    }

    @Override
    public AjaxResult page(Integer pageNum, Integer pageSize, String startTime, String endTime, Integer harmlessTreatmentId, String animalType) {
        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.eq(StringUtils.isNotEmpty(animalType),"animal_type", animalType);
        queryWrapper.orderByDesc("store_time").orderByDesc("id");
        return success(jsStoreMapper.selectPage(page, queryWrapper));
    }

    @Override
    public AjaxResult listAll() {
//        List<JsStore> jsStores = jsStoreMapper.selectList(new QueryWrapper<JsStore>().orderByDesc("id"));

        return success(jsStoreMapper.selectList(null));
    }
}
