/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.ejweb.modules.report.web;

import com.alibaba.fastjson.JSONObject;
import com.ejweb.core.base.BaseController;
import com.ejweb.core.persistence.Page;

import com.ejweb.core.utils.StringUtils;
import com.ejweb.modules.report.dao.CtBbtcTypeAreaDao;
import com.ejweb.modules.report.entity.CtBbtcBusType;
import com.ejweb.modules.report.entity.CtBbtcRegion;
import com.ejweb.modules.report.entity.CtBbtcTypeArea;
import com.ejweb.modules.report.entity.ReportEntity;
import com.ejweb.modules.report.service.CtBbtcBusTypeService;
import com.ejweb.modules.report.service.ReportService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.List;

/**
 * 业务类型Controller
 * @author haoqm
 * @version 2019-07-12
 */
@Controller
@RequestMapping(value = "${adminPath}/report/ctBbtcBusType")
public class CtBbtcBusTypeController extends BaseController {
	/**
	 * 管理基础路径
	 */
	@Value("${adminPath}")
	protected String adminPath;

	@Autowired
	private CtBbtcBusTypeService ctBbtcBusTypeService;

	@Autowired
	private CtBbtcTypeAreaDao ctBbtcTypeAreaDao;

	@Autowired
	private   ReportService reportService;
	
	@ModelAttribute
	public CtBbtcBusType get(@RequestParam(required=false) String id) {
		CtBbtcBusType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = ctBbtcBusTypeService.get(id);
		}
		if (entity == null){
			entity = new CtBbtcBusType();
		}
		return entity;
	}
	
	@RequiresPermissions("report:ctBbtcBusType:view")
	@RequestMapping(value = {"list", ""})
	public String list(CtBbtcBusType ctBbtcBusType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CtBbtcBusType> page = ctBbtcBusTypeService.findPage(new Page<CtBbtcBusType>(request, response), ctBbtcBusType); 
		model.addAttribute("page", page);
		return "modules/report/ctBbtcBusTypeList";
	}

	@RequiresPermissions("report:ctBbtcBusType:view")
	@RequestMapping(value = "form")
	public String form(CtBbtcBusType ctBbtcBusType, Model model) {
		/*跟据id查询typeArea表中parentid为id的数据行，动态回显*/
		CtBbtcTypeArea ctBbtcTypeArea0 =new CtBbtcTypeArea();
		ctBbtcTypeArea0.setParentId(ctBbtcBusType.getId());
		List<CtBbtcTypeArea> cloudAreaList = ctBbtcTypeAreaDao.getByParentId(ctBbtcTypeArea0);

		model.addAttribute("ctBbtcBusType", ctBbtcBusType);
		model.addAttribute("cloudAreaList", cloudAreaList);
		return "modules/report/ctBbtcBusTypeForm";
	}

	@RequiresPermissions("report:ctBbtcBusType:edit")
	@RequestMapping(value = "save")
	public String save(CtBbtcBusType ctBbtcBusType, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, ctBbtcBusType)){
			return form(ctBbtcBusType, model);
		}
		ctBbtcBusTypeService.save(ctBbtcBusType);
		addMessage(redirectAttributes, "保存业务类型成功");
		return "redirect:"+adminPath+"/report/ctBbtcBusType/?repage";
	}
	
	@RequiresPermissions("report:ctBbtcBusType:edit")
	@RequestMapping(value = "delete")
	public String delete(CtBbtcBusType ctBbtcBusType, RedirectAttributes redirectAttributes) {

		/*如果信息已在举报中存在  则不删除*/
		if(checkIsExist(ctBbtcBusType)){
			addMessage(redirectAttributes, "删除业务类型不成功，举报信息中已存在");
			return "redirect:"+adminPath+"/report/ctBbtcBusType/?repage";
		}

		ctBbtcBusTypeService.delete(ctBbtcBusType);
		addMessage(redirectAttributes, "删除业务类型成功");
		return "redirect:"+adminPath+"/report/ctBbtcBusType/?repage";
	}

	/**
	 * 校验  区域是否在举报信息中出现过
	 * @return true 出现过   false  未出现过
	 */
	public   boolean checkIsExist(CtBbtcBusType ctBbtcBusType){

		ReportEntity reportEntity=new ReportEntity();

		//如果id不为空    跟据id去查询列表
		if(StringUtils.isNotBlank(ctBbtcBusType.getId())){
			reportEntity.setSupplementType(ctBbtcBusType.getId());
			List<ReportEntity> reportList = reportService.getReportListByCheck(reportEntity);
			if(reportList.size()>0){
				return  true;
			}else{
				return  false;
			}
		}
		return false;
	}

	@RequestMapping(value = "getTypeList")
	public void getRegionList(String groupId, RedirectAttributes redirectAttributes,HttpServletResponse response,String area) {
		if (groupId != null && !"".equals(groupId) && area != null && !"".equals(area)) {
			JSONObject res = new JSONObject();
			List<CtBbtcTypeArea> ctBbtcTypeList = ctBbtcBusTypeService.getByArea(groupId, area);
			res.put("ctBbtcTypeList", ctBbtcTypeList);
			String jsonStr = res.toJSONString();
			try {
				PrintWriter out = response.getWriter();
				out.print(jsonStr);
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
}