/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.delivery.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.util.PageParam;
import com.yami.shop.delivery.comment.model.Fetch;
import com.yami.shop.delivery.comment.service.FetchService;
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 YXF
 * @date 2020-04-23 15:18:29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/platform/fetch" )
public class FetchController {

    private final FetchService fetchService;

    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param fetch 自提点
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<Fetch>> getFetchPage(PageParam<Fetch> page, Fetch fetch) {
        return ResponseEntity.ok(fetchService.getPage(page, fetch));
    }


    /**
     * 通过id查询自提点
     * @param fetchId id
     * @return 单个数据
     */
    @GetMapping("/info/{fetchId}" )
    public ResponseEntity<Fetch> getById(@PathVariable("fetchId") Long fetchId) {
        return ResponseEntity.ok(fetchService.getById(fetchId));
    }
//
//    /**
//     * 新增自提点
//     * @param fetch 自提点
//     * @return 是否新增成功
//     */
//    @SysLog("新增自提点" )
//    @PostMapping
//    @PreAuthorize("@pms.hasPermission('platform:fetch:save')" )
//    public ResponseEntity<Boolean> save(@RequestBody @Valid Fetch fetch) {
//        fetch.setShopId(Constant.PLATFORM_SHOP_ID);
//        Date date = new Date();
//        fetch.setCreateTime(date);
//        fetch.setUpdateTime(date);
//        return ResponseEntity.ok(fetchService.save(fetch));
//    }
//
//    /**
//     * 修改自提点
//     * @param fetch 自提点
//     * @return 是否修改成功
//     */
//    @SysLog("修改自提点" )
//    @PutMapping
//    @PreAuthorize("@pms.hasPermission('platform:fetch:update')" )
//    public ResponseEntity<Boolean> updateById(@RequestBody @Valid Fetch fetch) {
//        fetch.setUpdateTime(new Date());
//        return ResponseEntity.ok(fetchService.updateById(fetch));
//    }
//
//    /**
//     * 通过id删除自提点
//     * @param fetchId id
//     * @return 是否删除成功
//     */
//    @SysLog("删除自提点" )
//    @DeleteMapping("/{fetchId}" )
//    @PreAuthorize("@pms.hasPermission('platform:fetch:delete')" )
//    public ResponseEntity<Boolean> removeById(@PathVariable Long fetchId) {
//        return ResponseEntity.ok(fetchService.removeById(fetchId));
//    }

    /**
     * 下线活动
     */
    @PostMapping("/offline")
    @PreAuthorize("@pms.hasPermission('platform:fetch:update')" )
    public ResponseEntity<Void> offline(@RequestBody OfflineHandleEvent offlineHandleEvent) {
        Fetch fetch = fetchService.getById(offlineHandleEvent.getHandleId());

        if (fetch == null) {
            throw new GlobalException("未找到该自提点信息");
        }
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        fetchService.offline(fetch, offlineHandleEvent.getOfflineReason(), sysUserId);

//        // 移除缓存
//        fetchService.removeSeckillCacheById(seckill.getSeckillId());
        return ResponseEntity.ok().build();
    }

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


    /**
     * 审核活动
     */
    @PostMapping("/auditFetch")
    @PreAuthorize("@pms.hasPermission('platform:fetch:update')")
    public ResponseEntity<Void> auditSeckill(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Long sysUserId = SecurityUtils.getSysUser().getUserId();
        Fetch fetch = fetchService.getById(offlineHandleEventAuditParam.getHandleId());
        if (fetch == null) {
            throw new GlobalException("未找到自提点信息");
        }
        fetchService.auditSeckill(offlineHandleEventAuditParam, sysUserId);

//        // 移除缓存
//        seckillService.removeSeckillCacheById(seckill.getSeckillId());
        return ResponseEntity.ok().build();
    }

}
