package com.casic.missiles.modular.system.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.system.dao.DeviceModelsMapper;
import com.casic.missiles.modular.system.model.DeviceModels;
import com.casic.missiles.modular.system.model.DeviceType;
import com.casic.missiles.modular.system.service.IDeviceModelsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.StringUtil;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 设备型号表 服务实现类
 * </p>
 *
 * @author lwh123
 * @since 2019-05-23
 */
@Service
@Slf4j
public class DeviceModelsServiceImpl extends ServiceImpl<DeviceModelsMapper, DeviceModels> implements IDeviceModelsService {

    @Override
    public DeviceModels selectDeviceModelsBymodelName(String modelName) {
        QueryWrapper<DeviceModels> query = new QueryWrapper();
        query.eq("model_name", modelName);
        return this.baseMapper.selectOne(query);
    }

    @Override
    public int countDeviceByModels(Serializable modelId) {
        return this.baseMapper.countDeviceByModels(modelId);
    }

    @Override
    public List<DeviceModels> selectList(Long deviceType) {
        return this.baseMapper.selectModelList(deviceType);
    }

    @Override
    public DeviceModels selectDeviceTypeById(String id) {
        return this.baseMapper.selectById(id);
    }

    public ResponseData getListPage(Page<DeviceModels> page, String deviceType, String communication) {
        ResponseData responseData = new ResponseData();
        try {
            List<DeviceModels> deviceModelsList = this.baseMapper.getListPage(page,StringUtils.isEmpty(deviceType)?-1:Long.valueOf(deviceType), communication);
            Map<String, String> dictMap = this.baseMapper.getDictNameByCode();
            List<Map<String,String>> deviceTypeList= this.baseMapper.getDeviceTypeList();
            Map<String, Object> deviceTypeMap = deviceTypeList.stream().collect(
                    Collectors.toMap(e -> String.valueOf(e.get("id")), e -> String.valueOf(e.get("typeName")))
            );
            deviceModelsList.forEach(
                    deviceModels -> {
                        deviceModels.setDeviceTypeName(String.valueOf(deviceTypeMap.get(String.valueOf(deviceModels.getDeviceType()))));
                        String[] d = deviceModels.getCommunication().split(",");
                        if (d.length > 0) {
                            deviceModels.setCommunNameGroup(dictMap.get(d[0]));
                        }
                        for (int i = 1; i < d.length; i++) {
                            deviceModels.setCommunNameGroup(deviceModels.getCommunNameGroup() + "," + dictMap.get(d[i]));
                        }
                    }
            );
            page.setRecords(deviceModelsList);
            responseData.setMessage(ResponseData.DEFAULT_SUCCESS_MESSAGE);
            responseData.setCode(ResponseData.DEFAULT_SUCCESS_CODE);
        } catch (DataAccessException dae) {
            log.error("主题：查询异常，设备类型{},型号{},异常信息{}", deviceType, communication, dae.getMessage());
            responseData.setMessage(ResponseData.DEFAULT_ERROR_MESSAGE);
            responseData.setCode(ResponseData.DEFAULT_ERROR_CODE);
        }
        return responseData;
    }

    @Override
    public Map<String, String> getDictNameByCode() {
        return this.baseMapper.getDictNameByCode();
    }
}
