package com.ningdatech.carapi.driver.service.impl;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.exception.code.ExceptionCode;
import com.ningdatech.carapi.common.constant.CommonConstant;
import com.ningdatech.carapi.driver.call.RestTemplateToInterface;
import com.ningdatech.carapi.driver.constant.DriverAbnormalBehaviorOverseeEnum;
import com.ningdatech.carapi.driver.entity.DriverAbnormalBehaviorAnalysis;
import com.ningdatech.carapi.driver.entity.DriverAbnormalBehaviorAnalysisOverseeDTO;
import com.ningdatech.carapi.driver.entity.OverseeInterfaceCallDTO;
import com.ningdatech.carapi.driver.mapper.BehaviourAnalysisMapper;
import com.ningdatech.carapi.driver.model.po.AlarmWarnFilePO;
import com.ningdatech.carapi.driver.service.IBehaviourAnalysisService;
import com.ningdatech.carapi.user.util.LoginUserUtil;

import lombok.RequiredArgsConstructor;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author PoffyZhang
 * @since 2022-11-07
 */
@Service
@RequiredArgsConstructor
public class BehaviourAnalysisServiceImpl extends ServiceImpl<BehaviourAnalysisMapper, DriverAbnormalBehaviorAnalysis>
        implements IBehaviourAnalysisService {

    private final BehaviourAnalysisMapper behaviourAnalysisMapper;
    private final RestTemplateToInterface restTemplateToInterface;

    @Value("${send-urge-warn.url}")
    private String sendUrgeWarnUrl;

    @Override
    public Page<DriverAbnormalBehaviorAnalysis> pageBehaviour(Page page, LambdaQueryWrapper<DriverAbnormalBehaviorAnalysis> wrapper) {
        return behaviourAnalysisMapper.pageBehiviour(page, wrapper);
    }

    @Override
    public DriverAbnormalBehaviorAnalysis getOneBehaviour(LambdaQueryWrapper<DriverAbnormalBehaviorAnalysis> wrapper) {
        return behaviourAnalysisMapper.getOneBehiviour(wrapper);
    }

    @Override
    public List<DriverAbnormalBehaviorAnalysis> selectDriverAbnormalBehaviorAnalysisData(long index) {
        return behaviourAnalysisMapper.selectDriverAbnormalBehaviorAnalysisByCarPlate(index);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean oversee(DriverAbnormalBehaviorAnalysisOverseeDTO param, Long userId) {
        //校验传入的审核码
        if (!DriverAbnormalBehaviorOverseeEnum.OVERSEE.getCode().equals(param.getOverseeStatus())) {
            throw new BizException("请检查传入的督办code!");
        }
        DriverAbnormalBehaviorAnalysis driverAbnormalBehaviorAnalysis = behaviourAnalysisMapper.selectById(param.getId());
        if (Objects.isNull(driverAbnormalBehaviorAnalysis)) {
            throw new BizException(ExceptionCode.OPERATION_EX.getCode(), "驾驶员异常行为 {}为空", param.getId());
        }
        if (driverAbnormalBehaviorAnalysis.getOverseeStatus().equals(DriverAbnormalBehaviorOverseeEnum.OVERSEE.getCode())) {
            throw new BizException(ExceptionCode.OPERATION_EX.getCode(), "只能督办待督办的驾驶员异常行为 {}", param.getId());
        }
        //调用外部接口下发督办请求
        //装配请求参数
        OverseeInterfaceCallDTO req = new OverseeInterfaceCallDTO();
        req.setGnssCenterId(driverAbnormalBehaviorAnalysis.getGnssCenterId());
        req.setMSgSn(driverAbnormalBehaviorAnalysis.getAlarmMsgSn());
        req.setSupervisor(LoginUserUtil.getUsername());
        //String resultJson = restTemplateToInterface.doPostWith2(sendUrgeWarnUrl, req);
        //保存督办处理结果
        //driverAbnormalBehaviorAnalysis.setOverseeResult(resultJson);
        //JSONObject result = JSON.parseObject(resultJson);
        //String status = result.getString(CommonConstant.CALL_STATUS);
        String status = "ok";
        if (CommonConstant.CALL_STATUS_OK_VALUE.equals(status)) {
            //设置告警异常行为状态为已督办
            driverAbnormalBehaviorAnalysis.setOverseeStatus(param.getOverseeStatus());
            driverAbnormalBehaviorAnalysis.setOverseeTime(LocalDateTime.now());
            driverAbnormalBehaviorAnalysis.setUpdateBy(userId);
            driverAbnormalBehaviorAnalysis.setUpdateOn(LocalDateTime.now());
            behaviourAnalysisMapper.updateById(driverAbnormalBehaviorAnalysis);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public List<AlarmWarnFilePO> selectDriverAbnormalBehaviorAnalysisAppendixData(List<Integer> alarmMsgSnList) {
        return behaviourAnalysisMapper.selectDriverAbnormalBehaviorAnalysisAppendixData(alarmMsgSnList);
    }

    @Override
    public Long selectOneByAlarmMsgSn(Integer alarmMsgSn) {
        return behaviourAnalysisMapper.selectOneByAlarmMsgSn(alarmMsgSn);
    }

}
