package com.steerinfo.meterwork.meterworkimage.service.impl;

import cn.hutool.core.lang.Assert;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkimage.mapper.MeterWorkImageMapper;
import com.steerinfo.meterwork.meterworkimage.model.MeterWorkImage;
import com.steerinfo.meterwork.meterworkimage.service.IMeterWorkImageService;
import com.steerinfo.meterwork.meterworkimagetemp.mapper.MeterWorkImageTempMapper;
import com.steerinfo.meterwork.meterworkimagetemp.model.MeterWorkImageTemp;
import com.steerinfo.meterwork.meterworkrailwayactfirst.mapper.MeterWorkRailwayActFirstMapper;
import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
import com.steerinfo.meterwork.meterworkrailwayactual.mapper.MeterWorkRailwayActualMapper;
import com.steerinfo.meterwork.meterworkrailwayactual.model.MeterWorkRailwayActual;
import com.steerinfo.meterwork.meterworkrailwayreceived.mapper.MeterWorkRailwayReceivedMapper;
import com.steerinfo.meterwork.meterworkrailwayreceived.model.MeterWorkRailwayReceived;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.*;

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

    @Resource
    private MeterWorkImageMapper meterWorkImageMapper;

    @Resource
    private MeterWorkImageTempMapper meterWorkImageTempMapper;

    @Resource
    private MeterWorkRailwayReceivedMapper meterWorkRailwayReceivedMapper;

    @Resource
    private MeterWorkRailwayActualMapper meterWorkRailwayActualMapper;

    @Override
    protected IBaseMapper<MeterWorkImage, String> getMapper() {
        return meterWorkImageMapper;
    }
    public RESTfulResult queryUrl(String actualFirstNo)
    {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            if (actualFirstNo == null || "".equals(actualFirstNo)) {
                rm.setMessage("计量作业编号为空!!");
                return rm;
            }
            MeterWorkImage mwi = meterWorkImageMapper.selectByPrimaryKey(actualFirstNo);
            if(mwi==null) {
                rm.setMessage("该计量作业编号不存在图片!!");
                return rm;
            }
            ArrayList<String> urls=new ArrayList<>();
            if (mwi.getImageFile1() != null && !mwi.getImageFile1().equals("")) {
                String no = mwi.getImageFile1();
                urls.add(no);
            }
            if (mwi.getImageFile2() != null && !mwi.getImageFile2().equals("")) {
                String no = mwi.getImageFile2();
                urls.add(no);
            }
            if (mwi.getImageFile3() != null && !mwi.getImageFile3().equals("")) {
                String no = mwi.getImageFile3();
                urls.add(no);
            }
            if (mwi.getImageFile4() != null && !mwi.getImageFile4().equals("")) {
                String no = mwi.getImageFile4();
                urls.add(no);
            }
            if (mwi.getImageFile5() != null && !mwi.getImageFile5().equals("")) {
                String no = mwi.getImageFile5();
                urls.add(no);
            }
            if (mwi.getImageFile6() != null && !mwi.getImageFile6().equals("")) {
                String no = mwi.getImageFile6();
                urls.add(no);
            }
            if (mwi.getImageFile7() != null && !mwi.getImageFile7().equals("")) {
                String no = mwi.getImageFile7();
                urls.add(no);
            }
            if (mwi.getImageFile8() != null && !mwi.getImageFile8().equals("")) {
                String no = mwi.getImageFile8();
                urls.add(no);
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功");
            rm.setData(urls);
        } catch (Exception e){
             e.printStackTrace();
             rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult queryRailwayUrl(String actualFirstNo) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            if (actualFirstNo == null || "".equals(actualFirstNo)) {
                rm.setMessage("计量作业编号为空!!");
                return rm;
            }
            MeterWorkImage mwi = meterWorkImageMapper.selectByPrimaryKey(actualFirstNo);
            if(mwi==null) {
                mwi = meterWorkImageMapper.selectByPrimaryKey(actualFirstNo+"bak");
                if(mwi==null) {
                    rm.setMessage("该计量作业编号不存在图片!!");
                    return rm;
                }
            }
            ArrayList<String> urls=new ArrayList<>();
            if (mwi.getImageFile1() != null && !mwi.getImageFile1().equals("")) {
                String no = mwi.getImageFile1();
                urls.add(no);
            }
            if (mwi.getImageFile2() != null && !mwi.getImageFile2().equals("")) {
                String no = mwi.getImageFile2();
                urls.add(no);
            }
            if (mwi.getImageFile3() != null && !mwi.getImageFile3().equals("")) {
                String no = mwi.getImageFile3();
                urls.add(no);
            }
            if (mwi.getImageFile4() != null && !mwi.getImageFile4().equals("")) {
                String no = mwi.getImageFile4();
                urls.add(no);
            }
            if (mwi.getImageFile5() != null && !mwi.getImageFile5().equals("")) {
                String no = mwi.getImageFile5();
                urls.add(no);
            }
            if (mwi.getImageFile6() != null && !mwi.getImageFile6().equals("")) {
                String no = mwi.getImageFile6();
                urls.add(no);
            }
            if (mwi.getImageFile7() != null && !mwi.getImageFile7().equals("")) {
                String no = mwi.getImageFile7();
                urls.add(no);
            }
            if (mwi.getImageFile8() != null && !mwi.getImageFile8().equals("")) {
                String no = mwi.getImageFile8();
                urls.add(no);
            }
            rm.setSucceed();
            rm.setCode("200");
            rm.setMessage("操作成功");
            rm.setData(urls);
        } catch (Exception e){
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult getRailwayImageUrl(HashMap<String, Object> map) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            String grossNo = map.get("grossNo") == null ? "": "R" + map.get("grossNo").toString();
            String tareNo = map.get("tareNo") == null ? "": "R" + map.get("tareNo").toString();
            HashMap<String, Object> imageMap = new HashMap<>();
            List<String> grossImageURL = new ArrayList<>();
            List<String> tareImageURL = new ArrayList<>();
            if(grossNo.equals(tareNo)) {
                tareNo = "";
            }
            if(StringUtils.isNotEmpty(grossNo)) {
                MeterWorkImage mwi = meterWorkImageMapper.selectByPrimaryKey(grossNo);
                if(mwi != null) {
                    if (mwi.getImageFile1() != null && !mwi.getImageFile1().equals("")) {
                        String no = mwi.getImageFile1();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile2() != null && !mwi.getImageFile2().equals("")) {
                        String no = mwi.getImageFile2();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile3() != null && !mwi.getImageFile3().equals("")) {
                        String no = mwi.getImageFile3();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile4() != null && !mwi.getImageFile4().equals("")) {
                        String no = mwi.getImageFile4();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile5() != null && !mwi.getImageFile5().equals("")) {
                        String no = mwi.getImageFile5();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile6() != null && !mwi.getImageFile6().equals("")) {
                        String no = mwi.getImageFile6();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile7() != null && !mwi.getImageFile7().equals("")) {
                        String no = mwi.getImageFile7();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile8() != null && !mwi.getImageFile8().equals("")) {
                        String no = mwi.getImageFile8();
                        grossImageURL.add(no);
                    }
                }
            }

            if(StringUtils.isNotEmpty(tareNo)) {
                MeterWorkImage mwi = meterWorkImageMapper.selectByPrimaryKey(tareNo);
                if(mwi != null) {
                    if (mwi.getImageFile1() != null && !mwi.getImageFile1().equals("")) {
                        String no = mwi.getImageFile1();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile2() != null && !mwi.getImageFile2().equals("")) {
                        String no = mwi.getImageFile2();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile3() != null && !mwi.getImageFile3().equals("")) {
                        String no = mwi.getImageFile3();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile4() != null && !mwi.getImageFile4().equals("")) {
                        String no = mwi.getImageFile4();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile5() != null && !mwi.getImageFile5().equals("")) {
                        String no = mwi.getImageFile5();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile6() != null && !mwi.getImageFile6().equals("")) {
                        String no = mwi.getImageFile6();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile7() != null && !mwi.getImageFile7().equals("")) {
                        String no = mwi.getImageFile7();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile8() != null && !mwi.getImageFile8().equals("")) {
                        String no = mwi.getImageFile8();
                        tareImageURL.add(no);
                    }
                }
            }
            imageMap.put("grossImageURL", grossImageURL);
            imageMap.put("tareImageURL", tareImageURL);
            rm.setSucceed();
            rm.setCode("200");
            rm.setData(imageMap);
            rm.setMessage("操作成功");
        } catch (Exception e){
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult getRailwayActualImageUrl(HashMap<String, Object> map) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            String actualNo = map.get("actualNo") == null ? "": map.get("actualNo").toString();
            Assert.isTrue(StringUtils.isNotEmpty(actualNo), "净重计量编号为空");
            MeterWorkRailwayActual actualModel = meterWorkRailwayActualMapper.selectByPrimaryKey(actualNo);
            Assert.isTrue(actualModel != null, "净重数据不存在");
            String grossNo = actualModel.getActualFirst1No() == null? "": "R" + actualModel.getActualFirst1No();
            String tareNo = actualModel.getActualFirst2No() == null ? "": "R" + actualModel.getActualFirst2No();
            String tare_No = map.get("tareNo") == null? "": "R" + map.get("tareNo").toString();
            HashMap<String, Object> imageMap = new HashMap<>();
            List<String> grossImageURL = new ArrayList<>();
            List<String> tareImageURL = new ArrayList<>();
            List<String> tareNoImageURL = new ArrayList<>();

            if(StringUtils.isNotEmpty(grossNo)) {
                MeterWorkImage mwi = meterWorkImageMapper.selectByPrimaryKey(grossNo);
                if(mwi != null) {
                    if (mwi.getImageFile1() != null && !mwi.getImageFile1().equals("")) {
                        String no = mwi.getImageFile1();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile2() != null && !mwi.getImageFile2().equals("")) {
                        String no = mwi.getImageFile2();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile3() != null && !mwi.getImageFile3().equals("")) {
                        String no = mwi.getImageFile3();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile4() != null && !mwi.getImageFile4().equals("")) {
                        String no = mwi.getImageFile4();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile5() != null && !mwi.getImageFile5().equals("")) {
                        String no = mwi.getImageFile5();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile6() != null && !mwi.getImageFile6().equals("")) {
                        String no = mwi.getImageFile6();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile7() != null && !mwi.getImageFile7().equals("")) {
                        String no = mwi.getImageFile7();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile8() != null && !mwi.getImageFile8().equals("")) {
                        String no = mwi.getImageFile8();
                        grossImageURL.add(no);
                    }
                }
            }

            if(StringUtils.isNotEmpty(tareNo)) {
                MeterWorkImage mwi = meterWorkImageMapper.selectByPrimaryKey(tareNo);
                if(mwi != null) {
                    if (mwi.getImageFile1() != null && !mwi.getImageFile1().equals("")) {
                        String no = mwi.getImageFile1();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile2() != null && !mwi.getImageFile2().equals("")) {
                        String no = mwi.getImageFile2();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile3() != null && !mwi.getImageFile3().equals("")) {
                        String no = mwi.getImageFile3();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile4() != null && !mwi.getImageFile4().equals("")) {
                        String no = mwi.getImageFile4();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile5() != null && !mwi.getImageFile5().equals("")) {
                        String no = mwi.getImageFile5();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile6() != null && !mwi.getImageFile6().equals("")) {
                        String no = mwi.getImageFile6();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile7() != null && !mwi.getImageFile7().equals("")) {
                        String no = mwi.getImageFile7();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile8() != null && !mwi.getImageFile8().equals("")) {
                        String no = mwi.getImageFile8();
                        tareImageURL.add(no);
                    }
                }
            }
            if(StringUtils.isNotEmpty(tare_No)) {
                MeterWorkImage mwi = meterWorkImageMapper.selectByPrimaryKey(tare_No);
                if(mwi != null) {
                    if (mwi.getImageFile1() != null && !mwi.getImageFile1().equals("")) {
                        String no = mwi.getImageFile1();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile2() != null && !mwi.getImageFile2().equals("")) {
                        String no = mwi.getImageFile2();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile3() != null && !mwi.getImageFile3().equals("")) {
                        String no = mwi.getImageFile3();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile4() != null && !mwi.getImageFile4().equals("")) {
                        String no = mwi.getImageFile4();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile5() != null && !mwi.getImageFile5().equals("")) {
                        String no = mwi.getImageFile5();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile6() != null && !mwi.getImageFile6().equals("")) {
                        String no = mwi.getImageFile6();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile7() != null && !mwi.getImageFile7().equals("")) {
                        String no = mwi.getImageFile7();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile8() != null && !mwi.getImageFile8().equals("")) {
                        String no = mwi.getImageFile8();
                        tareNoImageURL.add(no);
                    }
                }
            }
            imageMap.put("grossImageURL", grossImageURL);
            imageMap.put("tareImageURL", tareImageURL);
            imageMap.put("tareNoImageURL", tareNoImageURL);
            rm.setSucceed();
            rm.setCode("200");
            rm.setData(imageMap);
            rm.setMessage("操作成功");
        } catch (Exception e){
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult getAllRailwayImageUrl(HashMap<String, Object> map) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            String actualNo = map.get("actualNo") == null ? "": map.get("actualNo").toString();
            String railwayTypeNo = map.get("railwayTypeNo") == null ? "": map.get("railwayTypeNo").toString();
            Assert.isTrue(StringUtils.isNotEmpty(actualNo), "净重计量编号为空");
            MeterWorkRailwayActual actualModel = meterWorkRailwayActualMapper.selectByPrimaryKey(actualNo);
            Assert.isTrue(actualModel != null, "净重数据不存在");
            String grossNo = "";
            String tareNo = "";
            String tare_No = "";
            if(railwayTypeNo.equals("001019002")
                    || railwayTypeNo.equals("001019004")) {
                grossNo = actualModel.getActualFirst1No() == null? "": actualModel.getActualFirst1No();
                tareNo = actualModel.getActualFirst2No() == null ? "": actualModel.getActualFirst2No();
                tare_No = map.get("tareNo") == null? "": map.get("tareNo").toString();
            }else {
                grossNo = actualModel.getActualFirst1No() == null? "": "R" + actualModel.getActualFirst1No();
                tareNo = actualModel.getActualFirst2No() == null ? "": "R" + actualModel.getActualFirst2No();
                tare_No = map.get("tareNo") == null? "": "R" + map.get("tareNo").toString();
            }
            if(grossNo.equals(tareNo)) {
                tareNo = "";
            }
            HashMap<String, Object> imageMap = new HashMap<>();
            List<String> grossImageURL = new ArrayList<>();
            List<String> tareImageURL = new ArrayList<>();
            List<String> tareNoImageURL = new ArrayList<>();

            if(StringUtils.isNotEmpty(grossNo)) {
                MeterWorkImage mwi = meterWorkImageMapper.selectByPrimaryKey(grossNo);
                if(mwi != null) {
                    if (mwi.getImageFile1() != null && !mwi.getImageFile1().equals("")) {
                        String no = mwi.getImageFile1();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile2() != null && !mwi.getImageFile2().equals("")) {
                        String no = mwi.getImageFile2();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile3() != null && !mwi.getImageFile3().equals("")) {
                        String no = mwi.getImageFile3();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile4() != null && !mwi.getImageFile4().equals("")) {
                        String no = mwi.getImageFile4();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile5() != null && !mwi.getImageFile5().equals("")) {
                        String no = mwi.getImageFile5();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile6() != null && !mwi.getImageFile6().equals("")) {
                        String no = mwi.getImageFile6();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile7() != null && !mwi.getImageFile7().equals("")) {
                        String no = mwi.getImageFile7();
                        grossImageURL.add(no);
                    }
                    if (mwi.getImageFile8() != null && !mwi.getImageFile8().equals("")) {
                        String no = mwi.getImageFile8();
                        grossImageURL.add(no);
                    }
                }
            }

            if(StringUtils.isNotEmpty(tareNo)) {
                MeterWorkImage mwi = meterWorkImageMapper.selectByPrimaryKey(tareNo);
                if(mwi != null) {
                    if (mwi.getImageFile1() != null && !mwi.getImageFile1().equals("")) {
                        String no = mwi.getImageFile1();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile2() != null && !mwi.getImageFile2().equals("")) {
                        String no = mwi.getImageFile2();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile3() != null && !mwi.getImageFile3().equals("")) {
                        String no = mwi.getImageFile3();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile4() != null && !mwi.getImageFile4().equals("")) {
                        String no = mwi.getImageFile4();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile5() != null && !mwi.getImageFile5().equals("")) {
                        String no = mwi.getImageFile5();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile6() != null && !mwi.getImageFile6().equals("")) {
                        String no = mwi.getImageFile6();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile7() != null && !mwi.getImageFile7().equals("")) {
                        String no = mwi.getImageFile7();
                        tareImageURL.add(no);
                    }
                    if (mwi.getImageFile8() != null && !mwi.getImageFile8().equals("")) {
                        String no = mwi.getImageFile8();
                        tareImageURL.add(no);
                    }
                }
            }
            if(StringUtils.isNotEmpty(tare_No)) {
                MeterWorkImage mwi = meterWorkImageMapper.selectByPrimaryKey(tare_No);
                if(mwi != null) {
                    if (mwi.getImageFile1() != null && !mwi.getImageFile1().equals("")) {
                        String no = mwi.getImageFile1();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile2() != null && !mwi.getImageFile2().equals("")) {
                        String no = mwi.getImageFile2();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile3() != null && !mwi.getImageFile3().equals("")) {
                        String no = mwi.getImageFile3();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile4() != null && !mwi.getImageFile4().equals("")) {
                        String no = mwi.getImageFile4();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile5() != null && !mwi.getImageFile5().equals("")) {
                        String no = mwi.getImageFile5();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile6() != null && !mwi.getImageFile6().equals("")) {
                        String no = mwi.getImageFile6();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile7() != null && !mwi.getImageFile7().equals("")) {
                        String no = mwi.getImageFile7();
                        tareNoImageURL.add(no);
                    }
                    if (mwi.getImageFile8() != null && !mwi.getImageFile8().equals("")) {
                        String no = mwi.getImageFile8();
                        tareNoImageURL.add(no);
                    }
                }
            }
            imageMap.put("grossImageURL", grossImageURL);
            imageMap.put("tareImageURL", tareImageURL);
            imageMap.put("tareNoImageURL", tareNoImageURL);
            rm.setSucceed();
            rm.setCode("200");
            rm.setData(imageMap);
            rm.setMessage("操作成功");
        } catch (Exception e){
            e.printStackTrace();
            rm.setMessage(e.getMessage());
        }
        return rm;
    }

    @Resource
    MeterWorkImageTempMapper tempMapper;
    /**
     * 图片新增操作
     * @param mwi
     * @return
     */
    public RESTfulResult doSaveWf(MeterWorkImage mwi){
        RESTfulResult rm = new RESTfulResult();
        rm.setSucceed();
        rm.setCode("200");
        try{
            String strCar = null;
            if(mwi.getImageFile2()!=null && StringUtils.isNotEmpty(mwi.getImageFile2())) {
                strCar = getStrEmpty(mwi.getImageFile2());
            }else if(mwi.getImageFile3()!=null && StringUtils.isNotEmpty(mwi.getImageFile3())) {
                strCar = getStrEmpty(mwi.getImageFile3());
            }else if(mwi.getImageFile4()!=null && StringUtils.isNotEmpty(mwi.getImageFile4())) {
                strCar = getStrEmpty(mwi.getImageFile4());
            }

            if(strCar != null && (strCar.length() == 7 || strCar.length() == 2)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String railwayNo = strCar;
                Date imageTime = mwi.getImageTime();
                Calendar beforeD = Calendar.getInstance();
                Calendar afterD = Calendar.getInstance();
                beforeD.setTime(imageTime);
                afterD.setTime(imageTime);
                beforeD.add(Calendar.MINUTE, -5);
                afterD.add(Calendar.MINUTE, 5);
                String startTime = sdf.format(beforeD.getTime());
                String endTime = sdf.format(afterD.getTime());
                HashMap<String, Object> obj = new HashMap<>();
                obj.put("railwayNo", railwayNo);
                obj.put("startTime", startTime);
                obj.put("endTime", endTime);
                MeterWorkRailwayReceived firstModel = meterWorkRailwayReceivedMapper.getPrimaryKey(obj);
                if(firstModel != null && firstModel.getActualFirstNo() != null && StringUtils.isNotEmpty(firstModel.getActualFirstNo())) {
                    mwi.setActualFirstNo(firstModel.getActualFirstNo());
                    MeterWorkImage mw= meterWorkImageMapper.selectByPrimaryKey(mwi.getActualFirstNo());
                    if(mw != null){
                        meterWorkImageMapper.updateByPrimaryKeySelective(mwi);
                    }else{
                        meterWorkImageMapper.insertSelective(mwi);
                    }
                }else{
                    String url="";
                    if(StringUtils.isNotEmpty(mwi.getImageFile3())){
                        url=mwi.getImageFile3();
                    }
                    if(StringUtils.isNotEmpty(mwi.getImageFile4()))
                        url=mwi.getImageFile4();

                    if(StringUtils.isNotEmpty(url)){
                        //http://10.99.81.20/icore.icp.web/pass/file/download/2022-08-21/DTJB420220821060550_0946046_P1.jpeg
                        MeterWorkImageTemp temp=new MeterWorkImageTemp();
                        temp.setImageNo(UUID.randomUUID().toString());
                        temp.setValueFlag("0");
                        temp.setImageUrl1(url);
                        temp.setImageTime(imageTime);
                        String[] strs=url.split("/");
                        String[] str2=strs[strs.length-1].split("_");
                        if(str2.length>1){
                            temp.setRailwayNo(str2[1]);
                            if(str2[0].length()>5) {
                                temp.setBaseSpotNo(str2[0].substring(0, 5));
                                tempMapper.insert(temp);
                            }
                        }
                    }
                }
            }else {
                //判断图片主键是否保存了从表数据
                List<MeterWorkRailwayReceived> receiveds = meterWorkRailwayReceivedMapper.selectByPrimaryKeyLike(mwi.getActualFirstNo());
                if(receiveds.size() == 1) {
                    mwi.setActualFirstNo(receiveds.get(0).getActualFirstNo());
                }
                MeterWorkImage mw= meterWorkImageMapper.selectByPrimaryKey(mwi.getActualFirstNo());
                if(mw != null){
                    meterWorkImageMapper.updateByPrimaryKeySelective(mwi);
                }else{
                    meterWorkImageMapper.insertSelective(mwi);
                }
            }

        }catch(Exception ex){
            rm.setFailed();
            rm.setCode("500");
            rm.setMessage("图片信息写入表异常："+ex.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult doSaveRailwayWf(MeterWorkImage mwi) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            MeterWorkImage image = meterWorkImageMapper.selectByPrimaryKey(mwi.getActualFirstNo());
            if(image != null) {
                image.setImageFile3(mwi.getImageFile3());
                image.setImageFile4(mwi.getImageFile4());
                meterWorkImageMapper.updateByPrimaryKeySelective(image);
            }else {
                mwi.setImageFile8("");
                meterWorkImageMapper.insertSelective(mwi);
            }
            MeterWorkImageTemp imageTemp = meterWorkImageTempMapper.selectByPrimaryKey(mwi.getImageFile8());
            if(imageTemp != null) {
                imageTemp.setValueFlag("1");
                meterWorkImageTempMapper.updateByPrimaryKeySelective(imageTemp);
            }
            rm.setSucceed(true);
            rm.setMessage("操作成功");
            rm.setCode("200");
        }catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("图片信息写入表异常：" + e.getMessage());
        }
        return rm;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult updateDynamicImage(MeterWorkImage mwi) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{

            MeterWorkImage image = meterWorkImageMapper.selectByPrimaryKey(mwi.getActualFirstNo());
            if(image != null) {
                if(mwi.getImageFile1() != null && mwi.getImageFile1().equals("")) {
                    image.setImageFile1(null);
                }
                if(mwi.getImageFile2() != null && mwi.getImageFile2().equals("")) {
                    image.setImageFile2(null);
                }
                if(mwi.getImageFile3() != null && mwi.getImageFile3().equals("")) {
                    image.setImageFile3(null);
                }
                if(mwi.getImageFile4() != null && mwi.getImageFile4().equals("")) {
                    image.setImageFile4(null);
                }
                if(mwi.getImageFile5() != null && mwi.getImageFile5().equals("")) {
                    image.setImageFile5(null);
                }
                if(mwi.getImageFile6() != null && mwi.getImageFile6().equals("")) {
                    image.setImageFile6(null);
                }
                if(mwi.getImageFile7() != null && mwi.getImageFile7().equals("")) {
                    image.setImageFile7(null);
                }
                if(mwi.getImageFile8() != null && mwi.getImageFile8().equals("")) {
                    image.setImageFile8(null);
                }
                meterWorkImageMapper.updateByPrimaryKey(image);
            }
            rm.setSucceed(true);
            rm.setMessage("操作成功");
            rm.setCode("200");
        }catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setMessage("图片信息写入表异常：" + e.getMessage());
        }
        return rm;
    }

    private String getStrEmpty(String mwi) {
        String strCar;
        String[] strImage = mwi.split("/");
        String[] strTemp = strImage[strImage.length - 1].split("_");
        strCar = strTemp[strTemp.length - 1].replaceAll(".jpeg", "");
        strCar.replaceAll(".jpg", "");
        if(strCar.length() == 2) {
            strTemp = strImage[strImage.length - 1].split("_");
            strCar = strTemp[strTemp.length - 2].replaceAll(".jpeg", "");
            strCar.replaceAll(".jpg", "");
        }
        return strCar;
    }

}
