package com.zcloud.controller.notice;

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.notice.NoticeCorpService;
import com.zcloud.service.notice.NoticeCorpUserService;
import com.zcloud.service.notice.ServiceNoticeService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Tools;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：通知公告
 * 作者：luoxiaobao
 * 官网：www.qdkjchina.com
 */
@Controller
@RequestMapping("/app/notice")
public class AppServiceNoticeController extends BaseController {

	@Autowired
	private ServiceNoticeService noticeService;
	@Autowired
	private NoticeCorpService noticecorpService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private NoticeCorpUserService noticecorpuseridService;
	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "通知公告",instructionsOperate = "通知公告",instructionsType = "列表")
	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();
		//获取发布给这个企业的信息
		pd.put("CORP_INFO_ID", pd.getString("corpinfoId"));
		page.setPd(pd);
		List<PageData>	varList = noticeService.getNotAndCorpByCorpId(page);

		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}


	 /**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "通知公告",instructionsOperate = "通知公告",instructionsType = "去修改页面获取数据")
	public Object goEdit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		PageData pd2 = new PageData();
		pd2.put("USER_ID", pd.getString("loginUserId"));
		pd.put("NOTICE_ID",pd.getString("NOTICECORPUSERID_ID"));
		pd = noticeService.findById(pd);	//根据ID读取


		PageData userPd = usersService.findById(pd2);
		if(!Tools.isEmpty(userPd.get("ISMAIN"))) {
			if("1".equals(userPd.get("ISMAIN"))) {
				PageData pdCorp = new PageData();
				pdCorp.put("TYPE", "1");
				pdCorp.put("NOTICE_ID", pd.getString("NOTICE_ID"));
				pdCorp.put("CREATOR", DateUtil.date2Str(new Date()));
				pdCorp.put("CORP_INFO_ID", userPd.getString("CORPINFO_ID"));
				noticeService.editCorpInfo(pdCorp);
			}
		}
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}
	/** 企业公告信息
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/listForCp")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "通知公告",instructionsOperate = "通知公告",instructionsType = "企业公告信息")
	public Object listForCp(Page page) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		/*
		 * 1.获取关系信息
		 * 2.根据关系信息找到公告信息
		 */
		pd.put("USER_ID", pd.getString("loginUserId"));
		page.setPd(pd);
		List<PageData> varList = noticecorpService.listByUserId(page);
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}
	 /**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEditForPc")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "通知公告",instructionsOperate = "通知公告",instructionsType = "去修改页面获取数据")
	public Object goEditForPc() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("NOTICECORPUSERID_ID", pd.getString("NOTICECORPUSERID_ID"));
		PageData valPd = noticecorpService.findByUserId(pd);	//根据ID读取
		//修改已读状态
		PageData pd2 = new PageData();//NOTICECORP_ID
		pd2.put("NOTICECORPUSERID_ID", pd.getString("NOTICECORPUSERID_ID"));
		pd2.put("USER_ID", pd.getString("loginUserId"));
		pd2.put("TYPE", "1");
		pd2.put("CREATOR", DateUtil.date2Str(new Date()));
		noticecorpuseridService.edit(pd2);

		map.put("pd", valPd);
		map.put("result", errInfo);
		return map;
	}

	/**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "通知公告",instructionsOperate = "通知公告",instructionsType = "删除通知")
	public Object delete() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		noticecorpuseridService.delete(pd);

		map.put("result", errInfo);
		return map;
	}

	/** 企业公告信息
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/getRedPoint")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "通知公告",instructionsOperate = "通知公告",instructionsType = "企业公告信息")
	public Object getRedPoint() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		int count1 = noticecorpService.getRedPoint(pd);
		//int count2 = noticeService.getRedPoint(pd);
		map.put("count", count1);
		map.put("result", errInfo);
		return map;
	}
}
