/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.JlrhJws.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.JlrhJws.entity.JlrhJwsEntity;
import org.springblade.modules.JlrhJws.excel.JlrhJwsExcel;
import org.springblade.modules.JlrhJws.service.IJlrhJwsService;
import org.springblade.modules.JlrhJws.vo.JlrhJwsVO;
import org.springblade.modules.JlrhJws.vo.JlrhJwsVOS;
import org.springblade.modules.JlrhJws.wrapper.JlrhJwsWrapper;
import org.springblade.modules.csInfo.entity.CsInfoEntity;
import org.springblade.modules.csInfo.service.ICsInfoService;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 警务室 控制器
 *
 * @author BladeX
 * @since 2023-07-07
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-JlrhJws/JlrhJws")
@Api(value = "警务室", tags = "警务室接口")
public class JlrhJwsController extends BladeController {

	private final IJlrhJwsService JlrhJwsService;

	private final IDeptService deptService;

	private final ICsInfoService csInfoService;

	/**
	 * 警务室 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入JlrhJws")
	public R<JlrhJwsVO> detail(Long id) {
		JlrhJwsVO data = JlrhJwsService.getDetail(id);
		return R.data(data);
	}
	/**
	 * 警务室 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入JlrhJws")
	public R<IPage<JlrhJwsVO>> list(@ApiIgnore @RequestParam Map<String, Object> JlrhJws, Query query) {
		IPage<JlrhJwsEntity> pages = JlrhJwsService.page(Condition.getPage(query), Condition.getQueryWrapper(JlrhJws, JlrhJwsEntity.class));
		return R.data(JlrhJwsWrapper.build().pageVO(pages));
	}

	/**
	 * 警务室 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入JlrhJws")
	public R<IPage<JlrhJwsVO>> page(@RequestParam Map<String, Object> params, Query query) {
		params.put("userId", AuthUtil.getUserId());
		params.put("deptId", AuthUtil.getDeptId());
		IPage<JlrhJwsVO> pages = JlrhJwsService.selectJlrhJwsPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 警务室采集字典-app端
	 */
	@GetMapping("/getJwsDept")
	public R<List<Dept>> getJwsDept(String parentId, Integer deptCategory) {
		Dept byId = deptService.getById(Long.parseLong(AuthUtil.getDeptId().split(",")[0]));
		if(byId !=null && !StringUtils.isNotBlank(parentId)){
			String ancestors = byId.getAncestors();
			String[] split = ancestors.split(",");
			if(split.length>2){
				parentId = split[2];
			}
		}
		QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("parent_id",Long.parseLong(parentId));
		queryWrapper.eq("dept_category",deptCategory);
		List<Dept> list = deptService.list(queryWrapper);
		return R.data(list);
	}

	/**
	 * 警务室 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入JlrhJws")
	public R save(@Valid @RequestBody JlrhJwsEntity JlrhJws) {
		return R.status(JlrhJwsService.save(JlrhJws));
	}

	/**
	 * 警务室 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入JlrhJws")
	public R update(@Valid @RequestBody JlrhJwsEntity JlrhJws) {
		return R.status(JlrhJwsService.updateById(JlrhJws));
	}

	/**
	 * 警务室 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入JlrhJws")
	public R submit(@Valid @RequestBody JlrhJwsEntity JlrhJws) {
		return R.status(JlrhJwsService.saveOrUpdate(JlrhJws));
	}

	/**
	 * 警务室 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(JlrhJwsService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@GetMapping("/export-JlrhJws")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入JlrhJws")
	public void exportJlrhJws(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
		params.put("deptId", AuthUtil.getDeptId());
		List<JlrhJwsExcel> list = JlrhJwsService.exportJlrhJws(params);
		ExcelUtil.export(response, "警务室数据" + DateUtil.time(), "警务室数据表", list, JlrhJwsExcel.class);
	}

	/**
	 * 警务室数量
	 */
	@GetMapping("selectJwsPcs")
	public R selectJwsPcs(JlrhJwsEntity jlrhJws, Query query){
		IPage<JlrhJwsEntity> page = JlrhJwsService.selectJwsPcs(Condition.getPage(query),jlrhJws,AuthUtil.getDeptId());
		if (page.getRecords().size()==0){
			IPage<JlrhJwsEntity> pages = JlrhJwsService.selectSqByDeptname(Condition.getPage(query),jlrhJws,AuthUtil.getDeptId());
			return R.data(pages);
		}
		for (JlrhJwsEntity jwsEntity:page.getRecords()){
			List<JlrhJwsVOS> list = JlrhJwsService.selectJwsSq(jwsEntity.getId(),jlrhJws.getDeptName());
			jwsEntity.setChildren(list);
		}
		return R.data(page);
	}

	/**
	 * 警务室信息
	 */
	@GetMapping("selectJwsxx")
	public R selectJwsxx(JlrhJwsEntity jlrhJwsEntity,Query query){
		return R.data(JlrhJwsService.selectJwsxx(Condition.getPage(query),jlrhJwsEntity));
	}

	/**
	 * 九小场所信息
	 */
	@GetMapping("selectCsxx")
	public R selectCsxx(CsInfoEntity csInfo,Query query){
		return R.data(csInfoService.selectCsxx(Condition.getPage(query),csInfo));
	}
}
