package com.prolog.rdc.tms.app.util;

import cn.hutool.core.date.DateUtil;
import com.prolog.common.util.ToolKits;
import com.prolog.rdc.tms.app.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.app.dao.mapper.TmsShipbillHeaderMapper;
import com.prolog.rdc.tms.app.model.dto.AutoSignInfoQueryDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class AutoSignJudge {

    @Autowired
    private TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

    public boolean autoSignJudge (String shipbillId,String autoSignType) {
        boolean autoSign = true ;
        List<AutoSignInfoQueryDTO>  autoSignInfoQueryDTOS = tmsShipbillHeaderMapper.autoSignInfoQuery(shipbillId);
        if (ToolKits.isEmpty(autoSignInfoQueryDTOS)) {
            return autoSign;
        }
        AutoSignInfoQueryDTO autoSignInfo = autoSignInfoQueryDTOS.get(0);
        Date cmpDt = null;
        if (autoSignType.equals("1")) {
            // 提货签到判定
            if (DataDictionaryConstant.PICK_UP_SIGN_TIME_PICK_START_DT.equals(autoSignInfo.getShipperSignTime())) {
                cmpDt = autoSignInfo.getEstimatedPickupStartTime();
            } else if (DataDictionaryConstant.PICK_UP_SIGN_TIME_PICK_END_DT.equals(autoSignInfo.getShipperSignTime())) {
                cmpDt = autoSignInfo.getEstimatedPickupEndTime();
            } else if (DataDictionaryConstant.PICK_UP_SIGN_TIME_ORDER_DT.equals(autoSignInfo.getShipperSignTime())) {
                cmpDt = autoSignInfo.getOrderDate();
            } else if (DataDictionaryConstant.PICK_UP_SIGN_TIME_COMMIT_DT.equals(autoSignInfo.getShipperSignTime())) {
                cmpDt = autoSignInfo.getCommitDt();
            } else if (DataDictionaryConstant.PICK_UP_SIGN_TIME_REVIEW_DT.equals(autoSignInfo.getShipperSignTime())) {
                cmpDt = autoSignInfo.getReviewDt();
            } else if (DataDictionaryConstant.PICK_UP_SIGN_TIME_DIS_DT.equals(autoSignInfo.getShipperSignTime())) {
                cmpDt = autoSignInfo.getDisDt();
            } else if (DataDictionaryConstant.PICK_UP_SIGN_TIME_DIS_CHECK_DT.equals(autoSignInfo.getShipperSignTime())) {
                cmpDt = autoSignInfo.getDisCheckDt();
            }
            if (ToolKits.isEmpty(cmpDt) || ToolKits.isEmpty(autoSignInfo.getShipperSignTimeUnit())
                    || ToolKits.isEmpty(autoSignInfo.getShipperSignTimeDura())) {
                return autoSign;
            }
            Date signStartDt = null;
            Date signEndDt = null;
            if (autoSignInfo.getShipperSignTimeUnit().equals(DataDictionaryConstant.SIGN_TIME_UNIT_MINUTE)) {
                // 分钟
                signStartDt = DateUtil.offsetMinute(cmpDt,-(autoSignInfo.getShipperSignTimeDura().intValue()));
                signEndDt = DateUtil.offsetMinute(cmpDt,autoSignInfo.getShipperSignTimeDura().intValue());
            } else if (autoSignInfo.getShipperSignTimeUnit().equals(DataDictionaryConstant.SIGN_TIME_UNIT_HOUR)) {
                // 小时
                signStartDt = DateUtil.offsetHour(cmpDt,-(autoSignInfo.getShipperSignTimeDura().intValue()));
                signEndDt = DateUtil.offsetHour(cmpDt,autoSignInfo.getShipperSignTimeDura().intValue());
            } else if (autoSignInfo.getShipperSignTimeUnit().equals(DataDictionaryConstant.SIGN_TIME_UNIT_DAY)) {
                // 天
                signStartDt = DateUtil.offsetDay(cmpDt,-(autoSignInfo.getShipperSignTimeDura().intValue()));
                signEndDt = DateUtil.offsetDay(cmpDt,autoSignInfo.getShipperSignTimeDura().intValue());
            } else {
                return autoSign;
            }
            Date date  = DateUtil.date();
            if (signStartDt.before(date) && signEndDt.after(date)) {
                autoSign = true;
            } else {
                autoSign = false;
            }
        } else {
            // 送达签到判定
            if (DataDictionaryConstant.DELIVERY_SIGN_TIME_DELIVERY_START_DT.equals(autoSignInfo.getConsigneeSignTime())) {
                cmpDt = autoSignInfo.getEstimatedDeliveryStartTime();
            } else if (DataDictionaryConstant.DELIVERY_SIGN_TIME_DELIVERY_END_DT.equals(autoSignInfo.getConsigneeSignTime())) {
                cmpDt = autoSignInfo.getEstimatedDeliveryEndTime();
            } else if (DataDictionaryConstant.DELIVERY_SIGN_TIME_ORDER_DT.equals(autoSignInfo.getConsigneeSignTime())) {
                cmpDt = autoSignInfo.getOrderDate();
            } else if (DataDictionaryConstant.DELIVERY_SIGN_TIME_COMMIT_DT.equals(autoSignInfo.getConsigneeSignTime())) {
                cmpDt = autoSignInfo.getCommitDt();
            } else if (DataDictionaryConstant.DELIVERY_SIGN_TIME_REVIEW_DT.equals(autoSignInfo.getConsigneeSignTime())) {
                cmpDt = autoSignInfo.getReviewDt();
            } else if (DataDictionaryConstant.DELIVERY_SIGN_TIME_DIS_DT.equals(autoSignInfo.getConsigneeSignTime())) {
                cmpDt = autoSignInfo.getDisDt();
            } else if (DataDictionaryConstant.DELIVERY_SIGN_TIME_DIS_CHECK_DT.equals(autoSignInfo.getConsigneeSignTime())) {
                cmpDt = autoSignInfo.getDisCheckDt();
            }
            if (ToolKits.isEmpty(cmpDt) || ToolKits.isEmpty(autoSignInfo.getConsigneeSignTimeUnit())
                    || ToolKits.isEmpty(autoSignInfo.getConsigneeSignTimeDura())) {
                return  autoSign;
            }
            Date signStartDt = null;
            Date signEndDt = null;
            if (autoSignInfo.getConsigneeSignTimeUnit().equals(DataDictionaryConstant.SIGN_TIME_UNIT_MINUTE)) {
                // 分钟
                signStartDt = DateUtil.offsetMinute(cmpDt,-(autoSignInfo.getConsigneeSignTimeDura().intValue()));
                signEndDt = DateUtil.offsetMinute(cmpDt,autoSignInfo.getConsigneeSignTimeDura().intValue());
            } else if (autoSignInfo.getConsigneeSignTimeUnit().equals(DataDictionaryConstant.SIGN_TIME_UNIT_HOUR)) {
                // 小时
                signStartDt = DateUtil.offsetHour(cmpDt,-(autoSignInfo.getConsigneeSignTimeDura().intValue()));
                signEndDt = DateUtil.offsetHour(cmpDt,autoSignInfo.getConsigneeSignTimeDura().intValue());
            } else if (autoSignInfo.getConsigneeSignTimeUnit().equals(DataDictionaryConstant.SIGN_TIME_UNIT_DAY)) {
                // 天
                signStartDt = DateUtil.offsetDay(cmpDt,-(autoSignInfo.getConsigneeSignTimeDura().intValue()));
                signEndDt = DateUtil.offsetDay(cmpDt,autoSignInfo.getConsigneeSignTimeDura().intValue());
            } else {
                return  autoSign;
            }

            Date date  = DateUtil.date();
            if (signStartDt.before(date) && signEndDt.after(date)) {
                autoSign = true;
            } else {
                autoSign = false;
            }
        }
        return autoSign;
    }
}
