package com.ruoyi.borrow.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.asset.domain.TbAssetInformation;
import com.ruoyi.asset.domain.enums.ManageStatus;
import com.ruoyi.asset.mapper.TbAssetInformationMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.borrow.mapper.TbAssetBorrowRecordMapper;
import com.ruoyi.borrow.domain.TbAssetBorrowRecord;
import com.ruoyi.borrow.service.ITbAssetBorrowRecordService;

/**
 * 资产借用Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-11
 */
@Service
public class TbAssetBorrowRecordServiceImpl implements ITbAssetBorrowRecordService 
{
    @Autowired
    private TbAssetBorrowRecordMapper tbAssetBorrowRecordMapper;

    @Autowired
    private TbAssetInformationMapper assetInformationMapper;

    @Override
    public String fastReturnByAssetNumber(String assetNumber) {
        // 根据资产编码查询资产
        TbAssetInformation tbAssetInformation = assetInformationMapper.selectTbAssetInformationByAssetNumber(assetNumber);
        // 判断资产是否存在
        if (tbAssetInformation == null) {
            return "资产不存在";
        }

        // 获取资产Id
        Long assetId = tbAssetInformation.getId();

        // 判断资产是否已借出
        TbAssetBorrowRecord isLentRecord = tbAssetBorrowRecordMapper.selectLentRecordByAssetId(assetId);
        if (isLentRecord == null) {
            return "资产已归还";
        }

        // 设置已归还
        isLentRecord.setIsReturn(1L);
        // 设置归还时间
        isLentRecord.setReturnTime(new Date());
        // 设置归还地点：原借出位置
        isLentRecord.setReturnLocationNumber(isLentRecord.getBorrowLocationNumber());
        // 设置备注
        isLentRecord.setRemark("快速归还");
        // 设置更新时间
        isLentRecord.setUpdateTime(DateUtils.getNowDate());

        int updateResult = tbAssetBorrowRecordMapper.updateTbAssetBorrowRecord(isLentRecord);
        if (updateResult > 0) {
            TbAssetInformation assetInformation = new TbAssetInformation();
            assetInformation.setId(isLentRecord.getAssetId());
            assetInformation.setManageStatus(ManageStatus.NORMAL.getCode());
            assetInformationMapper.updateTbAssetInformation(assetInformation);
        }

        return updateResult > 0 ? null : "归还失败";
    }

    /**
     * 查询资产借用
     * 
     * @param id 资产借用主键
     * @return 资产借用
     */
    @Override
    public TbAssetBorrowRecord selectTbAssetBorrowRecordById(Long id)
    {
        return tbAssetBorrowRecordMapper.selectTbAssetBorrowRecordById(id);
    }

    /**
     * 查询资产借用列表
     * 
     * @param tbAssetBorrowRecord 资产借用
     * @return 资产借用
     */
    @Override
    public List<TbAssetBorrowRecord> selectTbAssetBorrowRecordList(TbAssetBorrowRecord tbAssetBorrowRecord)
    {
        return tbAssetBorrowRecordMapper.selectTbAssetBorrowRecordList(tbAssetBorrowRecord);
    }

