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

import com.steerinfo.baseinfo.constants.DbConstants;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasetermtarecount.model.MeterBaseTermTareCount;
import com.steerinfo.baseinfo.meterbasetermtarecount.mapper.MeterBaseTermTareCountMapper;
import com.steerinfo.baseinfo.meterbasetermtarecount.service.IMeterBaseTermTareCountService;
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.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    private MeterBaseTermTareCountMapper meterBaseTermTareCountMapper;

    @Override
    protected IBaseMapper<MeterBaseTermTareCount, String> getMapper() {
        return meterBaseTermTareCountMapper;
    }

    @Override
    public String checkandadd(MeterBaseTermTareCount model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("valueFlag","0");//有效
        obj.put("typeNo", model.getTypeNo());//同一类型的只能有一条数据有效
        List<MeterBaseTermTareCount> list=meterBaseTermTareCountMapper.selectByParameters(obj);
        for (MeterBaseTermTareCount list1:list)
        {
            list1.setCountNo(list1.getCountNo());
            list1.setValueFlag("1");//无效
            meterBaseTermTareCountMapper.updateByPrimaryKeySelective(list1);
        }
        String no=meterBaseTermTareCountMapper.GetNewID();
        model.setCountNo(new SimpleDateFormat("yyyyMMdd").format(new Date())+no);
        model.setCreateTime(new Date());
        model.setValueFlag("0");//有效
        model.setCountEdition(new BigDecimal("1.0"));
        meterBaseTermTareCountMapper.insertSelective(model);
        return "";
    }

    @Override
    public String Recovery(MeterBaseTermTareCount model)
    {
        try {
            if (model == null ) {
                return "请传入参数!";
            }
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("valueFlag", DbConstants.VALID);//有效
            List<MeterBaseTermTareCount> list=meterBaseTermTareCountMapper.selectByParameters(obj);
            for (MeterBaseTermTareCount list1:list)
            {
                list1.setCountNo(list1.getCountNo());
                list1.setValueFlag(DbConstants.INVALID);//无效
                meterBaseTermTareCountMapper.updateByPrimaryKeySelective(list1);
            }
            model.setCountNo(model.getCountNo());
            model.setValueFlag("0");
            model.setUpdateTime(new Date());
            meterBaseTermTareCountMapper.updateByPrimaryKeySelective(model);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseTermTareCount model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        model.setUpdateTime(new Date());
        model.setCountEdition(model.getCountEdition().add(new BigDecimal("0.1")));
        meterBaseTermTareCountMapper.updateByPrimaryKey(model);
        return "";
    }

    @Override
    public String abolish(MeterBaseTermTareCount model)
    {
        try {
            if (model == null ) {
                return "请传入参数!";
            }
            model.setCountNo(model.getCountNo());
            model.setValueFlag("1");
            model.setUpdateManName(model.getUpdateManName());
            model.setUpdateManNo(model.getUpdateManNo());
            model.setUpdateTime(new Date());
            meterBaseTermTareCountMapper.updateByPrimaryKey(model);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    private String check (MeterBaseTermTareCount model){
        if (model == null){
            return "数据为空";
        }
        if (StringUtils.isEmpty(model.getContrastTypeNo())){
            return "对比类型编号不能为空！";
        }
        if (StringUtils.isEmpty(model.getContrastTypeName())){
            return "对比类型名称不能为空！";
        }
        if (StringUtils.isEmpty(model.getCountTypeNo())){
            return "计算方式编号不能为空！";
        }
        if (StringUtils.isEmpty(model.getCountTypeName())){
            return "计算方式名称不能为空！";
        }
        return "";
    }
}
