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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasedoublediff.model.MeterBaseDoubleDiff;
import com.steerinfo.baseinfo.meterbasedoublediff.mapper.MeterBaseDoubleDiffMapper;
import com.steerinfo.baseinfo.meterbasedoublediff.service.IMeterBaseDoubleDiffService;
import com.steerinfo.util.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;

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

    @Autowired
    private MeterBaseDoubleDiffMapper meterBaseDoubleDiffMapper;

    @Override
    protected IBaseMapper<MeterBaseDoubleDiff, String> getMapper() {
        return meterBaseDoubleDiffMapper;
    }

    @Override
    public String checkandadd(MeterBaseDoubleDiff model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        if (model.getScalePointOneNo().equals(model.getScalePointSecondNo())){
            return "两个秤点相同";
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("scalePointOneNo", model.getScalePointOneNo());
        obj.put("scalePointSecondNo", model.getScalePointSecondNo());
        List<MeterBaseDoubleDiff> com=meterBaseDoubleDiffMapper.selectByParameters(obj);
        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("scalePointOneNo", model.getScalePointSecondNo());
        obj1.put("scalePointSecondNo", model.getScalePointOneNo());
        List<MeterBaseDoubleDiff> com1=meterBaseDoubleDiffMapper.selectByParameters(obj1);
        if (! com.isEmpty() || ! com1.isEmpty()) {
            return model.getScalePointOneName()+"与"+model.getScalePointSecondName()+"已配置双秤差值，请直接修改！！";
        }
        String no = meterBaseDoubleDiffMapper.GetNewID();
        model.setDoubleDiffNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
        meterBaseDoubleDiffMapper.insert(model);
        return "";
    }

    @Override
    public String checkandupdate(MeterBaseDoubleDiff model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("scalePointOneNo", model.getScalePointOneNo());
        obj.put("scalePointSecondNo", model.getScalePointSecondNo());
        obj.put("doubleDiffNo", model.getDoubleDiffNo());
        List<MeterBaseDoubleDiff> com=meterBaseDoubleDiffMapper.selectForUpdate(obj);

        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("scalePointOneNo", model.getScalePointSecondNo());
        obj1.put("scalePointSecondNo", model.getScalePointOneNo());
        obj.put("doubleDiffNo", model.getDoubleDiffNo());

        List<MeterBaseDoubleDiff> com1=meterBaseDoubleDiffMapper.selectForUpdate(obj1);
        if (! com.isEmpty() || ! com1.isEmpty()) {
            return model.getScalePointOneName()+"与"+model.getScalePointSecondName()+"已配置双秤差值，请直接修改！！";
        }
        meterBaseDoubleDiffMapper.updateByPrimaryKey(model);
        return "";
    }

    @Override
    public String Recovery(MeterBaseDoubleDiff model)
    {
        try {
                model.setId(model.getId());
                model.setValueFlag("0");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
               meterBaseDoubleDiffMapper.updateByPrimaryKey(model);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String abolish(MeterBaseDoubleDiff model)
    {
        try {
            model.setId(model.getId());
            model.setValueFlag("1");
            model.setUpdateManName(model.getUpdateManName());
            model.setUpdateManNo(model.getUpdateManNo());
            model.setUpdateTime(new Date());
            meterBaseDoubleDiffMapper.updateByPrimaryKey(model);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    private  String check(MeterBaseDoubleDiff model){
        if (model == null){
            return "数据为空";
        }
        if (StringUtils.isEmpty(model.getBaseSpotNo())){
            return "计量点编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getBaseSpotName())){
            return "计量点名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getScalePointOneNo())){
            return "秤点1编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getScalePointOneName())){
            return "秤点1名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getScalePointSecondNo())){
            return "秤点2编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getScalePointSecondName())){
            return "秤点2名称不能为空！！";
        }
        if (model.getDiffWeight() == null){
            return "差值不能为空！！";
        }
        if(StringUtils.isEmpty(model.getDoubleDiffNo())){
            model.setCreateTime(new Date());
            model.setValueFlag("0");//有效
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
