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.InRecord;
import com.ruoyi.warehouse.mapper.InRecordMapper;
import com.ruoyi.warehouse.mapper.StockInfoMapper;
import com.ruoyi.warehouse.service.IInRecordService;

@Service
public class InRecordServiceImpl implements IInRecordService
{
    @Autowired
    private InRecordMapper inRecordMapper;

    @Autowired
    private StockInfoMapper stockInfoMapper;

    @Autowired
    private ISysUserService userService;

    @Override
    public List<InRecord> selectInRecordList(InRecord inRecord)
    {
        return inRecordMapper.selectInRecordList(inRecord);
    }

    @Override
    public InRecord selectInRecordById(Long id)
    {
        return inRecordMapper.selectInRecordById(id);
    }

    @Override
    public boolean checkInNoUnique(InRecord inRecord)
    {
        Long id = StringUtils.isNull(inRecord.getId()) ? -1L : inRecord.getId();
        InRecord info = inRecordMapper.checkInNoUnique(inRecord.getInNo());
        return StringUtils.isNull(info) || info.getId().longValue() == id.longValue() ? UserConstants.UNIQUE : UserConstants.NOT_UNIQUE;
    }

    @Override
    public int insertInRecord(InRecord inRecord)
    {
        inRecord.setInOperator(resolveNickName(inRecord.getInOperator()));
        validateInRecord(inRecord);
        if (StringUtils.isBlank(inRecord.getInNo()))
        {
            inRecord.setInNo("IN" + System.currentTimeMillis());
        }
        else if (!checkInNoUnique(inRecord))
        {
            throw new ServiceException("入库单号已存在");
        }
        inRecord.setInStatus(0);
        return inRecordMapper.insertInRecord(inRecord);
    }

    @Override
    public int updateInRecord(InRecord inRecord)
    {
        inRecord.setInOperator(resolveNickName(inRecord.getInOperator()));
        validateInRecord(inRecord);
        if (StringUtils.isNotBlank(inRecord.getInNo()) && !checkInNoUnique(inRecord))
        {
            throw new ServiceException("入库单号已存在");
        }
        InRecord dbRecord = inRecordMapper.selectInRecordById(inRecord.getId());
        if (dbRecord != null && Integer.valueOf(1).equals(dbRecord.getInStatus()))
        {
            throw new ServiceException("已入库记录不能修改");
        }
        return inRecordMapper.updateInRecord(inRecord);
    }

    @Override
    public int deleteInRecordByIds(Long[] ids)
    {
        for (Long id : ids)
        {
            InRecord dbRecord = inRecordMapper.selectInRecordById(id);
            if (dbRecord != null && Integer.valueOf(1).equals(dbRecord.getInStatus()))
            {
                throw new ServiceException("已入库记录不能删除");
            }
        }
        return inRecordMapper.deleteInRecordByIds(ids);
    }

    @Override
    @Transactional
    public int confirmInRecord(Long id, String username)
    {
        InRecord inRecord = inRecordMapper.selectInRecordByIdForUpdate(id);
        if (inRecord == null)
        {
            throw new ServiceException("入库记录不存在");
        }
        if (Integer.valueOf(1).equals(inRecord.getInStatus()))
        {
            throw new ServiceException("该入库记录已确认");
        }
        stockInfoMapper.upsertIncreaseStock(inRecord.getAreaId(), inRecord.getProductId(), inRecord.getInNum());
        InRecord confirmRecord = new InRecord();
        confirmRecord.setId(id);
        confirmRecord.setInStatus(1);
        confirmRecord.setOperator(username);
        confirmRecord.setConfirmTime(new Date());
        return inRecordMapper.confirmInRecord(confirmRecord);
    }

    private void validateInRecord(InRecord inRecord)
    {
        if (inRecord.getInNum() == null || inRecord.getInNum().signum() <= 0)
        {
            throw new ServiceException("入库数量必须大于0");
        }
        if (StringUtils.isBlank(inRecord.getInOperator()))
        {
            throw new ServiceException("入库人不能为空");
        }
    }

    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();
    }
}
