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

import com.steerinfo.baseinfo.meterbaseunloadtime.mapper.MeterBaseUnloadTimeMapper;
import com.steerinfo.baseinfo.meterbaseunloadtime.model.MeterBaseUnloadTime;
import com.steerinfo.baseinfo.meterbaseunloadtime.service.IMeterBaseUnloadTimeService;
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 javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

    @Resource
    private MeterBaseUnloadTimeMapper meterBaseUnloadTimeMapper;

    @Override
    protected IBaseMapper<MeterBaseUnloadTime, String> getMapper() {
        return meterBaseUnloadTimeMapper;
    }

    @Override
    public List<MeterBaseUnloadTime> queryAll(HashMap parmas){
        return meterBaseUnloadTimeMapper.selectByParameters(parmas);
    }
    @Override
    public String checkandadd(MeterBaseUnloadTime model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
            obj.put("baseSpotName", model.getBaseSpotName());
            obj.put("baseSpotNo", model.getBaseSpotNo());
            obj.put("loadPointName", model.getLoadPointName());
            obj.put("loadPointNo", model.getLoadPointNo());
        List<MeterBaseUnloadTime> list=meterBaseUnloadTimeMapper.selectByParameters(obj);
        if( ! list.isEmpty())
        {
            return model.getBaseSpotName()+"中的"+model.getLoadPointName()+"的间隔时间已设置，请先删除或者修改！";
        }
        String no=meterBaseUnloadTimeMapper.GetNewID();
        model.setUnloadTimeNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);
        model.setCreateTime(new Date());
        meterBaseUnloadTimeMapper.insert(model);
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseUnloadTime model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("baseSpotName", model.getBaseSpotName());
        obj.put("baseSpotNo", model.getBaseSpotNo());
        obj.put("loadPointName", model.getLoadPointName());
        obj.put("loadPointNo", model.getLoadPointNo());
        obj.put("unloadTimeNo", id);
        List<MeterBaseUnloadTime> list=meterBaseUnloadTimeMapper.selectForUpdate(obj);
        if( ! list.isEmpty())
        {
            return model.getBaseSpotName()+"中的"+model.getLoadPointName()+"的间隔时间已设置，请先删除或者修改！";
        }
        model.setUpdateTime(new Date());
        meterBaseUnloadTimeMapper.updateByPrimaryKey(model);
        return "";
    }

    private String check(MeterBaseUnloadTime model)
    {
        if( model == null){
            return "参数为空！！";
        }
        if (model.getLoadTime() == null){
            return "卸货时间不能为空！！";
        }
        if (StringUtils.isEmpty(model.getBaseSpotNo())){
            return "计量点编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getBaseSpotName())){
            return "计量点名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getLoadPointNo())){
            return "卸货点编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getLoadPointName())){
            return "卸货点名称不能为空！！";
        }
        return "";
    }

}
