package org.springblade.modules.jgInfo.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.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.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.jgInfo.entity.JgInfoEntity;
import org.springblade.modules.jgInfo.excel.JgInfoExcel;
import org.springblade.modules.jgInfo.service.IJgInfoService;
import org.springblade.modules.jgInfo.vo.*;
import org.springblade.modules.jgInfo.wrapper.JgInfoWrapper;
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-11-06
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-jgInfo/jgInfo")
@Api(value = "警格", tags = "警格接口")
public class JgInfoController extends BladeController {

	private final IJgInfoService jgInfoService;

	/**
	 * 警格 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入jgInfo")
	public R<JgInfoVO> detail(JgInfoEntity jgInfo) {
		JgInfoEntity detail = jgInfoService.getOne(Condition.getQueryWrapper(jgInfo));
		return R.data(JgInfoWrapper.build().entityVO(detail));
	}

	/**
	 * 警格 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入jgInfo")
	public R<IPage<JgInfoVO>> list(@ApiIgnore @RequestParam Map<String, Object> jgInfo, Query query) {
		IPage<JgInfoEntity> pages = jgInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(jgInfo, JgInfoEntity.class));
		return R.data(JgInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 警格 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入jgInfo")
	public R page(@RequestParam Map<String, Object> params) {
		params.put("deptId", AuthUtil.getDeptId());
		// 查分局
		List<FjVO> fjList = jgInfoService.getFjList(params);
		for (FjVO fj : fjList) {
			params.put("parentId", fj.getId());
			// 查派出所
			List<PcsVO> pcsList = jgInfoService.getPcsList(params);
			for (PcsVO pcs : pcsList) {
				params.put("parentId", pcs.getId());
				// 查社区
				List<SqVO> sqList = jgInfoService.getSqList(params);
				for (SqVO sq : sqList) {
					params.put("parentId", sq.getId());
					// 查警格
					List<JgVO> jgList = jgInfoService.getJgList(params);
					sq.setChildren(jgList);
				}
				pcs.setChildren(sqList);
			}
			fj.setChildren(pcsList);
		}
		return R.data(fjList);
	}

	/**
	 * 警员列表
	 */
	@GetMapping("/getJyList")
	public R getJyList(@RequestParam Map<String, Object> params) {
		List<JyVO> jyList = jgInfoService.getJyList(params);
		return R.data(jyList);
	}

	/**
	 * 根据社区id查询警格列表
	 */
	@GetMapping("/getJgList")
	public R getJgList(@RequestParam Map<String, Object> params) {
		QueryWrapper<JgInfoEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sq_id", Long.valueOf(params.get("sqId").toString()));
		queryWrapper.eq("is_deleted", 0);
		List<JgInfoEntity> data = jgInfoService.list(queryWrapper);
		return R.data(data);
	}

	/**
	 * 民警根据社区id查询警格列表-app
	 */
	@GetMapping("/getJgList-app")
	public R getJgListApp(@RequestParam Map<String, Object> params) {
		QueryWrapper<JgInfoEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("sq_id", Long.valueOf(params.get("sqId").toString()));
		queryWrapper.eq("jy_id", AuthUtil.getUserId());
		queryWrapper.eq("is_deleted", 0);
		List<JgInfoEntity> data = jgInfoService.list(queryWrapper);
		return R.data(data);
	}

	/**
	 * 警格 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入jgInfo")
	public R save(@Valid @RequestBody JgInfoEntity jgInfo) {
		return R.status(jgInfoService.save(jgInfo));
	}

	/**
	 * 警格 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入jgInfo")
	public R update(@Valid @RequestBody JgInfoEntity jgInfo) {
		return R.status(jgInfoService.updateById(jgInfo));
	}

	/**
	 * 警格 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入jgInfo")
	public R submit(@Valid @RequestBody JgInfoEntity jgInfo) {
		return R.status(jgInfoService.saveOrUpdate(jgInfo));
	}

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

	/**
	 * 导出数据
	 */
	@GetMapping("/export-jgInfo")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入jgInfo")
	public void exportjgInfo(@ApiIgnore @RequestParam Map<String, Object> jgInfo, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<JgInfoEntity> queryWrapper = Condition.getQueryWrapper(jgInfo, JgInfoEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(jgInfo::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(JgInfoEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<JgInfoExcel> list = jgInfoService.exportjgInfo(queryWrapper);
		ExcelUtil.export(response, "警格数据" + DateUtil.time(), "警格数据表", list, JgInfoExcel.class);
	}
}
