package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.dto.WxMerchantDto;
import com.iformall.domain.po.WxMerchant;
import com.iformall.domain.vo.WxMerchantVo;
import com.iformall.enums.EnumMerchantPublic;
import com.iformall.enums.EnumMerchantStatus;
import com.iformall.service.WxMerchantService;
import com.iformall.utils.HashUtil;
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.HashMap;

/**
 * @author Stormeye
 */
@RestController
@RequestMapping("/api/merchant")
public class WxMerchantController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMerchantService wxMerchantService;

    @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 WxMerchantDto wxMerchantDto, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::list");
        if (null == wxMerchantDto) wxMerchantDto = new WxMerchantDto();
        wxMerchantDto.setMerchantStatus(EnumMerchantStatus.VALID.getCode());
        wxMerchantDto.setIsPublic(EnumMerchantPublic.PUBLIC.getCode());
        return new ResultData(wxMerchantService.listAsPageCVo(wxMerchantDto, pageNum, pageSize));
    }

    @ApiOperation("根据code查询接口")
    @GetMapping("/findByCode")
    @ApiImplicitParam(name = "merchantCode", value = "merchantCode", dataType = "String", paramType = "query", required = true)
    public ResultData findByCode(String merchantCode) {
        logger.debug("[" + getIpAddr() + "] WxMerchantController::findByCode");
        if(merchantCode == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
        }
        String codeMD5 = HashUtil.DecodeByMD5(merchantCode);
        if(StringUtils.isBlank(codeMD5)) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
        }
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("tenantId", getTenantId());
        params.put("merchantCode", codeMD5);
        WxMerchantVo wxMerchant = wxMerchantService.getMerchantInfo(params);
        return new ResultData(wxMerchant);
    }

}
