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

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


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.coupon.comment.model.Coupon;
import com.yami.shop.coupon.comment.service.CouponService;
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;


/**
 * 优惠券信息管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2018-11-26
 */
@RestController("adminCouponController")
@RequestMapping("/platform/coupon")
@AllArgsConstructor
public class CouponController {

    private final CouponService couponService;
    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('platform:coupon:page')")
    public ResponseEntity<IPage<Coupon>> page(Coupon coupon, PageParam<Coupon> page) {
        IPage<Coupon> couponPage = couponService.getPlatformPage(page, coupon);
        return ResponseEntity.ok(couponPage);
    }

    /**
     * 获取平台下优惠券
     * @return
     */
    @GetMapping("/list")
    public IPage<Coupon> couponList(PageParam<Coupon> page){
        IPage<Coupon> couponList = couponService.page(page, new LambdaQueryWrapper<Coupon>().eq(Coupon::getShopId, Constant.PLATFORM_SHOP_ID));
        return couponList;
    }

    /**
     * 分页获取平台优惠券
     */
    @GetMapping("/pageByPlatform")
    @PreAuthorize("@pms.hasPermission('platform:coupon:page')")
    public ResponseEntity<IPage<Coupon>> pageByPlatform(Coupon coupon, PageParam<Coupon> page) {
        coupon.setShopId(Constant.PLATFORM_SHOP_ID);
        IPage<Coupon> couponPage = couponService.getPlatformPage(page, coupon);
        return ResponseEntity.ok(couponPage);
    }
    /**
     * 修改优惠券不为积分商品
     */
    @PutMapping("/removeScore")
    @PreAuthorize("@pms.hasPermission('platform:coupon:delete')")
    public ResponseEntity<IPage<Coupon>> removeScore(@RequestBody Long couponId) {
        Coupon coupon = couponService.getById(couponId);
        coupon.setIsScoreType(0);
        coupon.setScorePrice(0);
        couponService.updateById(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改平台券可以积分兑换
     */
    @PutMapping("/couponByScore")
    @PreAuthorize("@pms.hasPermission('platform:coupon:update')")
    public ResponseEntity<IPage<Coupon>> couponByScore(@RequestBody @Valid Coupon coupon) {
        if(coupon.getScorePrice() == null){
            throw new GlobalException("请输入正确的积分数量");
        }
        coupon.setIsScoreType(1);
        couponService.updateById(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 分页获取积分可换优惠券
     */
    @GetMapping("/scorePage")
    @PreAuthorize("@pms.hasPermission('platform:coupon:page')")
    public ResponseEntity<IPage<Coupon>> pageByScore(Coupon coupon, PageParam<Coupon> page) {
        coupon.setIsScoreType(1);
        IPage<Coupon> couponPage = couponService.getPlatformPage(page, coupon);
        return ResponseEntity.ok(couponPage);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('platform:coupon:info')")
    public ResponseEntity<Coupon> info(@PathVariable("id") Long id) {
        Coupon coupon = couponService.getCouponAndCouponProdsByCouponId(id);
        return ResponseEntity.ok(coupon);
    }

    /**
     * 删除
     */
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform:coupon:delete')")
    public ResponseEntity<Void> delete(@RequestBody Long couponId) {
        couponService.deleteByCouponId(couponId);
        return ResponseEntity.ok().build();
    }

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

    /**
     * 下线活动
     */
    @PostMapping("/offline")
    @PreAuthorize("@pms.hasPermission('platform:coupon:update')")
    public ResponseEntity<Void> offline(@RequestBody OfflineHandleEvent offlineHandleEvent) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        Coupon coupon = couponService.getCouponAndCouponProdsByCouponId(offlineHandleEvent.getHandleId());
        if (coupon == null) {
            throw new GlobalException("未找到该活动信息");
        }
        couponService.offline(coupon, offlineHandleEvent.getOfflineReason(), sysUserId);
        return ResponseEntity.ok().build();
    }

    /**
     * 审核活动
     */
    @PostMapping("/auditCoupon")
    @PreAuthorize("@pms.hasPermission('platform:coupon:update')")
    public ResponseEntity<Void> auditCoupon(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        Coupon coupon = couponService.getCouponAndCouponProdsByCouponId(offlineHandleEventAuditParam.getHandleId());
        if (coupon == null) {
            throw new GlobalException("未找到该活动信息");
        }
        couponService.auditCoupon(offlineHandleEventAuditParam, sysUserId);
        // 移除缓存
        couponService.removeCouponAndCouponProdsCache(coupon.getShopId());
        return ResponseEntity.ok().build();
    }


    /**
     * 保存
     */
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('platform:coupon:save')")
    public ResponseEntity<Void> save(@RequestBody @Valid Coupon coupon) {
        // 此处新增优惠券皆为平台通用
        coupon.setShopId(Constant.PLATFORM_SHOP_ID);
        coupon.setSourceStock(coupon.getStocks());
        couponService.insertCouponAndCouponProds(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseEntity<Void> update(@RequestBody @Valid Coupon coupon) {
        // 查询平台下优惠券
        Coupon dbCoupon = couponService.getById(coupon.getCouponId());
        if(null == dbCoupon){
            throw new GlobalException("优惠券不存在");
        }
        coupon.setShopId(dbCoupon.getShopId());
        couponService.updateCouponAndCouponProds(coupon);
        return ResponseEntity.ok().build();
    }


}
