package com.ibeetl.admin.console.web;

import com.ibeetl.admin.console.service.OrgConsoleService;
import com.ibeetl.admin.console.service.RoleConsoleService;
import com.ibeetl.admin.console.service.UserConsoleService;
import com.ibeetl.admin.console.web.dto.UserExcelExportData;
import com.ibeetl.admin.console.web.query.UserQuery;
import com.ibeetl.admin.console.web.query.UserRoleQuery;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.annotation.LogController;
import com.ibeetl.admin.core.annotation.Query;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.entity.CoreUserRole;
import com.ibeetl.admin.core.file.FileItem;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.*;
import com.ibeetl.admin.core.util.enums.GeneralStateEnum;
import com.ibeetl.admin.core.web.JsonResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户管理接口
 * 
 * @author xiandafu
 */
@Controller
public class UserConsoleController {
	private final Log log = LogFactory.getLog(this.getClass());
	private static final String MODEL = "/GAA/user";

	@Autowired
	UserConsoleService userConsoleService;

	@Autowired
	CorePlatformService platformService;

	@Autowired
	RoleConsoleService roleConsoleService;
	@Autowired
	OrgConsoleService orgConsoleService;
	@Autowired
	FileService fileService;
	@Value("${admin.encodeKey}")
	private String encodeKey;
   

	/* 页面 */

	@GetMapping(MODEL + "/index.do")
	@Function("user")
	public ModelAndView index() {
		ModelAndView view = new ModelAndView("/admin/user/index.html");
		view.addObject("search", UserQuery.class.getName());
		return view;
	}

	@GetMapping(MODEL + "/edit.do")
	@Function("user.edit")
	public ModelAndView edit(String id) {
		ModelAndView view = new ModelAndView("/admin/user/edit.html");
		CoreUser user = userConsoleService.queryById(id);
		view.addObject("user", user);
		return view;
	}

	@GetMapping(MODEL + "/add.do")
	@Function("user.add")
	public ModelAndView add() {
		ModelAndView view = new ModelAndView("/admin/user/add.html");
		return view;
	}

	@GetMapping(MODEL + "/changePassword.do")
	@Function("user.reset")
	public ModelAndView changePassword(Long id) {
		CoreUser user = userConsoleService.queryById(id);
		ModelAndView view = new ModelAndView("/admin/user/changePwd.html");
		view.addObject("user", user);
		return view;
	}

	@GetMapping(MODEL + "/role/list.do")
	@Function("user.role")
	public ModelAndView userRoleIndex(Long id) {
		CoreUser user = userConsoleService.queryById(id);
		ModelAndView view = new ModelAndView("/admin/user/userRole.html");
		view.addObject("search", UserRoleQuery.class.getName());
		view.addObject("user", user);
		return view;
	}

	@GetMapping(MODEL + "/role/add.do")
	@Function("user.role")
	@LogController(content="新增用户角色",type="1",description="")
	public ModelAndView userRoleAdd(Long id) {
		CoreUser user = userConsoleService.queryById(id);
		ModelAndView view = new ModelAndView("/admin/user/userRoleAdd.html");
		view.addObject("user", user);
		return view;
	}

	/* Json */

	@PostMapping(MODEL + "/delete.json")
	@Function("user.delete")
	@ResponseBody
	@LogController(content="删除用户",type="1",description="")
	public JsonResult delete(String ids) {
		List<Long> dels = ConvertUtil.str2longs(ids);
		userConsoleService.batchDelSysUser(dels);
		return JsonResult.success();
	}

	@PostMapping(MODEL + "/update.json")
	@Function("user.edit")
	@ResponseBody
	@LogController(content="更新用户",type="1",description="")
	public JsonResult update(@Validated(ValidateConfig.UPDATE.class) CoreUser user) {
		System.out.println("user = " + user);
		boolean success = userConsoleService.updateTemplate(user);
		if (success) {
			this.platformService.clearFunctionCache();
			return JsonResult.success();
		} else {
			return JsonResult.failMessage("保存失败！");
		}
	}

	@PostMapping(MODEL + "/add.json")
	@Function("user.add")
	@ResponseBody
	public JsonResult<Long> add(@Validated(ValidateConfig.ADD.class) CoreUser user) {
		if (!platformService.isAllowUserName(user.getCode())) {
			return JsonResult.failMessage("不允许的注册名字 " + user.getCode());
		}
		try {
			user.setPassword(DesUtilTo32.encode(user.getPassword(), encodeKey));
			user.setCreateTime(new Date());
			System.out.println("user = + " + user);
			userConsoleService.saveUser(user);
			return JsonResult.success(user.getId());

		}catch (Exception e){
			e.printStackTrace();
			return JsonResult.fail();
		}

	}

	@PostMapping(MODEL + "/view.json")
	@ResponseBody
	@Function("user.query")
	public JsonResult<CoreUser> view(Long id) {
		CoreUser user = userConsoleService.queryById(id);
		return JsonResult.success(user);
	}

	@PostMapping(MODEL + "/list.json")
	@Function("user.query")
	@ResponseBody
	public JsonResult<PageQuery<CoreUser>> index(UserQuery condtion) {

		PageQuery<CoreUser> page = condtion.getPageQuery();
		userConsoleService.queryByCondtion(page);
		return JsonResult.success(page);
	}

	@PostMapping(MODEL + "/list/condition.json")
	@Function("user.query")
	@ResponseBody
	public JsonResult<List<Map<String, Object>>> indexCondtion() {
		List<Map<String, Object>> list = AnnotationUtil.getInstance().getAnnotations(Query.class, UserQuery.class);
		return JsonResult.success(list);
	}

