package com.iformall.controller;

import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxActivity;
import com.iformall.domain.po.WxActivityJoin;
import com.iformall.domain.vo.WxActivityJoinQuestionAnswer;
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.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gongbiao
 */
@RestController
@RequestMapping("/api/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)})
    public ResultData list(@ModelAttribute WxActivityJoin wxActivityJoin, Integer pageNum, Integer pageSize) {
        if (null == wxActivityJoin) wxActivityJoin = new WxActivityJoin();
        wxActivityJoin.setUserId(getUserId());
        wxActivityJoin.setTenantId(getTenantId());
        wxActivityJoin.setSortColumns(BaseEntity.SortField.CreateTime_DESC);
        final PageInfo<WxActivity> page = wxActivityJoinService.clistAsPage(wxActivityJoin, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("报名")
    @PostMapping("join")
    public ResultData join(@RequestBody WxActivityJoin wxActivityJoin) {
        if (wxActivityJoin.getActivityId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "activityId不能为空");
        }
        wxActivityJoin.setTenantId(getTenantId());
        wxActivityJoin.setUserId(getUserId());
        wxActivityJoin.setNickName(getUser().getNickName());
        if (StringUtils.isNotEmpty(wxActivityJoin.getAnswer())) {
            List<WxActivityJoinQuestionAnswer> wxActivityJoinQuestionAnswers = JSONArray.parseArray(wxActivityJoin.getAnswer(), WxActivityJoinQuestionAnswer.class);
            List<WxActivityJoinQuestionAnswer> collect = wxActivityJoinQuestionAnswers.stream().filter(answer -> answer != null).collect(Collectors.toList());
            String answer = JSONArray.toJSONString(collect);
            wxActivityJoin.setAnswer(answer);
        }
        try {
            return wxActivityJoinService.join(wxActivityJoin);
        } catch (MallinkException e) {
            return new ResultData(e.getErrorCode(), e.getMessage());
        }
    }

    @ApiOperation("签到")
    @PostMapping("sign")
    public ResultData sign(@RequestBody WxActivityJoin wxActivityJoin) {
        if (wxActivityJoin.getActivityId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "activityId不能为空");
        }
        wxActivityJoin.setTenantId(getTenantId());
        wxActivityJoin.setUserId(getUserId());
        return wxActivityJoinService.sign(wxActivityJoin);
    }

}
