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

package com.yami.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.model.OfflineHandleEventItem;
import com.yami.shop.bean.param.OfflineHandleEventAuditParam;
import com.yami.shop.dao.OfflineHandleEventItemMapper;
import com.yami.shop.dao.OfflineHandleEventMapper;
import com.yami.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 下线处理事件
 *
 * @author
 * @date 2019-09-20 10:44:58
 */
@Service
@AllArgsConstructor
public class OfflineHandleEventServiceImpl extends ServiceImpl<OfflineHandleEventMapper, OfflineHandleEvent> implements OfflineHandleEventService {

    private final OfflineHandleEventMapper offlineHandleEventMapper;
    private final OfflineHandleEventItemMapper offlineHandleEventItemMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void apply(Long eventId, String reapplyReason) {
        // 更新事件为申请状态
        offlineHandleEventMapper.updateToApply(eventId);
        // 添加申请记录
        OfflineHandleEventItem offlineHandleEventItem = new OfflineHandleEventItem();
        offlineHandleEventItem.setCreateTime(new Date());
        offlineHandleEventItem.setEventId(eventId);
        offlineHandleEventItem.setReapplyReason(reapplyReason);
        offlineHandleEventItemMapper.insert(offlineHandleEventItem);
    }

    @Override
    public OfflineHandleEvent getProcessingEventByHandleTypeAndHandleId(Integer eventType, Long handleId) {
        return offlineHandleEventMapper.getProcessingEventByHandleTypeAndHandleId(eventType, handleId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateToApply(Long eventId, String reapplyReason) {
        // 更新事件状态
        offlineHandleEventMapper.updateToApply(eventId);

        // 添加事件申请
        OfflineHandleEventItem offlineHandleEventItem = new OfflineHandleEventItem();
        offlineHandleEventItem.setEventId(eventId);
        offlineHandleEventItem.setReapplyReason(reapplyReason);
        Date date = new Date();
        offlineHandleEventItem.setCreateTime(date);
        offlineHandleEventItem.setReapplyTime(date);
        offlineHandleEventItemMapper.insert(offlineHandleEventItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditOfflineEvent(OfflineHandleEventAuditParam offlineHandleEventAuditParam, Long sysUserId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventMapper.selectById(offlineHandleEventAuditParam.getEventId());
        // 审核通过
        if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue())) {
            offlineHandleEvent.setStatus(OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue());
        }
        // 审核不通过
        else if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue())) {
            offlineHandleEvent.setStatus(OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue());
        }
        Date date = new Date();
        offlineHandleEvent.setUpdateTime(date);
        offlineHandleEvent.setHandlerId(sysUserId);
        offlineHandleEventMapper.updateById(offlineHandleEvent);

        if(!Objects.equals(offlineHandleEvent.getHandleType(), OfflineHandleEventType.SHOP.getValue())){
            List<OfflineHandleEventItem> offlineHandleEventItems = offlineHandleEventItemMapper.selectList(new LambdaQueryWrapper<OfflineHandleEventItem>()
                    .eq(OfflineHandleEventItem::getEventId, offlineHandleEvent.getEventId()));
            OfflineHandleEventItem offlineHandleEventItem = offlineHandleEventItems.stream().sorted(Comparator.comparing(OfflineHandleEventItem::getCreateTime).reversed()).collect(Collectors.toList()).get(0);
            offlineHandleEventItem.setRefuseReason(offlineHandleEventAuditParam.getRefuseReason());
            offlineHandleEventItem.setAuditTime(date);
            offlineHandleEventItemMapper.updateById(offlineHandleEventItem);
        }
    }
}