	@PostMapping(MODEL + "/disable.json")
	@Function("user.disable")
	@ResponseBody
	@LogController(content="禁用用户",type="1",description="")
	public JsonResult disableUser(String ids) {

		List<Long> dels = ConvertUtil.str2longs(ids);

		userConsoleService.batchUpdateUserState(dels, GeneralStateEnum.DISABLE);
		for (Long id : dels) {
			CoreUser user = userConsoleService.queryById(id);
			this.platformService.restUserSession(user.getCode());
		}
		return JsonResult.success();

	}

	/**
	 * 启用用户操作
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/enable.json")
	@Function("user.enable")
	@ResponseBody
	@LogController(content="启用用户",type="1",description="")
	public JsonResult enableUser(String ids) {

		List<Long> enables = ConvertUtil.str2longs(ids);
		userConsoleService.batchUpdateUserState(enables, GeneralStateEnum.ENABLE);
		return JsonResult.success();

	}

	/**
	 * 用户个人修改密码
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/changePassword.json")
//	@Function("user.reset")
	@ResponseBody
	@LogController(content="重置密码",type="1",description="")
	public JsonResult changePassword(Long id, String password,String oldPassword,String password2) {
		try{
			password = DesUtilTo32.encode(DesUtilTo32.decode(password, DateUtil.date2String(new Date(), "yyyyMMdd")),encodeKey);
			//oldPassword = DesUtilTo32.encode(DesUtilTo32.decode(oldPassword, DateUtil.date2String(new Date(), "yyyyMMdd")),encodeKey);
			password2 = DesUtilTo32.encode(DesUtilTo32.decode(password2, DateUtil.date2String(new Date(), "yyyyMMdd")),encodeKey);
			//根据id查询对应的旧密码
			CoreUser temp = platformService.getCurrentUser();
			CoreUser realUser = userConsoleService.queryUserById(temp.getId());
			//判断旧密码与页面的旧密码是否一致
			if(!realUser.getPassword().equals(oldPassword)){
				return JsonResult.failMessage("旧密码不对");
			}else if (!password.equals(password2)) {
				return JsonResult.failMessage("新密码两次输入不一致");
			}else if (oldPassword.equals(password2)) {
				return JsonResult.failMessage("新密码不能与原密码相同");
			}else if(userConsoleService.ifPwdIdentical(realUser.getCode(),password)){
				return JsonResult.failMessage("新密码与前5次密码不能相同");
			} else{
				int i = userConsoleService.resetPassword(id,oldPassword, password,password2,realUser.getCode());
				return  JsonResult.success(i);
			}

		}catch (Exception e){
			e.printStackTrace();
			return JsonResult.failMessage(e.getMessage());
		}

		
		
		
	}
	


	/**
	 * 用户所有授权角色列表
	 * 
	 *            用户id
	 * @return
	 */
	@PostMapping(MODEL + "/role/list.json")
	@Function("user.role")
	@ResponseBody
	public JsonResult<List<CoreUserRole>> getRoleList(UserRoleQuery roleQuery) {
		List<CoreUserRole> list = userConsoleService.getUserRoles(roleQuery);
		return JsonResult.success(list);
	}

	/**
	 * 用户添加授权角色页
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/role/add.json")
	@Function("user.role")
	@ResponseBody
	@LogController(content="用户授权",type="1",description="")
	public JsonResult saveUserRole(@Validated CoreUserRole userRole) {
		userRole.setCreateTime(new Date());
		this.userConsoleService.saveUserRole(userRole);
		this.platformService.clearFunctionCache();
		return JsonResult.success(userRole.getId());

	}

	/**
	 * 删除用户角色授权
	 * 
	 * @return
	 */
	@PostMapping(MODEL + "/role/delete.json")
	@Function("user.role")
	@ResponseBody
	@LogController(content="删除用户",type="1",description="")
	public JsonResult delUserRole(String ids) {
		List<Long> dels = ConvertUtil.str2longs(ids);

		userConsoleService.deleteUserRoles(dels);
		this.platformService.clearFunctionCache();
		return JsonResult.success();
	}
	
	
	@SuppressWarnings("unchecked")
	@PostMapping(MODEL + "/excel/export.json")
	@Function("user.export")
	@ResponseBody
	@LogController(content="导出用户",type="1",description="")
	public JsonResult<String> export(HttpServletResponse response,UserQuery condtion) {
		String excelTemplate ="excelTemplates/admin/user/user_collection_template.xls";
		PageQuery<CoreUser> page = condtion.getPageQuery();
		//取出全部符合条件的
		page.setPageSize(Integer.MAX_VALUE);
		page.setPageNumber(1);
		page.setTotalRow(Integer.MAX_VALUE);
		List<UserExcelExportData> users =userConsoleService.queryExcel(page);
		try(InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(excelTemplate)) {
	        if(is==null) {
	        	throw new PlatformException("模板资源不存在："+excelTemplate);
	        }
	        FileItem item = fileService.createFileTemp("user_collection.xls");
	        OutputStream os = item.openOutpuStream();
	        Context context = new Context();
            context.putVar("users", users);
            JxlsHelper.getInstance().processTemplate(is, os, context);
            //下载参考FileSystemContorller
            return  JsonResult.success(item.getPath());
	    } catch (IOException e) {
			throw new PlatformException(e.getMessage());
		}
		
	}

}
