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


import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasetimespace.model.MeterBaseTimeSpace;
import com.steerinfo.baseinfo.meterbasetimespace.mapper.MeterBaseTimeSpaceMapper;
import com.steerinfo.baseinfo.meterbasetimespace.service.IMeterBaseTimeSpaceService;
import com.steerinfo.framework.utils.base.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    private MeterBaseTimeSpaceMapper meterBaseTimeSpaceMapper;

    @Override
    protected IBaseMapper<MeterBaseTimeSpace, String> getMapper() {
        return meterBaseTimeSpaceMapper;
    }

    @Override
    public String checkandadd(MeterBaseTimeSpace model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("spotTypeNo", model.getSpotTypeNo());
        obj.put("spotTypeName", model.getSpotTypeName());
        obj.put("meterTypeNo", model.getMeterTypeNo());
        obj.put("meterTypeName", model.getMeterTypeName());
        List<MeterBaseTimeSpace> list=meterBaseTimeSpaceMapper.selectByParameters(obj);
        if( ! list.isEmpty()) {
            return model.getSpotTypeName()+"中"+model.getMeterTypeName()+"的时间间隔已设置，请先删除或者修改！";
        }
        String no=meterBaseTimeSpaceMapper.GetNewID();
        model.setTimeSpaceNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);
        model.setCreateTime(new Date());
        meterBaseTimeSpaceMapper.insert(model);
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseTimeSpace model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
            obj.put("spotTypeNo", model.getSpotTypeNo());
            obj.put("spotTypeName", model.getSpotTypeName());
            obj.put("meterTypeNo", model.getMeterTypeNo());
            obj.put("meterTypeName", model.getMeterTypeName());
            obj.put("timeSpaceNo", id);
        List<MeterBaseTimeSpace> list=meterBaseTimeSpaceMapper.selectForUpdate(obj);
        if( ! list.isEmpty()) {
            return model.getSpotTypeName()+"中"+model.getMeterTypeName()+"的时间间隔已设置，请先删除或者直接修改！";
        }
        model.setUpdateTime(new Date());
        meterBaseTimeSpaceMapper.updateByPrimaryKey(model);
        return "";
    }

    private String check(MeterBaseTimeSpace model)
    {
        if(StringUtils.isEmpty(model.getSpotTypeNo())){
            return "计量点类型编号不能为空";
        }
        if(StringUtils.isEmpty(model.getSpotTypeName())){
            return "计量点类型名称不能为空";
        }
        if(StringUtils.isEmpty(model.getMeterTypeNo())){
            return "计量编号不能为空";
        }
        if(StringUtils.isEmpty(model.getMeterTypeName())){
        return "计量名称不能为空";
         }
        return "";
    }
}
