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

import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.baseinfo.combaseinfo.model.ComBaseInfo;
import com.steerinfo.baseinfo.meterbasespotinfo.mapper.MeterBaseSpotInfoMapper;
import com.steerinfo.baseinfo.meterbasespotinfo.model.MeterBaseSpotInfo;
import com.steerinfo.baseinfo.meterbasespotinfo.service.IMeterBaseSpotInfoService;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.utils.base.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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


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

    @Resource
    private ComBaseInfoMapper comBaseInfoMapper;

    @Resource
    private MeterBaseSpotInfoMapper meterBaseSpotInfoMapper;

    @Override
    protected IBaseMapper<MeterBaseSpotInfo, String> getMapper() {
        return meterBaseSpotInfoMapper;
    }

    @Override
    public String checkandupdate(String id,MeterBaseSpotInfo model)
    {
        HashMap<String, Object> map = new HashMap<>();
        map.put("baseCode", model.getSpotTypeNo());
        List<ComBaseInfo> models = comBaseInfoMapper.selectByParameters(map);
        if(models.size() >= 1) {
            model.setSpotTypeName(models.get(0).getBaseName());
        }
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
//            HashMap<String, Object> obj = new HashMap<>();
//            obj.put("baseSpotNo",id);
//            obj.put("baseSpotName",model.getBaseSpotName());
////            obj.put("videoIp",model.getVideoIp());
////            obj.put("ledIp",model.getLedIp());
////            obj.put("printerIp",model.getPrinterIp());
////            obj.put("controlIp",model.getControlIp());
////            obj.put("flatComputerIp",model.getFlatComputerIp());
//        List<MeterBaseSpotInfo> com = meterBaseSpotInfoMapper.selectOrParameters(obj);
//        if (com != null && com.size() > 0) {
//            return "数据重复（名称/Ip），请确认！！";
//        }
        meterBaseSpotInfoMapper.updateByPrimaryKeySelective(model);
        return "";
    }

    @Override
    public RESTfulResult checkandadd(MeterBaseSpotInfo model)
    {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            String msg=check(model);
            if (StringUtils.isNotEmpty(msg)){
                rm.setMessage(msg);
                return rm;
            }

            HashMap<String, Object> obj = new HashMap<>();
            obj.put("baseSpotNo", model.getBaseSpotNo());
            obj.put("baseSpotName",model.getBaseSpotName());
            List<MeterBaseSpotInfo> models = meterBaseSpotInfoMapper.selectOrParameters(obj);
            if (models.size() > 0) {
                rm.setMessage("计量点编号或者名称已存在不能进行新增，请确认！！");
                return rm;
            }
            meterBaseSpotInfoMapper.insertSelective(model);
            rm.setSucceed();
            rm.setCode("200");
            rm.setData(model);
            rm.setMessage("操作成功！！");
            return rm;
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！！>>>" + e.getMessage());
            return  rm;
        }
    }

    @Override
    public String recovery(List<MeterBaseSpotInfo> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseSpotInfo model:models) {
                model.setBaseSpotNo(model.getBaseSpotNo());
                model.setValidFlag("1");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseSpotInfoMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String abolish(List<MeterBaseSpotInfo> models)  {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseSpotInfo model:models) {
                model.setBaseSpotNo(model.getBaseSpotNo());
                model.setValidFlag("0");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseSpotInfoMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public List<MeterBaseSpotInfo> selectall()
    {
        List<MeterBaseSpotInfo> name=meterBaseSpotInfoMapper.selectall();
        return (name);
    }

    @Override
    public List<MeterBaseSpotInfo> selectcorrelation()
    {
        List<MeterBaseSpotInfo> list=meterBaseSpotInfoMapper.selectcorrelation();
        return (list);
    }

    @Override
    public List<MeterBaseSpotInfo> calibcorrelation()
    {
        List<MeterBaseSpotInfo> list=meterBaseSpotInfoMapper.calibcorrelation();
        return (list);
    }

    @Override
    public List<MeterBaseSpotInfo> queryall(Map<String,Object> parmas)
    {
        if (parmas.get("spotTypeNoStr") != null) {
            String[] spotNoStr = parmas.get("spotTypeNoStr").toString().split(",");
            parmas.put("spotNoStr", spotNoStr);
        }
        return meterBaseSpotInfoMapper.queryall(parmas);
    }

    @Override
    public List<MeterBaseSpotInfo> DynamicNoPage(Map<String, Object> parmas) {
        return meterBaseSpotInfoMapper.DynamicAll(parmas);
    }

    private String check(MeterBaseSpotInfo model)
    {
        if (StringUtils.isEmpty(model.getBaseSpotNo())) {
            return "计量点编号不能为空";
        }
        if (StringUtils.isEmpty(model.getBaseSpotName())) {
            return "计量点名称不能为空";
        }
        if (StringUtils.isEmpty(model.getSpotTypeNo())) {
            return "计量点类型编号不能为空";
        }
        if (StringUtils.isEmpty(model.getSpotTypeName())) {
            return "计量点类型编名称不能为空";
        }
        if (StringUtils.isEmpty(model.getSpotStateNo())) {
            return "计量点状态编号不能为空";
        }
        if (StringUtils.isEmpty(model.getSpotStateName())) {
            return "计量点状态名称不能为空";
        }

        model.setCreateTime(new Date());
        model.setValidFlag("1");
        return "";
    }

    @Override
    public MeterBaseSpotInfo doUpdateWf(MeterBaseSpotInfo model){
        meterBaseSpotInfoMapper.updateByPrimaryKeySelective(model);
        return meterBaseSpotInfoMapper.selectByPrimaryKey(model.getBaseSpotNo());
    }

    @Override
    public MeterBaseSpotInfo doUpdateNumWf(MeterBaseSpotInfo model) {
        model.setUsedNum((short) (model.getUsedNum()+1));
        meterBaseSpotInfoMapper.updateByPrimaryKeySelective(model);
        return meterBaseSpotInfoMapper.selectByPrimaryKey(model.getBaseSpotNo());
    }

    @Override
    public List<MeterBaseSpotInfo> TSDGSpotInfo(HashMap<String, Object> param) {
        List<MeterBaseSpotInfo> models = meterBaseSpotInfoMapper.TSDGSpotInfo(param);
        return models;
    }
}
