/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.gsdrsjb.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.secure.BladeUser;
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.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.gsdrsjb.entity.GsdrsjbEntity;
import org.springblade.modules.gsdrsjb.vo.GsdrsjbVO;
import org.springblade.modules.gsdrsjb.excel.GsdrsjbExcel;
import org.springblade.modules.gsdrsjb.wrapper.GsdrsjbWrapper;
import org.springblade.modules.gsdrsjb.service.IGsdrsjbService;
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.BladeConstant;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * gsdrsjb 控制器
 *
 * @author BladeX
 * @since 2024-01-21
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-gsdrsjb/gsdrsjb")
@Api(value = "gsdrsjb", tags = "gsdrsjb接口")
public class GsdrsjbController extends BladeController {

	private final IGsdrsjbService gsdrsjbService;

	/**
	 * gsdrsjb 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入gsdrsjb")
	public R<GsdrsjbVO> detail(GsdrsjbEntity gsdrsjb) {
		GsdrsjbEntity detail = gsdrsjbService.getOne(Condition.getQueryWrapper(gsdrsjb));
		return R.data(GsdrsjbWrapper.build().entityVO(detail));
	}

	/**
	 * gsdrsjb 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入gsdrsjb")
	public R<IPage<GsdrsjbVO>> list(@ApiIgnore @RequestParam Map<String, Object> gsdrsjb, Query query) {
		IPage<GsdrsjbEntity> pages = gsdrsjbService.page(Condition.getPage(query), Condition.getQueryWrapper(gsdrsjb, GsdrsjbEntity.class));
		return R.data(GsdrsjbWrapper.build().pageVO(pages));
	}

	/**
	 * gsdrsjb 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入gsdrsjb")
	public R<IPage<GsdrsjbVO>> page(GsdrsjbVO gsdrsjb, Query query) {
		IPage<GsdrsjbVO> pages = gsdrsjbService.selectGsdrsjbPage(Condition.getPage(query), gsdrsjb);
		return R.data(pages);
	}

	/**
	 * gsdrsjb 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入gsdrsjb")
	public R save(@Valid @RequestBody GsdrsjbEntity gsdrsjb) {
		return R.status(gsdrsjbService.save(gsdrsjb));
	}

	/**
	 * gsdrsjb 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入gsdrsjb")
	public R update(@Valid @RequestBody GsdrsjbEntity gsdrsjb) {
		return R.status(gsdrsjbService.updateById(gsdrsjb));
	}

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

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

	/**
	 * 导出数据
	 */
	@GetMapping("/export-gsdrsjb")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入gsdrsjb")
	public void exportGsdrsjb(@ApiIgnore @RequestParam Map<String, Object> gsdrsjb, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<GsdrsjbEntity> queryWrapper = Condition.getQueryWrapper(gsdrsjb, GsdrsjbEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Gsdrsjb::getTenantId, bladeUser.getTenantId());
		//}
//		queryWrapper.lambda().eq(GsdrsjbEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		QueryWrapper<GsdrsjbEntity> queryWrapper2 = Condition.getQueryWrapper(gsdrsjb, GsdrsjbEntity.class);

		List<GsdrsjbExcel> list = gsdrsjbService.exportGsdrsjb(queryWrapper2);
		ExcelUtil.export(response, "gsdrsjb数据" + DateUtil.time(), "gsdrsjb数据表", list, GsdrsjbExcel.class);
	}

	/**
	 * 导入用户
	 */
	@PostMapping("/import-gsdrsjb")
	public R importGsdrsjb(MultipartFile file) {
		int cnt = 0;
		List<GsdrsjbEntity> gsdrsjblist = new ArrayList<>();
		List<GsdrsjbExcel> list = ExcelUtil.read(file, GsdrsjbExcel.class);
		for (GsdrsjbExcel gsdrsjbExcel : list) {
			if (StringUtils.isBlank(gsdrsjbExcel.getShtyBm())
				|| StringUtils.isBlank(gsdrsjbExcel.getQyDzmc())
				|| StringUtils.isBlank(gsdrsjbExcel.getQyMc())
				|| StringUtils.isBlank(gsdrsjbExcel.getJyfwzy())
				|| StringUtils.isBlank(gsdrsjbExcel.getFrXm())
				|| StringUtils.isBlank(gsdrsjbExcel.getFeZjhm())) {
				return R.fail("主要信息不可为空！");
			}
			GsdrsjbEntity ge = BeanUtil.copy(gsdrsjbExcel, GsdrsjbEntity.class);
			ge.setCreateUser(AuthUtil.getUserId());
			ge.setCreateDept(Long.valueOf(AuthUtil.getDeptId()));
			ge.setCreateTime(DateUtil.now());
			ge.setIsDeleted(0);
			ge.setStatus(1);
			gsdrsjblist.add(ge);
			cnt++;
		}
		if (!gsdrsjbService.saveOrUpdateBatch(gsdrsjblist)) {
			return R.fail("导入失败，请联系管理员！");
		}
		return R.success("导入成功，本次共计导入：" + cnt + "条。");
	}

}
