package com.bweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.service.merchant.BsMerchantUserService;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
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.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "/merUser")
@Api(value = "商户管理")
public class BsMerchantUserController {

    private static Logger log = LoggerFactory.getLogger(BsMerchantUserController.class);

    @Resource
    private BsMerchantUserService merchantUserService;
    @Resource
    private UserCenter userCenter;

    @RequestMapping(value = "/queryList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData queryList(@RequestParam(value = "merNo", required = false) String merNo,
                                  @RequestParam(value = "userPhone", required = false) String userPhone,
                                  @RequestParam(value = "pageNum", required = true) Integer pageNum,
                                  @RequestParam(value = "pageSize", required = true) Integer pageSize) {
        try {
            SecUserSessionObject userSession = userCenter.getSessionModel(SecUserSessionObject.class);
            if (userSession == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("userPhone", userPhone);

            if (userSession.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type1.getCode())) {
                param.put("merNo", merNo);
            } else if (userSession.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type2.getCode())) {
                param.put("merId", userSession.getAccount().getObjectId());
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(merchantUserService.getList(param)));

        } catch (Exception e) {
            log.error("BsMerchantUserController --> queryList() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
