package com.steerinfo.pretrack.prestoragescale.service.impl;

import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.baseinfo.combaseinfo.model.ComBaseInfo;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.meterwork.constants.DbConstants;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.pretrack.prestoragescale.model.PreStorageScale;
import com.steerinfo.pretrack.prestoragescale.mapper.PreStorageScaleMapper;
import com.steerinfo.pretrack.prestoragescale.service.IPreStorageScaleService;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PreStorageScale服务实现：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2021-09-24 10:55
 * 类描述
 * 修订历史：
 * 日期：2021-09-24
 * 作者：generator
 * 参考：
 * 描述：PreStorageScale服务实现
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@Service(value = "preStorageScaleService")
public class PreStorageScaleServiceImpl extends BaseServiceImpl<PreStorageScale, String> implements IPreStorageScaleService {

    @Autowired
    private PreStorageScaleMapper preStorageScaleMapper;

    @Override
    protected IBaseMapper<PreStorageScale, String> getMapper() {
        return preStorageScaleMapper;
    }

    @Resource
    private ComBaseInfoMapper comBaseInfoMapper;

    /**
     * 批量新增预报数据，服务端做二次数据校验
     *
     * @param models
     * @return
     */
    @Override
    public String doAddWf(List<PreStorageScale> models) {
        String str = "";
        try {
            for (PreStorageScale preStorageScale : models) {
                if (StringUtils.isEmpty(preStorageScale.getCreateManName()) || StringUtils.isEmpty(preStorageScale.getCreateManNo())) {
                    return "创建人及创建人编码不允许为空！";
                }
                str = checkAddAndUpdate(preStorageScale);
                if (StringUtils.isNotEmpty(str)){
                    return  str;
                }
            }
            for (PreStorageScale preStorageScale : models){
                String maxID = comBaseInfoMapper.getConfigID("Pre_Storage_Scale", "Prediction_No");
                preStorageScale.setPredictionNo(maxID);
                preStorageScale.setCreateTime(new Date());
                preStorageScale.setValueFlag(DbConstants.STORAGE_SCALE_VALUE_NOTUSE);
                preStorageScaleMapper.insert(preStorageScale);
            }
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return str;
    }

    /**
     * 批量修改方法
     * @param models
     * @return
     */
    @Override
    public String doUpdateWf(List<PreStorageScale> models) {
        String str = "";
        try {
            for (PreStorageScale preStorageScale : models) {
                if (StringUtils.isEmpty(preStorageScale.getPredictionNo())) {
                    return "预报编号不允许为空！";
                }
                if (StringUtils.isEmpty(preStorageScale.getUpdateManName()) || StringUtils.isEmpty(preStorageScale.getUpdateManNo())) {
                    return "修改人名称及修改人编码不允许为空！";
                }
                //用钢号字段判断是否为作废或者还原
                if (!StringUtils.isEmpty(preStorageScale.getGradeName())){
                    str = checkAddAndUpdate(preStorageScale);
                    if (StringUtils.isNotEmpty(str)){
                        return  str;
                    }
                }
            }
            for (PreStorageScale preStorageScale : models){
                preStorageScale.setUpdateTime(new Date());
                preStorageScaleMapper.updateByPrimaryKeySelective(preStorageScale);
            }
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return str;
    }

    /**
     * 数据新增或者修改前的数据校验
     * @param preStorageScale
     * @return
     */
    private String checkAddAndUpdate(PreStorageScale preStorageScale){
        String str = "";
        try {
            if (StringUtils.isEmpty(preStorageScale.getPackageNo())) {
                return "顺序号不允许为空！";
            }
            if (StringUtils.isEmpty(preStorageScale.getBlockNum())) {
                return "总支数不允许为空！";
            }
            if (StringUtils.isEmpty(preStorageScale.getMeterClass())) {
                return "班次不允许为空！";
            }
            if (StringUtils.isEmpty(preStorageScale.getGradeName())) {
                return "钢种名称不允许为空！";
            }
            if (StringUtils.isEmpty(preStorageScale.getHeatNo())) {
                return "炉号不允许为空！";
            }
            if (StringUtils.isEmpty(preStorageScale.getSpecName())) {
                return "规格名称不允许为空！";
            }
            //钢种信息的验证
            Map<String,Object> mapGradeName= new HashMap<>(2);
            mapGradeName.put("baseName",preStorageScale.getGradeName());
            List<ComBaseInfo> comBaseInfoList = comBaseInfoMapper.selectByParameters(mapGradeName);
            if (ListUtils.isNotEmpty(comBaseInfoList)){
                if("0".equals(comBaseInfoList.get(0).getValidFlag())){
                    return "钢种["+preStorageScale.getGradeName()+"]已经被作废，不允许进行新增或修改操作，请重新打开界面后再进行选择！";
                }
            }else{
                return "未查询到钢种["+preStorageScale.getGradeName()+"]的信息，不允许进行新增或修改操作，请从下拉框中进行选择！";
            }

            //规格信息的验证
            Map<String,Object> mapSpecName= new HashMap<>(2);
            mapSpecName.put("baseName",preStorageScale.getSpecName());
            List<ComBaseInfo> comBaseInfoList2 = comBaseInfoMapper.selectByParameters(mapSpecName);
            if (ListUtils.isNotEmpty(comBaseInfoList2)){
                if("0".equals(comBaseInfoList2.get(0).getValidFlag())){
                    return "规格["+preStorageScale.getSpecName()+"]已经被作废，不允许进行新增或修改操作，请重新打开界面后再进行选择！";
                }
            }else{
                return "未查询到规格["+preStorageScale.getSpecName()+"]的信息，不允许进行新增或修改操作，请从下拉框中进行选择！";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return str;
    }
}
