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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasemutualdiff.model.MeterBaseMutualDiff;
import com.steerinfo.baseinfo.meterbasemutualdiff.mapper.MeterBaseMutualDiffMapper;
import com.steerinfo.baseinfo.meterbasemutualdiff.service.IMeterBaseMutualDiffService;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

    @Resource
    private MeterBaseMutualDiffMapper meterBaseMutualDiffMapper;

    @Override
    protected IBaseMapper<MeterBaseMutualDiff, String> getMapper() {
        return meterBaseMutualDiffMapper;
    }

    @Override
    public String checkandadd(MeterBaseMutualDiff model)
    {
        if (model.getBaseSpotOneNo().equals(model.getBaseSpotSecondNo()))
        {
            return "两个计量点相同！！";
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("baseSpotOneNo", model.getBaseSpotOneNo());
        obj.put("baseSpotSecondNo", model.getBaseSpotSecondNo());
        List<MeterBaseMutualDiff> com=meterBaseMutualDiffMapper.selectByParameters(obj);

        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("baseSpotOneNo", model.getBaseSpotSecondNo());
        obj1.put("baseSpotSecondNo", model.getBaseSpotOneNo());
        List<MeterBaseMutualDiff> com1=meterBaseMutualDiffMapper.selectByParameters(obj1);
        if (! com.isEmpty() || ! com1.isEmpty()) {
            return model.getBaseSpotOneName()+"与"+model.getBaseSpotSecondName()+"已配置互检差值，请直接修改！！";
        }
        if (model.getDiffWeight() == null) {
            return "差值不能为空";
        }
        String no = meterBaseMutualDiffMapper.GetNewID();
        model.setId(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
        model.setCreateTime(new Date());
        model.setValueFlag("0");
        meterBaseMutualDiffMapper.insert(model);
        return "";
    }

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

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

    @Override
    public String checkandupdate(MeterBaseMutualDiff model)
    {
        if (model.getBaseSpotOneNo().equals(model.getBaseSpotSecondNo()))
        {
            return "两个计量点相同！！";
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("baseSpotOneNo", model.getBaseSpotOneNo());
        obj.put("baseSpotSecondNo", model.getBaseSpotSecondNo());
        obj.put("mutualDiffNo", model.getMutualDiffNo());
        List<MeterBaseMutualDiff> com=meterBaseMutualDiffMapper.selectForUpdate(obj);

        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("baseSpotOneNo", model.getBaseSpotSecondNo());
        obj1.put("baseSpotSecondNo", model.getBaseSpotOneNo());
        obj.put("mutualDiffNo", model.getMutualDiffNo());
        List<MeterBaseMutualDiff> com1=meterBaseMutualDiffMapper.selectForUpdate(obj1);
        if (! com.isEmpty() || ! com1.isEmpty())
        {
            return model.getBaseSpotOneName()+"与"+model.getBaseSpotSecondName()+"已配置互检差值，请直接修改！！";
        }
        if (model.getDiffWeight() == null) {
            return "差值不能为空";
        }
        model.setUpdateTime(new Date());
        meterBaseMutualDiffMapper.updateByPrimaryKey(model);
        return "";
    }

    @Override
    public List<MeterBaseMutualDiff> doQueryWf(MeterBaseMutualDiff model)
    {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("baseSpotNo", model.getBaseSpotOneNo());
        List<MeterBaseMutualDiff> com=meterBaseMutualDiffMapper.selectBaseSpot(obj);
        return com;
    }
}
