package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.yonge.cooleshow.biz.dal.entity.TenantAlbumPurchase;
import com.yonge.cooleshow.biz.dal.service.TenantActivationCodeService;
import com.yonge.cooleshow.biz.dal.service.TenantAlbumPurchaseService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantActivationCodeWrapper;
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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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;


@Slf4j
@Validated
@RestController
@RequestMapping("/tenantActivationCode")
@Api(tags = "机构激活码")
public class TenantActivationCodeController extends BaseController {

    @Autowired
    private TenantActivationCodeService tenantActivationCodeService;

    @Autowired
    private TenantAlbumPurchaseService tenantAlbumPurchaseService;


    @ApiOperation(value = "查询分页", notes = "机构激活码- 传入 TenantActivationCodeVo.TenantActivationCodeQuery")
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<TenantActivationCodeWrapper.TenantActivationCode>> page(
            @RequestBody TenantActivationCodeWrapper.TenantActivationCodeQuery query) {

        if (StringUtils.isBlank(query.getOrderNo())) {
            throw new BizException("订单号不能为空");
        }
        // 查询订单购买的专辑
        TenantAlbumPurchase albumPurchase = tenantAlbumPurchaseService.getByOrderNo(query.getOrderNo());
        if (albumPurchase == null) {
            return succeed(new PageInfo<>());
        }
        query.setActivationStatus(true);
        query.setTenantAlbumPurchaseId(albumPurchase.getId());
        // 查询数据
        IPage<TenantActivationCodeWrapper.TenantActivationCode> pages =
                tenantActivationCodeService.selectPage(QueryInfo.getPage(query), query);
        PageInfo<TenantActivationCodeWrapper.TenantActivationCode> pageInfo = PageUtil.pageInfo(pages);

        if (query.getTenantAlbumPurchaseId() != null) {
            pageInfo.setStatInfo(albumPurchase);
        }

        return succeed(pageInfo);
    }

}