package com.huimv.receive.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.receive.common.exception.ExceptionEnum;
import com.huimv.receive.common.exception.MiException;
import com.huimv.receive.common.token.TokenSign;
import com.huimv.receive.common.utils.*;
import com.huimv.receive.entity.BillGate;
import com.huimv.receive.entity.SysAccountMultilevel;
import com.huimv.receive.entity.dto.WeCahtVo;
import com.huimv.receive.mapper.BillGateMapper;
import com.huimv.receive.service.IBaseJobService;
import com.huimv.receive.service.ILoginService;
import com.huimv.receive.service.ISysAccountMultilevelService;
import com.huimv.receive.session.AccessToken;
import com.huimv.receive.timer.test.TestWeight;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;

import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/admin/my")
@CrossOrigin
public class LoginController {

    @Autowired
    ILoginService iLoginService;
    @Autowired
    private ISysAccountMultilevelService accountMultilevelService;

    @Autowired
    private IBaseJobService baseJobService;

    @Autowired
    private UploadImage uploadImage;
    @Autowired
    private BillGateMapper billGateMapper;

    @PostMapping("/saveDiBang")
    public String logout(@RequestBody Map<String, List<TestWeight>> map) {
        List<TestWeight> dibang = map.get("dibang");
        for (TestWeight testWeight : dibang) {
            System.out.println(testWeight);

            BillGate billGate = billGateMapper.selectOne(new QueryWrapper<BillGate>().eq("stdno", testWeight.getFStdNo()));
            if (ObjectUtil.isEmpty(billGate)) {
                List<BillGate> billGate1 = billGateMapper.selectList(new QueryWrapper<BillGate>().eq("car_num", testWeight.getFCarNo())
                        .between("check_date", testWeight.getBeginTime(), testWeight.getEndTime()).eq("check_location", testWeight.getDeviceCode()));
                if (ObjectUtil.isNotEmpty(billGate1)) {
                    billGate = billGate1.get(0);
                    billGate.setGross(testWeight.getGross() + "");
                    billGate.setTare(testWeight.getMoney() + "");
                    billGate.setNet(testWeight.getNet() + "");
                    billGate.setStdno(testWeight.getFStdNo());
                    billGateMapper.insert(billGate);
                }
            }
        }
        return "";
    }


    @RequestMapping("/logout")
    public Result logout(@RequestParam(name = "accountName", required = true) String accountName, @RequestParam(name = "password", required = true) String password) {
        return iLoginService.logout();
    }

    @PostMapping("/test")
    public String test(@RequestParam(name = "picture1") MultipartFile picture1) throws IOException {
        String videoCom = uploadImage.getVideoCom(picture1);
        return videoCom;
    }


    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @GetMapping("/send")
    public void createImg(HttpServletRequest request, HttpServletResponse response) {
        try {
            //设置响应类型,告诉浏览器输出的内容为图片
            response.setContentType("image/jpeg");
            //设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            VerifyUtil randomValidateCode = new VerifyUtil();
            //输出验证码图片
            String code = randomValidateCode.getRandcode(request, response);
            System.out.println(code);
            //将生成的随机验证码存放到redis中
            String remoteHost = request.getRemoteHost();
            redisTemplate.opsForValue().set(remoteHost, code, 300, TimeUnit.SECONDS);
        } catch (Exception e) {
            System.out.println("获取验证码异常:" + e);
            throw new MiException(ExceptionEnum.VERIFCATION_FAID);
        }
    }

