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

import com.steerinfo.framework.controller.RESTfulResult;
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.meterworkrailwayscheduledhelp.mapper.MeterWorkRailwayScheduledHelpMapper;
import com.steerinfo.meterwork.meterworkrailwayscheduledhelp.model.MeterWorkRailwayScheduledHelp;
import com.steerinfo.meterwork.meterworkrailwayscheduledhelp.service.IMeterWorkRailwayScheduledHelpService;
import com.steerinfo.meterwork.meterworkscheduledhelp.model.MeterWorkScheduledHelp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

    @Resource
    private MeterWorkRailwayScheduledHelpMapper meterWorkRailwayScheduledHelpMapper;

    @Override
    protected IBaseMapper<MeterWorkRailwayScheduledHelp, String> getMapper() {
        return meterWorkRailwayScheduledHelpMapper;
    }

    @Override
    public PageList<MeterWorkRailwayScheduledHelp> likeByDesc(HashMap<String, Object> parameters, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        if(parameters.get("baseSpotNoStr") != null) {
            String spotNoStr[] = parameters.get("baseSpotNoStr").toString().split(",");
            parameters.put("spotNoStr", spotNoStr);
        }
        if(parameters.get("createManNoStr") != null) {
            String manNoStr[] = parameters.get("createManNoStr").toString().split(",");
            parameters.put("manNoStr", manNoStr);
        }
        List<MeterWorkRailwayScheduledHelp> rows = meterWorkRailwayScheduledHelpMapper.likeByDesc(parameters);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public RESTfulResult scheduledHelp(String railwayNo, String baseSpotNo, String baseSpotName, String helpContent, String warmType, String warmTypeName) {
        RESTfulResult rm = new RESTfulResult();
        //新建AppPhoneHelp类存储远程救助信息
        MeterWorkRailwayScheduledHelp helpModel = new MeterWorkRailwayScheduledHelp();
        //根据车号、电话、司机姓名获取远程求助信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("railwayNo", railwayNo);
        map.put("baseSpotNo", baseSpotNo);
        map.put("baseSpotName", baseSpotName);
        List<MeterWorkRailwayScheduledHelp> model = meterWorkRailwayScheduledHelpMapper.selectByParameters(map);
        //若是远程求助信息里面没有求助信息，重新生成Id直接插入
        if(model.size() < 1){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            helpModel.setHelpId(sdf.format(new Date()));
            helpModel.setRailwayNo(railwayNo);
            helpModel.setBaseSpotNo(baseSpotNo);
            helpModel.setBaseSpotName(baseSpotName);
            helpModel.setHelpContent(helpContent);
            helpModel.setWarmType(warmType);
            helpModel.setWarmTypeName(warmTypeName);
            helpModel.setCreateTime(new Date());
            helpModel.setValueFlag("0");
            int i = meterWorkRailwayScheduledHelpMapper.insertSelective(helpModel);
            if( i > 0){
                rm.setSucceed();
                rm.setMessage("数据正在处理，请等待！！");
                rm.setCode("200");
                rm.setData(helpModel);
            }else{
                rm.setMessage("操作失败");
                rm.setCode("500");
                rm.setFailed();
            }
        }else{  //若有求助信息，更新远程求助表
            model.get(0).setHelpContent(helpContent);
            model.get(0).setCreateTime(new Date());
            model.get(0).setValueFlag("0");
            int i = meterWorkRailwayScheduledHelpMapper.updateByPrimaryKeySelective(model.get(0));
            if( i > 0){
                rm.setSucceed();
                rm.setMessage("数据正在处理，请等待！！");
                rm.setCode("200");
                rm.setData(model);
            }else{
                rm.setMessage("操作失败");
                rm.setCode("500");
                rm.setFailed();
            }
        }
        return rm;
    }

    @Override
    public RESTfulResult scheduledHelpConfirm(String id, String createNo, String createName, String result) {
        RESTfulResult rm = new RESTfulResult();
        MeterWorkRailwayScheduledHelp model = meterWorkRailwayScheduledHelpMapper.selectByPrimaryKey(id);
        if(model == null){
            rm.setMessage("根据远程求助信息ID未找到相应远程求助信息，请确认！！");
            rm.setData(null);
            rm.setFailed();
        }else{
            model.setCreateNo(createNo);
            model.setCreateName(createName);
            model.setEndTime(new Date());
            model.setValueFlag("1");
            model.setResult(result);
            int i = meterWorkRailwayScheduledHelpMapper.updateByPrimaryKeySelective(model);
            if( i > 0){
                rm.setMessage("操作成功");
                rm.setSucceed();
            }else{
                rm.setMessage("操作失败");
                rm.setFailed();
            }
        }
        return rm;
    }

    @Override
    public RESTfulResult checkAdd(MeterWorkRailwayScheduledHelp model) {
        try{
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("200");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            String no = meterWorkRailwayScheduledHelpMapper.getNewID(afl);
            model.setHelpId(sdf.format(new Date()) + no);
            model.setCreateTime(new Date());
            model.setValueFlag("0");
            meterWorkRailwayScheduledHelpMapper.insertSelective(model);
            rm.setData(model.getHelpId());
            rm.setMessage("成功");
            return rm;
        }
        catch (MarkerMetException e){
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("500");
            rm.setFailed();
            rm.setMessage(e.getMessage());
            return rm;
        }
    }
}
