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


import com.steerinfo.baseinfo.meterbasecalibrationtime.model.MeterBaseCalibrationTime;
import com.steerinfo.baseinfo.meterbasecalibrationtime.service.impl.MeterBaseCalibrationTimeServiceImpl;
import com.steerinfo.baseinfo.meterbasespotinfo.model.MeterBaseSpotInfo;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasecalibrarelation.model.MeterBaseCalibraRelation;
import com.steerinfo.baseinfo.meterbasecalibrarelation.mapper.MeterBaseCalibraRelationMapper;
import com.steerinfo.baseinfo.meterbasecalibrarelation.service.IMeterBaseCalibraRelationService;
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.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private MeterBaseCalibraRelationMapper meterBaseCalibraRelationMapper;

    @Autowired
    private MeterBaseCalibrationTimeServiceImpl meterBaseCalibrationTimeService;

    @Override
    protected IBaseMapper<MeterBaseCalibraRelation, String> getMapper() {
        return meterBaseCalibraRelationMapper;
    }

    //新增数据标记位
    private  String flag;

    @Override
    public String checkandadd(List<MeterBaseCalibraRelation> models)
    {
        if (models == null || models.size() < 1) {
            return "请传入参数!";
        }
        for (MeterBaseCalibraRelation model:models) {
         /*   HashMap<String, Object> obj = new HashMap<>();
            obj.put("baseSpotNo", model.getBaseSpotNo());
            obj.put("calibrationTypeNo", model.getCalibrationTypeNo());
            List<MeterBaseCalibraRelation> com =meterBaseCalibraRelationMapper.selectByParameters(obj);
            if(! com.isEmpty() ){
                return model.getBaseSpotName()+"已配置校秤类型";
            }*/
            String no = meterBaseCalibraRelationMapper.GetNewID();
            model.setRelationNo(no);
            model.setCreateTime(new Date());
            meterBaseCalibraRelationMapper.insertSelective(model);
        }
        return "";
    }

    @Override
    public String addSpotTime(MeterBaseCalibrationTime mbCalibrationTime, MeterBaseSpotInfo mbSpotInfo) {

        try {
            MeterBaseCalibraRelation mtCalibraRelation = new MeterBaseCalibraRelation();

            if (mbSpotInfo == null ) {
                return "请选择计量点";
            }
            if (StringUtils.isEmpty(mbSpotInfo.getBaseSpotNo())) {
                return "计量点编号不能为空";
            }else{
                mtCalibraRelation.setBaseSpotNo(mbSpotInfo.getBaseSpotNo());
            }
            if (StringUtils.isEmpty(mbSpotInfo.getBaseSpotName())) {
                return "计量点名称不能为空";
            }else{
                mtCalibraRelation.setBaseSpotName(mbSpotInfo.getBaseSpotName());
            }


            if (mbCalibrationTime == null){
                return "请选择校称时间标准";
            }
            Integer validFlag = Integer.parseInt(mbCalibrationTime.getValidFlag());
            if (validFlag == 0){
                return "校称时间不能选择已经废弃的数据，请选择状态是'启用'的校称时间";
            }
            if (StringUtils.isEmpty(mbCalibrationTime.getCalibrationTimeNo())) {
                return "校称时间编号不能为空";
            }else{
                mtCalibraRelation.setCalibrationTimeNo(mbCalibrationTime.getCalibrationTimeNo());
                flag = "1"; //flag = "1"是绑定校称时间，当校称时间与计量点绑定的时候不允许修改校称时间
            }
            if (StringUtils.isEmpty(mbCalibrationTime.getCalibrationTimeUnit())) {
                return "校称时间单位不能为空";
            }else{
                mtCalibraRelation.setCalibrationTimeUnit(mbCalibrationTime.getCalibrationTimeUnit());
            }
            if (mbCalibrationTime.getStartTime() == null) {
                return "校称开始时间不能为空";
            }else{
                mtCalibraRelation.setStartTime(mbCalibrationTime.getStartTime());
            }
            if (mbCalibrationTime.getEndTime() == null) {
                return "校称结束时间不能为空";
            }else{
                mtCalibraRelation.setEndTime(mbCalibrationTime.getEndTime());
            }
            if (mbCalibrationTime.getDayNum() == null) {
                return "校称天数不能为空";
            }else{
                mtCalibraRelation.setDayNum(mbCalibrationTime.getDayNum());
            }
            if (StringUtils.isEmpty(mbCalibrationTime.getCalibrationTypeNo())) {
                return "校称类型编号不能为空";
            }else{
                mtCalibraRelation.setCalibrationTypeNo(mbCalibrationTime.getCalibrationTypeNo());
            }
            if (StringUtils.isEmpty(mbCalibrationTime.getCalibrationTypeName())) {
                return "校称类型名称不能为空";
            }else{
                mtCalibraRelation.setCalibrationTypeName(mbCalibrationTime.getCalibrationTypeName());
            }

            String no = meterBaseCalibraRelationMapper.GetNewID();
            mtCalibraRelation.setId(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);
            //mtCalibraRelation.setRelationNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);
            mtCalibraRelation.setValidFlag("1");
            ////设置创建人、创建时间
            mtCalibraRelation.setCreateManNo(mbCalibrationTime.getCreateManNo());
            mtCalibraRelation.setCreateManName(mbCalibrationTime.getCreateManName());
            mtCalibraRelation.setCreateTime(new Date());

            int key = meterBaseCalibraRelationMapper.insertSelective(mtCalibraRelation);
            String key2 = meterBaseCalibrationTimeService.updateStateByPrimaryKey(mbCalibrationTime.getCalibrationTimeNo(),flag);
            Assert.isTrue((key2 == null || key2.equals("")),"更新关联表错误"+key2);
        }catch (Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public List<MeterBaseCalibraRelation> queryall(Map<String,Object> parmas)
    {
        return meterBaseCalibraRelationMapper.selectByParameters(parmas);
    }

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

                meterBaseCalibraRelationMapper.updateByPrimaryKey(model);

                List<MeterBaseCalibraRelation> mbCalibRelations=meterBaseCalibraRelationMapper.selectByCalibTimeNo(model.getCalibrationTimeNo());
                if(mbCalibRelations.size() < 1){
                    //设置flag="0",解除计量点和校称时间的绑定
                    flag = "0";
                    String key2 = meterBaseCalibrationTimeService.updateStateByPrimaryKey(model.getCalibrationTimeNo(),flag);
                    Assert.isTrue((key2 == null || key2.equals("")),"更新关联表错误"+key2);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String updateBymbCalibTimeNo(String calTimeNo,MeterBaseCalibrationTime models) {
        try{
                if (StringUtils.isEmpty(calTimeNo) && (calTimeNo == models.getCalibrationTimeNo())){
                    return "校称时间编号不能为空";
                }
                List<MeterBaseCalibraRelation> mbCalibRelations=meterBaseCalibraRelationMapper.selectByCalibTimeNo(calTimeNo);
                for (MeterBaseCalibraRelation mbCalibRelation:mbCalibRelations){
                    if (StringUtils.isEmpty(mbCalibRelation.getRelationNo())){
                        return "关联数据不存在！";
                    }else{
                        mbCalibRelation.setCalibrationTimeUnit(models.getCalibrationTimeUnit());
                        mbCalibRelation.setStartTime(models.getStartTime());
                        mbCalibRelation.setEndTime(models.getEndTime());
                        mbCalibRelation.setDayNum(models.getDayNum());
                        mbCalibRelation.setCalibrationTypeNo(models.getCalibrationTypeNo());
                        mbCalibRelation.setCalibrationTypeName(models.getCalibrationTypeName());
                        mbCalibRelation.setUpdateManNo(models.getUpdateManNo());
                        mbCalibRelation.setUpdateManName(models.getUpdateManName());
                        meterBaseCalibraRelationMapper.updateByPrimaryKey(mbCalibRelation);
                    }
                }

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

}
