package com.zcloud.controller.system;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.service.system.FHlogService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * 说明：操作日志记录处理类
 * 作者：luoxiaobao
 * 官网：www.qdkjchina.com
 */
@Controller
@RequestMapping("/fhlog")
public class FHlogController extends BaseController {

	@Autowired
    private FHlogService fHlogService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private DepartmentService departmentService;

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	// @RequiresPermissions("fhlog:list")
	@ResponseBody
	public Object list(Page page) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String KEYWORDS = pd.getString("KEYWORDS");		//关键词检索条件
		if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
		String STARTTIME = pd.getString("STARTTIME");					//开始时间
		String ENDTIME = pd.getString("ENDTIME");						//结束时间
		if(Tools.notEmpty(STARTTIME))pd.put("STARTTIME", STARTTIME+" 00:00:00");
		if(Tools.notEmpty(ENDTIME))pd.put("ENDTIME", ENDTIME+" 00:00:00");
		page.setPd(pd);
		List<PageData>	varList = fHlogService.list(page);		//列出FHlog列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	/**删除
	 * @param out
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	// @RequiresPermissions("fhlog:del")
	@ResponseBody
	public Object delete() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		PageData pd = new PageData();
		pd = this.getPageData();
		fHlogService.delete(pd);
		map.put("result", "success");				//返回结果
		return map;
	}

	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	// @RequiresPermissions("fhlog:del")
	@ResponseBody
	public Object deleteAll() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String DATA_IDS = pd.getString("DATA_IDS");
		if(null != DATA_IDS && !"".equals(DATA_IDS)){
			String ArrayDATA_IDS[] = DATA_IDS.split(",");
			fHlogService.deleteAll(ArrayDATA_IDS);
		}else{
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/getList")
	// @RequiresPermissions("fhlog:list")
	@ResponseBody
	@LogAnno(menuType= "双重预控",menuServer= "离岗管理",instructionsOperate = "登录记录",instructionsType = "列表")
	public Object getList(Page page) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		PageData npd = new PageData();
		pd = this.getPageData();
		String KEYWORDS = pd.getString("KEYWORDS");		//关键词检索条件
		if(Tools.notEmpty(KEYWORDS))npd.put("KEYWORDS", KEYWORDS.trim());
		String STARTTIME = pd.getString("STARTTIME");					//开始时间
		String ENDTIME = pd.getString("ENDTIME");						//结束时间
		if(Tools.notEmpty(STARTTIME))npd.put("STARTTIME", STARTTIME+" 00:00:00");
		if(Tools.notEmpty(ENDTIME))npd.put("ENDTIME", ENDTIME+" 00:00:00");
		npd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		npd.put("ISMAIN", Jurisdiction.getIS_MAIN());
		npd.put("ISSUPERVISE", Jurisdiction.getISSUPERVISE());
		npd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
		String DEPTIDS = pd.getString("DEPTIDS");
		if(Tools.notEmpty(DEPTIDS)) {
			String DEPT_IDS[] = DEPTIDS.split(",");
			npd.put("DEPT_IDS", DEPT_IDS);
		}
		pd.put("USER_ID",Jurisdiction.getUSER_ID());
		PageData cpd = new PageData();
		cpd = usersService.findById(pd);
		npd.put("ISLEADER", cpd.getString("ISLEADER"));
		if(cpd.getString("ISLEADER") != null && cpd.getString("ISLEADER").equals("1")){
			String DEPARTMENT_ID = npd.getString("DEPARTMENT_ID");
			String ids = departmentService.getDEPARTMENT_IDS(DEPARTMENT_ID);
			ids=npd.getString("DEPARTMENT_ID")+","+ids;   //把自己部门插入进去
			if(ids!=null && Tools.notEmpty(ids)&& ids.lastIndexOf(",")>-1) {
				ids = ids.substring(0,ids.lastIndexOf(","));
				npd.put("DEPARTMENT_IDS", ids.split(","));
			}else {
				npd.put("DEPARTMENT_IDS", DEPARTMENT_ID);
			}
		}else {
			if(npd.getString("ISMAIN").equals("0")){
				npd.put("USER_ID",Jurisdiction.getUSER_ID());
			}
		}
		page.setPd(npd);
		List<PageData>	varList = fHlogService.getList(page);		//列出FHlog列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}
}
