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

import cn.hutool.core.bean.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.api.cloud.param.CloudDeviceParam;
import vip.xiaonuo.biz.api.cloud.param.DepotCloudAlarmAddParam;
import vip.xiaonuo.biz.api.cloud.param.TruckBookFlowParam;
import vip.xiaonuo.biz.core.Const.BizStr;
import vip.xiaonuo.biz.core.exception.BizExceptionEnum;
import vip.xiaonuo.biz.modular.depotAccessRecord.param.DepotAccessRecordAddPushParam;
import vip.xiaonuo.biz.modular.depotAccessRecord.service.DepotAccessRecordService;
import vip.xiaonuo.biz.modular.depotAlarm.service.DepotAlarmService;
import vip.xiaonuo.biz.modular.depotCloudMonitor.param.DepotCloudMonitorAddParam;
import vip.xiaonuo.biz.modular.depotCloudMonitor.service.DepotCloudMonitorService;
import vip.xiaonuo.biz.modular.depotDevice.service.DepotDeviceService;
import vip.xiaonuo.biz.modular.depotGrain.entity.DepotGrain;
import vip.xiaonuo.biz.modular.depotGrain.service.DepotGrainService;
import vip.xiaonuo.biz.modular.depotSampling.param.DepotSamplingAddParam;
import vip.xiaonuo.biz.modular.depotSampling.service.DepotSamplingService;
import vip.xiaonuo.biz.modular.depotTruckBook.entity.DepotTruckBook;
import vip.xiaonuo.biz.modular.depotTruckBook.service.DepotTruckBookService;
import vip.xiaonuo.biz.modular.depotTruckLade.param.DepotTruckLadeAddParam;
import vip.xiaonuo.biz.modular.depotTruckLade.service.DepotTruckLadeService;
import vip.xiaonuo.biz.modular.depotTruckWeighDouble.param.DepotTruckWeighDoubleAddParam;
import vip.xiaonuo.biz.modular.depotTruckWeighDouble.service.DepotTruckWeighDoubleService;
import vip.xiaonuo.biz.modular.depotWorkAlarm.service.DepotWorkAlarmService;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.util.BigDecimalUtil;
import vip.xiaonuo.common.util.DatesUtil;

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

/**
 * 云端实现类
 *
 * @author xiang
 * @date 2023/09/19 16:23
 **/
@Service
public class CloudService {

    @Resource
    private DepotAccessRecordService accessRecordService;
    @Resource
    private DepotTruckBookService depotTruckBookService;
    @Resource
    private DepotSamplingService samplingService;
    @Resource
    private DepotTruckWeighDoubleService truckWeighDoubleService;
    @Resource
    private DepotTruckLadeService truckLadeService;
    @Resource
    private DepotGrainService depotGrainService;
    @Resource
    private DepotWorkAlarmService depotWorkAlarmService;

    public Object truckBookFlowPush(TruckBookFlowParam param) {
        DepotTruckBook book = depotTruckBookService.getOneByBookId(param.getTruckBookId());
        //检测 身份报警
        if (depotWorkAlarmService.existAlarmIn(book.getDepotId(), book.getLicensePlate(), BizStr.ALARM_TYPE_B, "8"))
            throw new CommonException(500, "车辆身份检测报警未处理");

        Object result = null;
        if (Objects.equals(param.getPushFlag(), 1)) {
            result = truckBookFlowPushAdd(param);
        } else if (Objects.equals(param.getPushFlag(), 2)) {
            truckBookFlowPushUp(param);
        } else if (Objects.equals(param.getPushFlag(), 3)) {
            truckBookFlowPushDel(param);
        }
        return result;
    }

