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

import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.appphonehelp.model.AppPhoneHelp;
import com.steerinfo.baseinfo.appphonehelp.mapper.AppPhoneHelpMapper;
import com.steerinfo.baseinfo.appphonehelp.service.IAppPhoneHelpService;
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;

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

    @Resource
    private AppPhoneHelpMapper appPhoneHelpMapper;

    @Override
    protected IBaseMapper<AppPhoneHelp, String> getMapper() {
        return appPhoneHelpMapper;
    }

    @Override
    public RESTfulResult phoneHelp(String carNo, String phoneNo, String driverName) {
        RESTfulResult rm = new RESTfulResult();
        //新建AppPhoneHelp类存储远程救助信息
        AppPhoneHelp helpModel = new AppPhoneHelp();
        //根据车号、电话、司机姓名获取远程求助信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("carNo", carNo);
        map.put("phoneNo", phoneNo);
        map.put("driverName", driverName);
        AppPhoneHelp model = appPhoneHelpMapper.selectByMap(map);
        //若是远程求助信息里面没有求助信息，重新生成Id直接插入
        if(model == null){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            helpModel.setHelpId(sdf.format(new Date()));
            helpModel.setCarNo(carNo);
            helpModel.setPhoneNo(phoneNo);
            helpModel.setDriverName(driverName);
            helpModel.setCreateTime(new Date());
            helpModel.setValueFlag("0");
            helpModel.setHelpId(sdf.format(new Date()));
            int i = appPhoneHelpMapper.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.setCreateTime(new Date());
            model.setValueFlag("0");
            int i = appPhoneHelpMapper.updateByPrimaryKeySelective(model);
            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 phoneHelpComfirm(String id, String createNo, String createName, String result) {
        RESTfulResult rm = new RESTfulResult();
        AppPhoneHelp model = appPhoneHelpMapper.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 = appPhoneHelpMapper.updateByPrimaryKey(model);
            if( i > 0){
                rm.setMessage("操作成功");
                rm.setSucceed();
            }else{
                rm.setMessage("操作失败");
                rm.setFailed();
            }
        }
        return rm;
    }

}
