package com.zhgd.xmgl.modules.basicdata.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.jeecg.common.system.query.QueryGenerator;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.entity.BaseModule;
import com.zhgd.xmgl.modules.basicdata.entity.ModuleTemplate;
import com.zhgd.xmgl.modules.basicdata.service.IBaseModuleService;
import com.zhgd.xmgl.modules.basicdata.service.IModuleTemplateService;
import com.zhgd.xmgl.util.ParamEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Title: Controller
 * @Description: 系统资源-模块表
 * @author： pds
 * @date：   2023-02-09
 * @version： V1.0
 */
@RestController
@RequestMapping("/xmgl/baseModule")
@Slf4j
@Api(tags = "系统模块")
public class BaseModuleController {
	@Autowired
	private IBaseModuleService baseModuleService;

	@Autowired
	private IModuleTemplateService moduleTemplateService;
	
	/**
	  * 分页列表查询
	 * @return
	 */
	@OperLog(operModul = "系统模块管理", operType = "分页查询", operDesc = "分页列表查询系统资源-模块表信息")
	@ApiOperation(value = " 分页列表查询系统资源-模块表信息", notes = "分页列表查询系统资源-模块表信息", httpMethod="POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "moduleType", value = "模块类型:2政务模块，3企业模块，4项目模块", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "styleType", value = "模块风格", paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
	})
	@PostMapping(value = "/page")
	public Result<IPage<BaseModule>> queryPageList(@RequestBody Map<String, Object> map) {
		QueryWrapper<BaseModule> queryWrapper = QueryGenerator.initPageQueryWrapper(BaseModule.class, map);
		Page page = PageUtil.getPage(map);
		IPage<BaseModule> pageList = baseModuleService.pageList(page, queryWrapper);
		return Result.success(pageList);
	}

	 /**
	  * 列表查询(按模块类型分组)
	  * @return
	  */
	 @OperLog(operModul = "系统模块管理", operType = "列表查询", operDesc = "列表查询(按模块类型分组)")
	 @ApiOperation(value = " 列表查询(按模块类型分组)", notes = "列表查询(按模块类型分组)", httpMethod="POST")
	 @PostMapping(value = "/listByType")
		 public Result<Map<String, Object>> queryListByType(@RequestBody BaseModule baseModule, HttpServletRequest req) {
		 Result<Map<String, Object>> result = new Result<Map<String, Object>>();
		 QueryWrapper<BaseModule> queryWrapper = QueryGenerator.initQueryWrapper(baseModule, req.getParameterMap());
		 queryWrapper.orderByDesc("is_important_module");
		 queryWrapper.orderByAsc("label_sort_num", "priority");
		 List<BaseModule> list = baseModuleService.list(queryWrapper);
		 Map<String, Object> map = new HashMap<>();
		 map.put("government", list.stream().filter(m -> m.getModuleType() == 2).collect(Collectors.toList()));
		 map.put("enterprise", list.stream().filter(m -> m.getModuleType() == 3).collect(Collectors.toList()));
		 map.put("project", list.stream().filter(m -> m.getModuleType() == 4).collect(Collectors.toList()));
		 result.setSuccess(true);
		 result.setResult(map);
		 return result;
	 }

	
	/**
	  *   添加
	 * @param baseModule
	 * @return
	 */
	@OperLog(operModul = "系统模块管理", operType = "新增", operDesc = "添加系统资源-模块表信息")
	@ApiOperation(value = " 添加系统资源-模块表信息", notes = "添加系统资源-模块表信息" , httpMethod="POST")
	@PostMapping(value = "/add")
	public Result<BaseModule> add(@RequestBody BaseModule baseModule) {
		Result<BaseModule> result = new Result<BaseModule>();
		baseModuleService.save(baseModule);
		result.success("添加成功！");
		return result;
	}
	
	/**
	  *  编辑
	 * @param baseModule
	 * @return
	 */
	@OperLog(operModul = "系统模块管理", operType = "编辑", operDesc = "编辑系统资源-模块表信息")
	@ApiOperation(value = "编辑系统资源-模块表信息", notes = "编辑系统资源-模块表信息" , httpMethod="POST")
	@PostMapping(value = "/edit")
	public Result<BaseModule> edit(@RequestBody BaseModule baseModule) {
		Result<BaseModule> result = new Result<BaseModule>();
		BaseModule baseModuleEntity = baseModuleService.getById(baseModule.getModuleId());
		if(baseModuleEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = baseModuleService.updateById(baseModule);
			if(ok) {
				result.success("修改成功!");
			}
		}
		
		return result;
	}
	
	/**
	  * 通过id删除
	 * @return
	 */
	@OperLog(operModul = "系统模块管理", operType = "删除", operDesc = "删除系统资源-模块表信息")
	@ApiOperation(value = "删除系统资源-模块表信息", notes = "删除系统资源-模块表信息" , httpMethod="POST")
	@ApiImplicitParam(name = "id", value = "系统资源-模块表ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/delete")
	public Result<BaseModule> delete(@RequestBody Map<String,Object> map) {
		Result<BaseModule> result = new Result<BaseModule>();
		BaseModule baseModule = baseModuleService.getById(MapUtils.getString(map,"id"));
		if(baseModule==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = baseModuleService.removeInfo(MapUtils.getString(map,"id"));
			if(ok) {
				result.success("删除成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *  批量删除
	 * @return
	 */
	@OperLog(operModul = "系统模块管理", operType = "批量删除", operDesc = "批量删除系统资源-模块表信息")
	@ApiOperation(value = "批量删除系统资源-模块表信息", notes = "批量删除系统资源-模块表信息",  httpMethod="POST")
    @ApiImplicitParam(name = "ids", value = "系统资源-模块表ID字符串", paramType = "query", required = true, dataType = "String")
	@PostMapping(value = "/deleteBatch")
	public Result<BaseModule> deleteBatch(@RequestBody Map<String,Object> map) {
		 String ids = MapUtils.getString(map,"ids");
		Result<BaseModule> result = new Result<BaseModule>();
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.baseModuleService.removeByIds(Arrays.asList(ids.split(",")));
			result.success("删除成功!");
		}
		return result;
	}
	
	/**
	  * 通过id查询
	 * @return
	 */
	@OperLog(operModul = "系统模块管理", operType = "查询", operDesc = "通过id查询系统资源-模块表信息")
	@ApiOperation(value = "通过id查询系统资源-模块表信息", notes = "通过id查询系统资源-模块表信息" , httpMethod="POST")
    @ApiImplicitParam(name = "id", value = "系统资源-模块表ID", paramType = "query", required = true, dataType = "Integer")
	@PostMapping(value = "/queryById")
	public Result<BaseModule> queryById(@RequestBody Map<String,Object> map) {
		Result<BaseModule> result = new Result<BaseModule>();
		BaseModule baseModule = baseModuleService.getById(MapUtils.getString(map,"id"));
		if(baseModule==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(baseModule);
			result.setSuccess(true);
		}
		return result;
	}

	 /**
	  * 通过登录用户查询
	  * @return
	  */
	 @OperLog(operModul = "系统模块管理", operType = "查询", operDesc = "用户获取相对应的模块信息")
	 @ApiOperation(value = "当前用户获取相对应的模块信息", notes = "当前用户获取相对应的模块信息" , httpMethod="GET")
	 @GetMapping(value = "/queryBySelf")
	 public Result<Map<String, List<BaseModule>>> queryBySelf() {
		 List<BaseModule> list = baseModuleService.queryBySelf();
		 Map<String, List<BaseModule>> map = new HashMap<>();
		 map.put("important", list.stream().filter(m -> m.getIsImportantModule().equals("1")).collect(Collectors.toList()));
		 map.put("module1", list.stream().filter(m -> m.getLabelName().equals(ParamEnum.ModuleLabel.SAFE.getDesc())).collect(Collectors.toList()));
		 map.put("module2", list.stream().filter(m -> m.getLabelName().equals(ParamEnum.ModuleLabel.QUALITY.getDesc())).collect(Collectors.toList()));
		 map.put("module3", list.stream().filter(m -> m.getLabelName().equals(ParamEnum.ModuleLabel.WISDOM.getDesc())).collect(Collectors.toList()));
		 return Result.success(map);
	 }

	/**
	 * 查询政务下的模块模板信息
	 * @param moduleTemplate
	 * @param req
	 * @return
	 */
	@OperLog(operModul = "政务模块模板管理", operType = "查询", operDesc = "查询政务下的模块模板信息")
	@ApiOperation(value = " 查询政务下的模块模板信息", notes = "查询政务下的模块模板信息", httpMethod="POST")
	@PostMapping(value = "/listByGovernment")
	public Result<List<Long>> listByGovernment(@RequestBody ModuleTemplate moduleTemplate,
											   HttpServletRequest req) {
		// TODO  查询政务下的模块模板,包含模块类型以及模块名称、ID信息
		Result<List<Long>> result = new Result<List<Long>>();
		QueryWrapper<ModuleTemplate> queryWrapper = QueryGenerator.initQueryWrapper(moduleTemplate, req.getParameterMap());
		List<Long> moduleIds = moduleTemplateService.list(queryWrapper).stream()
				.map(m -> m.getModuleId()).collect(Collectors.toList());
		result.setSuccess(true);
		result.setResult(moduleIds);
		return result;
	}
}
