package org.springblade.modules.xxy.controller;

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.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.rhzf.fwInfo.util.GsonUtils;
import org.springblade.modules.rhzf.fwInfo.vo.LdBzdzVO;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.xxy.entity.XxyEntity;
import org.springblade.modules.xxy.excel.XxyExcel;
import org.springblade.modules.xxy.service.IXxyService;
import org.springblade.modules.xxy.vo.DyVO;
import org.springblade.modules.xxy.vo.XxyVO;
import org.springblade.modules.xxy.wrapper.XxyWrapper;
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-07-07
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-xxy/Xxy")
@Api(value = "信息员", tags = "信息员接口")
public class XxyController extends BladeController {

	private final IXxyService xxyService;

	private final IDeptService deptService;

	/**
	 * 信息员 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入Xxy")
	public R<XxyEntity> detail(XxyEntity Xxy) {
		XxyEntity detail = xxyService.getOne(Condition.getQueryWrapper(Xxy));
		return R.data(detail);
	}

	/**
	 * 信息员 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入Xxy")
	public R<IPage<XxyVO>> list(@ApiIgnore @RequestParam Map<String, Object> Xxy, Query query) {
		IPage<XxyEntity> pages = xxyService.page(Condition.getPage(query), Condition.getQueryWrapper(Xxy, XxyEntity.class));
		return R.data(XxyWrapper.build().pageVO(pages));
	}

	/**
	 * 信息员 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入Xxy")
	public R<IPage<XxyVO>> page(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		IPage<XxyVO> pages = xxyService.getXxyList(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 信息员 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入Xxy")
	public R save(@Valid @RequestBody XxyEntity Xxy) {
		return R.status(xxyService.save(Xxy));
	}

	/**
	 * 信息员 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入Xxy")
	public R update(@Valid @RequestBody XxyEntity Xxy) {
		return R.status(xxyService.updateById(Xxy));
	}

	/**
	 * 信息员 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入Xxy")
	public R submit(@Valid @RequestBody XxyEntity Xxy) {
		return R.status(xxyService.saveOrUpdate(Xxy));
	}

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


	/**
	 * 导出数据
	 */
	@GetMapping("/export-Xxy")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入Xxy")
	public void exportXxy(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) {
		params.put("deptId", AuthUtil.getDeptId());
		List<XxyExcel> list = xxyService.exportXxy(params);
		ExcelUtil.export(response, "信息员数据" + DateUtil.time(), "信息员数据表", list, XxyExcel.class);
	}

	/**
	 * 楼栋列表
	 */
	@GetMapping("/getLdList-pt")
	public R getLdListPt(@RequestParam Map<String, Object> params) {
		Dept byId = deptService.getById(Long.valueOf(AuthUtil.getDeptId().split(",")[0]));
		if (byId.getDeptCategory() == 7) {
			params.put("gajgdm", byId.getParentId().toString());
		} else {
			params.put("gajgdm", AuthUtil.getDeptId());
		}
		List<LdBzdzVO> ldList = xxyService.getJzwList(params);
		return R.data(GsonUtils.toGsonStr(ldList));
	}

	/**
	 * 楼栋详情
	 * params ldphxlh
	 */
	@GetMapping("/getLdDetail")
	public R getLdDetail(@RequestParam Map<String, Object> params) {
		// 查有几个单元
		List<DyVO> dyList = xxyService.getDyList(params);
		for (DyVO dyVO : dyList) {
			params.put("dyh", dyVO.getDyh());
			List<XxyVO> xxyList = xxyService.selectXxyList(params);
			dyVO.setXxyList(xxyList);
		}
		return R.data(dyList);
	}
}
