package com.ims.eval.controller;




import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ims.eval.entity.OrganizationStructure;
import com.ims.eval.entity.dto.result.R;
import com.ims.eval.service.IOrganizationStructureService;
import com.ims.eval.service.custom.PostService;
import com.ims.eval.service.custom.PostUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wang
 * @since 2023-03-01
 */
@RestController
@RequestMapping("//organization-structure")
public class OrganizationStructureController {


	@Autowired
	private HttpServletRequest request;

	@Autowired
	private IOrganizationStructureService organizationStructureService;


	@Autowired
	private PostService postService;

	@Autowired
	private PostUserService postUserService;
	/**
	 * 添加
	 *
	 * @param evaluateRuleInfo
	 * @return
	 */

	//@ImsPreAuth("eval:dataDictionary:edit")
	@PostMapping(value = "/save")
	@ApiOperation(value = "新增(修改)", notes = "新增(修改)")
	public R addAll(@RequestBody List<OrganizationStructure> evaluateRuleInfo) {

		boolean b = organizationStructureService.saveBatch(evaluateRuleInfo);
		if (b) {
			return R.ok().data(b);
		} else {
			return R.error().data("保存失败!");
		}
	}


	@GetMapping(value = "getTree")
	public R getTree(
		@RequestParam(value = "id", required = false) String id,
		@RequestParam(value = "num", required = false) Integer num,
		@RequestParam(value = "type", required = false) String type) {
		List<OrganizationStructure> list = organizationStructureService.getTree(id,num,type);
		return R.ok().data(list);
	}

	@GetMapping(value = "getList2")
	public R getList2(
		@RequestParam(value = "id", required = false) String id,
		@RequestParam(value = "num", required = false) Integer num,
		@RequestParam(value = "type", required = false) String type) {
		List<OrganizationStructure> list = organizationStructureService.getList2(id,num,type);
		return R.ok().data(list);
	}




	@GetMapping(value = "getList")
	public R getList(
		@RequestParam(value = "likeParentId", required = false) String id,
		@RequestParam(value = "parentId", required = false) String num,
		@RequestParam(value = "name", required = false) String name,
		@RequestParam(value = "type", required = false) String type) {
		List<OrganizationStructure> list = organizationStructureService.getList(id,num,name,type, request);
		return R.ok().data(list);
	}


	/**
	 * 根据组织获取岗位
	 * @param current
	 * @param size
	 * @param orgId
	 * @return
	 */
	@GetMapping(value = "getPostList")
	public R getPostList(
		@RequestParam(value = "current", required = false) Integer current,
		@RequestParam(value = "size", required = false) Integer size,
		@RequestParam(value = "orgId", required = false) String orgId) {

		try {
			Object list = postService.getPostList(current,size,orgId, request);
			if(null !=list){
				return R.ok().data(list);
			}
			return R.customError("失败");
		} catch (Exception e) {
			return R.customError("失败");
		}

	}


	/**
	 * 根据岗位获取人员
	 * @param current
	 * @param size
	 * @param posId
	 * @return
	 */
	@GetMapping(value = "getPostUserList")
	public R getPostUserList(
		@RequestParam(value = "current", required = false) Integer current,
		@RequestParam(value = "size", required = false) Integer size,
		@RequestParam(value = "posId", required = false) String posId) {

		try {
			Object list = postUserService.getPostUserList(current,size,posId, request);
			if(null !=list){
				return R.ok().data(list);
			}
			return R.customError("失败");
		} catch (Exception e) {
			e.printStackTrace();
			return R.customError("失败");
		}

	}


	/**
	 * 更具人员获取岗位
	 * @param current
	 * @param size
	 * @param userId
	 * @return
	 */
	@GetMapping(value = "getUserPostList")
	public R getUserPostList(
		@RequestParam(value = "current", required = false) Integer current,
		@RequestParam(value = "size", required = false) Integer size,
		@RequestParam(value = "userId", required = false) String userId) {

		try {
			Object list = postUserService.getUserPostList(current,size,userId, request);
			if(null !=list){
				return R.ok().data(list);
			}
			return R.customError("失败");
		} catch (Exception e) {
			return R.customError("失败");
		}

	}



}