    //登录-多牧场
    @PostMapping(value = "/loginMultilevel")
    public Result loginMultilevel(HttpServletRequest req, @RequestBody Map<String, String> map) {


        String accountName = map.get("accountName");
        String password = map.get("password");
//        String code = map.get("code");

        String remoteHost = req.getRemoteHost();

//        String redisCode = redisTemplate.opsForValue().get(remoteHost);
//        if (redisCode == null) {
//            return  new Result(10005,"验证码过期,点击刷新",false);
//        }
//        if (!redisCode.equals(code.toUpperCase())) {
//            return  new Result(10006,"验证码错误,登录失败",false);
//        }
        // 未对密码加密
        Result result = iLoginService.loginMultilevel(accountName, password);
        if (result.getCode() == 10000) {

            SysAccountMultilevel accountEntity = (SysAccountMultilevel) result.getData();
            Integer id = accountEntity.getId();
            IpTools.getRemoteHost(req);
            AccessToken accessToken = new AccessToken();
            accessToken.setCreated(new Date());
            accessToken.setAccountName(accountName);
            accessToken.setFarmId(accountEntity.getFarmIds());
            String token = TokenSign.sign(accountName, id, accountEntity.getLastFarmId().toString(), null, null);
            accessToken.setToken(token);

            accessToken.setId(id);
            accessToken.setLoginIp(remoteHost);
            accessToken.setType(accountEntity.getType());
            Integer lastFarmId = accountEntity.getLastFarmId();
            if (ObjectUtil.isEmpty(lastFarmId)) {
                lastFarmId = Integer.parseInt(accountEntity.getFarmIds().split(",")[0]);
            }
            accessToken.setLastFarmId(lastFarmId);
            accessToken.setUserName(accountEntity.getUserName());

            return new Result(ResultCode.SUCCESS, accessToken);
        }
        return result;
    }

    @PostMapping(value = "/switchUserType")
    public Result switchUserType(HttpServletRequest req, @RequestBody Map<String, String> map) {
        Integer userId = TokenSign.getMemberIdByJwtToken(req);

        String type = map.get("type");
        String remoteHost = req.getRemoteHost();
        // 未对密码加密
        SysAccountMultilevel accountEntity = accountMultilevelService.getById(userId);

        accountEntity.setType(Integer.parseInt(type));
        accountMultilevelService.updateById(accountEntity);
        accountEntity.setJobName(baseJobService.getById(type).getJobName());

        Integer id = accountEntity.getId();
        IpTools.getRemoteHost(req);
        AccessToken accessToken = new AccessToken();
        accessToken.setCreated(new Date());
        accessToken.setAccountName(accountEntity.getAccount());
        accessToken.setFarmId(accountEntity.getFarmIds());
        String token = TokenSign.sign(accountEntity.getUserName(), id, accountEntity.getFarmIds(), accountEntity.getWorkLocationType() + "", accountEntity.getType());
        accessToken.setToken(token);

        accessToken.setId(id);
        accessToken.setLoginIp(remoteHost);
        accessToken.setType(accountEntity.getType());
        Integer lastFarmId = accountEntity.getLastFarmId();
        if (ObjectUtil.isEmpty(lastFarmId)) {
            lastFarmId = Integer.parseInt(accountEntity.getFarmIds().split(",")[0]);
        }
        accessToken.setLastFarmId(lastFarmId);
        accessToken.setUserName(accountEntity.getUserName());
        accessToken.setSysAccountMultilevel(accountEntity);

        return new Result(ResultCode.SUCCESS, accessToken);
    }

    //测试-token
    @PostMapping(value = "/getToken")
    public String getToken(HttpServletRequest req) {
        String token = TokenSign.sign("superadmin", 1, null, null, null);
        return token;
    }

