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

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


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.CouponType;
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.coupon.comment.model.Coupon;
import com.yami.shop.coupon.comment.service.CouponService;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 多商户优惠券信息管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2018-12-27
 */
@RestController("adminCouponController")
@RequestMapping("/admin/coupon")
@AllArgsConstructor
public class CouponController {

    private final CouponService couponService;
    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<Coupon>> page(Coupon coupon, PageParam<Coupon> page) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        IPage<Coupon> coupons = couponService.page(page,
                new LambdaQueryWrapper<Coupon>()
                        .like(StrUtil.isNotBlank(coupon.getCouponName()), Coupon::getCouponName, coupon.getCouponName())
                        .eq(coupon.getOverdueStatus() != null, Coupon::getOverdueStatus, coupon.getOverdueStatus())
                        .eq(coupon.getPutonStatus() != null, Coupon::getPutonStatus, coupon.getPutonStatus())
                        .ge(coupon.getStartTime() != null, Coupon::getStartTime, coupon.getStartTime())
                        .le(coupon.getEndTime() != null, Coupon::getEndTime, coupon.getEndTime())
                        .eq(coupon.getCouponType() != null, Coupon::getCouponType, coupon.getCouponType())
                        .eq(Coupon::getShopId, shopId)
                        .orderByDesc(Coupon::getCouponId));
        return ResponseEntity.ok(coupons);
    }

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

    /**
     * 保存
     */
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody @Valid Coupon coupon) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        coupon.setShopId(shopId);
        coupon.setSourceStock(coupon.getStocks());
        //判断为领取后生效类型时，开始时间和结束时间为空
        if(Objects.equals(coupon.getValidTimeType(),2)){
            coupon.setEndTime(null);
            coupon.setStartTime(null);
        }
        couponService.insertCouponAndCouponProds(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseEntity<Void> update(@RequestBody @Valid Coupon coupon) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        Coupon dbCoupon = couponService.getById(coupon.getCouponId());
        if (!Objects.equals(dbCoupon.getShopId(), shopId)) {
            throw new GlobalException("没有权限修改该优惠券信息");
        }
        coupon.setShopId(shopId);
        couponService.updateCouponAndCouponProds(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    public ResponseEntity<Void> delete(@RequestBody Long couponId) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        Coupon dbCoupon = couponService.getById(couponId);
        if (!Objects.equals(dbCoupon.getShopId(), shopId)) {
            throw new GlobalException("没有权限修改该优惠券信息");
        }
        couponService.deleteByCouponId(couponId);
        return ResponseEntity.ok().build();
    }

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

    /**
     * 违规商品提交审核
     */
    @PostMapping("/auditApply")
    public ResponseEntity<Void> auditApply(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Coupon coupon = couponService.getCouponAndCouponProdsByCouponId(offlineHandleEventAuditParam.getHandleId());
        if (coupon == null) {
            throw new GlobalException("未找到该活动信息");
        }
        couponService.auditApply(offlineHandleEventAuditParam.getEventId(), offlineHandleEventAuditParam.getHandleId(), offlineHandleEventAuditParam.getReapplyReason());
        // 移除缓存
        couponService.removeCouponAndCouponProdsCache(coupon.getShopId());
        return ResponseEntity.ok().build();
    }
}