    /**
     * 新增资产借用
     * 
     * @param tbAssetBorrowRecord 资产借用
     * @return 结果
     */
    @Override
    public int insertTbAssetBorrowRecord(TbAssetBorrowRecord tbAssetBorrowRecord)
    {
        Long assetId = tbAssetBorrowRecord.getAssetId();

        TbAssetInformation tbAssetInformation;
        if (assetId != null) {
            tbAssetInformation = assetInformationMapper.selectTbAssetInformationById(assetId);
            if (tbAssetInformation == null) {
                throw new RuntimeException("资产不存在");
            }
        } else {
            String assetNumber = tbAssetBorrowRecord.getAssetNumber();
            tbAssetInformation = assetInformationMapper.selectTbAssetInformationByAssetNumber(assetNumber);

            if (tbAssetInformation == null) {
                throw new RuntimeException("资产不存在");
            }
            tbAssetBorrowRecord.setAssetId(tbAssetInformation.getId());
        }

        // 判断是否已借出
        TbAssetBorrowRecord isLentRecord = tbAssetBorrowRecordMapper.selectLentRecordByAssetId(tbAssetInformation.getId());
        if (isLentRecord != null) {
            throw new RuntimeException("资产已借出");
        }

        tbAssetBorrowRecord.setBorrowLocationNumber(tbAssetInformation.getLocationNumber());

        tbAssetBorrowRecord.setCreateTime(DateUtils.getNowDate());
        int insertResult = tbAssetBorrowRecordMapper.insertTbAssetBorrowRecord(tbAssetBorrowRecord);
        if (insertResult > 0) {
            TbAssetInformation assetInformation = new TbAssetInformation();
            assetInformation.setId(tbAssetBorrowRecord.getAssetId());
            assetInformation.setManageStatus(ManageStatus.BORROW.getCode());
            assetInformationMapper.updateTbAssetInformation(assetInformation);
        }
        return insertResult;
    }

    /**
     * 修改资产借用
     * 
     * @param tbAssetBorrowRecord 资产借用
     * @return 结果
     */
    @Override
    public int updateTbAssetBorrowRecord(TbAssetBorrowRecord tbAssetBorrowRecord)
    {
        Long isReturn = tbAssetBorrowRecord.getIsReturn();
        Date returnTime = tbAssetBorrowRecord.getReturnTime();
        String returnLocationNumber = tbAssetBorrowRecord.getReturnLocationNumber();

        if (isReturn > 0) { // 已归还
            // 未设置归还时间
            if (returnTime == null) {
                tbAssetBorrowRecord.setReturnTime(new Date());
            }
        } else { // 未归还
            // 根据资产Id判断是否已借出
            TbAssetBorrowRecord isLentRecord = tbAssetBorrowRecordMapper.selectLentRecordByAssetId(tbAssetBorrowRecord.getAssetId());
            // 判断是否非同一个
            if (isLentRecord != null && !tbAssetBorrowRecord.getId().equals(isLentRecord.getId())) {
                throw new RuntimeException("资产已借出");
            }

            // 已设置归还时间
            if (returnTime != null) {
                tbAssetBorrowRecord.setReturnTime(null);
            }
            // 已设置归还位置
            if (StrUtil.isNotBlank(returnLocationNumber)) {
                tbAssetBorrowRecord.setReturnLocationNumber(null);
            }
        }

        tbAssetBorrowRecord.setUpdateTime(DateUtils.getNowDate());
        int updateResult = tbAssetBorrowRecordMapper.updateTbAssetBorrowRecord(tbAssetBorrowRecord);
        if (updateResult > 0) {
            if (isReturn < 1) {
                tbAssetBorrowRecordMapper.updateIsReturn(tbAssetBorrowRecord);
            }

            TbAssetInformation assetInformation = new TbAssetInformation();
            assetInformation.setId(tbAssetBorrowRecord.getAssetId());
            if (isReturn > 0) {
                assetInformation.setManageStatus(ManageStatus.NORMAL.getCode());
            } else {
                assetInformation.setManageStatus(ManageStatus.BORROW.getCode());
            }
            assetInformationMapper.updateTbAssetInformation(assetInformation);
        }
        return 1;
    }

    /**
     * 批量删除资产借用
     * 
     * @param ids 需要删除的资产借用主键
     * @return 结果
     */
    @Override
    public int deleteTbAssetBorrowRecordByIds(Long[] ids)
    {
        return tbAssetBorrowRecordMapper.deleteTbAssetBorrowRecordByIds(ids);
    }

    /**
     * 删除资产借用信息
     * 
     * @param id 资产借用主键
     * @return 结果
     */
    @Override
    public int deleteTbAssetBorrowRecordById(Long id)
    {
        return tbAssetBorrowRecordMapper.deleteTbAssetBorrowRecordById(id);
    }
}
