/*
 *      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.rhzf.fwRy.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.rhzf.fwInfo.vo.SyjzryVO;
import org.springblade.modules.rhzf.fwRy.entity.FwRyEntity;
import org.springblade.modules.rhzf.fwRy.excel.FwRyExcel;
import org.springblade.modules.rhzf.fwRy.excel.RyxqExcel;
import org.springblade.modules.rhzf.fwRy.service.IFwRyService;
import org.springblade.modules.rhzf.fwRy.vo.FwRyVO;
import org.springblade.modules.rhzf.fwRy.wrapper.FwRyWrapper;
import org.springblade.modules.rhzf.ryCzrk.entity.RyCzrkEntity;
import org.springblade.modules.rhzf.ryCzrk.excel.RyCzrkExcel;
import org.springblade.modules.rhzf.ryCzrk.service.IRyCzrkService;
import org.springblade.modules.rhzf.ryJwrk.entity.RyJwrkEntity;
import org.springblade.modules.rhzf.ryJwrk.service.IRyJwrkService;
import org.springblade.modules.rhzf.ryJzrk.entity.RyJzrkEntity;
import org.springblade.modules.rhzf.ryJzrk.service.IRyJzrkService;
import org.springblade.modules.rhzf.ryWlrk.entity.RyWlrkEntity;
import org.springblade.modules.rhzf.ryWlrk.service.IRyWlrkService;
import org.springblade.modules.rhzf.ryZzrk.entity.RyZzrkEntity;
import org.springblade.modules.rhzf.ryZzrk.service.IRyZzrkService;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 入户走访_房屋_人员关联表 控制器
 *
 * @author BladeX
 * @since 2023-11-14
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-fwRy/fwRy")
@Api(value = "入户走访_房屋_人员关联表", tags = "入户走访_房屋_人员关联表接口")
public class FwRyController extends BladeController {

	private final IFwRyService fwRyService;

	private final IRyCzrkService czrkService;

	private final IRyJwrkService jwrkService;

	private final IRyJzrkService jzrkService;

	private final IRyWlrkService wlrkService;

	private final IRyZzrkService zzrkService;

	private final IUserService userService;

	/**
	 * 入户走访_房屋_人员关联表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入fwRy")
	public R<FwRyVO> detail(FwRyEntity fwRy) {
		FwRyEntity detail = fwRyService.getOne(Condition.getQueryWrapper(fwRy));
		return R.data(FwRyWrapper.build().entityVO(detail));
	}
	/**
	 * 入户走访_房屋_人员关联表 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入fwRy")
	public R<IPage<FwRyVO>> list(@ApiIgnore @RequestParam Map<String, Object> fwRy, Query query) {
		IPage<FwRyEntity> pages = fwRyService.page(Condition.getPage(query), Condition.getQueryWrapper(fwRy, FwRyEntity.class));
		return R.data(FwRyWrapper.build().pageVO(pages));
	}

	/**
	 * 入户走访_房屋_人员关联表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入fwRy")
	public R<IPage<FwRyVO>> page(FwRyVO fwRy, Query query) {
		IPage<FwRyVO> pages = fwRyService.selectFwRyPage(Condition.getPage(query), fwRy);
		return R.data(pages);
	}

	/**
	 * 入户走访_房屋_人员关联表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入fwRy")
	public R save(@Valid @RequestBody FwRyEntity fwRy) {
		return R.status(fwRyService.save(fwRy));
	}

	/**
	 * 入户走访_房屋_人员关联表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入fwRy")
	public R update(@Valid @RequestBody FwRyEntity fwRy) {
		return R.status(fwRyService.updateById(fwRy));
	}

	/**
	 * 入户走访_房屋_人员关联表 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入fwRy")
	public R submit(@Valid @RequestBody FwRyEntity fwRy) {
		return R.status(fwRyService.saveOrUpdate(fwRy));
	}

	/**
	 * 入户走访_房屋_人员关联表 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(fwRyService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * app删除人员
	 */
	@Transactional
	@PostMapping("/appRemove")
	public R appRemove(@RequestParam Map<String, String> params) {
		String ryType = params.get("ryType");
		Long ryId = Long.valueOf(params.get("ryId"));
		if (ryType.equals("czrk")) {
			// 删常住人口表
			RyCzrkEntity czrkEntity = new RyCzrkEntity();
			czrkEntity.setId(ryId);
			czrkService.deleteLogic(Func.toLongList(czrkEntity.getId().toString()));
		}else if (ryType.equals("zzrk")) {
			// 删暂住人口表
			RyZzrkEntity zzrkEntity = new RyZzrkEntity();
			zzrkEntity.setId(ryId);
			zzrkService.deleteLogic(Func.toLongList(zzrkEntity.getId().toString()));
		} else if (ryType.equals("wlrk")) {
			// 删未落人口表
			RyWlrkEntity wlrkEntity = new RyWlrkEntity();
			wlrkEntity.setId(ryId);
			wlrkService.deleteLogic(Func.toLongList(wlrkEntity.getId().toString()));
		} else if (ryType.equals("jwrk")) {
			// 删境外人口表
			RyJwrkEntity jwrkEntity = new RyJwrkEntity();
			jwrkEntity.setId(ryId);
			jwrkService.deleteLogic(Func.toLongList(jwrkEntity.getId().toString()));
		} else if (ryType.equals("jzrk")) {
			// 删寄住人口表
			RyJzrkEntity jzrkEntity = new RyJzrkEntity();
			jzrkEntity.setId(ryId);
			jzrkService.deleteLogic(Func.toLongList(jzrkEntity.getId().toString()));
		}
		// 删人员房屋关联表
		Map<String, Object> fwRyparams = new HashMap<>();
		fwRyparams.put("ry_id", ryId);
		fwRyparams.put("ry_type", ryType);
		fwRyService.removeByMap(fwRyparams);
		return R.success("操作成功");
	}

	/**
	 * 导出数据
	 */
	@GetMapping("/export-fwRy")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入fwRy")
	public void exportFwRy(@ApiIgnore @RequestParam Map<String, Object> fwRy, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<FwRyEntity> queryWrapper = Condition.getQueryWrapper(fwRy, FwRyEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(FwRy::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(FwRyEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<FwRyExcel> list = fwRyService.exportFwRy(queryWrapper);
		ExcelUtil.export(response, "入户走访_房屋_人员关联表数据" + DateUtil.time(), "入户走访_房屋_人员关联表数据表", list, FwRyExcel.class);
	}

	/**
	 * 通过房屋地址编码获取该房屋的人员信息
	 */
	@GetMapping("/getRyCountByDzbm")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "", notes = "传入dzbm")
	public R<List<SyjzryVO>> getRyCountByDzbm(Long fwId) {
		List<SyjzryVO> rySameList=fwRyService.getRyCountByDzbm(fwId);
		for (SyjzryVO ry:rySameList){
			User createUser = userService.getById(ry.getCreateUser());
			User updateUser = userService.getById(ry.getUpdateUser());
			if (createUser != null) {
				ry.setCreateUserName(createUser.getName());
			}
			if (updateUser != null) {
				ry.setUpdateUserName(updateUser.getName());
			}
		}
		return R.data(rySameList);
	}

	/**
	 * 房屋人员
	 */
	@GetMapping("/getFwry")
	public R getFwry(SyjzryVO syjzryVO, Query query) {
		syjzryVO.setDeptId(AuthUtil.getDeptId());
		IPage<SyjzryVO> rySameList = fwRyService.getFwry(Condition.getPage(query), syjzryVO);
		for (SyjzryVO ry:rySameList.getRecords()){
			User createUser = userService.getById(ry.getCreateUser());
			User updateUser = userService.getById(ry.getUpdateUser());
			if (createUser != null) {
				ry.setCreateUserName(createUser.getName());
			}
			if (updateUser != null) {
				ry.setUpdateUserName(updateUser.getName());
			}
		}
		return R.data(rySameList);
	}

	/**
	 * 房屋人员
	 */
	@GetMapping("/getFwrys")
	public R getFwrys(SyjzryVO syjzryVO) {
		syjzryVO.setDeptId(AuthUtil.getDeptId());
		List<SyjzryVO> fwryList = fwRyService.fwryList(syjzryVO);
		return R.data(fwryList);
	}

	/**
	 * 人员详情导出数据
	 */
	@GetMapping("/export-ryxq")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入ryCzrk")
	public void exportRyxq(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
		params.put("deptId", AuthUtil.getDeptId());
		List<RyxqExcel> list = fwRyService.exportRyxq(params);
		for (RyxqExcel ryxqExcel:list){
			User createUser = userService.getById(ryxqExcel.getCreateUser());
			User updateUser = userService.getById(ryxqExcel.getUpdateUser());
			if (createUser != null) {
				ryxqExcel.setCreateUserName(createUser.getName());
			}
			if (updateUser != null) {
				ryxqExcel.setUpdateUserName(updateUser.getName());
			}
		}
		ExcelUtil.export(response, "入户走访_人员数据" + DateUtil.time(), "入户走访_人员数据表", list, RyxqExcel.class);
	}
}
