package org.springblade.modules.rhzf.fwqz.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.rhzf.fwqz.excel.RhzfFwqzExcel;
import org.springblade.modules.rhzf.fwqz.pojo.entity.RhzfFwqzEntity;
import org.springblade.modules.rhzf.fwqz.pojo.vo.RhzfFwqzVO;
import org.springblade.modules.rhzf.fwqz.service.IRhzfFwqzService;
import org.springblade.modules.rhzf.fwqz.wrapper.RhzfFwqzWrapper;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
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-10-31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/rhzfFwqz")
@Api(value = "入户走访_服务群众", tags = "入户走访_服务群众接口")
public class RhzfFwqzController extends BladeController {

	private final IRhzfFwqzService rhzfFwqzService;

	private final IDeptService deptService;

	/**
	 * 入户走访_服务群众 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes  = "传入rhzfFwqz")
	public R<RhzfFwqzVO> detail(RhzfFwqzEntity rhzfFwqz) {
		RhzfFwqzEntity detail = rhzfFwqzService.getOne(Condition.getQueryWrapper(rhzfFwqz));
		return R.data(RhzfFwqzWrapper.build().entityVO(detail));
	}

	/**
	 * 入户走访_服务群众 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes  = "传入rhzfFwqz")
	public R<IPage<RhzfFwqzVO>> list(@RequestParam Map<String, Object> rhzfFwqz, Query query) {
		IPage<RhzfFwqzEntity> pages = rhzfFwqzService.page(Condition.getPage(query), Condition.getQueryWrapper(rhzfFwqz, RhzfFwqzEntity.class));
		return R.data(RhzfFwqzWrapper.build().pageVO(pages));
	}

	/**
	 * 入户走访_服务群众 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes  = "传入rhzfFwqz")
	public R<IPage<RhzfFwqzVO>> 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<RhzfFwqzVO> pages = rhzfFwqzService.selectRhzfFwqzPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 入户走访_服务群众 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes  = "传入rhzfFwqz")
	public R save(@Valid @RequestBody RhzfFwqzEntity rhzfFwqz) {
		return R.status(rhzfFwqzService.save(rhzfFwqz));
	}

	/**
	 * 入户走访_服务群众 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes  = "传入rhzfFwqz")
	public R update(@Valid @RequestBody RhzfFwqzEntity rhzfFwqz) {
		return R.status(rhzfFwqzService.updateById(rhzfFwqz));
	}

	/**
	 * 入户走访_服务群众 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes  = "传入rhzfFwqz")
	public R submit(@Valid @RequestBody RhzfFwqzEntity rhzfFwqz) {
		return R.status(rhzfFwqzService.saveOrUpdate(rhzfFwqz));
	}

	/**
	 * 入户走访_服务群众 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes  = "传入ids")
	public R remove(@RequestParam String ids) {
		return R.status(rhzfFwqzService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 导出数据
	 */
	@PreAuth(RoleConstant.HAS_ROLE_ADMIN)
	@GetMapping("/export-rhzfFwqz")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "导出数据", notes  = "传入rhzfFwqz")
	public void exportRhzfFwqz(@RequestParam Map<String, Object> rhzfFwqz, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<RhzfFwqzEntity> queryWrapper = Condition.getQueryWrapper(rhzfFwqz, RhzfFwqzEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(RhzfFwqzEntity::getTenantId, bladeUser.getTenantId());
		//}
		//queryWrapper.lambda().eq(RhzfFwqzEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<RhzfFwqzExcel> list = rhzfFwqzService.exportRhzfFwqz(queryWrapper);
		ExcelUtil.export(response, "入户走访_服务群众数据" + DateUtil.time(), "入户走访_服务群众数据表", list, RhzfFwqzExcel.class);
	}

}
