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

package com.yami.shop.discount.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.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.discount.comment.model.Discount;
import com.yami.shop.discount.comment.service.DiscountService;
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.*;


/**
 * 折扣信息管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2018-11-26
 */
@RestController
@RequestMapping("/platform/discount")
@AllArgsConstructor
public class DiscountController {

    private final DiscountService discountService;
    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('platform:discount:page')")
    public ResponseEntity<IPage<Discount>> page(Discount discount, PageParam<Discount> page) {
        IPage<Discount> discountIPage = discountService.getPlatformDiscountPage(page, discount);
        return ResponseEntity.ok(discountIPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{discountId}")
    @PreAuthorize("@pms.hasPermission('platform:discount:info')")
    public ResponseEntity<Discount> info(@PathVariable("discountId") Long discountId) {
        Discount discount = discountService.getDiscountAndItemAndProdById(discountId);
        return ResponseEntity.ok(discount);
    }

    /**
     * 下线活动
     */
    @PostMapping("/offline")
    @PreAuthorize("@pms.hasPermission('platform:discount:update')")
    public ResponseEntity<Void> offline(@RequestBody OfflineHandleEvent offlineHandleEvent) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        Discount discount = discountService.getById(offlineHandleEvent.getHandleId());
        if (discount == null) {
            throw new GlobalException("未找到此活动信息");
        }
        discountService.offline(discount, offlineHandleEvent.getOfflineReason(), sysUserId);

        // 移除缓存
        discountService.removeDiscountAndItemAndProdCacheById(discount.getDiscountId());
        discountService.removeDiscountsAndItemsCacheByShopId(discount.getShopId());
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{discountId}")
    @PreAuthorize("@pms.hasPermission('platform:discount:delete')")
    public ResponseEntity<Void> delete(@PathVariable Long discountId) {
        Discount discount = discountService.getDiscountAndItemAndProdById(discountId);
        if (discount == null) {
            throw new GlobalException("未找到该活动信息");
        }
        discountService.deleteDiscountsAndItemsAndProds(discountId, discount.getShopId());
        return ResponseEntity.ok().build();
    }

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

    /**
     * 审核活动
     */
    @PostMapping("/auditDiscount")
    @PreAuthorize("@pms.hasPermission('platform:discount:update')")
    public ResponseEntity<Void> auditDiscount(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        Discount discount = discountService.getDiscountAndItemAndProdById(offlineHandleEventAuditParam.getHandleId());
        if (discount == null) {
            throw new GlobalException("未找到该活动信息");
        }
        discountService.auditDiscount(offlineHandleEventAuditParam, sysUserId);

        // 移除缓存
        discountService.removeDiscountAndItemAndProdCacheById(discount.getDiscountId());
        discountService.removeDiscountsAndItemsCacheByShopId(discount.getShopId());
        return ResponseEntity.ok().build();
    }


}
