package com.ruoyi.project.system.controller.front;

import com.ruoyi.common.utils.ZhiHuiTokenUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.ZhCompanyInfo;
import com.ruoyi.project.system.domain.ZhGroupInfo;
import com.ruoyi.project.system.domain.vo.ZhCompanyVo;
import com.ruoyi.project.system.service.ISysUserService;
import com.ruoyi.project.system.service.IZhCompanyInfoService;
import com.ruoyi.project.system.service.IZhGroupInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/front/zhihui")
public class ZhiHuiController extends BaseController {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IZhCompanyInfoService zhCompanyInfoService;

    @Autowired
    private IZhGroupInfoService zhGroupInfoService;


    /**
     * 获取token
     *
     * @param params
     * @return
     */
    @PostMapping("/getToken")
    public AjaxResult getToken(@RequestBody Map<String, String> params) {
        String appid = params.get("appid");
        String channel = params.get("channel");
        String stamp = params.get("userId");
        String timestamp = String.valueOf(System.currentTimeMillis());
        String token = ZhiHuiTokenUtils.getToken(appid, channel, stamp, timestamp);

        return AjaxResult.success("success", token);
    }

    /**
     * 获取sig
     *
     * @param params
     * @return
     */
    @PostMapping("/getSig")
    public AjaxResult getSig(@RequestBody Map<String, String> params) {
        return AjaxResult.success("success", ZhiHuiTokenUtils.getSign(params));
    }

    /**
     * 更新用户信息
     *
     * @param sysUser
     * @return
     */
    @PostMapping("/updateUserInfo")
    public AjaxResult updateUserInfo(@RequestBody SysUser sysUser) {
        return toAjax(sysUserService.updateUserProfile(sysUser));
    }

    /**
     * 创建企业
     *
     * @return
     */
    @PostMapping("/saveCompany")
    public AjaxResult saveCompany(@RequestBody ZhCompanyVo zhCompanyVo) {
        ZhGroupInfo zhGroupInfo = new ZhGroupInfo();
        zhGroupInfo.setZhGroupId(zhCompanyVo.getZhGroupId());
        zhGroupInfo.setZhFirmId(zhCompanyVo.getZhFirmId());

        ZhCompanyInfo zhCompanyInfo = new ZhCompanyInfo();
        zhCompanyInfo.setZhFirmId(zhCompanyVo.getZhFirmId());
        zhCompanyInfo.setName(zhCompanyVo.getName());
        zhGroupInfoService.insertZhGroupInfo(zhGroupInfo);
        return toAjax(zhCompanyInfoService.insertZhCompanyInfo(zhCompanyInfo));
    }

    /**
     * 创建团队
     *
     * @param zhGroupInfo
     * @return
     */
    @PostMapping("/saveGroup")
    public AjaxResult saveGroup(@RequestBody ZhGroupInfo zhGroupInfo) {
        return toAjax(zhGroupInfoService.insertZhGroupInfo(zhGroupInfo));
    }

    /**
     * 更新团队信息
     */
    @PostMapping("/updateGroup")
    public AjaxResult updateGroup(@RequestBody ZhGroupInfo zhGroupInfo) {
        return toAjax(zhGroupInfoService.updateZhGroupInfoByGroupId(zhGroupInfo));
    }

    /**
     * 名称找用户
     *
     * @param user
     * @return
     */
    @PostMapping("/getUserIdByName")
    public AjaxResult getUserIdByName(@RequestBody SysUser user) {
        SysUser sysUser = sysUserService.selectUserByUserName(user.getUserName());
        return AjaxResult.success("success", sysUser);
    }

    @PostMapping("getUserByZhId")
    public AjaxResult getUserByZhId(@RequestBody SysUser user) {
        SysUser sysUser = sysUserService.selectUserByZhUserId(user.getZhUserId());
        return AjaxResult.success("success", sysUser);
    }
}
