package vip.xiaonuo.biz.api.cloud.service;

import cn.hutool.core.lang.Validator;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import vip.xiaonuo.biz.api.cloud.entity.CloudTruckIdentifyNotify;
import vip.xiaonuo.biz.api.cloud.mapper.CloudTruckIdentifyNotifyMapper;
import vip.xiaonuo.biz.api.cloud.param.DepotCloudAlarmAddParam;
import vip.xiaonuo.biz.core.Const.BizStr;
import vip.xiaonuo.biz.modular.depotAlarm.service.DepotAlarmService;
import vip.xiaonuo.biz.modular.depotTruckBook.entity.DepotTruckBook;
import vip.xiaonuo.biz.modular.depotTruckBook.service.DepotTruckBookService;
import vip.xiaonuo.biz.modular.depotTruckPlateList.entity.DepotTruckPlateList;
import vip.xiaonuo.biz.modular.depotTruckPlateList.service.DepotTruckPlateListService;
import vip.xiaonuo.common.util.DatesUtil;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 云端实现类
 *
 * @author xiang
 * @date 2023/09/19 16:23
 **/
@Service
public class CloudTruckIdentifyNotifyService extends ServiceImpl<CloudTruckIdentifyNotifyMapper, CloudTruckIdentifyNotify> {

    @Resource
    DepotAlarmService alarmService;
    @Resource
    DepotTruckBookService truckBookService;
    @Resource
    DepotTruckPlateListService truckPlateListService;

    /**
     * 推送保存并报警
     * 入口摄像头只报警假车身
     *
     * @param one 数据
     */
    public void notify(CloudTruckIdentifyNotify one) {
        save(one);

        //异常信息
        String alarmMsg = "";
        //检测异常
        if ("yes".equals(one.getChangeFlag())) {
            //套牌
            if (Strings.isNotBlank(one.getPlateMore())
                    && Validator.isPlateNumber(one.getPlateMore())
                    && !one.getPlateMore().equals(one.getPlate()))
                alarmMsg = alarmMsg + " 套牌:" + one.getPlateMore();
            //换壳
            if (Objects.equals("yes", one.getReplaceBody()))
                alarmMsg = alarmMsg + " 换壳车身";
            if (Strings.isBlank(alarmMsg))
                alarmMsg = "检测异常";
        }
        //白名单flag
        boolean whitePlateFlag = false;
        //黑白名单
        List<DepotTruckPlateList> plateList = truckPlateListService.lst(one.getDepotId(), one.getPlate());
        for (DepotTruckPlateList p : plateList) {
            if ("白".equals(p.getListType()))
                whitePlateFlag = true;
            if ("黑".equals(p.getListType()) && !alarmMsg.contains("黑名单"))
                alarmMsg = alarmMsg + " 黑名单";
        }
        //预约信息
        DepotTruckBook book = new DepotTruckBook();
        if (!"入口摄像头".equals(one.getAreaType())) {
            //已预约审批
            if (!whitePlateFlag && !alarmMsg.contains(" 黑名单")) {
                book = truckBookService.oneByBookLicensePlate(one.getDepotId(), one.getPlate(), BizStr.BOOK_RIGHT);
                if (book == null) {
                    alarmMsg = alarmMsg + " 未预约";
                    book = new DepotTruckBook();
                } else if (!BizStr.STATE_APPROVAL_Y.equals(book.getDepotOpApprovalState())) {
                    alarmMsg = alarmMsg + " 未审批";
                }
            }
        }
        if (Strings.isBlank(alarmMsg)) return;

        //时间处理 2024年03月21日17:30
        String time = one.getTime();
        if (time != null && time.length() == 16 && time.contains("年") && time.contains("月") && time.contains("日"))
            time = time.replace("年", "-").replace("月", "-").replace("日", " ") + ":00";
        else
            time = DatesUtil.forDateTime();
        //预约信息
        DepotCloudAlarmAddParam addParam = new DepotCloudAlarmAddParam(book.getTruckBookId(), "8", alarmMsg.trim());
        addParam.setDepotId(one.getDepotId());
        addParam.setLicensePlate(one.getPlate());
        addParam.setDepotBizType(BizStr.ALARM_IDENTITY);
        addParam.setCreateTime(time);
        addParam.setAreaType(one.getAreaType());
        alarmService.depotWorkAlarmAdd(addParam);
    }

    /**
     * 获取车辆预约名单
     *
     * @param depotId 项目
     */
    public Object plateWhiteList(String depotId) {
        List<DepotTruckBook> bookingLst = truckBookService.bookingLst(depotId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("depotId", depotId);
        jsonObject.put("plateLst", bookingLst.stream().map(DepotTruckBook::getLicensePlate).distinct());
        return jsonObject;
    }

}
