package org.springblade.modules.rk.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.rk.entity.RkEntity;
import org.springblade.modules.rk.excel.RkExcel;
import org.springblade.modules.rk.service.IRkService;
import org.springblade.modules.rk.vo.*;
import org.springblade.modules.rk.wrapper.RkWrapper;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IUserService;
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-08-05
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-rk/rk")
@Api(value = "房屋人口", tags = "房屋人口接口")
public class RkController extends BladeController {

	private final IRkService rkService;

	private final IUserService userService;

	private final IDeptService deptService;

	/**
	 * 房屋人口 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入rk")
	public R<RkEntity> detail(RkEntity rk) {
		RkEntity detail = rkService.getOne(Condition.getQueryWrapper(rk));
		return R.data(detail);
	}

	/**
	 * 房屋人口 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入rk")
	public R<IPage<RkVO>> list(@ApiIgnore @RequestParam Map<String, Object> rk, Query query) {
		IPage<RkEntity> pages = rkService.page(Condition.getPage(query), Condition.getQueryWrapper(rk, RkEntity.class));
		return R.data(RkWrapper.build().pageVO(pages));
	}

	/**
	 * 房屋人口 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入rk")
	public R<IPage<RkVO>> page(@RequestParam Map<String, Object> params, Query query) {
		IPage<RkVO> pages = rkService.selectRkPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 小区列表
	 */
	@GetMapping("/getXqList")
	public R getXqList(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		params.put("userId", AuthUtil.getUserId());
		IPage<XqVO> pages = rkService.getXqList(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 楼栋列表-APP
	 */
	@GetMapping("/getLdList")
	public R getLdList(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		params.put("userId", AuthUtil.getUserId());
		List<LdVO> ldList = rkService.getLdList(params);
		for (LdVO ld : ldList) {
			params.put("ldName", ld.getLdNname());
			// 根据楼栋名查单元列表
			List<DyWebVO> dyWebVO = rkService.getDyWebList(params);
			for (DyWebVO dy : dyWebVO) {
				params.put("unit", dy.getUnit());
				// 根据楼栋名+单元名查楼层列表
				List<LcWebVO> lcList = rkService.getLcList(params);
				for (LcWebVO lc : lcList) {
					params.put("lch", lc.getLch());
					List<FjVO> fjList = rkService.getFjWebList(params);
					lc.setFjList(fjList);
				}
				dy.setLcList(lcList);
			}
			ld.setDyList(dyWebVO);
		}
		return R.data(ldList);
	}

	/**
	 * 楼栋列表-WEB
	 */
	@GetMapping("/getLdList-Web")
	public R getLdListWeb(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		params.put("userId", AuthUtil.getUserId());
		List<LdVO> ldList = rkService.getLdList(params);
		return R.data(ldList);
	}

	/**
	 * 单元-楼层-房间列表-WEB
	 */
	@GetMapping("/ldDetail-Web")
	public R getLdDetail(@RequestParam Map<String, Object> params) {
		params.put("deptId", AuthUtil.getDeptId());
		params.put("userId", AuthUtil.getUserId());
		// 根据楼栋名查单元列表
		List<DyWebVO> dyWebVO = rkService.getDyWebList(params);
		for (DyWebVO dy : dyWebVO) {
			params.put("unit", dy.getUnit());
			// 根据楼栋名+单元名查楼层列表
			List<LcWebVO> lcList = rkService.getLcList(params);
			for (LcWebVO lc : lcList) {
				params.put("lch", lc.getLch());
				List<FjVO> fjList = rkService.getFjWebList(params);
				lc.setFjList(fjList);
			}
			dy.setLcList(lcList);
		}
		return R.data(dyWebVO);
	}

	/**
	 * 社区列表
	 */
	@GetMapping("selectSq")
	public R selectSq(){
		List<LbVO> lbVolist = userService.selectSq(AuthUtil.getUserId());
		for (LbVO lbVO:lbVolist){
			List<LbVOS> lbVolists = rkService.selectXq(AuthUtil.getUserId());
			lbVO.setChildren(lbVolists);
			for (LbVOS lbVOS:lbVolists){
				List<LbVOSS> lbVolistss = rkService.selectLd(lbVOS.getLabel());
				lbVOS.setChildren(lbVolistss);
			}
		}
		return R.data(lbVolist);
	}

	/**
	 * 社区民警列表
	 */
	@GetMapping("selectSqmj")
	public R selectSqmj(){
		List<LbVOS> sqmjlist = rkService.selectxqmj(AuthUtil.getDeptId());
		for (LbVOS lbVOS:sqmjlist){
			List<LbVOSS> sqmjlists = rkService.selectLd(lbVOS.getLabel());
			lbVOS.setChildren(sqmjlists);
		}
		return R.data(sqmjlist);
	}

	/**
	 * 查询当前用户权限
	 */
	@GetMapping("selectQx")
	public R selectQx(){
		return R.data(deptService.getById(Long.parseLong(AuthUtil.getDeptId())));
	}

	/**
	 * 房屋人口 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入rk")
	public R save(@Valid @RequestBody RkEntity rk) {
		return R.status(rkService.save(rk));
	}

	/**
	 * 房屋人口 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入rk")
	public R update(@Valid @RequestBody RkEntity rk) {
		return R.status(rkService.updateById(rk));
	}

	/**
	 * 房屋人口 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入rk")
	public R submit(@Valid @RequestBody RkEntity rk) {
		return R.status(rkService.saveOrUpdate(rk));
	}

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


	/**
	 * 导出数据
	 */
	@GetMapping("/export-rk")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入rk")
	public void exportRk(@ApiIgnore @RequestParam Map<String, Object> rk, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<RkEntity> queryWrapper = Condition.getQueryWrapper(rk, RkEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Rk::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(RkEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<RkExcel> list = rkService.exportRk(queryWrapper);
		ExcelUtil.export(response, "房屋人口数据" + DateUtil.time(), "房屋人口数据表", list, RkExcel.class);
	}

}
