package com.fuint.module.clientApi.controller;

import com.fuint.common.service.MerchantService;
import com.fuint.common.service.RaffleTypeService;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.web.BaseController;
import com.fuint.framework.web.ResponseObject;
import com.fuint.repository.model.MtRaffleType;
import com.fuint.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 抽奖类型controller
 *
 * Created by FSQ
 * CopyRight https://www.fuint.cn
 */
@Api(tags="会员端-抽奖类型相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/clientApi/raffleType")
public class ClientRaffleTypeController extends BaseController {

    /**
     * 抽奖类型服务接口
     */
    private RaffleTypeService raffleTypeService;

    /**
     * 商户服务接口
     */
    private MerchantService merchantService;

    /**
     * 抽奖类型列表
     */
    @ApiOperation(value = "抽奖类型列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @CrossOrigin
    public ResponseObject list(HttpServletRequest request) throws BusinessCheckException {
        String merchantNo = request.getHeader("merchantNo") == null ? "" : request.getHeader("merchantNo");
        Integer storeId = StringUtil.isEmpty(request.getHeader("storeId")) ? 0 : Integer.parseInt(request.getHeader("storeId"));

        Integer merchantId = 0;
        if (StringUtil.isNotEmpty(merchantNo)) {
            merchantId = merchantService.getMerchantId(merchantNo);
        }

        List<MtRaffleType> typeList = raffleTypeService.getAvailableRaffleType(merchantId, storeId);
        Map<String, Object> result = new HashMap<>();
        result.put("typeList", typeList);
        return getSuccessResult(result);
    }
}
