package com.cweb.controller;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMerchantUser;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.merchant.BsMerchantUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @className: BsMerchantUserController
 * @author: HuRui
 * @date: 2024/6/11
 **/
@Controller
@RequestMapping(value = "/merchantUser")
@Api(value = "登录业务")
public class BsMerchantUserController {
    private static Logger log = LoggerFactory.getLogger(BsMerchantUserController.class);
    @Resource
    private BsMerchantUserService merchantUserService;

    @Resource
    private UserCenter userCenter;
    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商户用户")
    public ResponseData getUser(@RequestParam(value = "merNo", required = true) String merNo) {
        try {
            UserSessionObject sessionObject = userCenter.getSessionModel(UserSessionObject.class);
            if (sessionObject == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }
            Map<String,Object> map = new HashMap<>();

            // 商户会员
            BsMerchantUser user = merchantUserService.getUser(merNo, sessionObject.getUser().getPhone());
            if (user != null) {
                map.put("vipLevel", user.getVipLevel());
                map.put("integral", user.getIntegral());
                map.put("discount", new ArrayList<>());
            } else {
                map.put("vipLevel", null);
                map.put("integral", 0);
                map.put("discount", new ArrayList<>());
            }
            return ResponseMsgUtil.success(map);

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }


}
