package com.iformall.controller;

import com.iformall.annotation.AuthIgnore;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.*;
import com.iformall.domain.vo.WxMerchantVo;
import com.iformall.enums.EnumMerchantStatus;
import com.iformall.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/mall")
@Api(description = "商场信息相关接口")
public class WxMallController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${version}")
    private String version;

    @Autowired
    private WxMallService wxMallService;

    @Autowired
    private WxAppinfoService wxAppinfoService;

    @Autowired
    private WxScoreRulesService wxScoreRulesService;

    @Autowired
    private WxLevelConfigService wxLevelConfigService;

    @AuthIgnore
    @ApiOperation("商场图标")
    @GetMapping("/getAppIcon")
    @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true)
    public ResultData getAppIcon(String appId) {
        WxAppinfo appInfo = wxAppinfoService.getByAppId(appId);
        if (appInfo == null) {
            return new ResultData(ErrorCode.APP_ID_NOT_FOUND);
        }
        WxMall mall = wxMallService.getByTenantId(appInfo.getTenantId());
        if (mall == null) {
            return new ResultData(ErrorCode.MALL_INFO_NOT_FOUND);
        }
        Map resultMap = new HashMap();
        resultMap.put("mallImgUrl", mall.getImgUrl());
        resultMap.put("mallName", mall.getName());
        return new ResultData(Result.SUCCESS, "查询成功", resultMap);
    }

    @AuthIgnore
    @ApiOperation("商场提示信息")
    @GetMapping("/getWeapNote")
    @ApiImplicitParam(name = "appId", value = "appId", dataType = "String", paramType = "query", required = true)
    public ResultData getWeapNote(String appId) {
        WxAppinfo appInfo = wxAppinfoService.getByAppId(appId);
        if (appInfo == null) {
            return new ResultData(ErrorCode.APP_ID_NOT_FOUND);
        }
        WxMall mall = wxMallService.getByTenantId(appInfo.getTenantId());
        if (mall == null) {
            return new ResultData(ErrorCode.MALL_INFO_NOT_FOUND);
        }
        Map resultMap = new HashMap();
        resultMap.put("weapNote", mall.getWeapNote());
        return new ResultData(Result.SUCCESS, "查询成功", resultMap);
    }

    @ApiOperation("商场信息")
    @GetMapping("/mallInfo")
    public ResultData getMallInfo() {
        WxMall mall = wxMallService.getByTenantId(getTenantId());
        if (mall == null) {
            return new ResultData(ErrorCode.MALL_INFO_NOT_FOUND);
        }
        return new ResultData(Result.SUCCESS, "查询成功", mall);
    }

    @ApiOperation("成长值相关信息")
    @GetMapping("/scoreLevelInfo")
    public ResultData getScoreLevelInfo() {
        Map map = new HashMap();
        WxScoreRules scoreRules = wxScoreRulesService.getScoreRules(getTenantId());
        map.put("score", scoreRules);
        List<WxLevelConfig> levelConfigList = wxLevelConfigService.getByTenantId(getTenantId());
        boolean disscountEnable = false;
        for(WxLevelConfig levelConfig: levelConfigList) {
            if (levelConfig.getDiscountEnable() > 0 ) {
                disscountEnable = true;
                break;
            }
        }
        map.put("levelList", levelConfigList);
        map.put("discountEnable", disscountEnable);
        return new ResultData(Result.SUCCESS, "查询成功", map);
    }


    @AuthIgnore
    @ApiOperation("获取后端版本号")
    @GetMapping("/version")
    public ResultData version() {
        return new ResultData(version);
    }


}
