package com.changhu.module.miniProgram.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changhu.common.exception.MessageException;
import com.changhu.common.utils.SnowFlakeIdUtil;
import com.changhu.module.miniProgram.mapper.MiniProgramUserMapper;
import com.changhu.module.miniProgram.mapper.SecurityUserMapper;
import com.changhu.module.miniProgram.pojo.entity.MiniProgramUser;
import com.changhu.module.miniProgram.pojo.params.MiniProgramUserRegisterParams;
import com.changhu.module.miniProgram.pojo.params.SaveOrUpdateSecurityUserParams;
import com.changhu.module.miniProgram.pojo.queryParams.ServiceProjectSecurityUserPagerQueryParams;
import com.changhu.module.miniProgram.pojo.vo.ServiceProjectSecurityUserPagerVo;
import com.changhu.module.miniProgram.service.MiniProgramUserService;
import com.changhu.module.miniProgram.service.ProjectManageIndexService;
import com.changhu.support.mybatisplus.pojo.params.PageParams;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * mini_program_user (小程序用户)  服务实现类
 * author: luozhun
 * desc 由groovy脚本自动生成
 */
@Service
public class MiniProgramUserServiceImpl extends ServiceImpl<MiniProgramUserMapper, MiniProgramUser> implements MiniProgramUserService {

    @Autowired
    private WxMaService wxMaService;

    @Autowired
    private SecurityUserMapper securityUserMapper;

    @Autowired
    private ProjectManageIndexService projectManageIndexService;

    @Override
    public void register(MiniProgramUserRegisterParams params) {
        MiniProgramUser miniProgramUser = BeanUtil.copyProperties(params, MiniProgramUser.class);
        try {
            WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(params.getCode());
            miniProgramUser.setOpenId(sessionInfo.getOpenid());
            miniProgramUser.setSessionKey(sessionInfo.getSessionKey());
        } catch (WxErrorException e) {
            throw new MessageException(e.getMessage());
        }
        boolean exists = baseMapper.exists(Wrappers.<MiniProgramUser>lambdaQuery().eq(MiniProgramUser::getOpenId, miniProgramUser.getOpenId()));
        if (exists) {
            throw new MessageException("该用户已存在，请勿重复注册!");
        }
        long userId = SnowFlakeIdUtil.snowflakeId();
        miniProgramUser.setSnowFlakeId(userId);
        boolean save = this.save(miniProgramUser);
        if (!save) {
            throw new MessageException();
        }
    }

    @Override
    public Page<ServiceProjectSecurityUserPagerVo> securityUserPager(PageParams<ServiceProjectSecurityUserPagerQueryParams, ServiceProjectSecurityUserPagerVo> queryParams) {
        return securityUserMapper.securityUserPager(queryParams.getPage(), queryParams.getParams());
    }

    @Override
    public void qrCodeFormInputSecurityUser(SaveOrUpdateSecurityUserParams params) {
        projectManageIndexService.saveOrUpdateSecurityUser(params);
    }
}
