package com.ruoyi.warehouse.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.warehouse.domain.StockCheckRecord;
import com.ruoyi.warehouse.domain.StockInfo;
import com.ruoyi.warehouse.mapper.StockCheckRecordMapper;
import com.ruoyi.warehouse.mapper.StockInfoMapper;
import com.ruoyi.warehouse.service.IStockCheckRecordService;

@Service
public class StockCheckRecordServiceImpl implements IStockCheckRecordService
{
    @Autowired
    private StockCheckRecordMapper stockCheckRecordMapper;

    @Autowired
    private StockInfoMapper stockInfoMapper;

    @Override
    public List<StockCheckRecord> selectStockCheckRecordList(StockCheckRecord stockCheckRecord)
    {
        return stockCheckRecordMapper.selectStockCheckRecordList(stockCheckRecord);
    }

    @Override
    public StockCheckRecord selectStockCheckRecordById(Long id)
    {
        return stockCheckRecordMapper.selectStockCheckRecordById(id);
    }

    @Override
    public boolean checkCheckNoUnique(StockCheckRecord stockCheckRecord)
    {
        Long id = StringUtils.isNull(stockCheckRecord.getId()) ? -1L : stockCheckRecord.getId();
        StockCheckRecord info = stockCheckRecordMapper.checkCheckNoUnique(stockCheckRecord.getCheckNo());
        return StringUtils.isNull(info) || info.getId().longValue() == id.longValue() ? UserConstants.UNIQUE : UserConstants.NOT_UNIQUE;
    }

    @Override
    public int insertStockCheckRecord(StockCheckRecord stockCheckRecord)
    {
        if (stockCheckRecord.getRealNum() == null || stockCheckRecord.getRealNum().signum() < 0)
        {
            throw new ServiceException("实际盘点数量不能小于0");
        }
        if (StringUtils.isBlank(stockCheckRecord.getCheckNo()))
        {
            stockCheckRecord.setCheckNo("CHK" + System.currentTimeMillis());
        }
        else if (!checkCheckNoUnique(stockCheckRecord))
        {
            throw new ServiceException("盘点单号已存在");
        }
        StockInfo stockInfo = stockInfoMapper.selectStockInfoByAreaProduct(stockCheckRecord.getAreaId(), stockCheckRecord.getProductId());
        BigDecimal bookNum = stockInfo == null ? BigDecimal.ZERO : stockInfo.getStockNum();
        stockCheckRecord.setBookNum(bookNum);
        stockCheckRecord.setDiffNum(stockCheckRecord.getRealNum().subtract(bookNum));
        stockCheckRecord.setCheckStatus(0);
        return stockCheckRecordMapper.insertStockCheckRecord(stockCheckRecord);
    }

    @Override
    public int updateStockCheckRecord(StockCheckRecord stockCheckRecord)
    {
        StockCheckRecord dbRecord = stockCheckRecordMapper.selectStockCheckRecordById(stockCheckRecord.getId());
        if (dbRecord == null)
        {
            throw new ServiceException("盘点记录不存在");
        }
        if (Integer.valueOf(1).equals(dbRecord.getCheckStatus()))
        {
            throw new ServiceException("已生效盘点记录不能修改");
        }
        stockCheckRecord.setDiffNum(stockCheckRecord.getRealNum().subtract(dbRecord.getBookNum()));
        return stockCheckRecordMapper.updateStockCheckRecord(stockCheckRecord);
    }

    @Override
    public int deleteStockCheckRecordByIds(Long[] ids)
    {
        for (Long id : ids)
        {
            StockCheckRecord dbRecord = stockCheckRecordMapper.selectStockCheckRecordById(id);
            if (dbRecord != null && Integer.valueOf(1).equals(dbRecord.getCheckStatus()))
            {
                throw new ServiceException("已生效盘点记录不能删除");
            }
        }
        return stockCheckRecordMapper.deleteStockCheckRecordByIds(ids);
    }

    @Override
    @Transactional
    public int confirmStockCheckRecord(Long id, String username)
    {
        StockCheckRecord checkRecord = stockCheckRecordMapper.selectStockCheckRecordByIdForUpdate(id);
        if (checkRecord == null)
        {
            throw new ServiceException("盘点记录不存在");
        }
        if (Integer.valueOf(1).equals(checkRecord.getCheckStatus()))
        {
            throw new ServiceException("该盘点记录已生效");
        }
        stockInfoMapper.upsertSetStock(checkRecord.getAreaId(), checkRecord.getProductId(), checkRecord.getRealNum());
        StockCheckRecord confirmRecord = new StockCheckRecord();
        confirmRecord.setId(id);
        confirmRecord.setCheckStatus(1);
        confirmRecord.setConfirmUser(username);
        confirmRecord.setConfirmTime(new Date());
        return stockCheckRecordMapper.confirmStockCheckRecord(confirmRecord);
    }
}