    /**
     * 新增
     *
     * @author xiang
     * @date 2023/09/19 16:23
     **/
    @Transactional(rollbackFor = Exception.class)
    public Object truckBookFlowPushAdd(TruckBookFlowParam param) {
        Object result = null;
        //已入场
        if (BizStr.FLOW_RU_CHANG.equals(param.getBizFlowName())) {
            DepotTruckBook book = depotTruckBookService.getOneByBookId(param.getTruckBookId());
            param.setTruckBookId(book.getTruckBookId());
            accessRecordService.add(BeanUtil.toBean(param, DepotAccessRecordAddPushParam.class));
            //车辆进程更新
            depotTruckBookService.upTruckBookFlow(book, param.getBizFlowName(), param.getInTime());
            result = book;
        }
        //已至扦样区
        else if (BizStr.FLOW_QIAN_YANG_IN.equals(param.getBizFlowName())) {
            samplingService.addFlowIn(param.getTruckBookId(), param.getTruckPicInOne(), param.getTruckPicInTwo(), param.getTruckInTime());
            //车辆进程更新
            depotTruckBookService.upTruckBookFlow(param.getTruckBookId(), param.getBizFlowName(), param.getTruckInTime());
        }
        //完成一次过磅
        else if (BizStr.FLOW_GUO_BANG_ONE.equals(param.getBizFlowName())) {
            DepotTruckBook book = depotTruckBookService.getOneByBookId(param.getTruckBookId());
            DepotTruckWeighDoubleAddParam addParam = BeanUtil.toBean(param, DepotTruckWeighDoubleAddParam.class);
            addParam.setDepotBizType(book.getDepotBizType());
            addParam.setGrainGroup(book.getGrainGroup());
            addParam.setGrainType(book.getGrainType());
            truckWeighDoubleService.add(addParam);
            //入库添加卸货记录
            String onLade = null;
            if (BizStr.KU_RU.equals(book.getDepotBizType())) {
                DepotGrain depotGrain = depotGrainService.getOne(book.getDepotId());
                onLade = depotGrain.getOnLade();
                if (BizStr.STATE_ON.equals(depotGrain.getOnLade())) {
                    onLade = depotGrain.getOnLade();
                    DepotTruckLadeAddParam ladeAddParam = BeanUtil.toBean(book, DepotTruckLadeAddParam.class);
                    ladeAddParam.setGrossWeight(param.getGrossWeight());
                    ladeAddParam.setGrossTime(DatesUtil.forDateTime(param.getGrossTime()));
                    ladeAddParam.setLimitInTime(param.getLimitInTime());
                    truckLadeService.add(ladeAddParam);
                }
            }
            //车辆进程更新
            depotTruckBookService.upTruckBookFlow(book, param.getBizFlowName(), DatesUtil.forDateTime(param.getGrossTime()), onLade);
        }
        return result;
    }

    /**
     * 更新
     *
     * @author xiang
     * @date 2023/09/19 16:23
     **/
    @Transactional(rollbackFor = Exception.class)
    public void truckBookFlowPushUp(TruckBookFlowParam param) {
        //已出场
        if (BizStr.FLOW_CHU_CHANG.equals(param.getBizFlowName())) {
            accessRecordService.editByBookId(BeanUtil.toBean(param, DepotAccessRecordAddPushParam.class));
            //车辆进程更新
            depotTruckBookService.upTruckBookFlow(param.getTruckBookId(), param.getBizFlowName(), param.getOutTime());
        }
        //离开扦样区
        else if (BizStr.FLOW_QIAN_YANG_OUT.equals(param.getBizFlowName())) {
            samplingService.addFlowOut(param.getTruckBookId(), param.getTruckPicOutOne(), param.getTruckPicOutTwo(), param.getTruckOutTime());
            //车辆进程更新
            depotTruckBookService.upTruckBookFlow(param.getTruckBookId(), param.getBizFlowName(), param.getTruckOutTime());
        }
        //完成二次过磅
        else if (BizStr.FLOW_GUO_BANG_TWO.equals(param.getBizFlowName())) {
            if (BigDecimalUtil.isNotNullOrEqZero(param.getTotalPrice()) && BigDecimalUtil.isNotNullOrEqZero(param.getNetWeight()))
                param.setUnitPrice(param.getTotalPrice().divide(param.getNetWeight().movePointLeft(3), 2, BigDecimal.ROUND_HALF_UP));
            truckWeighDoubleService.editByBookId(BeanUtil.toBean(param, DepotTruckWeighDoubleAddParam.class));
            //车辆进程更新
            depotTruckBookService.upTruckBookFlow(param.getTruckBookId(), param.getBizFlowName(), DatesUtil.forDateTime(param.getTareTime()));
        }
    }


    /**
     * 删除
     *
     * @author xiang
     * @date 2023/09/19 16:23
     **/
    public void truckBookFlowPushDel(TruckBookFlowParam param) {
        //已入场
        if (BizStr.FLOW_RU_CHANG.equals(param.getBizFlowName())) {
            accessRecordService.delByBookId(param.getTruckBookId());
        }
    }

    @Resource
    DepotCloudMonitorService cloudMonitorService;
    @Resource
    DepotAlarmService depotAlarmService;

    /**
     * 添加监控异常
     *
     * @author xiang
     * @date 2023/09/19 16:23
     **/
    public void depotWorkAlarmAdd(DepotCloudAlarmAddParam param) {
        cloudMonitorService.add(BeanUtil.toBean(param, DepotCloudMonitorAddParam.class));
        depotAlarmService.depotWorkAlarmAdd(param);
    }

    @Resource
    private DepotDeviceService depotDeviceService;

    /**
     * 设备同步
     *
     * @author xiang
     * @date 2023/09/19 16:23
     **/
    public void depotDevicePush(CloudDeviceParam param) {
        if (Objects.equals(param.getPushFlag(), 1)) {
            depotDeviceService.addByPush(param);
        } else if (Objects.equals(param.getPushFlag(), 2)) {
            depotDeviceService.editByPush(param);
        } else if (Objects.equals(param.getPushFlag(), 3)) {
            depotDeviceService.deleteByPush(param);
        }
    }

}
