package com.aucma.dms.service.impl;

//import com.hw.common.core.exception.ServiceException;
//import com.hw.common.core.utils.DateUtils;
//import com.hw.common.security.utils.SecurityUtils;
//import com.aucma.dms.domain.DmsBaseTransferInfo;
//import com.aucma.dms.mapper.DmsBaseTransferInfoMapper;
//import com.aucma.dms.service.IDmsBaseTransferInfoService;
//import com.hw.system.api.model.LoginUser;

import com.aucma.common.core.domain.model.LoginUser;
import com.aucma.common.exception.ServiceException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.SecurityUtils;
import com.aucma.dms.domain.DmsBaseTransferInfo;
import com.aucma.dms.mapper.DmsBaseTransferInfoMapper;
import com.aucma.dms.service.IDmsBaseTransferInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 设备转移信息Service业务层处理
 *
 * @author xins
 * @date 2024-01-02
 */
@Service
public class DmsBaseTransferInfoServiceImpl implements IDmsBaseTransferInfoService
{
    @Autowired
    private DmsBaseTransferInfoMapper dmsBaseTransferInfoMapper;

    /**
     * 查询设备转移信息
     *
     * @param transferInfoId 设备转移信息主键
     * @return 设备转移信息
     */
    @Override
    public DmsBaseTransferInfo selectDmsBaseTransferInfoByTransferInfoId(Long transferInfoId)
    {
        return dmsBaseTransferInfoMapper.selectDmsBaseTransferInfoByTransferInfoId(transferInfoId);
    }

    /**
     * 查询设备转移信息列表
     *
     * @param dmsBaseTransferInfo 设备转移信息
     * @return 设备转移信息
     */
    @Override
    public List<DmsBaseTransferInfo> selectDmsBaseTransferInfoList(DmsBaseTransferInfo dmsBaseTransferInfo)
    {
        return dmsBaseTransferInfoMapper.selectDmsBaseTransferInfoList(dmsBaseTransferInfo);
    }

    /**
     * 新增设备转移信息
     *
     * @param dmsBaseTransferInfo 设备转移信息
     * @return 结果
     */
    @Override
    public int insertDmsBaseTransferInfo(DmsBaseTransferInfo dmsBaseTransferInfo)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (dmsBaseTransferInfo.getDevicePositionNew().equals(dmsBaseTransferInfo.getDevicePosition())){
            throw new ServiceException("新位置不能与原位置相同");
        } else if (dmsBaseTransferInfo.getDevicePosition() == null) {
            throw new ServiceException("转移前必须有安装位置");
        }
        //dmsBaseTransferInfoSave是数据库原有的时间，dmsBaseTransferInfo是要新插入的时间
        DmsBaseTransferInfo dmsBaseTransferInfoSave = new DmsBaseTransferInfo();
        dmsBaseTransferInfoSave.setDeviceId(dmsBaseTransferInfo.getDeviceId());
        dmsBaseTransferInfoSave.setDevicePositionNew(dmsBaseTransferInfo.getDevicePosition());
        List<DmsBaseTransferInfo> dmsBaseTransferInfoList = dmsBaseTransferInfoMapper
               .selectDmsBaseTransferInfoList(dmsBaseTransferInfoSave);
        if (dmsBaseTransferInfoList.size() > 0) {
            for (DmsBaseTransferInfo dmsBaseTransferInfoDate : dmsBaseTransferInfoList) {
                Date oldDate = dmsBaseTransferInfoDate.getDeviceTransferTime();
                Date newDate = dmsBaseTransferInfo.getDeviceTransferTime();
                int result = newDate.compareTo(oldDate);
                if (result == -1) {
                    throw new ServiceException("设备转移时间不能小于之前记录的设备转移时间");
                }
            }
        }
        dmsBaseTransferInfo.setCreateBy(loginUser.getUserId());
        dmsBaseTransferInfo.setCreateTime(DateUtils.getNowDate());
        return dmsBaseTransferInfoMapper.insertDmsBaseTransferInfo(dmsBaseTransferInfo);
    }

    /**
     * 修改设备转移信息
     *
     * @param dmsBaseTransferInfo 设备转移信息
     * @return 结果
     */
    @Override
    public int updateDmsBaseTransferInfo(DmsBaseTransferInfo dmsBaseTransferInfo)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        dmsBaseTransferInfo.setUpdateBy(loginUser.getUserId());
        dmsBaseTransferInfo.setUpdateTime(DateUtils.getNowDate());
        return dmsBaseTransferInfoMapper.updateDmsBaseTransferInfo(dmsBaseTransferInfo);
    }

    /**
     * 批量删除设备转移信息
     *
     * @param transferInfoIds 需要删除的设备转移信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseTransferInfoByTransferInfoIds(Long[] transferInfoIds)
    {
        return dmsBaseTransferInfoMapper.deleteDmsBaseTransferInfoByTransferInfoIds(transferInfoIds);
    }

    /**
     * 删除设备转移信息信息
     *
     * @param transferInfoId 设备转移信息主键
     * @return 结果
     */
    @Override
    public int deleteDmsBaseTransferInfoByTransferInfoId(Long transferInfoId)
    {
        return dmsBaseTransferInfoMapper.deleteDmsBaseTransferInfoByTransferInfoId(transferInfoId);
    }
}
