package com.liquidnet.client.admin.web.controller.zhengzai.sweet;

import com.github.pagehelper.PageHelper;
import com.liquidnet.client.admin.common.annotation.Log;
import com.liquidnet.client.admin.common.core.controller.BaseController;
import com.liquidnet.client.admin.common.core.domain.AjaxResult;
import com.liquidnet.client.admin.common.core.page.PageDomain;
import com.liquidnet.client.admin.common.core.page.TableDataInfo;
import com.liquidnet.client.admin.common.core.page.TableSupport;
import com.liquidnet.client.admin.common.enums.BusinessType;
import com.liquidnet.client.admin.common.utils.StringUtils;
import com.liquidnet.client.admin.zhengzai.sweet.service.ISweetFormSubmissionAdminService;
import com.liquidnet.service.sweet.dto.param.admin.SweetFormSubmissionActivityListParam;
import com.liquidnet.service.sweet.dto.param.admin.SweetFormSubmissionExportParam;
import com.liquidnet.service.sweet.dto.admin.SweetFormSubmissionActivityVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Api(tags = "动态表单报名活动管理")
@Controller
@RequestMapping("/sweet/formSubmission")
public class SweetFormSubmissionAdminController extends BaseController {

    private String prefix = "zhengzai/sweet/formSubmission";

    @Autowired
    private ISweetFormSubmissionAdminService sweetFormSubmissionAdminService;

    @GetMapping("listView")
    @ApiOperation(value = "报名活动列表页面")
    public String listView() {
        return prefix + "/list";
    }

    @Log(title = "报名活动列表", businessType = BusinessType.LIST)
    @RequiresPermissions("sweet:formSubmission:list")
    @PostMapping("list")
    @ApiOperation(value = "报名活动列表数据")
    @ResponseBody
    public TableDataInfo list(SweetFormSubmissionActivityListParam param) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNull(pageNum)) {
            pageNum = 1;
        }
        if (StringUtils.isNull(pageSize)) {
            pageSize = 10;
        }
        // 聚合查询排序在 SQL 中固定，忽略前端 orderByColumn 避免 PageHelper 追加无效字段
        PageHelper.startPage(pageNum, pageSize);
        List<SweetFormSubmissionActivityVo> list = sweetFormSubmissionAdminService.activityList(param);
        return getDataTable(list);
    }

    @Log(title = "报名数据导出", businessType = BusinessType.EXPORT)
    @RequiresPermissions("sweet:formSubmission:export")
    @PostMapping("export")
    @ApiOperation(value = "报名数据导出")
    @ResponseBody
    public AjaxResult export(SweetFormSubmissionExportParam param) {
        return sweetFormSubmissionAdminService.export(param);
    }
}
