package com.iformall.controller.market;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxActivityJoin;
import com.iformall.exception.MallinkException;
import com.iformall.service.WxActivityJoinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author gongbiao
 */
@RestController
@RequestMapping("wxActivityJoin")
@Api(description = "活动参与信息")
public class WxActivityJoinController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxActivityJoinService wxActivityJoinService;

    @ApiOperation("分页列表接口")
    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "活动参与信息-列表")
    public ResultData list(@ModelAttribute WxActivityJoin wxActivityJoin, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxActivityJoinController::list");
        if (null == wxActivityJoin) {
            wxActivityJoin = new WxActivityJoin();
        }
        wxActivityJoin.setTenantId(getTenantId());
        wxActivityJoin.setSortColumns(BaseEntity.SortField.CreateTime_DESC,BaseEntity.SortField.Id_DESC);
        final PageInfo<WxActivityJoin> page = wxActivityJoinService.listAsPage(wxActivityJoin, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("确认或取消报名")
    @PostMapping("modifyStatus")
    @SystemControllerLog(description = "活动参与信息-确认或取消报名")
    public ResultData modifyStatus(@RequestBody WxActivityJoin wxActivityJoin) {
        logger.debug("[" + getIpAddr() + "] WxActivityJoinController::modifyStatus");

        if (wxActivityJoin.getId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "id不能为空");
        }
        if (wxActivityJoin.getActivityId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "activityId不能为空");
        }
        if (wxActivityJoin.getStatus() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "status不能为空");
        }
        return wxActivityJoinService.modifyStatus(wxActivityJoin);
    }

    @ApiOperation("导出报名表")
    @RequestMapping("/exportData")
    @SystemControllerLog(description = "活动参与信息-导出报名表")
    public void exportData(@ModelAttribute WxActivityJoin wxActivityJoin, HttpServletRequest request, HttpServletResponse response) {
        logger.debug("[" + getIpAddr() + "] WxActivityJoinController::exportData");
        if (wxActivityJoin.getActivityId() == null) {
            throw new MallinkException(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "activityId不能为空");
        }
        wxActivityJoin.setTenantId(getTenantId());
        wxActivityJoinService.exportData(request, response, wxActivityJoin);

    }

    @ApiOperation("查询报名总数")
    @GetMapping("queryJoinInfo")
    @SystemControllerLog(description = "活动参与信息-查询报名总数")
    public ResultData queryJoinInfo(@ModelAttribute WxActivityJoin wxActivityJoin) {
        logger.debug("[" + getIpAddr() + "] WxActivityJoinController::queryJoinInfo");

        if (wxActivityJoin.getActivityId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "activityId不能为空");
        }
        wxActivityJoin.setTenantId(getTenantId());
        return wxActivityJoinService.queryJoinInfo(wxActivityJoin);
    }

    @ApiOperation("发送短信")
    @GetMapping("sendMsg")
    @SystemControllerLog(description = "活动参与信息-发送短信")
    public ResultData sendMsg(@ModelAttribute WxActivityJoin wxActivityJoin) {
        logger.debug("[" + getIpAddr() + "] WxActivityJoinController::confirmCompletion");

        if (wxActivityJoin.getActivityId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "activityId不能为空");
        }
        wxActivityJoin.setTenantId(getTenantId());
        return wxActivityJoinService.sendMsg(wxActivityJoin);
    }

}
