
package com.tmerclub.cloud.platform.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.platform.constant.OfflineHandleEventStatus;
import com.tmerclub.cloud.api.platform.dto.OfflineHandleEventDTO;
import com.tmerclub.cloud.api.platform.vo.OfflineHandleEventVO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.platform.mapper.OfflineHandleEventMapper;
import com.tmerclub.cloud.platform.model.OfflineHandleEvent;
import com.tmerclub.cloud.platform.model.OfflineHandleEventItem;
import com.tmerclub.cloud.platform.service.OfflineHandleEventItemService;
import com.tmerclub.cloud.platform.service.OfflineHandleEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 下线处理事件
 *
 * @author YXF
 * @date 2021-01-15 17:46:26
 */
@Service
public class OfflineHandleEventServiceImpl implements OfflineHandleEventService {

    @Autowired
    private OfflineHandleEventMapper offlineHandleEventMapper;
    @Autowired
    private OfflineHandleEventItemService offlineHandleEventItemService;

    @Override
    public PageVO<OfflineHandleEventVO> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> offlineHandleEventMapper.list());
    }

    @Override
    public OfflineHandleEventVO getByEventId(Long eventId) {
        return BeanUtil.map(offlineHandleEventMapper.getByEventId(eventId), OfflineHandleEventVO.class);
    }

    @Override
    public void save(OfflineHandleEvent offlineHandleEvent) {
        offlineHandleEvent.setShopId(AuthUserContext.get().getTenantId());
        offlineHandleEvent.setHandlerId(AuthUserContext.get().getUserId());
        offlineHandleEvent.setStatus(Objects.isNull(offlineHandleEvent.getStatus()) ? OfflineHandleEventStatus.OFFLINE_BY_PLATFORM.getValue() : offlineHandleEvent.getStatus());
        offlineHandleEventMapper.save(offlineHandleEvent);
    }


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

    @Override
    public OfflineHandleEventVO getProcessingEventByHandleTypeAndHandleId(Integer handleType, Long handleId, Integer sysType) {
        return offlineHandleEventMapper.getProcessingEventByHandleTypeAndHandleId(handleType, handleId, sysType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateToApply(Long eventId, String reapplyReason) {
        OfflineHandleEventVO eventVO = getByEventId(eventId);
        boolean canApply = !Objects.equals(eventVO.getStatus(), OfflineHandleEventStatus.OFFLINE_BY_PLATFORM.getValue()) &&
                !Objects.equals(eventVO.getStatus(), OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue());
        if (canApply) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        // 更新事件状态
        offlineHandleEventMapper.updateToApply(eventId);

        // 添加事件申请
        OfflineHandleEventItem offlineHandleEventItem = new OfflineHandleEventItem();
        offlineHandleEventItem.setEventId(eventId);
        offlineHandleEventItem.setReapplyReason(reapplyReason);
        offlineHandleEventItemService.save(offlineHandleEventItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditOfflineEvent(OfflineHandleEventDTO offlineHandleEventDTO) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventMapper.getByEventId(offlineHandleEventDTO.getEventId());
        if (Objects.isNull(offlineHandleEvent) || !Objects.equals(offlineHandleEvent.getStatus(), OfflineHandleEventStatus.APPLY_BY_SHOP.getValue())) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        // 审核通过
        if (Objects.equals(offlineHandleEventDTO.getStatus(), OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue())) {
            offlineHandleEvent.setStatus(OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue());
        }
        // 审核不通过
        else if (Objects.equals(offlineHandleEventDTO.getStatus(), OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue())) {
            offlineHandleEvent.setStatus(OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue());
        }
        Date date = new Date();
        offlineHandleEvent.setHandlerId(AuthUserContext.get().getUserId());
        offlineHandleEventMapper.update(offlineHandleEvent);

        OfflineHandleEventItem offlineHandleEventItem = offlineHandleEventItemService.getNewOfflineHandleEventItem(offlineHandleEvent.getEventId());
        if (Objects.isNull(offlineHandleEventItem)) {
            return;
        }
        offlineHandleEventItem.setRefuseReason(offlineHandleEventDTO.getRefuseReason());
        offlineHandleEventItem.setAuditTime(date);
        offlineHandleEventItemService.update(offlineHandleEventItem);
    }

    @Override
    public int offlineCount(Integer handleType, Long handleId, Integer sysType) {
        return offlineHandleEventMapper.offlineCount(handleType, handleId, sysType);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OfflineHandleEventDTO offlineHandleEventDto) {
        OfflineHandleEvent offlineHandleEvent = BeanUtil.map(offlineHandleEventDto, OfflineHandleEvent.class);
        offlineHandleEventMapper.update(offlineHandleEvent);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByHandleTypeAndHandleId(Integer handleType, Long handleId) {
        List<Long> eventIds = offlineHandleEventMapper.listIdByHandleTypeAndHandleId(handleType, handleId);
        if (CollUtil.isEmpty(eventIds)) {
            return;
        }
        // 删除下线处理事件记录项
        offlineHandleEventItemService.deleteByEventIds(eventIds);
        // 删除下线处理事件
        offlineHandleEventMapper.deleteByHandleTypeAndHandleId(handleType, handleId);
    }

    @Override
    public void batchSave(List<OfflineHandleEventDTO> offlineHandleEventList) {
        offlineHandleEventMapper.batchSave(offlineHandleEventList);
    }
}
