package cn.com.countrygarden.bi.main.controller.personnel;

import cn.com.countrygarden.bi.main.controller.BaseController;
import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.dup.DupOrgan;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.dup.DupCommService;
import cn.com.countrygarden.bi.main.service.personnel.PersonnelCommenService;
import cn.com.countrygarden.bi.main.service.personnel.PersonnelService;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
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 java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * package: cn.com.countrygarden.bi.main.controller.center describe: creat_user:
 * lsl creat_date: 2018/7/2 creat_time: 16:13
 **/
@RestController("PersonInfoController")
@RequestMapping("/api/personInfo")
public class PersonInfoController extends BaseController {
	
	@Autowired
    private DupCommService dupCommService;
	
	@Autowired
    private AccessTokenService accessTokenService;

	@Autowired
	private PersonnelCommenService personnelCommenService;

	@Autowired
	private PersonnelService personnelService;

	@GetMapping(value = "/list", produces = "application/json;charset=utf-8")
	public Result selectInfoByUid(@RequestParam(value = "page", required = false, defaultValue = "10") int page,
			@RequestParam(value = "size", required = false, defaultValue = "1") int size,
			@RequestParam(value = "name", required = false, defaultValue = "") String name,
			@RequestParam(value = "userId", required = false, defaultValue = "") String userId,
			HttpServletRequest request) {
		return personnelService.selectInfoByUid(name, userId, page, size);
	}

	@GetMapping(value = "/one", produces = "application/json;charset=utf-8")
	public Result selectUid(@RequestParam(value = "uid", required = true) String uid, HttpServletRequest request) {
		return personnelService.selectUid(uid);
	}

	@GetMapping(value = "queryByCodeOrId", produces = "application/json;charset=utf-8")
	public Result queryByUid(@RequestParam(value = "search", required = true) String search,
			HttpServletRequest request) {
		return personnelService.selectByUserIdOrName(search, getAccessToken(request));
	}

	@GetMapping(value = "personInfoApp", produces = "application/json;charset=utf-8")
	public Result personInfoApp(@RequestParam(value = "uids", required = true) String uids, HttpServletRequest request)
			throws UnsupportedEncodingException {
		String jobId = new String(Base64.decodeBase64(uids));
		if (StringUtils.isNotBlank(jobId)) {
			jobId = URLDecoder.decode(jobId, "utf-8");
		}
		return personnelService.selectUidList(jobId);

	}

	@GetMapping(value = "/queryadByUid", produces = "application/json;charset=utf-8")
	public Result queryadByUid(@RequestParam(value = "search", required = true) String search,
			HttpServletRequest request) {

		return personnelService.queryadByUid(search, getAccessToken(request));
	}

	@GetMapping(value = "/personInfoBdid", produces = "application/json;charset=utf-8")
	public Result personInfoBdid(@RequestParam(value = "bdid", required = true) String bdid, HttpServletRequest request)
			throws UnsupportedEncodingException {
		System.out.println(personnelService.bdpsndoc(bdid));
		if (personnelService.bdpsndoc(bdid) == null) {
			return Response.response("没有找到此身份证的信息，请核对后再提交");
		} else {
			return Response.response("ok");
		}

	}

	@GetMapping(value = "/findRoleregionName", produces = "application/json;charset=utf-8")
	public Result findRoleregionName(HttpServletRequest request) {

		String accessToken = request.getParameter("AccessToken");
        AccessToken token = accessTokenService.findByToken(accessToken);
        List<DupOrgan> result = personnelCommenService.findDupCommByUserCode(token.getUserCode(),null,true);
        return Response.response(result);
		//return personnelCommenService.findRoleregionName(getAccessToken(request));
	}

}
