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

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.meterworkmeterlink.mapper.MeterWorkMeterLinkMapper;
import com.steerinfo.meterwork.meterworkprelink.mapper.MeterWorkPreLinkMapper;
import com.steerinfo.meterwork.meterworkprelink.model.MeterWorkPreLink;
import com.steerinfo.meterwork.meterworkprelink.service.IMeterWorkPreLinkService;
import com.steerinfo.meterwork.meterworkpreprocess.mapper.MeterWorkPreProcessMapper;
import com.steerinfo.meterwork.meterworkpreprocess.model.MeterWorkPreProcess;
import com.steerinfo.pretrack.pretrackscale.mapper.PreTrackScaleMapper;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.util.DoChinFilters;
import com.steerinfo.util.FieldsCollector;
import com.steerinfo.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

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

    @Resource
    private PreTrackScaleMapper preTrackScaleMapper;

    @Resource
    private MeterWorkPreLinkMapper meterWorkPreLinkMapper;

    @Resource
    private MeterWorkPreProcessMapper meterWorkPreProcessMapper;

    @Resource
    private MeterWorkMeterLinkMapper meterWorkMeterLinkMapper;

    @Override
    protected IBaseMapper<MeterWorkPreLink, String> getMapper() {
        return meterWorkPreLinkMapper;
    }

    //获取该环节的下一环节
    @Override
    public List<MeterWorkPreLink> getNextFlag(String predictionNo,String linkCode){
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("predictionNo", predictionNo);
        obj.put("linkCode", linkCode);
        List<MeterWorkPreLink> meterWorkPreLink = meterWorkPreLinkMapper.getNextFlag(obj);
        return meterWorkPreLink;
    }

    //获取该环节的之前的所有环节
    @Override
    public List<MeterWorkPreLink> getPer(String predictionNo, String linkCode){
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("predictionNo", predictionNo);
        obj.put("linkCode", linkCode);
        List<MeterWorkPreLink> mwpls = meterWorkPreLinkMapper.getPer(obj);
        return mwpls;
    }

    //获取该环节之后的所有环节
    @Override
    public List<MeterWorkPreLink> getNext(String predictionNo, String linkCode){
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("predictionNo", predictionNo);
        obj.put("linkCode", linkCode);
        List<MeterWorkPreLink> mwpls = meterWorkPreLinkMapper.getNext(obj);
        return mwpls;
    }

    @Override
    public RESTfulResult doQueryWf(MeterWorkPreLink oci){
        RESTfulResult rm=new RESTfulResult();
        try{
            rm.setCode("200");
            Map<String,Object> map= FieldsCollector.beanToMap(oci);
            List<MeterWorkPreLink> list = meterWorkPreLinkMapper.findWithCondition(map);
            rm.setData(list);
        }catch(Exception ex){
            rm.setCode("500");
            rm.setMessage("查询失败："+ex.getMessage());
        }
        return rm;
    }

    /**
     * 批次预报全部使用完后重新生成
     * @param predictionNo
     */
    @Override
    @Transactional
    public void batchReset(String predictionNo){
        PreTrackScale pts = preTrackScaleMapper.selectByPrimaryKey(predictionNo);
        //批次预报且状态为正使用
        if (pts.getPredictionType().equals("0") && pts.getValueFlag().equals("4")){
            //批次生成预环节的信息
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("predictionNo", predictionNo);
            obj.put("valueFlag","0");//待使用
            List<MeterWorkPreLink> mwpls = meterWorkPreLinkMapper.selectByParameters(obj);
            List<MeterWorkPreLink> mwplRe = new ArrayList<>();
            if( mwpls.size() == 0){
                HashMap<String, Object> obj1 = new HashMap<>();
                obj1.put("predictionNo",predictionNo);
                obj1.put("valueFlag","1");//已使用
                mwplRe = meterWorkPreLinkMapper.batchReset(obj1);
                for(MeterWorkPreLink mwpl : mwplRe){
                    String no1=meterWorkPreLinkMapper.getNewID(sdf.format(new Date()));
                    if(mwpl.getLinkCode().equals("001021001")){
                        mwpl.setValueFlag("1");
                    }else {
                        mwpl.setValueFlag("0");
                    }
                    mwpl.setPreLinkNo(sdf.format(new Date())+no1);
                    mwpl.setCreateTime(new Date());
                    meterWorkPreLinkMapper.insertSelective(mwpl);
                }
                //修改预流程的状态
                MeterWorkPreProcess mpp=new MeterWorkPreProcess();
                mpp.setPreProcessNo(mwplRe.get(0).getPreProcessNo());
                mpp.setValueFlag("1");
                meterWorkPreProcessMapper.updateByPrimaryKeySelective(mpp);
            }
        }
    }

    //获取预流程是否还有未使用的环节，若没有了则修改其状态
    @Override
    public void updatePrePro(MeterWorkPreLink oci){
        MeterWorkPreLink mplq=new MeterWorkPreLink();
        mplq.setPreProcessNo(oci.getPreProcessNo());
        mplq.setValueFlag("0");
        Map<String,Object> map=FieldsCollector.beanToMap(mplq);
        List<MeterWorkPreLink> list = meterWorkPreLinkMapper.selectByParameters(map);
        if(list.size()==0){
            MeterWorkPreProcess mpp=new MeterWorkPreProcess();
            mpp.setPreProcessNo(oci.getPreProcessNo());
            mpp.setValueFlag("2");
            meterWorkPreProcessMapper.updateByPrimaryKeySelective(mpp);
        }
    }

    @Override
    @Transactional
    public int combinedLink(MeterWorkPreLink model){
        if (StringUtils.isEmpty(model.getPredictionNo())){
            throw new MarkerMetException(500,"未获取到预报编号");
        }
        //查询到预报组合
        PreTrackScale pts =  preTrackScaleMapper.selectByPrimaryKey(model.getPredictionNo());
        List<String> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(pts.getPredictionCombination())){
            //去除当前的预报编号
            String [] str = pts.getPredictionCombination().split(",");
            String no = DoChinFilters.doChinFilters(str,pts.getPredictionNo());
            list = Arrays.asList(no.split(","));
        }
        List<MeterWorkPreLink> mwpl = meterWorkPreLinkMapper.combinedLink(list);
        return mwpl.size();
    }

    /**
     * 按车号查询：历史未卸货的数据
     * @param model
     * @return
     */
    @Override
    public RESTfulResult doQueryNoXh(MeterWorkPreLink model) {
        RESTfulResult rm=new RESTfulResult();
        try{
            rm.setCode("200");
            Map<String,Object> map= FieldsCollector.beanToMap(model);
            List<MeterWorkPreLink> list = meterWorkPreLinkMapper.doQueryNoXh(map);
            rm.setData(list);
        }catch(Exception ex){
            rm.setCode("500");
            rm.setMessage("查询失败："+ex.getMessage());
        }
        return rm;
    }
}
