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.QualityReagentStock;
import vip.xiaonuo.quality.mapper.QualityReagentInStockMapper;
import vip.xiaonuo.quality.mapper.QualityReagentStockMapper;
import vip.xiaonuo.quality.param.QualityStockPageParam;
import vip.xiaonuo.quality.service.IQualityReagentInStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.Date;

/**
 * <p>
 * 质量试剂入库 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-10
 */
@Service
public class QualityReagentInStockServiceImpl extends ServiceImpl<QualityReagentInStockMapper, QualityReagentInStock> implements IQualityReagentInStockService {


    @Autowired
    private QualityReagentInStockMapper inStockMapper;
    @Autowired
    private QualityReagentStockMapper stockMapper;

    @Override
    @Transactional
    public CommonResult add(QualityReagentInStock inStock) {
        if ("".equals(inStock.getOrgId()) || null == inStock.getOrgId()) {
            return CommonResult.error();
        } else {
            QueryWrapper<QualityReagentStock> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", inStock.getId());
            QualityReagentStock stock = stockMapper.selectOne(queryWrapper);
            QualityReagentInStock qualityReagentInStock = new QualityReagentInStock();
            qualityReagentInStock.setCreateTime(inStock.getCreateTime());
            qualityReagentInStock.setInValue(inStock.getInValue());
            qualityReagentInStock.setOrgId(inStock.getOrgId());
            qualityReagentInStock.setOrgName(inStock.getOrgName());
            qualityReagentInStock.setReagent(inStock.getReagent());
            qualityReagentInStock.setReagentId(inStock.getReagentId());
            inStockMapper.insert(qualityReagentInStock);
            if (ObjectUtil.isEmpty(stock)) {
                QualityReagentStock stock1 = new QualityReagentStock();
                stock1.setOrgId(inStock.getOrgId());
                stock1.setOrgName(inStock.getOrgName());
                stock1.setReagent(inStock.getReagent());
                stock1.setStock(inStock.getInValue());
                stockMapper.insert(stock1);
            } else {
                stock.setStock(stock.getStock() + inStock.getInValue());
                stock.setInTime(new Date());
                stockMapper.updateById(stock);
            }
        }
        return CommonResult.ok();
    }

    @Override
    public CommonResult edit(QualityReagentInStock inStock) {
        QualityReagentInStock inStock1 = inStockMapper.selectById(inStock);
        QueryWrapper<QualityReagentStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", inStock.getReagentId());
        QualityReagentStock stock = stockMapper.selectOne(queryWrapper);
        stock.setStock(stock.getStock() - inStock1.getInValue()+inStock.getInValue());
        stock.setInTime(new Date());
        stockMapper.updateById(stock);
        inStockMapper.updateById(inStock);
        return CommonResult.ok();
    }

    @Override
    public Page<QualityReagentInStock> page(QualityStockPageParam pageParam) {
        Integer pageSize = pageParam.getPageSize();
        Integer pageNum = pageParam.getPageNum();
        String orgId = pageParam.getOrgId();
        String reagent = pageParam.getReagent();
        Integer reagentId = pageParam.getId();
        QueryWrapper<QualityReagentInStock> 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<QualityReagentInStock> page = new Page<>(pageNum, pageSize);
        return inStockMapper.selectPage(page,queryWrapper);
    }
}
