package com.dgtis.dorm.manager.admin.web;

import com.github.pagehelper.PageInfo;

import com.dgtis.dorm.manager.db.domain.AlumniUser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.dgtis.dorm.manager.admin.annotation.LoginAdmin;
import com.dgtis.dorm.manager.db.service.UserService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/admin/user")
public class UserController {
	private final Log logger = LogFactory.getLog(UserController.class);

	@Autowired
	private UserService userService;

	/**
	 * 查询非审核中的用户列表
	 * @param adminId
	 * @param alumniUser
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @return
	 */
	@GetMapping("/list")
	public Object list(@LoginAdmin Integer adminId,
			AlumniUser alumniUser,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit,
			String sort, String order){
		if(adminId == null){
			return ResponseUtil.fail401();
		}

		PageInfo<AlumniUser> pageInfo = userService.queryAlumniUser(alumniUser, page, limit, sort, order);
		Map<String, Object> data = new HashMap<>();
		data.put("total", pageInfo.getTotal());
		data.put("items", pageInfo.getList());

		return ResponseUtil.ok(data);
	}

	/**
	 * 筛选导出全部用户
	 * @param adminId
	 * @param alumniUser
	 * @return
	 */
	@GetMapping("/exportAllUser")
	public Object exportAllUser(@LoginAdmin Integer adminId,
					   AlumniUser alumniUser){
		if(adminId == null){
			return ResponseUtil.fail401();
		}

		List<AlumniUser> pageInfo = userService.allAlumniUser(alumniUser);
		Map<String, Object> data = new HashMap<>();
		data.put("items", pageInfo);

		return ResponseUtil.ok(data);
	}

	/**
	 *查询游客角色的用户列表
	 * @param adminId
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @return
	 */
	@GetMapping("/findYKList")
	public Object findYKList(@LoginAdmin Integer adminId,
							 @RequestParam(value = "page", defaultValue = "1") Integer page,
							 @RequestParam(value = "limit", defaultValue = "10") Integer limit,
							 String sort, String order){
		if(adminId == null){
			return ResponseUtil.fail401();
		}
		PageInfo<AlumniUser> pageInfo = userService.queryAlumniUserNotYK(page, limit, sort, order);
		Map<String, Object> data = new HashMap<>();
		data.put("total", pageInfo.getTotal());
		data.put("items", pageInfo.getList());
		return ResponseUtil.ok(data);
	}

	/**
	 * 查询待审核的用户列表
	 * @param adminId
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @return
	 */
	@RequestMapping(value = "searchAwaitAuditUserList")
	public Object searchAwaitAuditUserList(@LoginAdmin Integer adminId,
			String userName, String phone, String temRoleType,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit,
			String sort, String order) {
		if(adminId == null){
			return ResponseUtil.fail401();
		}
		AlumniUser example = new AlumniUser();

        if(!StringUtils.isEmpty(userName)){
            example.setUserName(userName);
        }
        if(!StringUtils.isEmpty(phone)){
			example.setPhone(phone);
		}

        example.setAuditStatus(1);
		PageInfo<AlumniUser> pageInfo = userService.querySelective(example, page, limit, sort, order);
		Map<String, Object> data = new HashMap<>();
		data.put("total", pageInfo.getTotal());
		data.put("items", pageInfo.getList());
		return ResponseUtil.ok(data);
	}

	/**
	 * 批量认证审核
	 * @param adminId
	 * @param auditStatus
	 * @param reason
	 * @param selectedIDs
	 * @return
	 */
	@RequestMapping("/passAudit")
    public Object passAudit(@LoginAdmin Integer adminId,
    		Integer auditStatus,
    		String reason,
    		String selectedIDs ) {
		if(adminId == null){
			return ResponseUtil.fail401();
		}
    	if(selectedIDs == null || auditStatus == null) {
    		return ResponseUtil.fail402();
    	}
    	String[] ids = selectedIDs.split(",");
    	 Map<String, Object> data = userService.passAudit(auditStatus, reason, ids);

		return ResponseUtil.ok(data);
	}

	/**
	 * @description 描述：单个认证审核
	 * @author      koucx
	 * @return
	 * @date        2019/4/24 0024 14:22
	 * @param       * @param null
	 */
	@RequestMapping(value = "auditUser")
	public Object auditUser(@LoginAdmin Integer adminId, @RequestBody AlumniUser user) {
		if(adminId == null){
			return ResponseUtil.fail401();
		}
    	if(user == null) {
    		return ResponseUtil.fail402();
    	}
		logger.debug(user);
		Map<String, Object> data = userService.auditUser(user);
        return ResponseUtil.ok(data);
	}

	/**
	 * @description 描述：用户详情
	 * @author      koucx
	 * @return
	 * @date        2019/4/24 0024 14:22
	 * @param       * @param null
	 */
	@RequestMapping("/detail")
	public Object detail(@RequestParam(value = "id")Integer id) {
		if(id == null){
			return ResponseUtil.fail402();
		}
		Map user = userService.findAlumniUserByUserId(id);
		Map<String, Object> data = new HashMap<>();
		data.put("items", user);

		return ResponseUtil.ok(data);
	}


	/**
	 * @description 描述：查询我关注的用户
	 * @author      koucx
	 * @return
	 * @date        2019/4/24 0024 14:26
	 * @param
	 */
	@RequestMapping("/findUserConcerned")
	public Object findUserConcerned(@RequestBody AlumniUser user,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer size) {
		if(user == null){
			return ResponseUtil.fail402();
		}
		Map<String, Object> data = userService.selectUserConcernedByUserId(user.getId(), page, size);
		return ResponseUtil.ok(data);
	}

	/**
	 * 查询我加入的圈子
	 * @param id
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping("findMyOrganize")
	public Object findMyOrganize(@RequestParam Integer id,
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer size) {
		if(id == null) {
			return ResponseUtil.fail402();
		}
		Map<String, Object> data = userService.findMyOrganize(id, page, size);
		return ResponseUtil.ok(data);
	}

}
