package com.picc.grab.controller.grab;
import com.alibaba.fastjson.JSONObject;
import com.picc.grab.config.PiccGrabConfig;
import com.picc.grab.domain.Piccgrabaccount;
import com.picc.grab.entity.common.dto.piccLoginResult;
import com.picc.grab.entity.common.vo.ResultVO;
import com.picc.grab.service.IPiccgrabaccountService;
import com.picc.grab.util.GrabUtil;
import com.picc.grab.util.StringUtil;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/grabLogin"})
@Transactional
public class GrabLoginController {
 @Autowired
 private IPiccgrabaccountService piccgrabaccount;

 @RequestMapping({"/getCode"})
 public void getCode(Integer id, HttpServletResponse httpServletResponse) throws IOException {
    GrabUtil httpsGet = new GrabUtil();
    InputStream input = httpsGet.getHttpsConn(this.piccGrabConfig.getImgurl() + System.currentTimeMillis(), "GET", null, null);
    String cookieStore = httpsGet.cookieStore;
    ByteArrayOutputStream bitstream = httpsGet.cloneInputStream(input);
    byte[] img = bitstream.toByteArray();
    httpServletResponse.setContentType("image/png");
    ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
    Piccgrabaccount model = this.piccgrabaccount.selectById(id);
    model.setCookie(cookieStore);
    this.piccgrabaccount.updateAllColumnById(model);
    servletOutputStream.write(img);
    servletOutputStream.flush();
    servletOutputStream.close();
 }

 @Autowired
 private PiccGrabConfig piccGrabConfig;

 @RequestMapping({"/getToken"})
 @ResponseBody
 public ResultVO getToken(String code, Integer id) {
     GrabUtil httpsUtil = new GrabUtil();
     Piccgrabaccount model = this.piccgrabaccount.selectById(id);
     Map<String, String> param = new HashMap<>();
     param.put("password", model.getPassword());
     param.put("username", model.getAccount());
     param.put("validCode", code);
     String str = httpsUtil.getHttpsByCookieConn(this.piccGrabConfig.getLoginurl(), param, model.getCookie(), "");
     piccLoginResult result = JSONObject.parseObject(str, piccLoginResult.class);
     System.err.println(result.getCode());
     System.err.println(result.getToken());
     System.err.println(result.getMsg());
     System.err.println(result.getState());
     if (result.getCode().equals("200") && StringUtil.isAllNotBlank(new CharSequence[] { result.getToken() })) {
       model.setToken(result.getToken());
       model.setYzm(code);
       model.setLogIn(Integer.valueOf(1));
       this.piccgrabaccount.updateAllColumnById(model);
       return ResultVO.success();
     }
     return ResultVO.error("登录失败,"+result.getMsg());
 }

    @RequestMapping({"/RBlogin"})
    @ResponseBody
    public ResultVO RBlogin(String cookie,String token, Integer id) {
        Piccgrabaccount model = this.piccgrabaccount.selectById(id);
        model.setCookie(cookie);
        model.setToken(token);
        model.setLogIn(Integer.valueOf(1));
        boolean b = this.piccgrabaccount.updateAllColumnById(model);
        if (b){
            return ResultVO.success();
        }else{
            return ResultVO.error("登录失败");
        }
    }


 @RequestMapping({"/getList"})
 public String getList(int id) {
 return "";
 }


 @RequestMapping({"/bidPrice"})
 public String bidPrice(int id) {
 this.piccgrabaccount.sendBidPrice(id);
 return "";
 }
}
