package com.jeequan.jeepay.mgr.ctrl.merchant;

import cn.hutool.core.codec.Base64;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeequan.jeepay.bizcommons.manage.sms.SmsManager;
import com.jeequan.jeepay.components.mq.model.CleanMchLoginAuthCacheMQ;
import com.jeequan.jeepay.components.mq.model.ResetIsvMchAppInfoConfigMQ;
import com.jeequan.jeepay.components.mq.vender.IMQSender;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.core.model.smsconfig.SmsBizDiyContentModel;
import com.jeequan.jeepay.core.utils.SeqKit;
import com.jeequan.jeepay.db.entity.MchInfo;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.MchInfoService;
import com.jeequan.jeepay.service.impl.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 商户管理类
 *
 * @author pangxiaoyu
 * @site https://www.jeequan.com
 * @date 2021-06-07 07:15
 */
@RestController
@RequestMapping("/api/mchInfo")
public class MchInfoController extends CommonCtrl {

    @Autowired private MchInfoService mchInfoService;
    @Autowired private SysUserService sysUserService;
    @Autowired private IMQSender mqSender;
    @Autowired private SmsManager smsManager;

    /**
     * @author: pangxiaoyu
     * @date: 2021/6/7 16:14
     * @describe: 商户信息列表
     */
    @PreAuthorize("hasAuthority('ENT_MCH_LIST')")
    @RequestMapping(value="", method = RequestMethod.GET)
    public ApiRes list() {
        MchInfo mchInfo = getObject(MchInfo.class);
        // 扩展员仅查询自己的数据
        if (getCurrentUser().isEpUser()){
            mchInfo.setCreatedUid(getCurrentUser().getSysUser().getSysUserId());
        }

        Page<MchInfo> listPage = mchInfoService.getListPage(getIPage(), mchInfo);

        return ApiRes.page(listPage);
    }

    /**
     * @author: pangxiaoyu
     * @date: 2021/6/7 16:14
     * @describe: 新增商户信息
     */
    @PreAuthorize("hasAuthority('ENT_MCH_INFO_ADD')")
    @MethodLog(remark = "新增商户")
    @RequestMapping(value="", method = RequestMethod.POST)
    public ApiRes add() {
        MchInfo mchInfo = getObject(MchInfo.class);
        // 获取传入的商户登录名、登录密码
        String loginPassword = getValString("loginPassword");
        Byte isNotify = getValByteRequired("isNotify");
        if (StringUtils.isBlank(loginPassword)) {
            loginPassword = null;
        }
        mchInfo.setMchNo(SeqKit.genMchNo());
        // 当前登录用户信息
        SysUser sysUser = getCurrentUser().getSysUser();
        mchInfo.setCreatedUid(sysUser.getSysUserId());
        mchInfo.setCreatedBy(sysUser.getRealname());

        mchInfoService.addMch(mchInfo, loginPassword);
        // 发送短信通知
        if (isNotify == CS.YES) {

            smsManager.sendDiyContentSms(SmsBizDiyContentModel.genUserOpenAccount(mchInfo.getContactTel(), mchInfo.getLoginUsername(), loginPassword));
        }
        return ApiRes.ok();
    }

    /**
     * @author: pangxiaoyu
     * @date: 2021/6/7 16:14
     * @describe: 删除商户信息
     */
    @PreAuthorize("hasAuthority('ENT_MCH_INFO_DEL')")
    @MethodLog(remark = "删除商户")
    @RequestMapping(value="/{mchNo}", method = RequestMethod.DELETE)
    public ApiRes delete(@PathVariable("mchNo") String mchNo) {
        List<Long> userIdList = mchInfoService.removeByMchNo(mchNo);

        // 推送mq删除redis用户缓存
        mqSender.send(CleanMchLoginAuthCacheMQ.build(userIdList));

        return ApiRes.ok();
    }

    /**
     * @author: pangxiaoyu
     * @date: 2021/6/7 16:14
     * @describe: 更新商户信息
     */
    @PreAuthorize("hasAuthority('ENT_MCH_INFO_EDIT')")
    @MethodLog(remark = "更新商户信息")
    @RequestMapping(value="/{mchNo}", method = RequestMethod.PUT)
    public ApiRes update(@PathVariable("mchNo") String mchNo) {

        //获取查询条件
        MchInfo mchInfo = getObject(MchInfo.class);
        mchInfo.setMchNo(mchNo); //设置商户号主键

        mchInfo.setType(null); //防止变更商户类型
//        mchInfo.setIsvNo(null);
        mchInfo.setLoginUsername(null); // 防止修改用户登录名

        // 是否重置密码 & 确认新密码
        boolean resetPass = getReqParamJSON().getBooleanValue("resetPass");
        String confirmPwd = null;
        if(resetPass){
            confirmPwd = getReqParamJSON().getBoolean("defaultPass") ? null : Base64.decodeStr(getValStringRequired("confirmPwd"));
        }

        // 是否修改状态为停用，设置为由运营平台停用
        if (mchInfo.getState() != null && mchInfo.getState() != MchInfo.STATE_YES) {
            mchInfo.setState(MchInfo.STATE_NO_MGR);
        }

        // 更新商户 & 得到需要删除的商户用户ID的记录
        Set<Long> removeCacheUserIdList = mchInfoService.updateMch(mchInfo, resetPass, confirmPwd);

        // 推送mq删除redis用户认证信息
        if (!removeCacheUserIdList.isEmpty()) {
            mqSender.send(CleanMchLoginAuthCacheMQ.build(new ArrayList<>(removeCacheUserIdList)));
        }

        // 推送mq到目前节点进行更新数据
        mqSender.send(ResetIsvMchAppInfoConfigMQ.build(ResetIsvMchAppInfoConfigMQ.RESET_TYPE_MCH_INFO, null, mchNo, null));

        return ApiRes.ok();
    }

    /**
     * @author: pangxiaoyu
     * @date: 2021/6/7 16:14
     * @describe: 查询商户信息
     */
    @PreAuthorize("hasAnyAuthority('ENT_MCH_INFO_VIEW', 'ENT_MCH_INFO_EDIT')")
    @RequestMapping(value="/{mchNo}", method = RequestMethod.GET)
    public ApiRes detail(@PathVariable("mchNo") String mchNo) {
        MchInfo mchInfo = mchInfoService.getById(mchNo);
        if (mchInfo == null) {
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_SEARCH);
        }

        if (mchInfo.getState() == MchInfo.STATE_NO_MGR) {
            mchInfo.setState(CS.NO);
        }

        return ApiRes.ok(mchInfo);
    }

    /**
     * 获取默认渠道
     *
     * @param mchNo
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_MCH_INFO_VIEW')")
    @GetMapping("/defIsv/{mchNo}")
    public ApiRes getDefaultIsv(@PathVariable("mchNo") String mchNo) {
        String ifCode = getValString("ifCode");

        List<Map<String, String>> defIsv = mchInfoService.getDefIsv(mchNo, ifCode);

        return ApiRes.ok(defIsv);
    }

    /**
     * 设置默认渠道
     *
     * @param mchNo
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ENT_MCH_INFO_VIEW', 'ENT_MCH_INFO_EDIT')")
    @PostMapping("/defIsv/{mchNo}")
    public ApiRes setDefaultIsv(@PathVariable("mchNo") String mchNo) {
        String ifCode = getValStringRequired("ifCode");
        String isvNo = getValStringRequired("isvNo");
        mchInfoService.setDefIsv(mchNo, ifCode, isvNo);

        return ApiRes.ok();
    }
}
