package org.springblade.modules.wgr.check.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
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.wgr.check.entity.CheckEntity;
import org.springblade.modules.wgr.check.excel.CheckExcel;
import org.springblade.modules.wgr.check.service.ICheckService;
import org.springblade.modules.wgr.check.vo.CheckVO;
import org.springblade.modules.wgr.check.wrapper.CheckWrapper;
import org.springblade.modules.wgr.info.service.IWgrInfoService;
import org.springframework.transaction.annotation.Transactional;
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-12-14
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-check/check")
@Api(value = "外国人检查", tags = "外国人检查接口")
public class CheckController extends BladeController {

	private final ICheckService checkService;

	private final IWgrInfoService wgrinfoService;

	/**
	 * 外国人检查 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入check")
	public R<CheckVO> detail(CheckEntity check) {
		CheckEntity detail = checkService.getOne(Condition.getQueryWrapper(check));
		return R.data(CheckWrapper.build().entityVO(detail));
	}
	/**
	 * 外国人检查 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入check")
	public R<IPage<CheckVO>> list(@ApiIgnore @RequestParam Map<String, Object> check, Query query) {
		IPage<CheckEntity> pages = checkService.page(Condition.getPage(query), Condition.getQueryWrapper(check, CheckEntity.class));
		return R.data(CheckWrapper.build().pageVO(pages));
	}

	/**
	 * 外国人检查 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入check")
	public R<IPage<CheckVO>> page(@RequestParam Map<String, Object> params, Query query) {
		IPage<CheckVO> pages = checkService.selectCheckPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * app-外国人核查-已核查列表
	 */
	@GetMapping("/getChecked")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入check")
	public R getChecked(@RequestParam Map<String, Object> params, Query query) {
		params.put("userId", AuthUtil.getUserId());
		IPage<CheckVO> pages = checkService.getChecked(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 外国人检查 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入check")
	public R save(@Valid @RequestBody CheckEntity check) {
		return R.status(checkService.save(check));
	}

	/**
	 * 外国人检查 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入check")
	public R update(@Valid @RequestBody CheckEntity check) {
		return R.status(checkService.updateById(check));
	}

	/**
	 * 外国人检查 新增或修改
	 */
	@Transactional
	@DS("jl")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入check")
	public R submit(@Valid @RequestBody CheckEntity check) {
		checkService.saveOrUpdate(check);
		wgrinfoService.resetBm(check.getWgrId());
		return R.success("操作成功");
	}

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


	/**
	 * 导出数据
	 */
	@GetMapping("/export-check")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入check")
	public void exportCheck(@ApiIgnore @RequestParam Map<String, Object> check, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<CheckEntity> queryWrapper = Condition.getQueryWrapper(check, CheckEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Check::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(CheckEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<CheckExcel> list = checkService.exportCheck(queryWrapper);
		ExcelUtil.export(response, "外国人检查数据" + DateUtil.time(), "外国人检查数据表", list, CheckExcel.class);
	}

}
