package com.jeequan.jeepay.mgr.ctrl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.ctrls.AbstractCtrl;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.model.BaseModel;
import com.jeequan.jeepay.core.model.security.JeeUserDetails;
import com.jeequan.jeepay.db.entity.*;
import com.jeequan.jeepay.mgr.config.SystemYmlConfig;
import com.jeequan.jeepay.service.impl.*;
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.LinkedList;
import java.util.List;

/*
* 定义通用CommonCtrl
*
* @author terrfly
*
* @date 2021/6/8 17:09
*/
public abstract class CommonCtrl extends AbstractCtrl {

    @Autowired protected SystemYmlConfig mainConfig;

    @Autowired protected PayInterfaceDefineService payInterfaceDefineService;

    @Autowired protected MchInfoService mchInfoService;

    @Autowired protected MchStoreService mchStoreService;

    @Autowired protected AgentInfoService agentInfoService;

    @Autowired protected SysConfigService sysConfigService;

    @Autowired protected MchApplymentService mchApplymentService;

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

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

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

    /** model 存入商户名称 **/
    public void setMchName(List<? extends BaseModel> modeList) {

        if(modeList == null || modeList.isEmpty()){
            return ;
        }
        ArrayList<String> mchNoList = new ArrayList<>();
        for (BaseModel model:modeList) {
            JSONObject json = (JSONObject) JSONObject.toJSON(model);
            String mchNo = json.getString("mchNo");
            mchNoList.add(mchNo);
        }
        List<MchApplyment> mchInfoList = mchApplymentService.list(MchApplyment.gw().select(MchApplyment::getMchNo, MchApplyment::getMchFullName,MchApplyment::getMchShortName).in(MchApplyment::getMchNo, mchNoList));
        for (BaseModel model:modeList) {
            JSONObject json = (JSONObject) JSONObject.toJSON(model);
            String mchNo = json.getString("mchNo");

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

            for (MchApplyment info:mchInfoList) {
                if (mchNo.equals(info.getMchNo())) {
                    model.addExt("mchName", info.getMchFullName());
                    model.addExt("mchShortName", info.getMchShortName());
                }
            }
        }
    }

    /** model 存入门店名称 **/
    public void setStoreName(List<? extends BaseModel> modeList) {

        if(modeList == null || modeList.isEmpty()){
            return ;
        }
        ArrayList<String> storeIdList = new ArrayList<>();
        for (BaseModel model:modeList) {
            JSONObject json = (JSONObject) JSONObject.toJSON(model);
            String storeId = json.getString("storeId");
            storeIdList.add(storeId);
        }
        List<MchStore> mchInfoList = mchStoreService.list(MchStore.gw().select(MchStore::getStoreId, MchStore::getStoreName).in(MchStore::getStoreId, storeIdList));
        for (BaseModel model:modeList) {
            JSONObject json = (JSONObject) JSONObject.toJSON(model);
            String storeId = json.getString("storeId");

            if(storeId == null) {
                continue;
            }

            for (MchStore store : mchInfoList) {
                if (storeId.equals(store.getStoreId())) {
                    model.addExt("storeName", store.getStoreName());
                }
            }
        }
    }

    /** model 存入支付接口名称 **/
    public void setIfName(List<? extends BaseModel> modeList) {

        if(modeList == null || modeList.isEmpty()){
            return ;
        }
        ArrayList<String> ifCodeList = new ArrayList<>();
        for (BaseModel model:modeList) {
            JSONObject json = (JSONObject) JSONObject.toJSON(model);
            String ifCode = json.getString("ifCode");
            ifCodeList.add(ifCode);
        }
        List<PayInterfaceDefine> defineList = payInterfaceDefineService.list(PayInterfaceDefine.gw().select(PayInterfaceDefine::getIfCode, PayInterfaceDefine::getIfName).in(PayInterfaceDefine::getIfCode, ifCodeList));
        for (BaseModel model : modeList) {
            JSONObject json = (JSONObject) JSONObject.toJSON(model);
            String ifCode = json.getString("ifCode");

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

            for (PayInterfaceDefine define : defineList) {
                if (ifCode.equals(define.getIfCode())) {
                    model.addExt("ifName", define.getIfName());
                }
            }
        }
    }

    /** 根据服务商号，查询服务商下的商户号List **/
    public List<String> getMchNoListByAgentNo(String agentNo) {

        if (StringUtils.isBlank(agentNo)) {
            return null;
        }

        AgentInfo agentInfo = agentInfoService.getById(agentNo);
        if (agentInfo == null || agentInfo.getState() != CS.YES) {
            throw new BizException("服务商不存在或状态不正确");
        }

        List<MchInfo> list = mchInfoService.list(MchInfo.gw().eq(com.jeequan.jeepay.core.entity.MchInfo::getAgentNo, agentNo));
        if (CollUtil.isEmpty(list)) {
            return null;
        }

        List<String> mchNoList = new LinkedList<>();
        list.forEach(mchInfo -> mchNoList.add(mchInfo.getMchNo()));
        return mchNoList;
    }

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