package com.bonus.sharedstation.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.sharedstation.api.DictDataApi;
import com.bonus.sharedstation.api.vo.DictDataInfoDO;
import com.bonus.sharedstation.web.domain.EmergencyEquipment;
import com.bonus.sharedstation.web.domain.LocationDetail;
import com.bonus.sharedstation.web.dto.EmergencyEquipmentDTO;
import com.bonus.sharedstation.web.mapper.EmergencyEquipmentMapper;
import com.bonus.sharedstation.web.service.IEmergencyEquipmentService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Service实现
 * @author 云归
 * @date 2024-03-15
 */
@Service
@Transactional(readOnly = true)
@Slf4j
public class EmergencyEquipmentServiceImpl extends ServiceImpl<EmergencyEquipmentMapper, EmergencyEquipment> implements IEmergencyEquipmentService {

    @Resource
    private EmergencyEquipmentMapper emergencyEquipmentMapper;

    @Resource
    private DictDataApi dictDataApi;


    @Override
    public AjaxResult getEmergencyEquipmentListApp(EmergencyEquipmentDTO dto) {
        LambdaQueryWrapper<EmergencyEquipment> queryWrapper = new LambdaQueryWrapper<>();
        // 创建分页对象
        Page<EmergencyEquipment> page = new Page<>(dto.getCurrentPage(), dto.getLimit());
        queryWrapper
                .like(EmergencyEquipment::getEquipmentName, dto.getEquipmentName())
                .eq(EmergencyEquipment::getIsDeleted, dto.getIsDeleted())
                .eq(EmergencyEquipment::getEquipmentType, dto.getEquipmentType())
                .orderByDesc(EmergencyEquipment::getCreatedTime);

        Page<EmergencyEquipment> result = emergencyEquipmentMapper.selectPage(page, queryWrapper);
        List<EmergencyEquipment> records = result.getRecords();
        if (StringUtils.isNotEmpty(records)) {
            records.forEach(firstAidInfo -> {
                String instruction = firstAidInfo.getStorageLocation();
                if (StringUtils.isNotEmpty(instruction)) {
                    try {
                        ObjectMapper objectMapper = new ObjectMapper();
                        List<LocationDetail> operationSteps = objectMapper.readValue(instruction, new TypeReference<List<LocationDetail>>() {});
                        if (StringUtils.isNotEmpty(operationSteps)) {
                            firstAidInfo.setLocationDetails(operationSteps);
                        }
                    } catch (IOException e) {
                        log.error("应急设备json 转换步骤失败 查询详情", e);
                        throw new RuntimeException("设备位置查询失败", e);
                    }
                }
            });
        }
        //拿到数据字典
        List<DictDataInfoDO> equipmentTypeList = dictDataApi.getDictionaryInfo("equipment_type");

        if (equipmentTypeList.isEmpty()) {
            throw new ServiceException("请先配置设备数据字典");
        }
        Map<String, String> map = equipmentTypeList.stream()
                .collect(Collectors.toMap(DictDataInfoDO::getDataCode, DictDataInfoDO::getDataValue));
        if (StringUtils.isNotEmpty(records)) {
            records.forEach(emergencyEquipment -> {
                if (map.containsKey(emergencyEquipment.getEquipmentType())) {
                    emergencyEquipment.setEquipmentTypeName(map.get(emergencyEquipment.getEquipmentType()));
                }
            });
        }
        return AjaxResult.success(result);
    }

}
