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

import com.steerinfo.baseinfo.appphonehelp.model.AppPhoneHelp;
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.meterworkscheduledhelp.mapper.MeterWorkScheduledHelpMapper;
import com.steerinfo.meterwork.meterworkscheduledhelp.model.MeterWorkScheduledHelp;
import com.steerinfo.meterwork.meterworkscheduledhelp.service.IMeterWorkScheduledHelpService;
import com.steerinfo.util.PublicMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailParseException;
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;

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

    @Resource
    private MeterWorkScheduledHelpMapper meterWorkScheduledHelpMapper;

    @Override
    protected IBaseMapper<MeterWorkScheduledHelp, String> getMapper() {
        return meterWorkScheduledHelpMapper;
    }

    @Override
    public RESTfulResult scheduledHelp(String carNo, String baseSpotNo, String baseSpotName, String helpContent) {
        RESTfulResult rm = new RESTfulResult();
        //新建AppPhoneHelp类存储远程救助信息
        MeterWorkScheduledHelp helpModel = new MeterWorkScheduledHelp();
        //根据车号、电话、司机姓名获取远程求助信息
        HashMap<String, Object> map = new HashMap<>();
        map.put("carNo", carNo);
        map.put("baseSpotNo", baseSpotNo);
        map.put("baseSpotName", baseSpotName);
        List<MeterWorkScheduledHelp> model = meterWorkScheduledHelpMapper.selectByParameters(map);
        //若是远程求助信息里面没有求助信息，重新生成Id直接插入
        if(model.size() < 1){
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//            helpModel.setHelpId(sdf.format(new Date()));
            String no =  PublicMethod.IdCreate(3);
            helpModel.setHelpId(no);
            helpModel.setCarNo(carNo);
            helpModel.setBaseSpotNo(baseSpotNo);
            helpModel.setBaseSpotName(baseSpotName);
            helpModel.setHelpContent(helpContent);
            helpModel.setCreateTime(new Date());
            helpModel.setValueFlag("0");
            int i = meterWorkScheduledHelpMapper.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 = meterWorkScheduledHelpMapper.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();
        MeterWorkScheduledHelp model = meterWorkScheduledHelpMapper.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 = meterWorkScheduledHelpMapper.updateByPrimaryKeySelective(model);
            if( i > 0){
                rm.setMessage("操作成功");
                rm.setSucceed();
            }else{
                rm.setMessage("操作失败");
                rm.setFailed();
            }
        }
        return rm;
    }

    @Override
    public RESTfulResult checkAdd(MeterWorkScheduledHelp model) {
        try{
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("200");
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            String afl = sdf.format(new Date());
//            String no = meterWorkScheduledHelpMapper.getNewID(afl);
//            model.setHelpId(sdf.format(new Date()) + no);

            String no =  PublicMethod.IdCreate(3);
            model.setHelpId(no);
            model.setCreateTime(new Date());
            model.setValueFlag("0");
            meterWorkScheduledHelpMapper.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;
        }
    }

    @Override
    public PageList<MeterWorkScheduledHelp> likeByDesc(Map<String, Object> parameters, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkScheduledHelp> rows = meterWorkScheduledHelpMapper.likeByDesc(parameters);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public PageList<MeterWorkScheduledHelp> HelpMsgByDesc(Map<String, Object> parameters, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterWorkScheduledHelp> rows = meterWorkScheduledHelpMapper.HelpMsgByDesc(parameters);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }
}
