/*
 *      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.fxpc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.secure.BladeUser;
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.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.fxpc.entity.FxpcEntity;
import org.springblade.modules.fxpc.vo.FxpcVO;
import org.springblade.modules.fxpc.excel.FxpcExcel;
import org.springblade.modules.fxpc.wrapper.FxpcWrapper;
import org.springblade.modules.fxpc.service.IFxpcService;
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.BladeConstant;
import springfox.documentation.annotations.ApiIgnore;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * 风险排查 控制器
 *
 * @author BladeX
 * @since 2024-05-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-fxpc/fxpc")
@Api(value = "风险排查", tags = "风险排查接口")
public class FxpcController extends BladeController {

	private final IFxpcService fxpcService;

	private final IDeptService deptService;

	private final IDictBizService dictBizService;

	/**
	 * 风险排查 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入fxpc")
	public R<FxpcVO> detail(FxpcEntity fxpc) {
		FxpcEntity detail = fxpcService.getOne(Condition.getQueryWrapper(fxpc));
		return R.data(FxpcWrapper.build().entityVO(detail));
	}
	/**
	 * 风险排查 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入fxpc")
	public R<IPage<FxpcVO>> list(@ApiIgnore @RequestParam Map<String, Object> fxpc, Query query) {
		IPage<FxpcEntity> pages = fxpcService.page(Condition.getPage(query), Condition.getQueryWrapper(fxpc, FxpcEntity.class));
		return R.data(FxpcWrapper.build().pageVO(pages));
	}

	/**
	 * 风险排查 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入fxpc")
	public R<IPage<FxpcEntity>> page(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		params.put("roleName", AuthUtil.getUserRole());
		IPage<FxpcEntity> pages = fxpcService.selectFxpcPage(Condition.getPage(query), params);
		for (FxpcEntity fxpcVO:pages.getRecords()) {
			String[] gsjyms = fxpcVO.getGsjyms().split(",");
			String gsjymsname = "";
			for (String jyms:gsjyms) {
				String code = "gsjyms";
				gsjymsname += dictBizService.getValue(code, jyms) + "，";
			}
			int lastIndex = gsjymsname.lastIndexOf("，");
			gsjymsname = gsjymsname.substring(0, lastIndex);
			fxpcVO.setGsjymsname(gsjymsname);
			String[] gsrcjyqk = fxpcVO.getGsrcjyqk().split(",");
			String gsrcjyqkname = "";
			for (String rcjyqk:gsrcjyqk) {
				String code = "gsrcjyqk";
				gsrcjyqkname += dictBizService.getValue(code, rcjyqk) + "，";
			}
			int lastIndex2 = gsrcjyqkname.lastIndexOf("，");
			gsrcjyqkname = gsrcjyqkname.substring(0, lastIndex2);
			fxpcVO.setGsrcjyqkname(gsrcjyqkname);
		}
		return R.data(pages);
	}

	/**
	 * 风险排查 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入fxpc")
	public R save(@Valid @RequestBody FxpcEntity fxpc) {
		String deptId = AuthUtil.getDeptId().split(",")[0];
		Dept byId = deptService.getById(Long.valueOf(deptId));
		if (byId.getDeptCategory().equals("6")) {
			Dept fj = deptService.getById(byId.getParentId());
			Dept sj = deptService.getById(fj.getParentId());
			fxpc.setSspcsBm(byId.getId());
			fxpc.setSspcsMc(byId.getDeptName());
			fxpc.setSsfjBm(fj.getId());
			fxpc.setSsfjMc(fj.getDeptName());
			fxpc.setSssjBm(sj.getId());
			fxpc.setSssjMc(sj.getDeptName());
		} else if (byId.getDeptCategory().equals("5")) {
			Dept sj = deptService.getById(byId.getParentId());
			fxpc.setSsfjBm(byId.getId());
			fxpc.setSsfjMc(byId.getDeptName());
			fxpc.setSssjBm(sj.getId());
			fxpc.setSssjMc(sj.getDeptName());
		} else if (byId.getDeptCategory().equals("7")) {
			QueryWrapper<Dept> sssq = new QueryWrapper<>();
			sssq.eq("id", Long.valueOf(deptId));
			Dept sq = deptService.getOne(sssq);
			fxpc.setSssqBm(sq.getId());
			fxpc.setSssqMc(sq.getDeptName());
			QueryWrapper<Dept> sspcs = new QueryWrapper<>();
			sspcs.eq("id", sq.getParentId());
			Dept pcs = deptService.getOne(sspcs);
			fxpc.setSspcsBm(pcs.getId());
			fxpc.setSspcsMc(pcs.getDeptName());
			QueryWrapper<Dept> ssfj = new QueryWrapper<>();
			ssfj.eq("id", pcs.getParentId());
			Dept fj = deptService.getOne(ssfj);
			fxpc.setSsfjBm(fj.getId());
			fxpc.setSsfjMc(fj.getDeptName());
			QueryWrapper<Dept> sssj = new QueryWrapper<>();
			sssj.eq("id", fj.getParentId());
			Dept sj = deptService.getOne(sssj);
			fxpc.setSssjBm(sj.getId());
			fxpc.setSssjMc(sj.getDeptName());
		}
		return R.status(fxpcService.save(fxpc));
	}

	/**
	 * 风险排查 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入fxpc")
	public R update(@Valid @RequestBody FxpcEntity fxpc) {
		return R.status(fxpcService.updateById(fxpc));
	}

	/**
	 * 风险排查 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入fxpc")
	public R submit(@Valid @RequestBody FxpcEntity fxpc) {
		return R.status(fxpcService.saveOrUpdate(fxpc));
	}

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


	/**
	 * 导出数据
	 */
	@GetMapping("/export-fxpc")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入fxpc")
	public void exportFxpc(@ApiIgnore @RequestParam Map<String, Object> fxpc, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<FxpcEntity> queryWrapper = Condition.getQueryWrapper(fxpc, FxpcEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Fxpc::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(FxpcEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<FxpcExcel> list = fxpcService.exportFxpc(queryWrapper);
		ExcelUtil.export(response, "风险排查数据" + DateUtil.time(), "风险排查数据表", list, FxpcExcel.class);
	}

}
