package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.UserOrderDao;
import com.yonge.cooleshow.biz.dal.dto.search.OrderSearch;
import com.yonge.cooleshow.biz.dal.dto.search.VipCardRecordSearch;
import com.yonge.cooleshow.biz.dal.enums.EVipRecordStatus;
import com.yonge.cooleshow.biz.dal.enums.GoodTypeEnum;
import com.yonge.cooleshow.biz.dal.enums.SourceTypeEnum;
import com.yonge.cooleshow.biz.dal.service.VipCardRecordService;
import com.yonge.cooleshow.biz.dal.vo.UserOrderVo;
import com.yonge.cooleshow.biz.dal.vo.VipCardRecordVo;
import com.yonge.cooleshow.biz.dal.wrapper.VipCardRecordWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.stream.Collectors;

@RestController
@RequestMapping("${app-config.url.admin:}/vipCardRecord")
@Api(value = "购买会员卡记录表", tags = "购买会员卡记录表")
public class VipCardRecordController extends BaseController {

    @Autowired
    private VipCardRecordService vipCardRecordService;

    @Resource
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private UserOrderDao userOrderDao;

    /**
     * 查询单条
     */
    @GetMapping("/detail/{orderDetilId}")
    @ApiOperation(value = "详情", notes = "传入订单详情id")
    public HttpResponseResult<VipCardRecordVo> detail(@PathVariable("orderDetilId") Long orderDetilId) {
        return succeed(vipCardRecordService.detail(orderDetilId));
    }

    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入vipCardRecordSearch")
    public HttpResponseResult<PageInfo<VipCardRecordVo>> page(@RequestBody VipCardRecordSearch query) {
        IPage<VipCardRecordVo> pages = vipCardRecordService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
    }


    @ApiOperation("添加/扣减会员")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('vipCardRecord/add')")
    public HttpResponseResult<Void> add(@RequestBody @Validated VipCardRecordWrapper.AddVipCardRecord addVipCardRecord) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        addVipCardRecord.setCreateBy(sysUser.getId());
        if (addVipCardRecord.getStatus().equals(EVipRecordStatus.ADD)) {
            addVipCardRecord.setSourceType(SourceTypeEnum.PLATFORM);
        } else if (addVipCardRecord.getStatus().equals(EVipRecordStatus.DEDUCTION)) {
            addVipCardRecord.setSourceType(SourceTypeEnum.PLATFORM_DEDUCT);
        }

        // 判断是否有待支付订单 如果有返回不可下单
        OrderSearch search = new OrderSearch();
        search.setOrderClient(addVipCardRecord.getClientType().name());
        search.setGoodType(Lists.newArrayList(GoodTypeEnum.VIP, GoodTypeEnum.SVIP).stream().map(GoodTypeEnum::name).collect(Collectors.joining(",")));
        search.setUserId(addVipCardRecord.getUserId());

        UserOrderVo userOrderVo = userOrderDao.getPendingOrder(search);
        if (null != userOrderVo) {
            throw new BizException(997, "当前用户存在未支付订单，不可操作添加/扣减会员");
        }
        vipCardRecordService.add(addVipCardRecord);
        return succeed();
    }
}