    @PostMapping(value = "/loginMultilevelClent")
    public Result loginMultilevelClent(HttpServletRequest req, @RequestBody Map<String, String> map) {


        String accountName = map.get("accountName");
        String password = map.get("password");
//        String code = map.get("code");

        String remoteHost = req.getRemoteHost();

//        String redisCode = redisTemplate.opsForValue().get(remoteHost);
//        if (redisCode == null) {
//            return  new Result(10005,"验证码过期,点击刷新",false);
//        }
//        if (!redisCode.equals(code.toUpperCase())) {
//            return  new Result(10006,"验证码错误,登录失败",false);
//        }
        // 未对密码加密
        Result result = iLoginService.loginMultilevelClient(accountName, password);
        if (result.getCode() == 10000) {

            SysAccountMultilevel accountEntity = (SysAccountMultilevel) result.getData();
            Integer id = accountEntity.getId();
            IpTools.getRemoteHost(req);
            AccessToken accessToken = new AccessToken();
            accessToken.setCreated(new Date());
            accessToken.setAccountName(accountName);
            accessToken.setFarmId(accountEntity.getFarmIds());
            Integer lastFarmId1 = accountEntity.getLastFarmId();
            if (ObjectUtil.isEmpty(lastFarmId1)) {
                lastFarmId1 = accountEntity.getFarmId();
            }

            String token = TokenSign.sign(accountName, id, lastFarmId1.toString(), accountEntity.getWorkLocationType(), accountEntity.getType());
            accessToken.setToken(token);

            accessToken.setId(id);
            accessToken.setLoginIp(remoteHost);
            accessToken.setType(accountEntity.getType());
            Integer lastFarmId = accountEntity.getLastFarmId();
            if (ObjectUtil.isEmpty(lastFarmId)) {
                lastFarmId = Integer.parseInt(accountEntity.getFarmIds().split(",")[0]);
            }
            accessToken.setLastFarmId(lastFarmId);
            accessToken.setUserName(accountEntity.getUserName());

            return new Result(ResultCode.SUCCESS, accessToken);
        }
        return result;
    }

    @RequestMapping("/logoutClient")
    public Result logoutClient(HttpServletRequest req, @RequestBody Map<String, String> map) {
        String accountName = map.get("accountName");
        String password = map.get("password");
        String remoteHost = req.getRemoteHost();
        Result result = iLoginService.loginMultilevel(accountName, password);
        if (result.getCode() == 10000) {
            SysAccountMultilevel data = (SysAccountMultilevel) result.getData();
            AccessToken accessToken = new AccessToken();
            accessToken.setSysAccountMultilevel(data);
            accessToken.setAccountName(accountName);
            accessToken.setUserName(data.getUserName());
            accessToken.setFarmId(data.getFarmIds());
            accessToken.setType(data.getType());
            accessToken.setId(data.getId());
            String token = TokenSign.sign(data.getUserName(), data.getId(), data.getLastFarmId().toString(), data.getWorkLocationType() + "", data.getType());
            accessToken.setToken(token);
            Integer lastFarmId = data.getLastFarmId();
            if (ObjectUtil.isEmpty(lastFarmId)) {
                lastFarmId = Integer.parseInt(data.getFarmIds().split(",")[0]);
            }
            accessToken.setLastFarmId(lastFarmId);
            accessToken.setLoginIp(remoteHost);
            return new Result(ResultCode.SUCCESS, accessToken);
        } else {
            return result;
        }
    }


/*    @RequestMapping("/logoutWeChat")
    public Result logoutWeChat(HttpServletRequest req, @RequestBody Map<String, String> map) {
        String remark = map.get("remark");
        QueryWrapper<SysAccountMultilevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account", remark);
        SysAccountMultilevel one = accountMultilevelService.getOne(queryWrapper);
        AccessToken accessToken = new AccessToken();
        if (ObjectUtil.isEmpty(one)) {
            SysAccountMultilevel accountMultilevel = new SysAccountMultilevel();
            accountMultilevel.setAccount(remark);
            accountMultilevel.setFarmIds("25");
            accountMultilevel.setColor("");
            accountMultilevel.setMode(true);
            accountMultilevel.setUserName("访客");
            accountMultilevel.setType(7);
            accountMultilevelService.save(accountMultilevel);
            QueryWrapper<SysAccountMultilevel> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("account", remark);
            SysAccountMultilevel one1 = accountMultilevelService.getOne(queryWrapper1);

            accessToken.setAccountName(remark);
            accessToken.setUserName("访客");
            accessToken.setFarmId("25");
            accessToken.setType(7);
            accessToken.setId(one1.getId());
            String token = TokenSign.sign(remark, one1.getId(), one1.getFarmIds(), null,7);
            accessToken.setToken(token);
        } else {
            accessToken.setAccountName(one.getUserName());
            accessToken.setUserName("访客");
            accessToken.setFarmId("25");
            accessToken.setType(7);
            accessToken.setId(one.getId());
            String token = TokenSign.sign(remark, one.getId(), one.getFarmIds(), one.getWorkLocationId(),7);
            accessToken.setToken(token);
        }
        return new Result(ResultCode.SUCCESS,accessToken);
  *      String accountName = map.get("accountName");
        String password = map.get("password");
        String remoteHost = req.getRemoteHost();
        Result result = iLoginService.loginMultilevel(accountName, password);
        if (result.getCode() == 10000) {
            SysAccountMultilevel data = (SysAccountMultilevel) result.getData();
            AccessToken accessToken = new AccessToken();
            accessToken.setAccountName(accountName);
            accessToken.setUserName(data.getUserName());
            accessToken.setFarmId(data.getFarmIds());
            accessToken.setType(data.getType());
            accessToken.setId(data.getId());
            String token = TokenSign.sign(accountName,data.getId(),data.getFarmIds(),data.getWorkLocationId());
            accessToken.setToken(token);
            Integer lastFarmId = data.getLastFarmId();
            if (ObjectUtil.isEmpty(lastFarmId)){
                lastFarmId = Integer.parseInt(data.getFarmIds().split(",")[0]);
            }
            accessToken.setLastFarmId(lastFarmId);
            accessToken.setLoginIp(remoteHost);
            return new Result(ResultCode.SUCCESS,accessToken);
        } else {
            return result;
        }*//*
    }*/

