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

import com.steerinfo.baseinfo.meterbasecalibrarelation.service.impl.MeterBaseCalibraRelationServiceImpl;
import com.steerinfo.baseinfo.meterbasespotinfo.model.MeterBaseSpotInfo;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasecalibrationtime.model.MeterBaseCalibrationTime;
import com.steerinfo.baseinfo.meterbasecalibrationtime.mapper.MeterBaseCalibrationTimeMapper;
import com.steerinfo.baseinfo.meterbasecalibrationtime.service.IMeterBaseCalibrationTimeService;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.framework.utils.base.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.Assert;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private MeterBaseCalibrationTimeMapper meterBaseCalibrationTimeMapper;

    @Override
    protected IBaseMapper<MeterBaseCalibrationTime, String> getMapper() {
        return meterBaseCalibrationTimeMapper;
    }


    @Override
    public String checkandadd(MeterBaseCalibrationTime model)
    {
        if (model == null ) {
            return "请传入参数!";
        }
        if (StringUtils.isEmpty(model.getCalibrationTimeUnit())) {
            return "校秤时间单位不能为空";
        }
        if (StringUtils.isEmpty(model.getCalibrationTypeName())) {
            return "校秤类型名称不能为空";
        }
        String no=meterBaseCalibrationTimeMapper.GetNewID();
        model.setId(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);


        model.setCreateTime(new Date());
        model.setValidFlag("0");
        model.setStateFlag("0");

        meterBaseCalibrationTimeMapper.insertSelective(model);
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseCalibrationTime model)
    {
        try {
            if (model == null ) {
                return "请传入参数!";
            }
            if (StringUtils.isEmpty(model.getCalibrationTimeUnit())) {
                return "校秤时间单位不能为空";
            }
            if (StringUtils.isEmpty(model.getCalibrationTypeName())) {
                return "校秤类型名称不能为空";
            }if ("1".equals(model.getStateFlag())){
                return "该数据已经使用与计量点匹配，无法修改，请废除已绑定数据后再修改！";
            }

            int key1=meterBaseCalibrationTimeMapper.updateByPrimaryKey(model);
            Assert.isTrue(key1 == 1,"更新校称时间错误");


        }catch (Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }
    //meterBaseCalibraRelationService.updateBymbCalibTimeNo(id,model);
    @Override
    public List<MeterBaseCalibrationTime> queryall(Map<String,Object> parmas)
    {
        return meterBaseCalibrationTimeMapper.selectByParameters(parmas);
    }

    @Override
    public String abolish(List<MeterBaseCalibrationTime> models)  {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseCalibrationTime model:models) {
                model.setCalibrationTimeNo(model.getCalibrationTimeNo());
                model.setValidFlag("0");
                model.setDeleteTime(new Date());

                meterBaseCalibrationTimeMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String recovery(List<MeterBaseCalibrationTime> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseCalibrationTime model:models) {
                model.setCalibrationTimeNo(model.getCalibrationTimeNo());
                model.setValidFlag("1");
                model.setDeleteTime(new Date());

                meterBaseCalibrationTimeMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String updateStateByPrimaryKey(String calTimeNo,String flag) {

        if (calTimeNo == null ) {
            return "请传入校称时间编号参数!";
        }
        if (StringUtils.isBlank(flag)){
            return "请传入操作状态!";
        }
        MeterBaseCalibrationTime meterBaseCalibrationTime = new MeterBaseCalibrationTime();
        meterBaseCalibrationTime.setCalibrationTimeNo(calTimeNo);
        //flag=1是绑定校称时间，flag=0是废除（解除）校称时间绑定
        if(flag.equals("1")){
            meterBaseCalibrationTime.setStateFlag("1");
        }else{
            meterBaseCalibrationTime.setStateFlag("0");
        }

        meterBaseCalibrationTimeMapper.updateStateByPrimaryKey(meterBaseCalibrationTime);
        return "";
    }
}
