package org.linlinjava.jz.admin.web;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;

import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.jz.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.jz.admin.service.AdminBuiltListService;
import org.linlinjava.jz.admin.service.AdminSkillGroupService;
import org.linlinjava.jz.admin.vo.AssignTaskDto;
import org.linlinjava.jz.admin.vo.SkillGroupVo;
import org.linlinjava.jz.core.util.ResponseUtil;
import org.linlinjava.jz.core.validator.Order;
import org.linlinjava.jz.core.validator.Sort;
import org.linlinjava.jz.db.domain.SysUser;
import org.linlinjava.jz.db.service.JzSysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "Admin-builtRecovery-Controller", description = "任务回收分配")
@RestController
@RequestMapping("/admin/builtRecovery")
@Validated
public class AdminBuiltRecoveryController {

	private final Log logger = LogFactory.getLog(AdminBuiltRecoveryController.class);


	@Autowired
	private JzSysUserService adminService;

	@Autowired
	private AdminSkillGroupService adminSkillGroupService;

	
	@Autowired
	AdminBuiltListService adminBuiltListService;

	@RequiresPermissions("admin:builtRecovery:list")
	@RequiresPermissionsDesc(menu = { "电访中心", "任务回收分配" }, button = "查询离职任务数量")
	@GetMapping("/list")
	public Object list(@RequestParam(required = false) String username,
			@RequestParam(required = false) String userAccount, 
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "10") Integer limit,
			@Sort @RequestParam(defaultValue = "create_time") String sort,
			@Order @RequestParam(defaultValue = "desc") String order) {

		List<SysUser> adminList = adminService.querySelectQuit(username, userAccount, page, limit, sort, order);

		return ResponseUtil.okList(adminList);
	}

	@RequiresPermissions("admin:builtRecovery:getDepUser")
	@RequiresPermissionsDesc(menu = { "电访中心", "任务回收分配" }, button = "获取部门用户")
	@GetMapping("/getDepUser")
	public Object getDepUser() {
		// 获取系统所有操作用户信息，二级结构：部门-》成员
		List<SkillGroupVo> allUsers = adminSkillGroupService.getSysUser();

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("allUsers", allUsers);
		return ResponseUtil.ok(data);
	}

	@RequiresPermissions("admin:builtRecovery:assignTask")
	@RequiresPermissionsDesc(menu = { "电访中心", "任务回收分配" }, button = "回收分配任务操作")
	@PostMapping("/assignTask")
	public Object assignTasks(@RequestBody AssignTaskDto assignTaskDto) {

		 if(null!=assignTaskDto.getBuiltTaskList()&&assignTaskDto.getBuiltTaskList().size()>0) {
			 
			return adminBuiltListService.assignTasks(assignTaskDto);
		 }
		
		return ResponseUtil.fail();
	}

}
