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

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.DeviceTypeMapper;
import com.casic.missiles.modular.system.model.DeviceType;
import com.casic.missiles.modular.system.service.IDeviceTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.expression.AccessException;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 设备类型表 服务实现类
 * </p>
 *
 * @author lwh123
 * @since 2019-05-23
 */
@Slf4j
@Service
public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceType> implements IDeviceTypeService {

    @Override
    public DeviceType selectDeviceTypeByName(String typeName) {
        QueryWrapper<DeviceType> query = new QueryWrapper();
        query.eq("TYPE_NAME", typeName);
        return this.baseMapper.selectOne(query);
    }

    @Override
    public int countDeviceByType(Serializable typeId) {
        return this.baseMapper.countDeviceByType(typeId);
    }

    @Override
    public DeviceType selectDeviceTypeById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public ResponseData listPage(Page<Map<String,Object>> page, String typeName, String isValid) {
        ResponseData responseData = new ResponseData();
        try {
            List<Map<String,Object>> pageList = this.baseMapper.getlistPage(page, typeName, isValid);
            page.setRecords(pageList);
            responseData.setMessage("查询成功");
            responseData.setCode(200);
            responseData.setData(page);
        } catch (DataAccessException dex) {
            log.error("主题：查询分页出现异常，设备类型名称{}，是否激活{},异常信息为{}", typeName, isValid, dex);
            responseData.setCode(500);
            responseData.setMessage("数据库异常");
        }
        return responseData;
    }

}
