package com.huimv.beeboxs.controller;

import com.huimv.beeboxs.common.token.TokenSign;
import com.huimv.beeboxs.common.utils.Result;
import com.huimv.beeboxs.common.utils.ResultCode;
import com.huimv.beeboxs.entity.SysAccountMultilevel;
import com.huimv.beeboxs.service.ILoginService;
import com.huimv.beeboxs.session.AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
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 javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
@RequestMapping("/hive")
public class OpenCotroller {

    @Autowired
    ILoginService iLoginService;

    @GetMapping("/token")
    public  Result test(@RequestParam(name = "account") String account,
                        @RequestParam(name = "password") String password,
                        HttpServletRequest req){

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

        String remoteHost = req.getRemoteHost();
        Result result = iLoginService.loginMultilevel(account, password);
        if(result.getCode() == 10000){

            SysAccountMultilevel accountEntity = (SysAccountMultilevel) result.getData();
            Integer id = accountEntity.getId();


            AccessToken accessToken = new AccessToken();
            accessToken.setCreated(new Date());
            accessToken.setAccountName(account);
            accessToken.setCode(result.getCode());
            accessToken.setMessage(result.getMessage());
            accessToken.setFarmId(accountEntity.getFarmIds());
            String token = TokenSign.sign(account,id,accountEntity.getFarmIds());
            accessToken.setToken(token);

            accessToken.setId(id);
            accessToken.setLoginIp(remoteHost);
            accessToken.setType(accountEntity.getType());
            return new Result(ResultCode.SUCCESS,accessToken);
        }
        return  result;
    }

    @GetMapping("/station/list")
    public  Result test(@RequestParam(name = "pageNo") String pageNo,
                        @RequestParam(name = "pageSize") String pageSize,
                        @RequestParam(name = "netstatus") String netstatus){

return null;
    }
}
