package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.microsvc.toolkit.common.response.paging.PageInfo;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.yonge.cooleshow.biz.dal.entity.TenantActivationCode;
import com.yonge.cooleshow.biz.dal.entity.TenantAlbumPurchase;
import com.yonge.cooleshow.biz.dal.entity.TenantInfo;
import com.yonge.cooleshow.biz.dal.service.TenantActivationCodeService;
import com.yonge.cooleshow.biz.dal.service.TenantAlbumPurchaseService;
import com.yonge.cooleshow.biz.dal.service.TenantInfoService;
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.cooleshow.tenant.vo.TenantActivationCodeVo;
import com.yonge.toolset.base.exception.BizException;
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.security.access.prepost.PreAuthorize;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.admin:}/tenantActivationCode")
@Api(tags = "机构激活码")
public class TenantActivationCodeController extends BaseController {

    @Autowired
    private TenantActivationCodeService tenantActivationCodeService;

    @Autowired
    private TenantAlbumPurchaseService tenantAlbumPurchaseService;

    @Autowired
    private TenantInfoService tenantInfoService;

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

        String activationTime = query.getActivationTime();
        if (StringUtils.isNotEmpty(activationTime)) {
            Date parse = new SimpleDateFormat("yyyy-MM").parse(activationTime, new ParsePosition(0));
            query.setActivationStartTime(parse);
            Calendar instance = Calendar.getInstance();
            instance.setTime(parse);
            instance.add(Calendar.MONTH, 1);
            instance.add(Calendar.DATE, -1);
            instance.set(Calendar.HOUR_OF_DAY, 23);
            instance.set(Calendar.MINUTE, 59);
            instance.set(Calendar.MILLISECOND, 59);
            query.setActivationEndTime(instance.getTime());
        }
        IPage<TenantActivationCodeWrapper.TenantActivationCode> pages =
                tenantActivationCodeService.selectPage(QueryInfo.getPage(query), query);
        com.microsvc.toolkit.common.response.paging.PageInfo<TenantActivationCodeWrapper.TenantActivationCode> pageInfo = QueryInfo.pageInfo(pages,
                pages.getRecords());

        if (query.getTenantAlbumPurchaseId() != null) {
            TenantActivationCodeWrapper.TenantActivationCode extra =
                    new TenantActivationCodeWrapper.TenantActivationCode();
            TenantAlbumPurchase purchase = tenantAlbumPurchaseService.getById(query.getTenantAlbumPurchaseId());
            extra.setActiveQuantity(purchase.getActiveQuantity());
            pageInfo.setExtra(extra);
        }

        return succeed(pageInfo);
    }

    @ApiOperation(value = "激活码发放", notes = "机构激活码- 传入 TenantActivationCodeVo.TenantActivationCodeSend")
    @PostMapping("/send")
    @PreAuthorize("@pcs.hasPermissions('tenantActivationCode/send')")
    public HttpResponseResult<Boolean> send(@Validated @RequestBody TenantActivationCodeVo.TenantActivationCodeSend send) {
        tenantActivationCodeService.sendActivationCode(send.getTenantId(), send.getTenantAlbumPurchaseId(),
                send.getActivationCodeList(),
                send.getStudentIdList());
        return succeed();
    }

    @ApiOperation(value = "激活码重发", notes = "机构激活码- 传入 TenantActivationCodeVo.TenantActivationCodeSend")
    @PostMapping("/resend")
    @PreAuthorize("@pcs.hasPermissions('tenantActivationCode/resend')")
    public HttpResponseResult<Boolean> resend(@Validated @RequestBody TenantActivationCodeVo.TenantActivationCodeResend send) {
        tenantActivationCodeService.resend(send.getCode(), send.getUserId(), send.getTenantId(), send.getTenantAlbumPurchaseId());
        return succeed();
    }

    @ApiOperation(value = "激活码发放取消", notes = "传入 激活码的ID")
    @PostMapping("/sendCancel")
    @PreAuthorize("@pcs.hasPermissions('tenantActivationCode/sendCancel')")
    public HttpResponseResult<Boolean> sendCancel(@Validated @RequestBody TenantActivationCodeVo.CodeSend codeSend) {
        TenantActivationCode activationCode = tenantActivationCodeService.getById(codeSend.getId());
        TenantInfo tenantInfo = tenantInfoService.getById(codeSend.getTenantId());
        tenantActivationCodeService.sendCancel(tenantInfo, activationCode);
        return succeed();
    }
}