    @GetMapping("/logoutWeChat")
    public Result logoutWeChat(HttpServletRequest req, @RequestParam String jsCode, @RequestParam String phone) {
        String remoteHost = req.getRemoteHost();
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("appid", "wx221c66f51278c05f");
        paramsMap.put("secret", "b553cf2e2de240ac93db6b6c8d135f55");
        paramsMap.put("js_code", jsCode);
        paramsMap.put("grant_type", "authorization_code");
        String result = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session", paramsMap);
        System.out.println(result);
        JSONObject jsonObject = JSON.parseObject(result);
        System.out.println(jsonObject);
        String openid = (String) jsonObject.get("openid");
        String sessionKey = (String) jsonObject.get("session_key");
        System.out.println("拿到了openid" + openid);
        System.out.println("拿到了sessionKey" + sessionKey);

        QueryWrapper<SysAccountMultilevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wechat_id", openid);
        SysAccountMultilevel data = accountMultilevelService.getOne(queryWrapper);
        WeCahtVo weCahtVo = new WeCahtVo();
        weCahtVo.setOpenId(openid);
        weCahtVo.setSessionKey(sessionKey);
        if (ObjectUtil.isEmpty(data)) {
            QueryWrapper<SysAccountMultilevel> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("phone", phone);
            SysAccountMultilevel data1 = accountMultilevelService.getOne(queryWrapper1);
            if (ObjectUtil.isEmpty(data1)) {
                return new Result(ResultCode.FAIL, weCahtVo);
            } else {
                if ("".equals(data1.getWechatId()) || null == data1.getWechatId()) {
                    //手机号已经代注册
                    data1.setWechatId(openid);
                    accountMultilevelService.updateById(data1);
                    AccessToken accessToken = new AccessToken();
                    if (data1.getType() == 7) {
                        accessToken.setIsAdmin(0);
                    } else {
                        accessToken.setIsAdmin(1);
                    }
                    data1.setType(7);
                    accessToken.setSysAccountMultilevel(data1);
                    accessToken.setAccountName(data1.getAccount());
                    accessToken.setUserName(data1.getUserName());
                    accessToken.setFarmId(data1.getFarmIds());
                    accessToken.setType(7);
                    accessToken.setId(data1.getId());
                    String token = TokenSign.sign(data1.getUserName(), data1.getId(), data1.getLastFarmId().toString(), data1.getWorkLocationType() + "", 7);
                    accessToken.setToken(token);
                    Integer lastFarmId = data1.getLastFarmId();
                    if (ObjectUtil.isEmpty(lastFarmId)) {
                        lastFarmId = Integer.parseInt(data1.getFarmIds().split(",")[0]);
                    }
                    accessToken.setLastFarmId(lastFarmId);
                    accessToken.setLoginIp(remoteHost);
                    return new Result(ResultCode.SUCCESS, accessToken);
                } else {
                    //返回错误，输入的手机号已经进行过微信登录
                    return new Result(ResultCode.UNAUTHENTICATED, "输入的手机号已被他人进行过微信登录！");
                }
            }
        } else {
            if (phone.equals(data.getPhone())) {
                AccessToken accessToken = new AccessToken();
                if (data.getType() == 7) {
                    accessToken.setIsAdmin(0);
                } else {
                    accessToken.setIsAdmin(1);
                }
                data.setType(7);
                accessToken.setSysAccountMultilevel(data);
                accessToken.setAccountName(data.getAccount());
                accessToken.setUserName(data.getUserName());
                accessToken.setFarmId(data.getFarmIds());
                accessToken.setType(7);
                accessToken.setId(data.getId());
                String token = TokenSign.sign(data.getUserName(), data.getId(), data.getLastFarmId().toString(), data.getWorkLocationType() + "", 7);
                accessToken.setToken(token);
                Integer lastFarmId = data.getLastFarmId();
                if (ObjectUtil.isEmpty(lastFarmId)) {
                    lastFarmId = Integer.parseInt(data.getFarmIds().split(",")[0]);
                }
                accessToken.setLastFarmId(lastFarmId);
                accessToken.setLoginIp(remoteHost);
                return new Result(ResultCode.SUCCESS, accessToken);
            } else {
                return new Result(ResultCode.UNAUTHENTICATED, "输入的手机号已被他人进行过微信登录！");
            }
        }
    }

