package org.springblade.modules.yjjl.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.csjcInfo.service.ICsjcInfoService;
import org.springblade.modules.system.vo.DeptListVo;
import org.springblade.modules.yjjl.entity.YjjlEntity;
import org.springblade.modules.yjjl.excel.YjjlExcel;
import org.springblade.modules.yjjl.service.IYjjlService;
import org.springblade.modules.yjjl.vo.YjjlVO;
import org.springblade.modules.yjjl.wrapper.YjjlWrapper;
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-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-yjjl/yjjl")
@Api(value = "场所检查-移交记录", tags = "场所检查-移交记录接口")
public class YjjlController extends BladeController {

	private final IYjjlService yjjlService;

	private final ICsjcInfoService csjcInfoService;

	/**
	 * 场所检查-移交记录 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入yjjl")
	public R<YjjlVO> detail(YjjlEntity yjjl) {
		YjjlEntity detail = yjjlService.getOne(Condition.getQueryWrapper(yjjl));
		return R.data(YjjlWrapper.build().entityVO(detail));
	}

	/**
	 * 场所检查-移交记录 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入yjjl")
	public R<IPage<YjjlVO>> list(@ApiIgnore @RequestParam Map<String, Object> yjjl, Query query) {
		IPage<YjjlEntity> pages = yjjlService.page(Condition.getPage(query), Condition.getQueryWrapper(yjjl, YjjlEntity.class));
		return R.data(YjjlWrapper.build().pageVO(pages));
	}

	/**
	 * 场所检查-移交记录 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入yjjl")
	public R page(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		IPage<YjjlVO> pages = yjjlService.getYjjlPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 场所检查-移交记录 自定义分页
	 */
	@GetMapping("/getHistoryById")
	public R getHistoryById(@RequestParam Map<String, Object> params) {
		List<YjjlVO> pages = yjjlService.getHistoryById(params);
		return R.data(pages);
	}

	/**
	 * 场所检查-移交记录 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入yjjl")
	public R save(@Valid @RequestBody YjjlEntity yjjl) {
		return R.status(yjjlService.save(yjjl));
	}

	/**
	 * 场所检查-移交记录 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入yjjl")
	public R update(@Valid @RequestBody YjjlEntity yjjl) {
		return R.status(yjjlService.updateById(yjjl));
	}

	/**
	 * 整改下发
	 */
	@PostMapping("/updateByIds")
	public R updateByIds(@RequestParam String ids) {
		String[] idList = ids.split(",");
		for (String id : idList) {
			YjjlEntity yjjl = new YjjlEntity();
			yjjl.setCsjcId(Long.valueOf(id));
			yjjl.setZt(4);
			// 根绝csjc_Id查询所属社区id，再查该社区的消防办事员
			Long deptId = csjcInfoService.getSqId(id);
			yjjl.setYjdw(deptId);
//			yjjl.setYjdw(Long.valueOf(AuthUtil.getDeptId()));
			yjjl.setType(2);
			yjjlService.save(yjjl);
		}
		return R.success("操作成功");
	}

	/**
	 * 场所检查-移交记录 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入yjjl")
	public R submit(@Valid @RequestBody YjjlEntity yjjl) {
		return R.status(yjjlService.saveOrUpdate(yjjl));
	}

	/**
	 * 场所检查-移交记录 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(yjjlService.deleteLogic(Func.toLongList(ids)));
	}


	/**
	 * 导出数据
	 */
	@GetMapping("/export-yjjl")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入yjjl")
	public void exportYjjl(@ApiIgnore @RequestParam Map<String, Object> yjjl, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<YjjlEntity> queryWrapper = Condition.getQueryWrapper(yjjl, YjjlEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Yjjl::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(YjjlEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<YjjlExcel> list = yjjlService.exportYjjl(queryWrapper);
		ExcelUtil.export(response, "场所检查-移交记录数据" + DateUtil.time(), "场所检查-移交记录数据表", list, YjjlExcel.class);
	}

	/**
	 * 查询移交单位
	 */
	@GetMapping("/getYjdw")
	public R getYjdw(@RequestParam Map<String, Object> params) {
		params.put("deptId", AuthUtil.getDeptId());
		DeptListVo dept = new DeptListVo();
		if (params.get("deptCategory").toString().equals("9")) {
			// 街道、乡镇消救委
			dept = yjjlService.getJdDept(params);
		} else if (params.get("deptCategory").toString().equals("10")) {
			// 户政
			dept = yjjlService.getHzXfDept(params);
		} else if (params.get("deptCategory").toString().equals("11")) {
			// 消防
			dept = yjjlService.getXfDept(params);
		} else if (params.get("deptCategory").toString().equals("5")) {
			// 分局
			dept = yjjlService.getFjDept(params);
		} else if (params.get("deptCategory").toString().equals("12")) {
			// 县区消救委
			dept = yjjlService.getXqxjw(params);
		}
		return R.data(dept);
	}

	/**
	 * 查询报告事由
	 */
	@GetMapping("selectBgsy")
	public R selectBgsy(YjjlVO yjjlVO) {
		return R.data(yjjlService.selectBgsy(yjjlVO));
	}
}
