package org.springblade.modules.temDuizhao.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.temDuizhao.entity.TemDuizhaoEntity;
import org.springblade.modules.temDuizhao.excel.TemDuizhaoExcel;
import org.springblade.modules.temDuizhao.service.ITemDuizhaoService;
import org.springblade.modules.temDuizhao.vo.TemDuizhaoVO;
import org.springblade.modules.temDuizhao.wrapper.TemDuizhaoWrapper;
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-30
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-temDuizhao/temDuizhao")
@Api(value = "入户人员对照", tags = "入户人员对照接口")
public class TemDuizhaoController extends BladeController {

	private final ITemDuizhaoService temDuizhaoService;

	/**
	 * 入户人员对照 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入temDuizhao")
	public R<TemDuizhaoVO> detail(TemDuizhaoEntity temDuizhao) {
		TemDuizhaoEntity detail = temDuizhaoService.getOne(Condition.getQueryWrapper(temDuizhao));
		return R.data(TemDuizhaoWrapper.build().entityVO(detail));
	}
	/**
	 * 入户人员对照 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入temDuizhao")
	public R<IPage<TemDuizhaoVO>> list(@ApiIgnore @RequestParam Map<String, Object> temDuizhao, Query query) {
		IPage<TemDuizhaoEntity> pages = temDuizhaoService.page(Condition.getPage(query), Condition.getQueryWrapper(temDuizhao, TemDuizhaoEntity.class));
		return R.data(TemDuizhaoWrapper.build().pageVO(pages));
	}

	/**
	 * 入户人员对照 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入temDuizhao")
	public R<IPage<TemDuizhaoVO>> page(@RequestParam Map<String, String> params, Query query) {
		params.put("account", AuthUtil.getUser().getAccount());
		IPage<TemDuizhaoVO> pages = temDuizhaoService.selectTemDuizhaoPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 入户人员对照 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入temDuizhao")
	public R save(@Valid @RequestBody TemDuizhaoEntity temDuizhao) {
		return R.status(temDuizhaoService.save(temDuizhao));
	}

	/**
	 * 入户人员对照 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入temDuizhao")
	public R update(@Valid @RequestBody TemDuizhaoEntity temDuizhao) {
		return R.status(temDuizhaoService.updateById(temDuizhao));
	}

	/**
	 * 入户人员对照 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入temDuizhao")
	public R submit(@Valid @RequestBody TemDuizhaoEntity temDuizhao) {
		return R.status(temDuizhaoService.saveOrUpdate(temDuizhao));
	}

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


	/**
	 * 导出数据
	 */
	@GetMapping("/export-temDuizhao")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入temDuizhao")
	public void exportTemDuizhao(@ApiIgnore @RequestParam Map<String, Object> temDuizhao, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<TemDuizhaoEntity> queryWrapper = Condition.getQueryWrapper(temDuizhao, TemDuizhaoEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(TemDuizhao::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(TemDuizhaoEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<TemDuizhaoExcel> list = temDuizhaoService.exportTemDuizhao(queryWrapper);
		ExcelUtil.export(response, "入户人员对照数据" + DateUtil.time(), "入户人员对照数据表", list, TemDuizhaoExcel.class);
	}

}
