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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasereturnzero.model.MeterBaseReturnZero;
import com.steerinfo.baseinfo.meterbasereturnzero.mapper.MeterBaseReturnZeroMapper;
import com.steerinfo.baseinfo.meterbasereturnzero.service.IMeterBaseReturnZeroService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

    @Autowired
    private MeterBaseReturnZeroMapper meterBaseReturnZeroMapper;

    @Override
    protected IBaseMapper<MeterBaseReturnZero, String> getMapper() {
        return meterBaseReturnZeroMapper;
    }
    public String checkandadd(MeterBaseReturnZero model)
    {
        if (model.getUpperLimitWeight() == null || model.getUpperLimitWeight().equals("")) {
            return "上限重量不能为空";
        }
        if (model.getLowerLimitWeight() == null || model.getLowerLimitWeight().equals("")) {
            return "上限重量不能为空";
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("baseSpotNo", model.getBaseSpotNo());
        obj.put("baseSpotName", model.getBaseSpotName());
        obj.put("scalePointNo", model.getScalePointNo());
        obj.put("scalePointName", model.getScalePointName());
        List<MeterBaseReturnZero> list=meterBaseReturnZeroMapper.selectByParameters(obj);
        if( ! list.isEmpty()) {
            return model.getBaseSpotName()+"计量点下的"+model.getScalePointName()+"秤点已配置，请先删除或者修改！";
        }
        String no=meterBaseReturnZeroMapper.GetNewID();
        model.setReturnZeroNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);
        model.setCreateTime(new Date());
        meterBaseReturnZeroMapper.insertSelective(model);
        return "";
    }

    public String checkandupdate(String id,MeterBaseReturnZero model)
    {
        if (model.getUpperLimitWeight() == null || model.getUpperLimitWeight().equals("")) {
            return "上限重量不能为空";
        }
        if (model.getLowerLimitWeight() == null || model.getLowerLimitWeight().equals("")) {
            return "上限重量不能为空";
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("baseSpotNo", model.getBaseSpotNo());
        obj.put("baseSpotName", model.getBaseSpotName());
        obj.put("scalePointNo", model.getScalePointNo());
        obj.put("scalePointName", model.getScalePointName());
        obj.put("returnZeroNo", model.getReturnZeroNo());
        List<MeterBaseReturnZero> list=meterBaseReturnZeroMapper.selectForUpdate(obj);
        if( ! list.isEmpty()) {
            return model.getBaseSpotName()+"计量点下的"+model.getScalePointName()+"秤点已配置，请直接修改！";
        }
        model.setUpdateTime(new Date());
        meterBaseReturnZeroMapper.updateByPrimaryKey(model);
        return "";
    }
}
