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

import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.baseinfo.meterbaseoperationlog.mapper.MeterBaseOperationLogMapper;
import com.steerinfo.baseinfo.meterbaseoperationlog.model.MeterBaseOperationLog;
import com.steerinfo.baseinfo.meterbaseoperationlog.model.MeterBaseOperationLogVo;
import com.steerinfo.baseinfo.meterbaseoperationlog.service.IMeterBaseOperationLogService;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.util.CommonPage;
import com.steerinfo.util.CompareUtil;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

    @Resource
    private MeterBaseOperationLogMapper meterBaseOperationLogMapper;
    @Resource
    private ComBaseInfoMapper comBaseInfoMapper;

    @Override
    protected IBaseMapper<MeterBaseOperationLog, String> getMapper() {
        return meterBaseOperationLogMapper;
    }

    @Override
    public List<MeterBaseOperationLog> addList(List<MeterBaseOperationLog> models) {
        try {
            List<MeterBaseOperationLog> logList = new ArrayList<MeterBaseOperationLog>();
            for (MeterBaseOperationLog model : models) {
                String id = PublicMethod.IdCreate(2);
                model.setId(id);
                if (model.getCreateTime() == null) {
                    model.setCreateTime(new Date());
                }
                if (model.getUpdateManName() != null) {
                    model.setUpdateTime(new Date());
                }
                meterBaseOperationLogMapper.insertSelective(model);
                logList.add(model);
            }
            return logList;
        } catch (Exception e) {
            throw new MarkerMetException(500, "操作异常" + e);
        }
    }

    @Override
    public PageList<MeterBaseOperationLog> likeByDesc(HashMap<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if(params.get("operationPageStr") != null) {
            String[] pageStr = params.get("operationPageStr").toString().split(",");
            params.put("pageStr", pageStr);
        }
        if(params.get("operationFunctionStr") != null) {
            String[] pageStr = params.get("operationFunctionStr").toString().split(",");
            params.put("functionStr", pageStr);
        }
        List<MeterBaseOperationLogVo> rows = meterBaseOperationLogMapper.likeByDesc(params);
        if(rows.size() >= 1) {
            for(MeterBaseOperationLogVo model: rows) {
                if (StringUtils.isNotEmpty(model.getOperationBeforeFix())) {
                    String str = model.getOperationBeforeFix();
                    if(str.indexOf("MeterWorkCarActualFirst") >= 0){
                        model.setOperationBeforeFix(translationModel(str));
                    }else if(str.indexOf("MeterWorkCarActual") >= 0) {
                        model.setOperationBeforeFix(translationModel(str));
                    }else if(str.indexOf("MeterWorkRailwayActFirst") >= 0) {
                        model.setOperationBeforeFix(translationModel(str));
                    }else if(str.indexOf("MeterWorkRailwayActual") >= 0) {
                        model.setOperationBeforeFix(translationModel(str));
                    }else if(str.indexOf("MeterWorkRailwayReceived") >= 0) {
                        model.setOperationBeforeFix(translationModel(str));
                    }
                }
                if (StringUtils.isNotEmpty(model.getOperationAfterFix())) {
                    String str = model.getOperationAfterFix();
                    if(str.indexOf("MeterWorkCarActualFirst") >= 0){
                        model.setOperationAfterFix(translationModel(str));
                    }else if(str.indexOf("MeterWorkCarActual") >= 0) {
                        model.setOperationAfterFix(translationModel(str));
                    }else if(str.indexOf("MeterWorkRailwayActFirst") >= 0) {
                        model.setOperationAfterFix(translationModel(str));
                    }else if(str.indexOf("MeterWorkRailwayActual") >= 0) {
                        model.setOperationAfterFix(translationModel(str));
                    }else if(str.indexOf("MeterWorkRailwayReceived") >= 0) {
                        model.setOperationAfterFix(translationModel(str));
                    }
                }
            }
        }
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    public String translationModel(String str) {
        String strAll = "";
        if(str.indexOf("MeterWorkCarActualFirst") >= 0){
            if(str.indexOf("predictionNo=") >= 1) {
                str = str.substring(str.indexOf("predictionNo="));
                strAll += "委托号:" + str.substring("predictionNo=".length(), str.indexOf(","));
            }
            if(str.indexOf("carNo=") >= 1) {
                str = str.substring(str.indexOf("carNo="));
                strAll += ", 车号:" + str.substring("carNo=".length(), str.indexOf(","));
            }
            if(str.indexOf("meterWeight=") >= 1) {
                str = str.substring(str.indexOf("meterWeight="));
                String meterWeightStr = str.substring("meterWeight=".length(), str.indexOf(","));
                String meterWeight = "0.00";
                if(!meterWeightStr.equals("null")) {
                    meterWeight = String.format("%.2f", Double.parseDouble(meterWeightStr)/1000);
                }
                strAll += ", 重量:" + meterWeight + "t";
            }
            if(str.indexOf("baseSpotName=") >= 1) {
                str = str.substring(str.indexOf("baseSpotName="));
                strAll += ", 计量点:" + str.substring("baseSpotName=".length(), str.indexOf(","));
            }
        }
        if(str.indexOf("MeterWorkCarActual") >= 0){
            if(str.indexOf("predictionNo=") >= 1) {
                str = str.substring(str.indexOf("predictionNo="));
                strAll += "委托号:" + str.substring("predictionNo=".length(), str.indexOf(","));
            }
            if(str.indexOf("carNo=") >= 1) {
                str = str.substring(str.indexOf("carNo="));
                strAll += ", 车号:" + str.substring("carNo=".length(), str.indexOf(","));
            }
            if(str.indexOf("grossWeight=") >= 1) {
                str = str.substring(str.indexOf("grossWeight="));
                String grossStr = str.substring("grossWeight=".length(), str.indexOf(","));
                String grossWeight = "0.00";
                if(!grossStr.equals("null")) {
                    grossWeight = String.format("%.2f", Double.parseDouble(grossStr)/1000);
                }
                strAll += ", 毛重:" + grossWeight + "t";
            }
            if(str.indexOf("baseSpot1Name=") >= 1) {
                str = str.substring(str.indexOf("baseSpot1Name="));
                strAll += ", 毛重计量点:" + str.substring("baseSpot1Name=".length(), str.indexOf(","));
            }
            if(str.indexOf("tareWeight=") >= 1) {
                str = str.substring(str.indexOf("tareWeight="));
                String tareStr = str.substring("tareWeight=".length(), str.indexOf(","));
                String tareWeight = "0.00";
                if(!tareStr.equals("null")) {
                    tareWeight = String.format("%.2f", Double.parseDouble(tareStr)/1000);
                }
                strAll += ", 皮重:" + tareWeight + "t";
            }
            if(str.indexOf("baseSpot2Name=") >= 1) {
                str = str.substring(str.indexOf("baseSpot2Name="));
                strAll += ", 皮重计量点:" + str.substring("baseSpot2Name=".length(), str.indexOf(","));
            }
            if(str.indexOf("netWeight=") >= 1) {
                str = str.substring(str.indexOf("netWeight="));
                String netStr = str.substring("netWeight=".length(), str.indexOf(","));
                String netWeight = "0.00";
                if(!netStr.equals("null")) {
                    netWeight = String.format("%.2f", Double.parseDouble(netStr)/1000);
                }
                strAll += ", 净重:" + netWeight + "t";
            }
            if(str.indexOf("netSpot3Name=") >= 1) {
                str = str.substring(str.indexOf("netSpot3Name="));
                strAll += ", 净重计量点:" + str.substring("netSpot3Name=".length(), str.indexOf(","));
            }
        }
        if(str.indexOf("MeterWorkRailwayActFirst") >= 0){
            if(str.indexOf("predictionNo=") >= 1) {
                str = str.substring(str.indexOf("predictionNo="));
                strAll += "委托号:" + str.substring("predictionNo=".length(), str.indexOf(","));
            }
            if(str.indexOf("railwayNo=") >= 1) {
                str = str.substring(str.indexOf("railwayNo="));
                strAll += ", 车号:" + str.substring("railwayNo=".length(), str.indexOf(","));
            }
            if(str.indexOf("meterWeight=") >= 1) {
                str = str.substring(str.indexOf("meterWeight="));
                String meterWeightStr = str.substring("meterWeight=".length(), str.indexOf(","));
                String meterWeight = "0.00";
                if(!meterWeightStr.equals("null")) {
                    meterWeight = String.format("%.2f", Double.parseDouble(meterWeightStr)/1000);
                }
                strAll += ", 重量:" + meterWeight + "t";
            }
            if(str.indexOf("baseSpotName=") >= 1) {
                str = str.substring(str.indexOf("baseSpotName="));
                strAll += ", 计量点:" + str.substring("baseSpotName=".length(), str.indexOf(","));
            }
        }
        if(str.indexOf("MeterWorkRailwayActual") >= 0){
            if(str.indexOf("predictionNo=") >= 1) {
                str = str.substring(str.indexOf("predictionNo="));
                strAll += "委托号:" + str.substring("predictionNo=".length(), str.indexOf(","));
            }
            if(str.indexOf("railwayNo=") >= 1) {
                str = str.substring(str.indexOf("railwayNo="));
                strAll += ", 车号:" + str.substring("railwayNo=".length(), str.indexOf(","));
            }
            if(str.indexOf("grossWeight=") >= 1) {
                str = str.substring(str.indexOf("grossWeight="));
                String grossStr = str.substring("grossWeight=".length(), str.indexOf(","));
                String grossWeight = "0.00";
                if(!grossStr.equals("null")) {
                    grossWeight = String.format("%.2f", Double.parseDouble(grossStr)/1000);
                }
                strAll += ", 毛重:" + grossWeight + "t";
            }
            if(str.indexOf("baseSpot1Name=") >= 1) {
                str = str.substring(str.indexOf("baseSpot1Name="));
                strAll += ", 毛重计量点:" + str.substring("baseSpot1Name=".length(), str.indexOf(","));
            }
            if(str.indexOf("tareWeight=") >= 1) {
                str = str.substring(str.indexOf("tareWeight="));
                String tareStr = str.substring("tareWeight=".length(), str.indexOf(","));
                String tareWeight = "0.00";
                if(!tareStr.equals("null")) {
                    tareWeight = String.format("%.2f", Double.parseDouble(tareStr)/1000);
                }
                strAll += ", 皮重:" + tareWeight + "t";
            }
            if(str.indexOf("baseSpot2Name=") >= 1) {
                str = str.substring(str.indexOf("baseSpot2Name="));
                strAll += ", 皮重计量点:" + str.substring("baseSpot2Name=".length(), str.indexOf(","));
            }
            if(str.indexOf("netWeight=") >= 1) {
                str = str.substring(str.indexOf("netWeight="));
                String netStr = str.substring("netWeight=".length(), str.indexOf(","));
                String netWeight = "0.00";
                if(!netStr.equals("null")) {
                    netWeight = String.format("%.2f", Double.parseDouble(netStr)/1000);
                }
                strAll += ", 净重:" + netWeight + "t";
            }
            if(str.indexOf("netSpot3Name=") >= 1) {
                str = str.substring(str.indexOf("netSpot3Name="));
                strAll += ", 净重计量点:" + str.substring("netSpot3Name=".length(), str.indexOf(","));
            }
        }
        if(str.indexOf("MeterWorkRailwayReceived") >= 0){
            if(str.indexOf("predictionNo=") >= 1) {
                str = str.substring(str.indexOf("predictionNo="));
                strAll += "委托号:" + str.substring("predictionNo=".length(), str.indexOf(","));
            }
            if(str.indexOf("railwayNo=") >= 1) {
                str = str.substring(str.indexOf("railwayNo="));
                strAll += ", 车号:" + str.substring("railwayNo=".length(), str.indexOf(","));
            }
            if(str.indexOf("meterWeight=") >= 1) {
                str = str.substring(str.indexOf("meterWeight="));
                String meterWeightStr = str.substring("meterWeight=".length(), str.indexOf(","));
                String meterWeight = "0.00";
                if(!meterWeightStr.equals("null")) {
                    meterWeight = String.format("%.2f", Double.parseDouble(meterWeightStr)/1000);
                }
                strAll += ", 重量:" + meterWeight + "t";
            }
            if(str.indexOf("baseSpotName=") >= 1) {
                str = str.substring(str.indexOf("baseSpotName="));
                strAll += ", 计量点:" + str.substring("baseSpotName=".length(), str.indexOf(","));
            }
        }
        return strAll;
    }

    @Override
    public int doAddLog(MeterBaseOperationLog model) {
        try {
            String maxID = comBaseInfoMapper.getConfigID("METER_BASE_OPERATION_LOG", "ID");
            model.setId(maxID);
            model.setCreateTime(new Date());
            return meterBaseOperationLogMapper.insertSelective(model);
        } catch (Exception ex) {
            throw ex;
        }
    }

    @Override
    public List<MeterBaseOperationLog> doQueryAndMultiple(CommonPage<MeterBaseOperationLog> model) {
        HashMap<String, Object> obj = new HashMap<>();
        if (model.getParam().getCreateTime() != null) {
            obj.put("createTime", model.getParam().getCreateTime());
            obj.put("startTime1", model.getStartTime());
            obj.put("endTime1", model.getEndTime());
        }
        if (model.getParam().getOperationPage() != null) {
            obj.put("operationPage", model.getParam().getOperationPage());//操作页面
        }
        if (model.getParam().getOperationFunction() != null) {
            obj.put("operationFunction", model.getParam().getOperationFunction());//功能
        }
        if (model.getParam().getOperationId() != null) {
            obj.put("operationId", model.getParam().getOperationId());//数据主键
        }
        if (model.getParam().getOperationPredictionNo() != null) {
            obj.put("operationPredictionNo", model.getParam().getOperationPredictionNo());
        }
        if (model.getParam().getOperationCarnNo() != null) {
            obj.put("operationCarnNo", model.getParam().getOperationCarnNo());
        }
        if (model.getParam().getOperationBeforeFix() != null) {
            obj.put("operationBeforeFix", model.getParam().getOperationBeforeFix());
        }
        if (model.getParam().getOperationAfterFix() != null) {
            obj.put("operationAfterFix", model.getParam().getOperationAfterFix());
        }
        if (model.getParam().getLogType() != null) {
            obj.put("logType", model.getParam().getLogType());
        }
        if (model.getParam().getLogTypeCode() != null) {
            obj.put("logTypeCode", model.getParam().getLogTypeCode());
        }
        return meterBaseOperationLogMapper.doQueryAndMultiple(obj);
    }

    /**
     * 根据传入传出的参数，插入日志数据
     *
     * @param source 修改前的数据
     * @param target 修改后的数据
     * @param model 日志的必要参数
     * @return
     */
    @Override
    public String addComparePropertiesLog(Object source, Object target, MeterBaseOperationLog model) {
        String id = PublicMethod.IdCreate(2);
        model.setId(id);
        if (model.getCreateTime() == null) {
            model.setCreateTime(new Date());
        }
        if (model.getUpdateManName() != null) {
            model.setUpdateTime(new Date());
        }
        String[] strComPare = CompareUtil.compareProperties(source, target);
        if (strComPare != null && strComPare.length == 2) {
            model.setOperationBeforeFix(strComPare[0]);
            model.setOperationAfterFix(strComPare[1]);
        }
        meterBaseOperationLogMapper.insertSelective(model);
        return "";
    }

    @Override
    public PageList<HashMap> allFirstStateView(HashMap<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<HashMap> operationLogs = meterBaseOperationLogMapper.allFirstStateView(params);
        PageList pageInfo = new PageList(operationLogs);
        return pageInfo;
    }

    @Override
    public PageList<HashMap> allActualStateView(HashMap<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<HashMap> operationLogs = meterBaseOperationLogMapper.allActualStateView(params);
        PageList pageInfo = new PageList(operationLogs);
        return pageInfo;
    }

    @Override
    public List<String> operationPageInfo(HashMap<String, Object> params) {
        List<String> rows = meterBaseOperationLogMapper.operationPageInfo(params);
        return rows;
    }

    @Override
    public List<String> operationFunctionInfo(HashMap<String, Object> params) {
        List<String> rows = meterBaseOperationLogMapper.operationFunctionInfo(params);
        return rows;
    }
}
