package com.zcloud.modules.sys.controller;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.zcloud.common.utils.AliSmsToolUtil;
import com.zcloud.common.utils.R;
import com.zcloud.common.utils.RedisUtils;
import com.zcloud.common.utils.WxPushUtil;
import com.zcloud.modules.oa.service.OaDepartmentService;
import com.zcloud.modules.sys.entity.PageData;
import com.zcloud.modules.sys.entity.SysUserEntity;
import com.zcloud.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * 微信公众号绑定相关
 *
 * @author fangjiakai
 * @date 2022/08/09 8:53
 */
@RestController
@RequestMapping("/wxpost")
public class WxPostController {

    @Autowired
    private RedisUtils redisutil;
    @Autowired
    private SysUserService usersservice;
    @Autowired
    private OaDepartmentService departmentService;
    /**
     * 获取openid
     *
     * @param
     * @param
     * @return
     */
    @RequestMapping("/getopenid")
    public R getopenid(@RequestParam("code") String code) throws Exception {
        return R.ok().put("openid", WxPushUtil.getOpenid(code));
    }


    /**
     * 获取验证码
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/getVerifyCode")
    public R getVerifyCode(@RequestParam("phone") String phone) throws Exception {
        SysUserEntity user = usersservice.findByMobile(phone);
        if (user == null) {
            return R.error(200, "您绑定的手机号不存在!");
        } else if (user.getOpenid() != null) {
            return R.error(200, "您绑定的手机号已被绑定,无法重复绑定!");
        }
        String verifycode = CAPTCHA(4);
        redisutil.set("verifycode" + phone, verifycode, 300);
        //发送短信
        // 第1个参数
        List<PageData> paramsList = new ArrayList<>();
        PageData params = new PageData();
        params.put("name", "code");// 存入短信模板中的参数名称
        params.put("value", verifycode);// 存入上面参数名称所传递的值
        paramsList.add(params);
        try {
            send("SMS_213355099", phone, paramsList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    /**
     * 获取验证码
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/verify")
    public R verify(@RequestParam("USERNAME") String phone, @RequestParam("verifyCode") String userVerifyCode, @RequestParam("OPENID") String openid) throws Exception {
        String verifyCode = redisutil.get("verifycode" + phone).toString();
        if (verifyCode.equals(userVerifyCode)) {
            usersservice.editOpenid(phone, openid);
            departmentService.editOpenid(phone, openid);
        } else {
            return R.error(200, "验证码错误!");
        }
        return R.ok();
    }

    private static String CAPTCHA(int n) {
        Random r = new Random();
        String code = "";//分配一个空字符内存
        for (int i = 0; i < n; i++) {
            int m = r.nextInt(8);//生成0~9的随机数
            code += (m + 1);
        }
        return code;
    }

    /**
     * 发送短信
     *
     * @param pd
     * @return
     * @throws Exception
     */
    public SendSmsResponse send(String templateCode, String phone, List<PageData> paramsList) throws Exception {
        Map<String, Object> mes = new HashMap<String, Object>();
        mes.put("phone", phone);
        mes.put("templateCode", templateCode);
        // 发送短信
        SendSmsResponse ssms = AliSmsToolUtil.sendSms(mes, paramsList);
        return ssms;
    }
}
