package com.yanzhu.system.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.yanzhu.common.core.domain.R;
import com.yanzhu.common.core.utils.bean.BeanUtils;
import com.yanzhu.common.core.utils.poi.ExcelUtil;
import com.yanzhu.common.core.web.controller.BaseController;
import com.yanzhu.common.core.web.domain.AjaxResult;
import com.yanzhu.common.core.web.page.TableDataInfo;
import com.yanzhu.common.log.annotation.Log;
import com.yanzhu.common.log.enums.BusinessType;
import com.yanzhu.common.security.annotation.InnerAuth;
import com.yanzhu.common.security.annotation.RequiresPermissions;
import com.yanzhu.system.api.domain.SysUserUniopen;
import com.yanzhu.system.service.ISysUserUniopenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 用户授权Controller
 * 
 * @author JiangYuQi
 * @date 2024-10-13
 */
@RestController
@RequestMapping("/sysUserUniopen")
public class SysUserUniopenController extends BaseController
{
    @Autowired
    private ISysUserUniopenService sysUserUniopenService;

    @Autowired
    private WxMaService wxMaService;

    private static final Logger log = LoggerFactory.getLogger(SysUserController.class);

    /**
     * 查询用户授权列表
     */
    @RequiresPermissions("@ss.hasPermi('system:sysUserUniopen:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUserUniopen sysUserUniopen)
    {
        startPage();
        List<SysUserUniopen> list = sysUserUniopenService.selectSysUserUniopenList(sysUserUniopen);
        return getDataTable(list);
    }

    /**
     * 导出用户授权列表
     */
    @RequiresPermissions("@ss.hasPermi('system:sysUserUniopen:export')")
    @Log(title = "用户授权", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserUniopen sysUserUniopen)
    {
        List<SysUserUniopen> list = sysUserUniopenService.selectSysUserUniopenList(sysUserUniopen);
        ExcelUtil<SysUserUniopen> util = new ExcelUtil<SysUserUniopen>(SysUserUniopen.class);
        util.exportExcel(response, list, "用户授权数据");
    }

    /**
     * 获取用户授权详细信息
     */
    @RequiresPermissions("@ss.hasPermi('system:sysUserUniopen:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysUserUniopenService.selectSysUserUniopenById(id));
    }

    /**
     * 新增用户授权
     */
    @RequiresPermissions("@ss.hasPermi('system:sysUserUniopen:add')")
    @Log(title = "用户授权", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysUserUniopen sysUserUniopen)
    {
        return toAjax(sysUserUniopenService.insertSysUserUniopen(sysUserUniopen));
    }

    /**
     * 修改用户授权
     */
    @RequiresPermissions("@ss.hasPermi('system:sysUserUniopen:edit')")
    @Log(title = "用户授权", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysUserUniopen sysUserUniopen)
    {
        return toAjax(sysUserUniopenService.updateSysUserUniopen(sysUserUniopen));
    }

    /**
     * 删除用户授权
     */
    @RequiresPermissions("@ss.hasPermi('system:sysUserUniopen:remove')")
    @Log(title = "用户授权", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysUserUniopenService.deleteSysUserUniopenByIds(ids));
    }

    /**
     * 小程序授权
     */
    @InnerAuth
    @GetMapping("/getMaOpenId/{code}")
    public R<Map<String, Object>> getMaOpenId(@PathVariable("code") String code) {
        try {
            WxMaJscode2SessionResult sessionInfo =
                    wxMaService.getUserService().getSessionInfo(code);
            log.info("getMaOpenId==>{}...{}",code,sessionInfo.getOpenid());
            return R.ok(BeanUtils.beanToMap(sessionInfo));
        }catch (Exception e){
            return R.fail("小程序授权异常！！！");
        }
    }

    @InnerAuth
    @GetMapping("/findByUserId/{userId}")
    public R<List<SysUserUniopen>> findByUserId(@PathVariable("userId") Long userId) {
        return R.ok(sysUserUniopenService.selectSysUserUniopenByUserId(userId));
    }

    @InnerAuth
    @GetMapping("/findByUserName/{userName}")
    public R<List<SysUserUniopen>> findByUserName(@PathVariable("userName") String userName) {
        return R.ok(sysUserUniopenService.selectSysUserUniopenByUserName(userName));
    }

    @InnerAuth
    @GetMapping("/findByUnionid/{unionid}")
    public R<SysUserUniopen> findByUnionid(@PathVariable("unionid") String unionid) {
        return R.ok(sysUserUniopenService.selectSysUserUniopenByUnionid(unionid));
    }

    @InnerAuth
    @GetMapping("/findByMaOpenId/{maOpenId}")
    public R<SysUserUniopen> findByMaOpenId(@PathVariable("maOpenId") String maOpenId) {
        return R.ok(sysUserUniopenService.selectSysUserUniopenByMaOpenId(maOpenId));
    }

    @InnerAuth
    @GetMapping("/findByMpOpenId/{mpOpenId}")
    public R<SysUserUniopen> findByMpOpenId(@PathVariable("mpOpenId") String mpOpenId) {
        return R.ok(sysUserUniopenService.selectSysUserUniopenByMpOpenId(mpOpenId));
    }
}
