package com.dgtly.api.controller;

import com.dgtly.apiframework.exception.LessParamException;
import com.dgtly.apiframework.util.TokenUtil;
import com.dgtly.common.annotation.ApiPassToken;
import com.dgtly.common.constant.UserConstants;
import com.dgtly.common.core.controller.BaseController;
import com.dgtly.common.core.domain.AjaxResult;
import com.dgtly.common.utils.security.EncryptPassWordClass;
import com.dgtly.system.domain.SysUser;
import com.dgtly.system.service.ISysUserService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    /**
     * 通用上传请求
     */
    @PostMapping("/getGridList")
    @ResponseBody
    public Object getGridList(){
        SysUser user = new SysUser();
        List<SysUser> users=sysUserService.selectGridPeople(user);
        return success().putKV("list",users);
    }

    /**
     * 通用上传请求
     */
    @PostMapping("/addGrid")
    @ResponseBody
    @ApiPassToken
    public Object addGrid(){
        Map param = getJsonMap();
        SysUser addUser = new SysUser();
        Long[] i = {2L};
        addUser.setRoleIds(i);
        if(!param.containsKey("phonenumber")||!param.containsKey("username")||!param.containsKey("idcard")
                ||!param.containsKey("sex")||!param.containsKey("password")||!param.containsKey("chargeCommunityId")){
            throw  new LessParamException();
        }
        addUser.setPhonenumber(param.get("phonenumber").toString());
        addUser.setLoginName(param.get("phonenumber").toString());
        addUser.setPassword(param.get("password").toString());
        addUser.setChargeCommunityId(Long.parseLong(param.get("chargeCommunityId").toString()));
        addUser.setUserName(param.get("username").toString());
        addUser.setIdCard(param.get("idcard").toString());
        addUser.setSex(param.get("sex").toString());
        if (UserConstants.USER_NAME_NOT_UNIQUE.equals(sysUserService.checkLoginNameUnique(addUser.getLoginName(),0L)))
        {
            return error("新增用户'" + addUser.getLoginName() + "'失败，登录账号已存在");
        }
        else if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(sysUserService.checkPhoneUnique(addUser)))
        {
            return error("新增用户'" + addUser.getLoginName() + "'失败，手机号码已存在");
        }
        addUser.setSalt(randomSalt());
        addUser.setPassword(EncryptPassWordClass.encryptPassword(addUser.getLoginName(), addUser.getPassword(), addUser.getSalt()));

        int num = sysUserService.insertUser(addUser);
        if(num>0){
            return success();
        }else{
            return error();
        }

    }


    /**
     * 生成随机盐
     */
    public static String randomSalt()
    {
        // 一个Byte占两个字节，此处生成的3字节，字符串长度为6
        SecureRandomNumberGenerator secureRandom = new SecureRandomNumberGenerator();
        String hex = secureRandom.nextBytes(3).toHex();
        return hex;
    }


}
