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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.jeecg.common.system.query.QueryGenerator;
import com.zhgd.xmgl.modules.basicdata.entity.SystemDept;
import com.zhgd.xmgl.modules.basicdata.service.ISystemDeptService;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


 /**
 * @Title: Controller
 * @Description: 政府内部部门
 * @author： pds
 * @date：   2023-02-15
 * @version： V1.0
 */
@RestController
@RequestMapping("/xmgl/systemDept")
@Slf4j
@Api(tags = "政府内部部门信息")
public class SystemDeptController {
	@Autowired
	private ISystemDeptService systemDeptService;

	 /**
	  * 列表查询
	  * @param systemDept
	  * @param req
	  * @return
	  */
	 @OperLog(operModul = "部门配置", operType = "列表查询", operDesc = "列表查询政府内部部门信息")
	 @ApiOperation(value = " 列表查询政府内部部门信息", notes = "列表查询政府内部部门信息", httpMethod="POST")
	 @PostMapping(value = "/list")
	 public Result<List<SystemDept>> queryList(@RequestBody SystemDept systemDept,
													HttpServletRequest req) {
		 Result<List<SystemDept>> result = new Result<List<SystemDept>>();
		 SecurityUser user = SecurityUtil.getUser();
		 systemDept.setType(user.getAccountType());
		 systemDept.setSn(user.getSn());
		 QueryWrapper<SystemDept> queryWrapper = QueryGenerator.initQueryWrapper(systemDept, req.getParameterMap());
		 List<SystemDept> list = systemDeptService.list(queryWrapper);
		 result.setSuccess(true);
		 result.setResult(list);
		 return result;
	 }

	 /**
	  * 层级查询
	  * @return
	  */
	 @OperLog(operModul = "部门配置", operType = "层级查询", operDesc = "层级查询政府内部部门信息")
	 @ApiOperation(value = " 层级查询政府内部部门信息", notes = "层级查询政府内部部门信息", httpMethod="POST")
	 @PostMapping(value = "/tree")
	 public Result<List<SystemDept>> queryTreeList() {
		 Result<List<SystemDept>> result = new Result<List<SystemDept>>();
		 List<SystemDept> tree = systemDeptService.tree(0);
		 return Result.success(tree);
	 }
	
	/**
	  *   添加
	 * @param systemDept
	 * @return
	 */
	@OperLog(operModul = "部门配置", operType = "新增", operDesc = "添加政府内部部门信息")
	@ApiOperation(value = " 添加政府内部部门信息", notes = "添加政府内部部门信息" , httpMethod="POST")
	@PostMapping(value = "/add")
	public Result<SystemDept> add(@RequestBody SystemDept systemDept) {
		Result<SystemDept> result = new Result<SystemDept>();
		SecurityUser user = SecurityUtil.getUser();
		systemDept.setType(user.getAccountType());
		systemDept.setSn(user.getSn());
		systemDeptService.save(systemDept);
		result.success("添加成功！");
		return result;
	}
	
	/**
	  *  编辑
	 * @param systemDept
	 * @return
	 */
	@OperLog(operModul = "部门配置", operType = "修改", operDesc = "编辑政府内部部门信息")
	@ApiOperation(value = "编辑政府内部部门信息", notes = "编辑政府内部部门信息" , httpMethod="POST")
	@PostMapping(value = "/edit")
	public Result<SystemDept> edit(@RequestBody SystemDept systemDept) {
		Result<SystemDept> result = new Result<SystemDept>();
		SystemDept systemDeptEntity = systemDeptService.getById(systemDept.getDeptId());
		if(systemDeptEntity==null) {
			result.error500("未找到对应实体");
		}else {
			SecurityUser user = SecurityUtil.getUser();
			systemDept.setType(user.getAccountType());
			systemDept.setSn(user.getSn());
			boolean ok = systemDeptService.updateById(systemDept);
			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<SystemDept> delete(@RequestBody Map<String,Object> map) {
		Result<SystemDept> result = new Result<SystemDept>();
		SystemDept systemDept = systemDeptService.getById(MapUtils.getString(map,"id"));
		if(systemDept==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = systemDeptService.removeById(MapUtils.getString(map,"id"));
			if(ok) {
				result.success("删除成功!");
			}
		}
		
		return result;
	}
	
	/**
	  *  批量删除
	 * @return
	 */
	@OperLog(operModul = "部门配置", operType = "批量删除", operDesc = "批量删除政府内部部门信息")
	@ApiOperation(value = "批量删除政府内部部门信息", notes = "批量删除政府内部部门信息",  httpMethod="POST")
    @ApiImplicitParam(name = "id", value = "政府内部部门ID字符串", paramType = "query", required = true, dataType = "String")
	@PostMapping(value = "/deleteBatch")
	public Result<SystemDept> deleteBatch(@RequestBody Map<String,Object> map) {
		Result<SystemDept> result = new Result<SystemDept>();
		String ids = MapUtils.getString(map,"ids");
		if(ids==null || "".equals(ids.trim())) {
			result.error500("参数不识别！");
		}else {
			this.systemDeptService.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<SystemDept> queryById(@RequestBody Map<String,Object> map) {
		Result<SystemDept> result = new Result<SystemDept>();
		SystemDept systemDept = systemDeptService.getById(MapUtils.getString(map,"id"));
		if(systemDept==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(systemDept);
			result.setSuccess(true);
		}
		return result;
	}
}
