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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasecarholdtime.model.MeterBaseCarHoldTime;
import com.steerinfo.baseinfo.meterbasecarholdtime.mapper.MeterBaseCarHoldTimeMapper;
import com.steerinfo.baseinfo.meterbasecarholdtime.service.IMeterBaseCarHoldTimeService;
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;

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

    @Autowired
    private MeterBaseCarHoldTimeMapper meterBaseCarHoldTimeMapper;

    @Override
    protected IBaseMapper<MeterBaseCarHoldTime, String> getMapper() {
        return meterBaseCarHoldTimeMapper;
    }

    @Override
    public String checkandadd(MeterBaseCarHoldTime model)
    {
        if (model.getRetentionTime() == null || model.getRetentionTime().equals("")) {
            return "停留时间不能为空";
        }
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("baseSpotName", model.getBaseSpotName());
            obj.put("baseSpotNo", model.getBaseSpotNo());
            List<MeterBaseCarHoldTime> list=meterBaseCarHoldTimeMapper.selectByParameters(obj);
            if( ! list.isEmpty()) {
                return model.getBaseSpotName()+"的滞磅时间已设置，请先删除或者修改！";
            }
            String no=meterBaseCarHoldTimeMapper.GetNewID();
            model.setHoldTimeNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);
            model.setCreateTime(new Date());
            meterBaseCarHoldTimeMapper.insertSelective(model);
            return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseCarHoldTime model)
    {
        if (model.getRetentionTime() == null || model.getRetentionTime().equals("")) {
            return "停留时间不能为空";
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("baseSpotName", model.getBaseSpotName());
        obj.put("baseSpotNo", model.getBaseSpotNo());
        obj.put("holdTimeNo", model.getHoldTimeNo());
        List<MeterBaseCarHoldTime> list=meterBaseCarHoldTimeMapper.selectForUpdate(obj);
        if( ! list.isEmpty()) {
            return model.getBaseSpotName()+"的滞磅时间已设置，请先删除或者修改！";
        }
        model.setUpdateTime(new Date());
        meterBaseCarHoldTimeMapper.updateByPrimaryKey(model);
        return "";
    }
}
