package com.ruoyi.front.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.front.service.IWxService;
import com.ruoyi.logistics.domain.SysDeptRate;
import com.ruoyi.logistics.service.ISysDeptRateService;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.system.service.ISysPermissionService;
import com.ruoyi.system.service.ISysUserService;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * WX-用户信息管理
 */
@RestController
@RequestMapping("/front/wxmini")
public class WxMiniController {

    @Autowired
    private IWxService wxService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysPermissionService permissionService;

    @Autowired
    private ISysConfigService configService;

    @InnerAuth
    @GetMapping("/getOpenId")
    public R<String> getOpenId(@RequestParam("jsCode") String jsCode) {
        try {
            return R.ok(wxService.getOpenId(jsCode));
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }

    @InnerAuth
    @GetMapping("/getPhoneNumber")
    public R<String>  getPhoneNumber(@RequestParam("code") String code) {
        try {
            return R.ok(wxService.getPhoneNumber(code));
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }

    @InnerAuth
    @PostMapping("/getUserByOpenId")
    public R<LoginUser> getUserByOpenId(@RequestBody SysUser sysUser) {
        SysUser currentUser = sysUserService.selectUserByOpenId(sysUser);
        if (StringUtils.isNull(currentUser))
        {
            currentUser = sysUserService.selectUserByPhoneNumber(sysUser.getPhonenumber());
            if (StringUtils.isNull(currentUser))
            {
                return R.fail("用户不存在");
            }
            currentUser.setOpenId(sysUser.getOpenId());
            // 更新用户openid
            sysUserService.updateUserProfile(currentUser);
        }
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(currentUser);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(currentUser);
        LoginUser sysUserVo = new LoginUser();
        sysUserVo.setSysUser(currentUser);
        sysUserVo.setRoles(roles);
        sysUserVo.setPermissions(permissions);
        return R.ok(sysUserVo);
    }


    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getSysUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);

        sysUserService.loginUserExtendInfo(ajax, user.getDeptId());

        return ajax;
    }

    /**
     * 联系客服，通过获取系统参数得到
     */
    @GetMapping("/getContact")
    public AjaxResult getContact() {
        //通过系统参数rj.customer.service，获取客服电话
        return AjaxResult.success().put("contact", configService.selectConfigByKey("rj.customer.service"));
    }

}
