package org.springblade.modules.sqjw.jwtdzrq.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.csInfo.service.ICsInfoService;
import org.springblade.modules.sqjw.jwtdzrq.entity.JwtdZrqEntity;
import org.springblade.modules.sqjw.jwtdzrq.excel.JwtdZrqExcel;
import org.springblade.modules.sqjw.jwtdzrq.service.IJwtdZrqService;
import org.springblade.modules.sqjw.jwtdzrq.vo.JwtdZrqVO;
import org.springblade.modules.sqjw.jwtdzrq.wrapper.JwtdZrqWrapper;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.vo.DeptAllVO;
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 2024-04-03
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-jwtdzrq/jwtdzrq")
@Api(value = "警务团队-警务责任区", tags = "警务团队-警务责任区接口")
public class JwtdZrqController extends BladeController {

	private final IJwtdZrqService jwtdzrqService;

	private final ICsInfoService csInfoService;

	/**
	 * 警务团队-警务责任区 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入jwtdzrq")
	public R<JwtdZrqVO> detail(JwtdZrqEntity jwtdzrq) {
		JwtdZrqEntity detail = jwtdzrqService.getOne(Condition.getQueryWrapper(jwtdzrq));
		return R.data(JwtdZrqWrapper.build().entityVO(detail));
	}

	/**
	 * 警务团队-警务责任区 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入jwtdzrq")
	public R<IPage<JwtdZrqVO>> list(@ApiIgnore @RequestParam Map<String, Object> jwtdzrq, Query query) {
		IPage<JwtdZrqEntity> pages = jwtdzrqService.page(Condition.getPage(query), Condition.getQueryWrapper(jwtdzrq, JwtdZrqEntity.class));
		return R.data(JwtdZrqWrapper.build().pageVO(pages));
	}

	/**
	 * 警务团队-警务责任区 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入jwtdzrq")
	public R page(@RequestParam Map<String, String> params) {
		params.put("deptId", AuthUtil.getDeptId());
		List<JwtdZrqVO> data = jwtdzrqService.selectJwtdZrqList(params);
		for (JwtdZrqVO jwtdZrqVO : data) {
			List<User> mjList = jwtdzrqService.getMjList(jwtdZrqVO.getSssqbm());
			jwtdZrqVO.setMjList(mjList);
		}
		return R.data(data);
	}

	/**
	 * 警务团队-警务责任区 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入jwtdzrq")
	public R save(@Valid @RequestBody JwtdZrqEntity jwtdzrq) {
		JwtdZrqEntity oldData = csInfoService.getBySqBm(jwtdzrq.getSssqbm());
		if (oldData != null) {
			return R.fail("操作失败,当前责任区已经画过点位图");
		}
		DeptAllVO deptAll = csInfoService.getParentInfo(jwtdzrq.getSssqbm());
		jwtdzrq.setSssq(deptAll.getSqMc());
		// 派出所
		jwtdzrq.setPcsbm(deptAll.getPcsId());
		jwtdzrq.setSspcs(deptAll.getPcsMc());
		// 分局
		jwtdzrq.setSsfjbm(deptAll.getFjId());
		jwtdzrq.setSsfj(deptAll.getFjMc());
		// 市局
		jwtdzrq.setSssjbm(deptAll.getSjId());
		jwtdzrq.setSssj(deptAll.getSjMc());
		return R.status(jwtdzrqService.save(jwtdzrq));
	}

	/**
	 * 警务团队-警务责任区 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入jwtdzrq")
	public R update(@Valid @RequestBody JwtdZrqEntity jwtdzrq) {
		return R.status(jwtdzrqService.updateById(jwtdzrq));
	}

	/**
	 * 警务团队-警务责任区 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入jwtdzrq")
	public R submit(@Valid @RequestBody JwtdZrqEntity jwtdzrq) {
		return R.status(jwtdzrqService.saveOrUpdate(jwtdzrq));
	}

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

	/**
	 * 导出数据
	 */
	@GetMapping("/export-jwtdzrq")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入jwtdzrq")
	public void exportJwtdZrq(@ApiIgnore @RequestParam Map<String, Object> jwtdzrq, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<JwtdZrqEntity> queryWrapper = Condition.getQueryWrapper(jwtdzrq, JwtdZrqEntity.class);
		queryWrapper.lambda().eq(JwtdZrqEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<JwtdZrqExcel> list = jwtdzrqService.exportJwtdZrq(queryWrapper);
		ExcelUtil.export(response, "警务团队-警务责任区数据" + DateUtil.time(), "警务团队-警务责任区数据表", list, JwtdZrqExcel.class);
	}
}
