package com.zd.bottle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zd.bottle.domain.BottleStorage;
import com.zd.bottle.domain.BottleStorageError;
import com.zd.bottle.domain.UseRecord;
import com.zd.bottle.mapper.BottleStorageErrorMapper;
import com.zd.bottle.service.BottleStorageErrorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zd.bottle.service.BottleStorageService;
import com.zd.bottle.service.UseRecordService;
import com.zd.bottle.vo.BottleStorageErrorVo;
import com.zd.bottle.vo.UseRecordVo;
import com.zd.common.core.utils.bean.TransferUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 气瓶异常记录表 服务实现类
 * </p>
 *
 * @author hanson
 * @since 2022-09-08
 */
@Service
public class BottleStorageErrorServiceImpl extends ServiceImpl<BottleStorageErrorMapper, BottleStorageError> implements BottleStorageErrorService {

    @Resource
    private BottleStorageService storageService;
    @Resource
    private UseRecordService useRecordService;

    @Override
    public List<BottleStorageError> list(BottleStorageError bottleStorageError) {
        LambdaQueryWrapper<BottleStorageError> queryWrapper = Wrappers.lambdaQuery(BottleStorageError.class);
        String startTime = bottleStorageError.getStartTime();
        String endTime = bottleStorageError.getEndTime();
        queryWrapper.apply(StringUtils.hasLength(startTime), "DATE_FORMAT(create_time,'%Y-%m-%d') >=date_format('" + startTime + "','%Y-%m-%d')")
                .apply(StringUtils.hasLength(endTime), "DATE_FORMAT(create_time,'%Y-%m-%d') <=date_format('" + endTime + "','%Y-%m-%d')");
        String searchValue = bottleStorageError.getSearchValue();
        if (StringUtils.hasLength(searchValue)) {
            queryWrapper.like(BottleStorageError::getCreateBy, searchValue)
                    .or()
                    .like(BottleStorageError::getPhone, searchValue);
        }

        List<Long> ids = bottleStorageError.getIds();
        if (ids != null && !ids.isEmpty()) {
            queryWrapper.in(BottleStorageError::getId, ids);
        }
        queryWrapper.orderByDesc(true, BottleStorageError::getCreateTime);
        return list(queryWrapper);
    }

    @Override
    public void getBottleInfo(List<BottleStorageErrorVo> storageErrorVos) {
        Map<Long, BottleStorage> storageMap = getStorageMap(storageErrorVos);
        storageErrorVos.forEach(se -> {
            Long storageId = se.getStorageId();
            if (storageMap.containsKey(storageId)) {
                BottleStorage storage = storageMap.get(storageId);
                se.setAirName(storage.getAirName())
                        .setConfigName(storage.getConfigName())
                        .setElectronicTag(storage.getElectronicTag())
                        .setAirNumber(storage.getAirNumber());
            }
        });
    }

    @Override
    public BottleStorageErrorVo getStorageErrorInfo(Long id) {
        BottleStorageError storageError = getById(id);
        BottleStorageErrorVo storageErrorVo = new BottleStorageErrorVo();
        BeanUtils.copyProperties(storageError, storageErrorVo);
        BottleStorage bottleStorage = storageService.getById(storageError.getStorageId());
        storageErrorVo.setAirName(bottleStorage.getAirName())
                .setConfigName(bottleStorage.getConfigName())
                .setAirComponent(bottleStorage.getAirComponent())
                .setAirNumber(bottleStorage.getAirNumber())
                .setElectronicTag(bottleStorage.getElectronicTag());
        List<UseRecord> useRecords = useRecordService.getByStorageId(storageError.getStorageId());
        List<UseRecordVo> recordVos = TransferUtils.transferList(useRecords, UseRecordVo.class);
        storageErrorVo.setRecordVos(recordVos);
        return storageErrorVo;
    }

    private Map<Long, BottleStorage> getStorageMap(List<BottleStorageErrorVo> storageErrorVos) {
        List<Long> storageIds = storageErrorVos.stream().map(BottleStorageErrorVo::getStorageId).collect(Collectors.toList());
        List<BottleStorage> storages = storageService.listByIds(storageIds);
        return storages.stream().collect(Collectors.toMap(BottleStorage::getId, s -> s));
    }
}
