package com.ruoyi.web.controller.chenyanlogin.huicunutil;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.chenyanlogin.service.HuiCunService;
import com.ruoyi.web.domain.entity.PersonInfo;
import com.ruoyi.web.service.PersonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

import static com.ruoyi.common.core.domain.AjaxResult.error;

@Api(tags ="慧村接口 同步")
@RestController
@RequestMapping("/huicun")
public class HuiCunDanDianController {
    @Autowired
    private PersonInfoService personInfoService;
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private SysLoginService loginService;


    //基础数据查询接口
    @ApiOperation("验证token")
    @PostMapping("/verification")
    public AjaxResult getBasicData(@RequestBody Map<String ,String> map){
        String hcToken = map.get("hcToken");
        
        //TODO 手机号应该从hcTOken 中验证获取
        String phone = "";
        if ("1".equals(hcToken)){
             phone = "13980669640";
        }else {
            phone = "15330094466";
        }
        
        //逻辑，拿到手机号，实现登录，获取token ,然后去人员查类型

        SysUser phonenumber = sysUserService.selectUserByPhone(phone);
        if (ObjectUtil.isEmpty(phonenumber)){
            return error("用户不存在");
        }else{
            AjaxResult ajax = AjaxResult.success();

            String token = loginService.loginNoCaptcha(phonenumber.getUserName(),"admin123");
            Map endMap = new HashMap();
            endMap.put("token",token);
            PersonInfo byId = personInfoService.getById(phonenumber.getUserId());
            endMap.put("userType",byId.getUserType());
            ajax.put(Constants.TOKEN, endMap);
            return ajax;
        }


    }

}
