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

import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.baseinfo.constants.DbConstants;
import com.steerinfo.baseinfo.meterbasethread.mapper.MeterBaseThreadMapper;
import com.steerinfo.baseinfo.meterbasethread.model.MeterBaseThread;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasedatamigration.model.MeterBaseDataMigration;
import com.steerinfo.baseinfo.meterbasedatamigration.mapper.MeterBaseDataMigrationMapper;
import com.steerinfo.baseinfo.meterbasedatamigration.service.IMeterBaseDataMigrationService;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

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

    @Autowired
    private MeterBaseDataMigrationMapper meterBaseDataMigrationMapper;

    @Override
    protected IBaseMapper<MeterBaseDataMigration, String> getMapper() {
        return meterBaseDataMigrationMapper;
    }


    @Override
    public String checkandadd(MeterBaseDataMigration model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        String no = meterBaseDataMigrationMapper.GetNewID();
        model.setDataMigrationNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
        meterBaseDataMigrationMapper.insert(model);
        return "";
    }

    @Override
    public String Recovery(List<MeterBaseDataMigration> models)
    {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseDataMigration model:models) {
                model.setId(model.getId());
                model.setValueFlag("0");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseDataMigrationMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String abolish(List<MeterBaseDataMigration> models)
    {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseDataMigration model:models) {
                model.setId(model.getId());
                model.setValueFlag("1");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseDataMigrationMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseDataMigration model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        meterBaseDataMigrationMapper.updateByPrimaryKey(model);
        return "";
    }

    private String check (MeterBaseDataMigration model){
        if (model == null){
            return "参数为空！";
        }
        if (StringUtils.isEmpty(model.getCountTypeNo())){
            return "迁出表名不能为空！！";
        }
        if (StringUtils.isEmpty(model.getContrastTypeNo())){
            return "迁入表名不能为空！！";
        }
        if (StringUtils.isEmpty(model.getRateValue())){
            return "迁出条件表达式不能为空！！";
        }

        if(StringUtils.isEmpty(model.getDataMigrationNo())){
            model.setCreateTime(new Date());
            model.setValueFlag("0");//有效
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }

}
