package com.steerinfo.baseinfo.meterbasevideoinfo.service.impl;

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasevideoinfo.model.MeterBaseVideoinfo;
import com.steerinfo.baseinfo.meterbasevideoinfo.mapper.MeterBaseVideoinfoMapper;
import com.steerinfo.baseinfo.meterbasevideoinfo.service.IMeterBaseVideoinfoService;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * MeterBaseVideoinfo服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2020-10-15 03:15
 * 类描述
 * 修订历史：
 * 日期：2020-10-15
 * 作者：generator
 * 参考：
 * 描述：MeterBaseVideoinfo服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "meterBaseVideoinfoService")
public class MeterBaseVideoinfoServiceImpl extends BaseServiceImpl<MeterBaseVideoinfo, String> implements IMeterBaseVideoinfoService {

    @Resource
    private MeterBaseVideoinfoMapper meterBaseVideoinfoMapper;

    @Override
    protected IBaseMapper<MeterBaseVideoinfo, String> getMapper() {
        return meterBaseVideoinfoMapper;
    }

    @Override
    public int checkandadd(MeterBaseVideoinfo model)throws MarkerMetException
    {
        check(model);
        MeterBaseVideoinfo mbv=meterBaseVideoinfoMapper.selectByPrimaryKey(model.getPointid());
        if(mbv != null)
        {
            throw  new MarkerMetException(500,"该计量点已经配置视频信息!!");
        }
        int num=meterBaseVideoinfoMapper.insertSelective(model);
        return num;
    }

    @Override
    public int checkandupdate(String id,MeterBaseVideoinfo model)throws MarkerMetException
    {
            check(model);
            if( ! model.getPointid().equals(id))
            {
                throw  new MarkerMetException(500,"计量点不允许修改！");
            }
            int num=meterBaseVideoinfoMapper.updateByPrimaryKeySelective(model);
            return num;
    }

    @Override
    public int checkanddelete(String pointid)
    {
        try{
            MeterBaseVideoinfo mbv=meterBaseVideoinfoMapper.selectByPrimaryKey(pointid);
            if(mbv == null )
            {
                throw  new MarkerMetException(500,"未查找到该条信息！！");
            }
            int num=meterBaseVideoinfoMapper.deleteByPrimaryKey(pointid);
            return num;
        }catch (MarkerMetException e){
            throw  e;
        }
    }

    @Override
    public List<MeterBaseVideoinfo> queryAll(Map<String, Object> parameters)
    {
        return meterBaseVideoinfoMapper.selectByParameters(parameters);
    }


    private void check(MeterBaseVideoinfo model)throws MarkerMetException
    {
       if(model==null) {
           throw  new MarkerMetException(500,"参数为空!!");
       }
       if(StringUtils.isEmpty(model.getPointid())) {
           throw  new MarkerMetException(500,"未获取到计量点信息!!");
       }
        if(StringUtils.isEmpty(model.getVideoip())) {
            throw  new MarkerMetException(500,"硬盘录像机ip不能为空!!");
        }
    }
}
