/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */




package com.yami.shop.delivery.comment.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.enums.OfflineHandleEventStatus;
import com.yami.shop.bean.enums.OfflineHandleEventType;
import com.yami.shop.bean.model.OfflineHandleEvent;
import com.yami.shop.bean.param.OfflineHandleEventAuditParam;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.delivery.comment.Enums.FetchEnum;
import com.yami.shop.delivery.comment.dao.FetchMapper;
import com.yami.shop.delivery.comment.model.Fetch;
import com.yami.shop.delivery.comment.service.FetchService;
import com.yami.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 自提点
 *
 * @author YXF
 * @date 2020-04-23 15:18:29
 */
@Service
@AllArgsConstructor
public class FetchServiceImpl extends ServiceImpl<FetchMapper, Fetch> implements FetchService {

    private final FetchMapper fetchMapper;

    private final OfflineHandleEventService offlineHandleEventService;

    @Override
    public IPage<Fetch> getPage(PageParam<Fetch> page, Fetch fetch) {
        return fetchMapper.getPage(page,fetch);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditApply(Long eventId, Long handleId, String reapplyReason) {
        // 更新活动为待审核状态
        fetchMapper.updateStatus(handleId, FetchEnum.WAIT_AUDIT.getValue());

        // 更新事件状态
        offlineHandleEventService.updateToApply(eventId, reapplyReason);
    }

    @Override
    public void offline(Fetch fetch, String offlineReason, Long sysUserId) {
        // 添加下线处理记录
        Date now = new Date();
        OfflineHandleEvent offlineHandleEvent = new OfflineHandleEvent();
        offlineHandleEvent.setShopId(fetch.getShopId());
        offlineHandleEvent.setHandleId(fetch.getFetchId());
        offlineHandleEvent.setHandleType(OfflineHandleEventType.FETCH.getValue());
        offlineHandleEvent.setOfflineReason(offlineReason);
        offlineHandleEvent.setCreateTime(now);
        offlineHandleEvent.setHandlerId(sysUserId);
        offlineHandleEvent.setStatus(OfflineHandleEventStatus.OFFLINE_BY_PLATFORM.getValue());
        offlineHandleEvent.setUpdateTime(now);
        offlineHandleEventService.save(offlineHandleEvent);

        // 更新活动状态为下线
        fetchMapper.updateStatus(fetch.getFetchId(), FetchEnum.OFFLINE.getValue());
    }

    @Override
    public void auditSeckill(OfflineHandleEventAuditParam offlineHandleEventAuditParam, Long sysUserId) {
        // 审核通过
        if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue())) {
            // 更新秒杀活动为正常状态
            fetchMapper.updateStatus(offlineHandleEventAuditParam.getHandleId(), FetchEnum.NORMAL.getValue());
        }
        // 审核不通过
        else if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue())) {
            fetchMapper.updateStatus(offlineHandleEventAuditParam.getHandleId(), FetchEnum.AUDIT_FAIL.getValue());
        }
        // 更新审核时间
        offlineHandleEventService.auditOfflineEvent(offlineHandleEventAuditParam, sysUserId);
    }


}
