/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.distribution.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.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.Enums.BasicSetDistributionSwitchEnum;
import com.yami.shop.distribution.comment.dao.DistributionBasicSetMapper;
import com.yami.shop.distribution.comment.model.DistributionAuditing;
import com.yami.shop.distribution.comment.model.DistributionBasicSet;
import com.yami.shop.distribution.comment.service.DistributionBasicSetService;
import com.yami.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author lgh on 2019/04/01.
 */
@Service
@AllArgsConstructor
public class DistributionBasicSetServiceImpl extends ServiceImpl<DistributionBasicSetMapper, DistributionBasicSet> implements DistributionBasicSetService {

    private final DistributionBasicSetMapper distributionBasicSetMapper;
    private final OfflineHandleEventService offlineHandleEventService;

    @Override
    @Cacheable(cacheNames = "DistributionBasicSet", key = "'0'")
    public DistributionBasicSet getByShopId() {
        return distributionBasicSetMapper.getByShopId(Constant.PLATFORM_SHOP_ID);
    }

    @Override
    @CacheEvict(cacheNames = "DistributionBasicSet", key = "'0'")
    public void removeCacheByShopId(Long shopId) {

    }

    @Override
    public IPage<DistributionBasicSet> getDistributionBasicSetPage(PageParam<DistributionAuditing> page, DistributionBasicSet distributionBasicSet) {
        return distributionBasicSetMapper.getDistributionBasicSetPage(page, distributionBasicSet);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void auditDistributionBasicSet(OfflineHandleEventAuditParam offlineHandleEventAuditParam, Long sysUserId) {
        // 审核通过
        if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.AGREE_BY_PLATFORM.getValue())) {
            // 更新分销功能为未开启状态
            distributionBasicSetMapper.updateDistributionSwitch(offlineHandleEventAuditParam.getHandleId(), BasicSetDistributionSwitchEnum.DISABLE.getValue());
        }
        // 审核不通过
        else if (Objects.equals(offlineHandleEventAuditParam.getStatus(), OfflineHandleEventStatus.DISAGREE_BY_PLATFORM.getValue())) {
            distributionBasicSetMapper.updateDistributionSwitch(offlineHandleEventAuditParam.getHandleId(), BasicSetDistributionSwitchEnum.OFFLINE.getValue());
        }
        // 更新活动
        offlineHandleEventService.auditOfflineEvent(offlineHandleEventAuditParam, sysUserId);
    }

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

        // 更新分销状态为平台下线
        distributionBasicSetMapper.updateDistributionSwitch(distributionBasicSet.getBasicSetId(), BasicSetDistributionSwitchEnum.OFFLINE.getValue());

    }


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

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