package com.gic.haoban.manage.web.controller.content;

import com.gic.api.base.commons.BusinessException;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.PageResult;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.web.qo.PageQo;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.marketing.pro.api.dto.activity.gathering.GatheringActivityMemberIdActivityRecordDTO;
import com.gic.marketing.pro.api.dto.activity.gathering.GatheringActivityWxActivityDetailDTO;
import com.gic.marketing.pro.api.qdto.activity.gathering.GatheringActivityWxActivityDetailQDTO;
import com.gic.marketing.pro.api.service.activity.GatheringActivityApiService;
import com.gic.marketing.pro.api.service.activity.GatheringActivityWxApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 互动集会活动记录
 */
@Slf4j
@RestController
@RequestMapping("/gathering-activity")
public class GatheringActivityController {

    @Autowired
    private GatheringActivityApiService activityApiService;

    @Autowired
    private GatheringActivityWxApiService activityWxApiService;

    /**
     * 根据会员查询活动信息
     *
     * @param memberId
     * @return
     */
    @RequestMapping("/activity-by-member")
    public RestResponse<Page<GatheringActivityMemberIdActivityRecordDTO>> getActivityRecordByMemberId(@RequestParam String memberId,
                                                                                                      @RequestParam(required = false) Integer pageSize,
                                                                                                      @RequestParam(required = false) Integer pageNum) {
        PageQo pageQo = new PageQo();
        if (pageSize != null) {
            pageQo.setPageSize(pageSize);
        }
        if (pageNum != null) {
            pageQo.setPageNum(pageNum);
        }
        ServiceResponse<PageResult<GatheringActivityMemberIdActivityRecordDTO>> serviceResponse = activityApiService.getActivityRecordByMemberId(memberId, pageSize, pageNum);
        if (!serviceResponse.isSuccess()) {
            return RestResponse.failure(serviceResponse.getCode(), serviceResponse.getMessage());
        }
        return RestResponse.successResult(pageTransform(serviceResponse.getResult(), Function.identity()));
    }

    /**
     * 活动详情
     *
     * @param activityId
     * @param memberId
     * @param enterpriseId
     * @return
     */
    @RequestMapping("/activity-detail")
    public RestResponse<GatheringActivityWxActivityDetailDTO> getActivityDetail(@RequestParam("activityId") String activityId,
                                                                                @RequestParam("memberId") String memberId,
                                                                                @RequestParam(value = "enterpriseId", required = false) String enterpriseId) {
        GatheringActivityWxActivityDetailQDTO detailQDTO = new GatheringActivityWxActivityDetailQDTO();
        detailQDTO.setActivityId(activityId);
        detailQDTO.setEnterpriseId(enterpriseId);
        detailQDTO.setMemberId(memberId);
        ServiceResponse<GatheringActivityWxActivityDetailDTO> activityDetail = activityWxApiService.getActivityDetail(detailQDTO);
        if (!activityDetail.isSuccess()) {
            return RestResponse.failure(activityDetail.getCode(), activityDetail.getMessage());
        }
        return RestResponse.successResult(activityDetail.getResult());
    }

    public static <T, R> Page<R> pageTransform(PageResult<T> pageResult, Function<T, R> function) {
        Page<R> result = new Page<>();
        result.setPageSize(pageResult.getPageSize());
        result.setCurrentPage(pageResult.getPageNum());
        result.setTotalCount((int) pageResult.getTotal());
        result.setTotalPage(pageResult.getPages());
        result.setResult(pageResult.getList().stream().map(function).collect(Collectors.toList()));
        return result;
    }
}
