package com.jeequan.jeepay.mch.ctrl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.ctrls.AbstractCtrl;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.BaseModel;
import com.jeequan.jeepay.core.model.security.JeeUserDetails;
import com.jeequan.jeepay.db.entity.MemberInfo;
import com.jeequan.jeepay.service.impl.MemberInfoService;
import com.jeequan.jeepay.service.impl.SysConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 通用ctrl类
 *
 * @author terrfly
 * @modify zhuxiao
 * @date 2021-04-27 15:50
 */
public abstract class CommonCtrl extends AbstractCtrl {

    @Autowired protected SysConfigService sysConfigService;
    @Autowired protected MemberInfoService memberInfoService;

    /** 获取当前用户ID */
    protected JeeUserDetails getCurrentUser(){

        return (JeeUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    /** 获取当前商户ID **/
    protected String getCurrentMchNo() {
        return getCurrentUser().getSysUser().getBelongInfoId();
    }

    /**
     * 获取当前用户登录IP
     * @return
     */
    protected String getIp() {
        return getClientIp();
    }

    /**
     * 校验当前用户是否为超管
     * @return
     */
    protected ApiRes checkIsAdmin() {
        SysUser sysUser = getCurrentUser().getSysUser();
        if (sysUser.getUserType() != SysUser.UEST_TYPE_ADMIN) {
            return ApiRes.fail(ApiCodeEnum.SYS_PERMISSION_ERROR);
        }else {
            return null;
        }

    }

    /** 当前是否为普通商户 **/
    protected boolean getIsNormalMch() {
        return getCurrentUser().getMchType() == CS.MCH_TYPE_NORMAL;
    }

    /** model 存入会员头像 **/
    public void setAvatarUrl(List<? extends BaseModel> modeList) {

        if(modeList == null || modeList.isEmpty()){
            return ;
        }
        ArrayList<String> mbrIdList = new ArrayList<>();
        for (BaseModel model:modeList) {
            JSONObject json = (JSONObject) JSON.toJSON(model);
            String mbrId = json.getString("mbrId");
            mbrIdList.add(mbrId);
        }
        List<MemberInfo> defineList = memberInfoService.list(MemberInfo.gw().select(MemberInfo::getAvatarUrl, MemberInfo::getMbrId).in(MemberInfo::getMbrId, mbrIdList));
        for (BaseModel model : modeList) {
            JSONObject json = (JSONObject) JSON.toJSON(model);
            String mbrId = json.getString("mbrId");

            if(StringUtils.isBlank(mbrId)) {
                continue;
            }

            for (MemberInfo define : defineList) {
                if (mbrId.equals(define.getMbrId())) {
                    model.addExt("avatarUrl", define.getAvatarUrl());
                }
            }
        }
    }

    /** 会员头像地址默认值获取 **/
    public String getMemberAvatarDefault() {
        Collections.shuffle(CS.MEMBER_AVATAR_ICON);
        return CS.MEMBER_AVATAR_ICON.get(0);
    }

}