    @GetMapping("/logWeChatGetPhone")
    public Result logWeChatGetPhone(HttpServletRequest req, @RequestParam String jsCode) {
        String access_token = "";
        if (!redisTemplate.hasKey("weChatGetPhone") || redisTemplate.getExpire("weChatGetPhone") == null || redisTemplate.getExpire("weChatGetPhone") <= 0) {
            String result = HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?appid=wx221c66f51278c05f&secret=b553cf2e2de240ac93db6b6c8d135f55&grant_type=client_credential");
            System.out.println(result);
            JSONObject jsonObject = JSON.parseObject(result);
            access_token = (String) jsonObject.get("access_token");
            redisTemplate.opsForValue().set("weChatGetPhone", access_token);
            redisTemplate.expire("weChatGetPhone", 7200, TimeUnit.SECONDS);
        } else {
            access_token = redisTemplate.opsForValue().get("weChatGetPhone");
        }
        RestTemplate restTemplate = new RestTemplate();
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("code", jsCode);
        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity("https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + access_token, paramsMap, String.class);

        System.out.println(stringResponseEntity.getBody());
        return new Result(ResultCode.SUCCESS, JSON.parseObject(stringResponseEntity.getBody()));
    }

    @PostMapping("/register2")
    public String register2(@RequestParam(value = "video", required = false) MultipartFile video) throws IOException {
        return uploadImage.getVideoCom(video);
    }

    @PostMapping("/register")
    public Result addAdmission(HttpServletRequest req, @RequestParam("userName") String userName,
                               @RequestParam("phone") String phone,
                               @RequestParam("workName") String workName,
                               @RequestParam("papersType") String papersType,
                               @RequestParam("papersCode") String papersCode,
                               @RequestParam(value = "imgUrl", required = false) MultipartFile imgUrl,
                               @RequestParam("openId") String openId) throws IOException {
        String remoteHost = req.getRemoteHost();

        MultipartFile multipartFile = ImageSizeUtil.byte2Base64StringFun(imgUrl);
        boolean result = uploadImage.checkFileSize(multipartFile.getSize(), 100, "K");
        if (!result) {
            return new Result(10001, "上传个人照片太大，请重新上传", false);
        }
        QueryWrapper<SysAccountMultilevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 7).and(i -> i.eq("phone", phone).or().eq("papers_code", papersCode));
        if (ObjectUtil.isNotEmpty(accountMultilevelService.getOne(queryWrapper))) {
            return new Result(10001, "该手机号或身份证号已被使用！", false);
        }
        if (!PhoneNumberValidator.isValidPhoneNumber(phone)) {
            return new Result(10001, "手机号格式错误！", false);
        }
        if (papersType.equals("0")) {
//            if (!IDCardValidator.isValidIDCard(papersCode)) {
//                return new Result(10001, "身份证号格式错误!", false);
//            }
        }
        SysAccountMultilevel sysAccountMultilevel = new SysAccountMultilevel();
        sysAccountMultilevel.setUserName(userName);
        sysAccountMultilevel.setPapersCode(papersCode);
        sysAccountMultilevel.setPhone(phone);
        sysAccountMultilevel.setAccount(phone);
        sysAccountMultilevel.setPid(0);
        sysAccountMultilevel.setPids("0");
        sysAccountMultilevel.setPassword("123456");
        sysAccountMultilevel.setWorkName(workName);
        sysAccountMultilevel.setAccountStatus(1);
        sysAccountMultilevel.setFarmIds("25");
        sysAccountMultilevel.setLastFarmId(25);
        sysAccountMultilevel.setPapersType(papersType);
        sysAccountMultilevel.setWechatId(openId);
        sysAccountMultilevel.setType(7);
        String imageCom = uploadImage.getImageCom(multipartFile);
        if (imageCom.equals("上传失败")) {
            return new Result(10001, "图片上传失败！", false);
        }
        sysAccountMultilevel.setImgUrl(imageCom);
        accountMultilevelService.save(sysAccountMultilevel);
        AccessToken accessToken = new AccessToken();
        accessToken.setSysAccountMultilevel(sysAccountMultilevel);
        accessToken.setAccountName(sysAccountMultilevel.getAccount());
        accessToken.setUserName(sysAccountMultilevel.getUserName());
        accessToken.setFarmId(sysAccountMultilevel.getFarmIds());
        accessToken.setType(sysAccountMultilevel.getType());
        accessToken.setId(sysAccountMultilevel.getId());
        String token = TokenSign.sign(sysAccountMultilevel.getUserName(), sysAccountMultilevel.getId(), sysAccountMultilevel.getLastFarmId().toString(), sysAccountMultilevel.getWorkLocationType() + "", sysAccountMultilevel.getType());
        accessToken.setToken(token);
        Integer lastFarmId = sysAccountMultilevel.getLastFarmId();
        if (ObjectUtil.isEmpty(lastFarmId)) {
            lastFarmId = Integer.parseInt(sysAccountMultilevel.getFarmIds().split(",")[0]);
        }
        accessToken.setLastFarmId(lastFarmId);
        accessToken.setLoginIp(remoteHost);
        return new Result(ResultCode.SUCCESS, accessToken);
    }

