package vip.xiaonuo.quality.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.quality.entity.QualityReagentInStock;
import vip.xiaonuo.quality.entity.QualityReagentOutStock;
import vip.xiaonuo.quality.entity.QualityReagentStock;
import vip.xiaonuo.quality.mapper.QualityReagentOutStockMapper;
import vip.xiaonuo.quality.mapper.QualityReagentStockMapper;
import vip.xiaonuo.quality.param.QualityStockPageParam;
import vip.xiaonuo.quality.service.IQualityReagentOutStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 质量试剂出库 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-10
 */
@Service
public class QualityReagentOutStockServiceImpl extends ServiceImpl<QualityReagentOutStockMapper, QualityReagentOutStock> implements IQualityReagentOutStockService {

    @Autowired
    private QualityReagentOutStockMapper outStockMapper;
    @Autowired
    private QualityReagentStockMapper stockMapper;

    @Override
    @Transactional
    public CommonResult add(QualityReagentOutStock outStock) {
        QueryWrapper<QualityReagentStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", outStock.getId());
        QualityReagentStock stock = stockMapper.selectOne(queryWrapper);
        Integer integer = stock.getStock();
        if (integer < outStock.getInValue()) {
            return CommonResult.error("出库数量大于库存量");
        }
        QualityReagentOutStock qualityReagentOutStock = new QualityReagentOutStock();
        qualityReagentOutStock.setCreateTime(outStock.getCreateTime());
        qualityReagentOutStock.setInValue(outStock.getInValue());
        qualityReagentOutStock.setOrgId(outStock.getOrgId());
        qualityReagentOutStock.setOrgName(outStock.getOrgName());
        qualityReagentOutStock.setReagent(outStock.getReagent());
        qualityReagentOutStock.setReagentId(outStock.getId());
        qualityReagentOutStock.setReason(outStock.getReason());
        outStockMapper.insert(qualityReagentOutStock);
        stock.setStock(stock.getStock() - outStock.getInValue());
        stock.setOutTime(new Date());
        stockMapper.updateById(stock);
        return CommonResult.ok();
    }

    @Override
    public CommonResult edit(QualityReagentOutStock outStock) {
        QualityReagentOutStock outStock1 = outStockMapper.selectById(outStock);
        QueryWrapper<QualityReagentStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", outStock.getReagentId());
        QualityReagentStock stock = stockMapper.selectOne(queryWrapper);
        if (stock.getStock()+outStock1.getInValue() < outStock.getInValue()) {
            return CommonResult.error("出库数量大于库存量");
        }

        stock.setStock(stock.getStock() + outStock1.getInValue()-outStock.getInValue());
        stock.setInTime(new Date());
        stockMapper.updateById(stock);
        outStockMapper.updateById(outStock);
        return CommonResult.ok();
    }

    @Override
    public Page<QualityReagentOutStock> page(QualityStockPageParam pageParam) {
        Integer pageSize = pageParam.getPageSize();
        Integer pageNum = pageParam.getPageNum();
        String orgId = pageParam.getOrgId();
        String reagent = pageParam.getReagent();
        Integer reagentId = pageParam.getId();
        QueryWrapper<QualityReagentOutStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.eq(StringUtils.isNotBlank(reagent), "reagent", reagent);
        if (null != reagentId) {
            queryWrapper.eq("reagent_id", reagentId);
        }
        queryWrapper.orderByDesc("create_time");
        Page<QualityReagentOutStock> page = new Page<>(pageNum, pageSize);
        return outStockMapper.selectPage(page,queryWrapper);
    }
}
