package com.ruoyi.warehouse.service.impl;

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.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.warehouse.domain.OutRecord;
import com.ruoyi.warehouse.domain.StockInfo;
import com.ruoyi.warehouse.mapper.OutRecordMapper;
import com.ruoyi.warehouse.mapper.StockInfoMapper;
import com.ruoyi.warehouse.service.IOutRecordService;

@Service
public class OutRecordServiceImpl implements IOutRecordService
{
    @Autowired
    private OutRecordMapper outRecordMapper;

    @Autowired
    private StockInfoMapper stockInfoMapper;

    @Autowired
    private ISysUserService userService;

    @Override
    public List<OutRecord> selectOutRecordList(OutRecord outRecord)
    {
        return outRecordMapper.selectOutRecordList(outRecord);
    }

    @Override
    public OutRecord selectOutRecordById(Long id)
    {
        return outRecordMapper.selectOutRecordById(id);
    }

    @Override
    public boolean checkOutNoUnique(OutRecord outRecord)
    {
        Long id = StringUtils.isNull(outRecord.getId()) ? -1L : outRecord.getId();
        OutRecord info = outRecordMapper.checkOutNoUnique(outRecord.getOutNo());
        return StringUtils.isNull(info) || info.getId().longValue() == id.longValue() ? UserConstants.UNIQUE : UserConstants.NOT_UNIQUE;
    }

    @Override
    @Transactional
    public int insertOutRecord(OutRecord outRecord)
    {
        outRecord.setOutOperator(resolveNickName(outRecord.getOutOperator()));
        if (outRecord.getOutType() == null)
        {
            throw new ServiceException("出库类型不能为空");
        }
        if (outRecord.getOutNum() == null || outRecord.getOutNum().signum() <= 0)
        {
            throw new ServiceException("出库数量必须大于0");
        }
        if (StringUtils.isBlank(outRecord.getOutOperator()))
        {
            throw new ServiceException("出库人不能为空");
        }
        if (StringUtils.isBlank(outRecord.getOutNo()))
        {
            outRecord.setOutNo("OUT" + System.currentTimeMillis());
        }
        else if (!checkOutNoUnique(outRecord))
        {
            throw new ServiceException("出库单号已存在");
        }
        StockInfo stockInfo = stockInfoMapper.selectStockInfoByAreaProductForUpdate(outRecord.getAreaId(), outRecord.getProductId());
        if (stockInfo == null)
        {
            throw new ServiceException("库存不存在，不能出库");
        }
        if (stockInfo.getStockNum().compareTo(outRecord.getOutNum()) < 0)
        {
            throw new ServiceException("库存不足，当前库存：" + stockInfo.getStockNum());
        }
        outRecord.setOutTime(new Date());
        int rows = stockInfoMapper.deductStock(outRecord.getAreaId(), outRecord.getProductId(), outRecord.getOutNum());
        if (rows <= 0)
        {
            throw new ServiceException("库存扣减失败");
        }
        return outRecordMapper.insertOutRecord(outRecord);
    }

    private String resolveNickName(String value)
    {
        if (StringUtils.isBlank(value))
        {
            return value;
        }
        SysUser user = userService.selectUserByUserName(value);
        if (user != null && StringUtils.isNotBlank(user.getNickName()))
        {
            return user.getNickName();
        }
        return value.trim();
    }
}
