package org.springblade.modules.xlfk.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 lombok.AllArgsConstructor;

import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.annotation.PreAuth;
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.Func;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.xlfk.excel.XlfkExcel;
import org.springblade.modules.xlfk.pojo.entity.XlfkEntity;
import org.springblade.modules.xlfk.pojo.vo.XlfkVO;
import org.springblade.modules.xlfk.service.IXlfkService;
import org.springblade.modules.xlfk.wrapper.XlfkWrapper;
import org.springframework.web.bind.annotation.*;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.tool.constant.RoleConstant;

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

/**
 * 巡逻防控 控制器
 *
 * @author BladeX
 * @since 2024-11-06
 */
@RestController
@AllArgsConstructor
@RequestMapping("/xlfk")
@Api(value = "巡逻防控", tags = "巡逻防控接口")
public class XlfkController extends BladeController {

	private final IXlfkService xlfkService;

	private final IDeptService deptService;

	/**
	 * 巡逻防控 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes  = "传入xlfk")
	public R<XlfkVO> detail(XlfkEntity xlfk) {
		XlfkEntity detail = xlfkService.getOne(Condition.getQueryWrapper(xlfk));
		return R.data(XlfkWrapper.build().entityVO(detail));
	}

	/**
	 * 巡逻防控 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes  = "传入xlfk")
	public R<IPage<XlfkVO>> list(@RequestParam Map<String, Object> xlfk, Query query) {
		IPage<XlfkEntity> pages = xlfkService.page(Condition.getPage(query), Condition.getQueryWrapper(xlfk, XlfkEntity.class));
		return R.data(XlfkWrapper.build().pageVO(pages));
	}

	/**
	 * 巡逻防控 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes  = "传入xlfk")
	public R<IPage<XlfkVO>> page(@RequestParam Map<String, Object> params, Query query) {
		Dept dept = deptService.getById(Long.valueOf(AuthUtil.getDeptId().split(",")[0]));
		if (dept.getDeptCategory() == 7) {
			params.put("pcsId", dept.getParentId());
		} else {
			params.put("deptId", AuthUtil.getDeptId());
		}
		IPage<XlfkVO> pages = xlfkService.selectXlfkPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 巡逻防控 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes  = "传入xlfk")
	public R save(@Valid @RequestBody XlfkEntity xlfk) {
		return R.status(xlfkService.save(xlfk));
	}

	/**
	 * 巡逻防控 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes  = "传入xlfk")
	public R update(@Valid @RequestBody XlfkEntity xlfk) {
		return R.status(xlfkService.updateById(xlfk));
	}

	/**
	 * 巡逻防控 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes  = "传入xlfk")
	public R submit(@Valid @RequestBody XlfkEntity xlfk) {
		return R.status(xlfkService.saveOrUpdate(xlfk));
	}

	/**
	 * 巡逻防控 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes  = "传入ids")
	public R remove(@RequestParam String ids) {
		return R.status(xlfkService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导出数据
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-xlfk")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "导出数据", notes  = "传入xlfk")
	public void exportXlfk(@RequestParam Map<String, Object> xlfk, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<XlfkEntity> queryWrapper = Condition.getQueryWrapper(xlfk, XlfkEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(XlfkEntity::getTenantId, bladeUser.getTenantId());
		//}
		//queryWrapper.lambda().eq(XlfkEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<XlfkExcel> list = xlfkService.exportXlfk(queryWrapper);
		ExcelUtil.export(response, "巡逻防控数据" + DateUtil.time(), "巡逻防控数据表", list, XlfkExcel.class);
	}

}
