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

import com.steerinfo.framework.utils.base.StringUtils;
import com.steerinfo.baseinfo.meterbaseloadpoint.mapper.MeterBaseLoadPointMapper;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbaseloadpoint.model.MeterBaseLoadPoint;
import com.steerinfo.baseinfo.meterbaseloadpoint.service.IMeterBaseLoadPointService;
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;

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

    @Resource
    private MeterBaseLoadPointMapper meterBaseLoadPointMapper;

    @Override
    protected IBaseMapper<MeterBaseLoadPoint, String> getMapper() {
        return meterBaseLoadPointMapper;
    }
    //将model参数检查是否规范后进行新增
    @Override
    public String checkandadd(MeterBaseLoadPoint model)
    {
        if (model == null) {
            return "数据为空！";
        }
        if (StringUtils.isEmpty(model.getLoadPointName())) {
            return "装卸点名称不能为空";
        }
        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("loadPointName", model.getLoadPointName());
        List<MeterBaseLoadPoint> com = meterBaseLoadPointMapper.selectByParameters(obj1);
        if( ! com.isEmpty()) {
            return "该装卸点已存在";
        }
        HashMap<String, Object> obj2 = new HashMap<>();
        obj2.put("meterCode", model.getMeterCode());
        List<MeterBaseLoadPoint> com2 = meterBaseLoadPointMapper.selectByParameters(obj2);
        if( ! com2.isEmpty()) {
            return "该计量点已存在";
        }
        String no=meterBaseLoadPointMapper.GetNewID();
        model.setLoadPointNo(no);
        model.setCreateTime(new Date());
        model.setValidFlag("1");
        meterBaseLoadPointMapper.insert(model);
        return "";
    }


    @Override
    public String checkandupdate(String id, MeterBaseLoadPoint model)
    {
            if (model == null) {
                return "数据为空！";
            }
            if (StringUtils.isEmpty(id)) {
                return "修改前的id为空！";
            }
            if (StringUtils.isEmpty(model.getLoadPointName())) {
                return "装卸点名称不能为空";
            }
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("meterCode", model.getMeterCode());
            obj.put("loadPointNo", id);
            List<MeterBaseLoadPoint> com=meterBaseLoadPointMapper.selectForUpdate(obj);
                if(com != null && com.size() > 0) {
                    return "该计量码已存在！";
                }
            HashMap<String, Object> obj1 = new HashMap<>();
            obj1.put("loadPointName", model.getLoadPointName());
            obj1.put("loadPointNo", id);
            List<MeterBaseLoadPoint> com1=meterBaseLoadPointMapper.selectForUpdate(obj1);
                if( ! com1.isEmpty()) {
                    return "装卸点已存在";
                }
            model.setUpdateTime(new Date());
           meterBaseLoadPointMapper.updateByPrimaryKeySelective(model);
            return "";
    }

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

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

    @Override
    public   List<MeterBaseLoadPoint> queryAll(Map<String,Object> parmas)
    {
        return meterBaseLoadPointMapper.selectByParameters(parmas);
    }
}
