package com.yami.shop.distribution.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
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.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
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.security.platform.util.SecurityUtils;
import com.yami.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 分销管理
 */
@RestController
@RequestMapping("/platform/distributionBasicSet")
@AllArgsConstructor
public class DistributionBasicSetController {

    private final DistributionBasicSetService distributionBasicSetService;
    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 获取信息(根据店铺)
     */
    @GetMapping("/info")
    @PreAuthorize("@pms.hasPermission('platform:distributionBasicSet:info')")
    public ResponseEntity<DistributionBasicSet> info() {
        DistributionBasicSet distributionSet = distributionBasicSetService.getByShopId();
        return ResponseEntity.ok(distributionSet);
    }
    /**
     * 修改
     */
    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform:distributionBasicSet:update')")
    public ResponseEntity<Void> update(@RequestBody @Valid DistributionBasicSet distributionBasicSet){
        distributionBasicSetService.updateById(distributionBasicSet);
        distributionBasicSetService.removeCacheByShopId(Constant.PLATFORM_SHOP_ID);
        return ResponseEntity.ok().build();
    }

    /**
     * 分页获取所有店铺的分销信息
     */
    @GetMapping("/getShopDistributionPage")
    @PreAuthorize("@pms.hasPermission('platform:distributionBasicSet:page')")
    public ResponseEntity<IPage<DistributionBasicSet>> getShopDistributionBasicSetPage(PageParam<DistributionAuditing> page, DistributionBasicSet distributionBasicSet) {
        IPage<DistributionBasicSet> distributionBasicSetPage = distributionBasicSetService.getDistributionBasicSetPage(page, distributionBasicSet);
        return ResponseEntity.ok(distributionBasicSetPage);
    }

    /**
     * 通过活动id获取下线信息
     */
    @GetMapping("/getOfflineHandleEventByBasicSetId/{basicSetId}")
    @PreAuthorize("@pms.hasPermission('platform:distributionBasicSet:info')")
    public ResponseEntity<OfflineHandleEvent> getOfflineHandleEventByBasicSetId(@PathVariable("basicSetId") Long basicSetId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.DISTRIBUTION.getValue(), basicSetId);
        return ResponseEntity.ok(offlineHandleEvent);
    }

    /**
     * 下线活动
     */
    @PostMapping("/offline")
    @PreAuthorize("@pms.hasPermission('platform:distributionBasicSet:update')")
    public ResponseEntity<Void> offline(@RequestBody OfflineHandleEvent offlineHandleEvent) {
        DistributionBasicSet distributionBasicSet = distributionBasicSetService.getById(offlineHandleEvent.getHandleId());
        if (distributionBasicSet == null) {
            throw new GlobalException("未找到该店铺分销基础设置信息");
        }
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        distributionBasicSetService.offline(distributionBasicSet, offlineHandleEvent.getOfflineReason(), sysUserId);
        // 清理缓存
        distributionBasicSetService.removeCacheByShopId(distributionBasicSet.getShopId());

        return ResponseEntity.ok().build();
    }

    /**
     * 审核活动
     */
    @PostMapping("/auditDistribution")
    @PreAuthorize("@pms.hasPermission('platform:distributionBasicSet:update')")
    public ResponseEntity<Void> auditDistribution(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        DistributionBasicSet distributionBasicSet = distributionBasicSetService.getById(offlineHandleEventAuditParam.getHandleId());
        if (distributionBasicSet == null) {
            throw new GlobalException("未找到该店铺分销基础设置信息");
        }
        distributionBasicSetService.auditDistributionBasicSet(offlineHandleEventAuditParam, sysUserId);
        // 清理缓存
        distributionBasicSetService.removeCacheByShopId(distributionBasicSet.getShopId());
        return ResponseEntity.ok().build();
    }

}
