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


import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasescalerange.model.MeterBaseScaleRange;
import com.steerinfo.baseinfo.meterbasescalerange.mapper.MeterBaseScaleRangeMapper;
import com.steerinfo.baseinfo.meterbasescalerange.service.IMeterBaseScaleRangeService;
import com.steerinfo.framework.utils.base.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;

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

    @Resource
    private MeterBaseScaleRangeMapper meterBaseScaleRangeMapper;

    @Override
    protected IBaseMapper<MeterBaseScaleRange, String> getMapper() {
        return meterBaseScaleRangeMapper;
    }

    @Override
    public RESTfulResult checkandadd(MeterBaseScaleRange model)
    {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            String msg=check(model);
            if (StringUtils.isNotEmpty(msg)){
                rm.setMessage(msg);
                return rm;
            }
            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<MeterBaseScaleRange> list=meterBaseScaleRangeMapper.selectByParameters(obj);
            if(list.size()>0 && list!=null) {
                rm.setMessage(model.getBaseSpotName()+"计量点下的"+model.getScalePointName()+"秤点已存在，请先删除或者修改！");
                return rm;
            }
            String no=meterBaseScaleRangeMapper.GetNewID();
            model.setScaleRangeNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);
            int i = meterBaseScaleRangeMapper.insertSelective(model);
            if(i >= 1) {
                rm.setSucceed();
                rm.setCode("200");
                rm.setMessage("操作成功");
            }
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！>>>" + e.getMessage());
        }
        return rm;

    }

    @Override
    public String checkandupdate(String id,MeterBaseScaleRange model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        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("scaleRangeNo", model.getScaleRangeNo());
        List<MeterBaseScaleRange> list=meterBaseScaleRangeMapper.selectForUpdate(obj);
        if(list.size()>0 && list!=null)
        {
            return model.getBaseSpotName()+"该秤点的量程已配置，请直接进行修改！";
        }
        model.setUpdateTime(new Date());
        meterBaseScaleRangeMapper.updateByPrimaryKey(model);
        return "";
    }

    private String check (MeterBaseScaleRange model)
    {
        if (model.getCounterWeight() == null &&  model.getCounterWeight().equals("")) {
            return "砝码重量不能为空";
        }
        if (model.getCounterWeightDiff() == null && model.getCounterWeightDiff().equals("")) {
            return "砝码校秤允许差值不能为空";
        }
        if (model.getUpperRange() == null &&  model.getUpperRange().equals("")) {
            return "最大量程不能为空";
        }
        if (model.getIndexValue() == null && model.getIndexValue().equals("")) {
            return "仪表分度值不能为空";
        }
        if (model.getUpperLimitClearZero() == null && model.getUpperLimitClearZero().equals("")) {
            return "仪表清零上限不能为空";
        }
        if (model.getLowerLimitClearZero() == null && model.getLowerLimitClearZero().equals("")) {
            return "仪表清零下限不能为空";
        }
        if(StringUtils.isEmpty(model.getScaleRangeNo())){
            model.setCreateTime(new Date());
        }else{
            model.setUpdateTime(new Date());
        }
        return  "";
    }
}
