package com.steerinfo.meterwork.meterworkstoragesinglewgt.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.meterwork.meterworkstoragesinglewgt.model.MeterWorkStorageSingleWgt;
import com.steerinfo.meterwork.meterworkstoragesinglewgt.mapper.MeterWorkStorageSingleWgtMapper;
import com.steerinfo.meterwork.meterworkstoragesinglewgt.service.IMeterWorkStorageSingleWgtService;
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;

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

    @Autowired
    private MeterWorkStorageSingleWgtMapper meterWorkStorageSingleWgtMapper;

    @Resource
    private ComBaseInfoMapper comBaseInfoMapper;

    @Override
    protected IBaseMapper<MeterWorkStorageSingleWgt, String> getMapper() {
        return meterWorkStorageSingleWgtMapper;
    }

    /**
     * 批量新增
     * @param models
     * @return
     */
    @Override
    public String doAddWf(List<MeterWorkStorageSingleWgt> models) {
        String str = "";
        try {
            for (MeterWorkStorageSingleWgt singleWgt : models) {
                if (StringUtils.isEmpty(singleWgt.getCreateManName()) || StringUtils.isEmpty(singleWgt.getCreateManNo())) {
                    return "创建人及创建人编码不允许为空！";
                }
                //重量类型（0=单只实际重量，1=预报理论重量，2=标准件重量）

                    str = checkAddAndUpdate(singleWgt);
                    if (StringUtils.isNotEmpty(str)){
                        return  str;
                    }


            }
            for (MeterWorkStorageSingleWgt singleWgt : models){
                String maxID = comBaseInfoMapper.getConfigID("meter_work_storage_single_wgt", "SINGLE_WGT_NO");
                singleWgt.setSingleWgtNo(maxID);
                singleWgt.setCreateTime(new Date());
                singleWgt.setValueFlag(DbConstants.STORAGE_ACTUAL_VALUE_VALID);
                meterWorkStorageSingleWgtMapper.insert(singleWgt);
            }
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
        return str;
    }

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

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

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