package com.zd.airbottle.service.impl;

import com.zd.airbottle.domain.QpBottleStorageError;
import com.zd.airbottle.domain.QpTask;
import com.zd.airbottle.domain.QpUseRecord;
import com.zd.airbottle.domain.vo.QpBottleStorageErrorVo;
import com.zd.airbottle.domain.vo.QpBottleStorageRVo;
import com.zd.airbottle.domain.vo.QpBottleStorageVO;
import com.zd.airbottle.domain.vo.QpUseRecordVo;
import com.zd.airbottle.mapper.QpBottleStorageErrorMapper;
import com.zd.airbottle.service.IQpBottleStorageErrorService;
import com.zd.airbottle.service.IQpBottleStorageService;
import com.zd.airbottle.service.IQpTaskService;
import com.zd.airbottle.service.IQpUseRecordService;
import com.zd.common.core.constant.HttpStatus;
import com.zd.common.core.constant.SecurityConstants;
import com.zd.common.core.domain.R;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.utils.bean.TransferUtils;
import com.zd.system.api.RemoteUserService;
import com.zd.system.api.domain.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 气瓶异常记录Service业务层处理
 */
@Service
public class QpBottleStorageErrorServiceImpl implements IQpBottleStorageErrorService {
    @Autowired
    private QpBottleStorageErrorMapper qpBottleStorageErrorMapper;

    @Autowired
    private IQpBottleStorageService storageService;

    @Autowired
    private RemoteUserService userService;

    @Autowired
    private IQpUseRecordService recordService;

    @Autowired
    private IQpTaskService taskService;
    /**
     * 查询气瓶异常记录
     *
     * @param id 气瓶异常记录主键
     * @return 气瓶异常记录
     */
    @Override
    public QpBottleStorageError selectQpBottleStorageErrorById(Long id) {
        return qpBottleStorageErrorMapper.selectQpBottleStorageErrorById(id);
    }

    /**
     * 查询气瓶异常记录列表
     *
     * @param qpBottleStorageError 气瓶异常记录
     * @return 气瓶异常记录
     */
    @Override
    public List<QpBottleStorageError> selectQpBottleStorageErrorList(QpBottleStorageError qpBottleStorageError) {
        return qpBottleStorageErrorMapper.selectQpBottleStorageErrorList(qpBottleStorageError);
    }

    /**
     * 新增气瓶异常记录
     *
     * @param qpBottleStorageError 气瓶异常记录
     * @return 结果
     */
    @Override
    public int insertQpBottleStorageError(QpBottleStorageError qpBottleStorageError) {
        String username = SecurityUtils.getUsername();
        R<SysUser> userResult = userService.getUserInfoByUserName(username, SecurityConstants.INNER);
        if (userResult.getCode()!= HttpStatus.SUCCESS||userResult.getData()==null){
            throw new ServiceException("请求异常");
        }

        //查询实验室id
        QpBottleStorageVO bottleStorageVO = storageService.selectQpBottleStorageById(qpBottleStorageError.getStorageId());
        if(bottleStorageVO!=null){
            QpTask task = taskService.selectQpTaskById(bottleStorageVO.getTaskId());
            if (task!=null){
                qpBottleStorageError.setLocationId(task.getLocationId());
                qpBottleStorageError.setLocation(task.getLocation());
            }
        }

        SysUser data = userResult.getData();
        qpBottleStorageError.setOpTime(Calendar.getInstance().getTime());
        qpBottleStorageError.setCreateBy(data.getNickName());
        qpBottleStorageError.setPhone(data.getPhonenumber());
        return qpBottleStorageErrorMapper.insertQpBottleStorageError(qpBottleStorageError);
    }

    /**
     * 修改气瓶异常记录
     *
     * @param qpBottleStorageError 气瓶异常记录
     * @return 结果
     */
    @Override
    public int updateQpBottleStorageError(QpBottleStorageError qpBottleStorageError) {
        return qpBottleStorageErrorMapper.updateQpBottleStorageError(qpBottleStorageError);
    }

    /**
     * 批量删除气瓶异常记录
     *
     * @param ids 需要删除的气瓶异常记录主键
     * @return 结果
     */
    @Override
    public int deleteQpBottleStorageErrorByIds(Long[] ids) {
        return qpBottleStorageErrorMapper.deleteQpBottleStorageErrorByIds(ids);
    }

    /**
     * 删除气瓶异常记录信息
     *
     * @param id 气瓶异常记录主键
     * @return 结果
     */
    @Override
    public int deleteQpBottleStorageErrorById(Long id) {
        return qpBottleStorageErrorMapper.deleteQpBottleStorageErrorById(id);
    }

    @Override
    public void getBottleInfo(List<QpBottleStorageErrorVo> storageErrorVos) {
        Map<Long, QpBottleStorageRVo> storageRVoMap = getStorageRVoMap(storageErrorVos);
        storageErrorVos.forEach(se->{
            Long storageId = se.getStorageId();
            if (storageRVoMap.containsKey(storageId)){
                QpBottleStorageRVo storageRVo = storageRVoMap.get(storageId);
                se.setAirName(storageRVo.getAirName())
                        .setConfigName(storageRVo.getConfigName());
            }
        });
    }

    @Override
    public QpBottleStorageErrorVo getStorageErrorInfo(Long id) {
        QpBottleStorageError storageError = qpBottleStorageErrorMapper.selectQpBottleStorageErrorById(id);
        QpBottleStorageErrorVo storageErrorVo=new QpBottleStorageErrorVo();
        BeanUtils.copyProperties(storageError,storageErrorVo);
        QpBottleStorageRVo storageRVo = storageService.getById(storageError.getStorageId());
        storageErrorVo.setAirName(storageRVo.getAirName())
                .setConfigName(storageRVo.getConfigName())
                .setCompanyName(storageRVo.getCompanyName())
                .setAirConstituents(storageRVo.getAirConstituents())
                .setAirNumber(storageRVo.getAirNumber());
        List<QpUseRecord> useRecords = recordService.selectQpUseRecordList(new QpUseRecord().setStorageId(storageError.getStorageId()));
        List<QpUseRecordVo> recordVos = TransferUtils.transferList(useRecords, QpUseRecordVo.class);
        storageErrorVo.setRecordVos(recordVos);
        return storageErrorVo;
    }

    private Map<Long, QpBottleStorageRVo> getStorageRVoMap(List<QpBottleStorageErrorVo> storageErrorVos) {
        List<Long> storageIds = storageErrorVos.stream().map(QpBottleStorageErrorVo::getStorageId).collect(Collectors.toList());
        List<QpBottleStorageRVo> storageRVos = storageService.getByIds(storageIds);
        return storageRVos.stream().collect(Collectors.toMap(QpBottleStorageRVo::getId, s -> s));
    }
}