    @PostMapping("/test1")
    public Result test1() throws IOException {
        List<SysAccountMultilevel> list = accountMultilevelService.list();
        for (SysAccountMultilevel sysAccountMultilevel : list) {
            String imgUrl = sysAccountMultilevel.getImgUrl();
            if (null == imgUrl || "".equals(imgUrl)) {}else{
                URL url = new URL(imgUrl);
                // 将在线图片地址转换为URL对象
// 打开URL连接
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 获取输入流
                InputStream inputStream = connection.getInputStream();
// 读取输入流中的数据，并保存到字节数组中
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    byteArrayOutputStream.write(buffer, 0, bytesRead);
                }
// 将字节数组转换为字节数组输入流
                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
// 创建MultipartFile对象
                MultipartFile file = new MockMultipartFile("file", "filename.jpg", "image/jpg", byteArrayInputStream);
                MultipartFile multipartFile = ImageSizeUtil.byte2Base64StringFun(file);
                boolean result = uploadImage.checkFileSize(multipartFile.getSize(), 100, "K");
                if (!result) {
                    return new Result(10001, "上传个人照片太大，请重新上传", false);
                }
                String imageCom = uploadImage.getImageCom(multipartFile);
                if (imageCom.equals("上传失败")) {
                    return new Result(10001, "图片上传失败！", false);
                }
                sysAccountMultilevel.setImgUrl(imageCom);
                accountMultilevelService.updateById(sysAccountMultilevel);
            }

        }
        return new Result(ResultCode.SUCCESS);
    }


}
