package com.uas.platform.b2b.manage.controller;

import com.uas.platform.b2b.manage.controller.support.PageResultBean;
import com.uas.platform.b2b.manage.core.support.SystemSession;
import com.uas.platform.b2b.manage.model.AccessToken;
import com.uas.platform.b2b.manage.model.EnterpriseBaseInfo;
import com.uas.platform.b2b.manage.model.UseType;
import com.uas.platform.b2b.manage.page.PageInfo;
import com.uas.platform.b2b.manage.page.PageParams;
import com.uas.platform.b2b.manage.service.AccessTokenService;
import com.uas.platform.b2b.manage.service.EnterpriseBaseInfoService;
import com.uas.platform.b2b.manage.web.BaseController;
import com.uas.platform.core.util.AgentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.rmi.AccessException;

/**
 * 企业数据查询接口
 *
 * @author hejq
 * @date 2018-04-23
 */
@RequestMapping("/enterprise")
@RestController
public class EnterpriseController extends BaseController {

    @Autowired
    private EnterpriseBaseInfoService baseInfoService;

    @Autowired
    private AccessTokenService accessTokenService;

    /**
     * b2b域名
     */
    private static final String B2B_DOMAIN = "b2b.usoftchina.com";

    /**
     * 分页查询企业列表
     *
     * @param params 分页参数
     * @param keyword 搜索词
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public PageResultBean<EnterpriseBaseInfo> findByPageInfo(PageParams params, String keyword, HttpServletRequest request) {
        PageInfo info = new PageInfo(params);
        Sort sort = new Sort(Sort.Direction.DESC, "enDate");
        info.setSort(sort);
        if (!StringUtils.isEmpty(keyword)) {
            keyword = keyword.trim();
        }
        useLogService.appendLog(UseType.ENGERPRISE_SEARCH.code(), keyword, AgentUtils.getIp(request));
        return new PageResultBean<>(baseInfoService.findEnterPageByKeyword(info, keyword));
    }

    /**
     * 通过UU号进行跳转到平台
     *
     * @param response HttpServletResponse
     * @param enUU uu号
     * @throws IOException
     */
    @RequestMapping("/authed/redirect/{enUU}")
    public void redirect(HttpServletResponse response, HttpServletRequest request, @PathVariable("enUU") long enUU)
            throws IOException {
        Object bindObj = enUU;
        if (null == SystemSession.getUser()) {
            throw new AccessException("请先登录");
        }
        AccessToken token = accessTokenService.createNew(bindObj);
        request.getSession().setAttribute("user", SystemSession.getUser());
        StringBuffer redirectUrl = new StringBuffer();
        redirectUrl.append("https://").append(enUU).append(".").append(B2B_DOMAIN);
        redirectUrl.append("?client_type=manage&access_token=").append(token.getId());
        useLogService.appendLog(UseType.REDIRECT.code(), redirectUrl.toString(), AgentUtils.getIp(request));
        response.sendRedirect(redirectUrl.toString());
    }
}
