package com.jeesite.modules.consult.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.consult.entity.ConsultHelp;
import com.jeesite.modules.consult.entity.ConsultHelpSlave;
import com.jeesite.modules.consult.service.ConsultHelpService;
import com.jeesite.modules.consult.service.ConsultHelpSlaveService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 咨询帮助 主表Controller
 * @author wxm
 * @version 2022-06-07
 */
@Controller
@RequestMapping(value = "${adminPath}/consult/consultHelp")
public class ConsultHelpController extends BaseController {

	private String prefix = "modules/consult";

	@Autowired
	private ConsultHelpService consultHelpService;
	@Autowired
	private ConsultHelpSlaveService consultHelpSlaveService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ConsultHelp get(String id, boolean isNewRecord) {
		return consultHelpService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("consult:consultHelp:view")
	@RequestMapping(value = {"list", ""})
	public String list(ConsultHelp consultHelp, Model model) {
		System.out.println("数量为："+consultHelpService.test());
		model.addAttribute("consultHelp", consultHelp);
		return prefix + "/consultHelpList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("consult:consultHelp:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ConsultHelp> listData(ConsultHelp consultHelp, HttpServletRequest request, HttpServletResponse response) {
		consultHelp.setPage(new Page<>(request, response));
		Page<ConsultHelp> page = consultHelpService.findPage(consultHelp);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("consult:consultHelp:view")
	@RequestMapping(value = "form")
	public String form(ConsultHelp consultHelp, Model model) {
		model.addAttribute("consultHelp", consultHelp);
		return prefix + "/consultHelpForm";
	}

	@RequiresPermissions("consult:consultHelp:view")
	@RequestMapping(value = "form2")
	public String addConsult(ConsultHelp consultHelp, Model model) {
		model.addAttribute("consultHelp", consultHelp);
		return prefix + "/addConsult";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("consult:consultHelp:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ConsultHelp consultHelp) {
		consultHelp.setUserId(UserUtils.getUser().getUserCode());
		consultHelp.setUserName(UserUtils.getUser().getUserName());
		consultHelp.setCreateTime(new Date());
		consultHelpService.save(consultHelp);
		return renderResult(Global.TRUE, text("保存咨询帮助成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("consult:consultHelp:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ConsultHelp consultHelp) {
		consultHelpService.delete(consultHelp);
		return renderResult(Global.TRUE, text("删除咨询帮助成功！"));
	}

	/**
	 * 查询所有回复我的数据集合
	 * @return
	 */
	@PostMapping(value = "/allDataList")
	@ResponseBody
	public Page<ConsultHelp> getAllDataList(@RequestParam(value = "searchType",required = false) String searchType, ConsultHelp consultHelp, HttpServletRequest request, HttpServletResponse response) {
		consultHelp.setPage(new Page<>(request, response));
		if(searchType != null && !searchType.equals("全部")){
			//todo 默认查询全部,如果不是全部则查询自己提交过的咨询帮助数据
			consultHelp.setUserId(UserUtils.getUser().getUserCode());
		}
		//具备分页信息的咨询帮助
		Page<ConsultHelp> page = consultHelpService.findPage(consultHelp);
		List<ConsultHelp> list = page.getList();
		List<ConsultHelpSlave> consultHelpSlaves = consultHelpSlaveService.findList(new ConsultHelpSlave());
		if(!CollectionUtils.isEmpty(consultHelpSlaves)){
			Map<String, List<ConsultHelpSlave>> slaveMap = consultHelpSlaves.stream().collect(Collectors.groupingBy(e -> e.getConsultId()));
			if(slaveMap != null && slaveMap.size() > 0){
				for (ConsultHelp entity : list) {
					for (String key : slaveMap.keySet()) {
						if(key.equals(entity.getId())){
							entity.setConsultHelpSlaves(slaveMap.get(key));
						}
					}
				}
			}
		}
		page.setList(list);

		return page;
	}
	
}