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.entity.JsInStock;
import com.ruoyi.web.v2.v1.entity.JsOutStock;
import com.ruoyi.web.v2.v1.entity.JsStock;
import com.ruoyi.web.v2.v1.mapper.JsOutStockMapper;
import com.ruoyi.web.v2.v1.mapper.JsStockMapper;
import com.ruoyi.web.v2.v1.service.IJsOutStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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-23
 */
@Service
public class JsOutStockServiceImpl extends ServiceImpl<JsOutStockMapper, JsOutStock> implements IJsOutStockService {

    @Autowired
    private JsOutStockMapper jsOutStockMapper;
    @Autowired
    private JsStockMapper stockMapper;


    @Override
    public AjaxResult add(JsOutStock inStock) {
        String materialBatch = inStock.getMaterialBatch();
        QueryWrapper<JsStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("material_batch", materialBatch);
        JsStock jsStock = stockMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(jsStock) || "0".equals(jsStock.getStockAmount())) {
            return error("该批次可领用数量为0");
        }
        String stockAmount = jsStock.getStockAmount();
        if (Double.valueOf(inStock.getStockAmount()) > Double.valueOf(stockAmount)) {
            return error("当前批次可领用数量小于领用数量");
        }
        double v = Double.valueOf(stockAmount) - Double.valueOf(inStock.getStockAmount());
        jsStock.setStockAmount(String.valueOf(v));
        jsStock.setUpdateTime(new Date());
        stockMapper.updateById(jsStock);
        jsOutStockMapper.insert(inStock);
        return success();
    }

    @Override
    public AjaxResult edit(JsOutStock outStock) {
        JsOutStock jsOutStock = jsOutStockMapper.selectById(outStock.getId());
        String materialBatch = outStock.getMaterialBatch();
        QueryWrapper<JsStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("material_batch", materialBatch);
        JsStock jsStock = stockMapper.selectOne(queryWrapper);
        if ("0".equals(jsStock.getStockAmount())) {
            return error("该批次可领用数量为0");
        }
        String stockAmount = jsStock.getStockAmount();
        if (Double.valueOf(outStock.getStockAmount()) > Double.valueOf(stockAmount)+Double.valueOf(jsOutStock.getStockAmount())) {
            return error("可领用数量小于领用数量");
        }
        jsStock.setStockAmount(String.valueOf(Double.valueOf(stockAmount)+Double.valueOf(jsOutStock.getStockAmount()) - Double.valueOf(outStock.getStockAmount())));
        jsStock.setUpdateTime(new Date());
        stockMapper.updateById(jsStock);
        jsOutStockMapper.updateById(outStock);
        return success();
    }

    @Override
    public AjaxResult delete(String ids) {
        return null;
    }

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

        } else {
            queryWrapper.between("out_time",startTime+" 00:00:00", endTime+" 23:59:59");
        }
        queryWrapper.like(StringUtils.isNotEmpty(materialBatch), "material_batch", materialBatch)
                .or().like(StringUtils.isNotEmpty(materialName), "material_name", materialName);
        if (null != materialType) {
            queryWrapper.eq("material_type", materialType);
        }
        queryWrapper.orderByDesc("out_time");
        return success(jsOutStockMapper.selectPage(page, queryWrapper));
    }

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