package com.liquidnet.service.sweet.controller.syncRedis;

import com.liquidnet.commons.lang.util.HttpUtil;
import com.liquidnet.service.sweet.service.impl.syncRedis.UserDataServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;

/**
 * <p>
 * 外部用户注册、微信服务号关注用户数据同步
 * </p>
 *
 * @author jiangxiulong
 * @since 2021-08-12
 */
@Api(tags = "数据迁移")
@RestController
@RequestMapping("/wechatSync")
@Slf4j
public class UserDataController {

    @Autowired
    private UserDataServiceImpl userDataService;

    @GetMapping("regMobile")
    @ApiOperation("外部手机号调用adam注册")
    public void regMobile() throws IOException {
        String filePath = "/Users/jiangxiulong/Desktop/keys2.txt";
        String encoding = "UTF-8";
        File file = new File(filePath);
        if (file.isFile() && file.exists()) { //判断文件是否存在
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);//考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);
            String mobile = null;
            while ((mobile = bufferedReader.readLine()) != null) {
                log.info("mobile：[{}]", mobile);
                try {
                    MultiValueMap<String, String> params = new LinkedMultiValueMap();
                    params.add("mobile", mobile);

//                    String url = "http://127.0.0.1:9001/adam/rsc/reg/mobile";
//                    String url = "http://adam.zhengzai.tv/adam/rsc/reg/mobile";
                    String url = "";
                    HttpUtil.post(url, params);
                } catch (Exception e) {
                    log.info("regMobileException：mobile：[{}]", mobile);
                }

            }
            read.close();
        } else {
            System.out.println("找不到指定的文件");
        }
    }

    @GetMapping("users")
    @ApiOperation("同步所有关注服务号用户")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "query", dataType = "Integer", name = "type", value = "type 1正在 2摩登", required = true)
    })
    public void getUsers(@RequestParam() Integer type) {
        try {
            userDataService.userInfo(type);
        } catch (Exception e) {
            log.error("getUsers", e);
        }
    }

    @GetMapping("user")
    @ApiOperation("以openid同步单个关注服务号的用户")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "query", dataType = "String", name = "openId", value = "openId", required = true),
            @ApiImplicitParam(type = "query", dataType = "Integer", name = "type", value = "type 1正在 2摩登", required = true)
    })
    public void getUser(@RequestParam() String openId, @RequestParam() Integer type) {
        try {
            userDataService.getUser(openId, type);
        } catch (Exception e) {
            log.error("getUser", e);
        }
    }

    @GetMapping("userOpenIdDel")
    @ApiOperation("以openid删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "query", dataType = "String", name = "openId", value = "openId", required = true),
            @ApiImplicitParam(type = "query", dataType = "Integer", name = "type", value = "type 1正在 2摩登", required = true)
    })
    public void userOpenIdDel(@RequestParam() String openId, @RequestParam() Integer type) {
        try {
            userDataService.userOpenIdDel(openId, type);
        } catch (Exception e) {
            log.error("userOpenIdDel", e);
        }
    }

    @GetMapping("userPhoneDel")
    @ApiOperation("以phone删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(type = "query", dataType = "String", name = "phone", value = "phone", required = true)
    })
    public void userPhoneDel(@RequestParam() String phone) {
        try {
            userDataService.userPhoneDel(phone);
        } catch (Exception e) {
            log.error("userPhoneDel", e);
        }
    }

}
