package org.springblade.modules.sqjw.pcsInfo.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.common.cache.DictCache;
import org.springblade.common.enums.DictEnum;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.cache.utils.CacheUtil;
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.support.Kv;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.log.logPcs.entity.LogPcsEntity;
import org.springblade.modules.log.logPcs.service.ILogPcsService;
import org.springblade.modules.sqjw.pcsInfo.entity.PcsInfoEntity;
import org.springblade.modules.sqjw.pcsInfo.excel.PcsInfoExcel;
import org.springblade.modules.sqjw.pcsInfo.service.IPcsInfoService;
import org.springblade.modules.sqjw.pcsInfo.vo.DzxxVO;
import org.springblade.modules.sqjw.pcsInfo.vo.PcsInfoVO;
import org.springblade.modules.sqjw.pcsInfo.vo.PcsZdVO;
import org.springblade.modules.sqjw.pcsInfo.wrapper.PcsInfoWrapper;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.vo.ZdVO;
import org.springframework.cache.annotation.Cacheable;
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.List;
import java.util.Map;

import static org.springblade.core.cache.constant.CacheConstant.SYS_CACHE;

/**
 * 派出所信息表 控制器
 *
 * @author BladeX
 * @since 2024-01-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-pcsInfo/pcsInfo")
@Api(value = "派出所信息表", tags = "派出所信息表接口")
public class PcsInfoController extends BladeController {

	private final IPcsInfoService pcsInfoService;

	private final ILogPcsService pcsLogService;

	private final IDeptService deptService;

	/**
	 * 派出所信息表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入pcsInfo")
	public R<PcsInfoVO> detail(Long deptId) {
		PcsInfoVO detail = pcsInfoService.getDetail(deptId);
		return R.data(detail);
	}

	/**
	 * 派出所信息表 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入pcsInfo")
	public R<IPage<PcsInfoVO>> list(@ApiIgnore @RequestParam Map<String, Object> pcsInfo, Query query) {
		IPage<PcsInfoEntity> pages = pcsInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(pcsInfo, PcsInfoEntity.class));
		return R.data(PcsInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 派出所信息表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入pcsInfo")
	public R page(@RequestParam Map<String, Object> params, Query query) {
		params.put("deptId", AuthUtil.getDeptId());
		IPage<PcsInfoVO> pages = pcsInfoService.selectPcsInfoPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * 派出所信息表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入pcsInfo")
	public R save(@Valid @RequestBody PcsInfoEntity pcsInfo) {
		return R.status(pcsInfoService.save(pcsInfo));
	}

	/**
	 * 派出所信息表 修改
	 */
	@Transactional
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入pcsInfo")
	public R update(@Valid @RequestBody PcsInfoVO pcsInfoVO) {
		// 先插入blade_dept表
		Dept dept = new Dept();
		dept.setId(Long.valueOf(pcsInfoVO.getGajgbm()));
		dept.setParentId(Long.valueOf(pcsInfoVO.getSsdwbm()));
		dept.setDeptCategory(6);
		dept.setFullName(pcsInfoVO.getGajgmc());
		dept.setDeptName(pcsInfoVO.getGajgmc());
		dept.setSort(1);
		if (deptService.submit(dept)) {
			CacheUtil.clear(SYS_CACHE);
			CacheUtil.clear(SYS_CACHE, Boolean.FALSE);
			// 返回懒加载树更新节点所需字段
			Kv.create().set("id", String.valueOf(dept.getId())).set("tenantId", dept.getTenantId()).set("deptCategoryName", DictCache.getValue(DictEnum.ORG_CATEGORY, dept.getDeptCategory()));
		}
		PcsInfoEntity pcsInfoEntity = new PcsInfoEntity();
		BeanUtil.copy(pcsInfoVO, pcsInfoEntity);
		if (null == pcsInfoEntity.getId()) {
			pcsInfoService.save(pcsInfoEntity);
		} else {
			pcsInfoService.updateById(pcsInfoEntity);
		}
		// 保存日志
		LogPcsEntity pcsLog = new LogPcsEntity();
		BeanUtil.copy(pcsInfoEntity, pcsLog);
		pcsLogService.save(pcsLog);
		return R.success("操作成功");
	}

	/**
	 * 派出所信息表 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入pcsInfo")
	public R submit(@Valid @RequestBody PcsInfoEntity pcsInfo) {
		return R.status(pcsInfoService.saveOrUpdate(pcsInfo));
	}

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

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

	/**
	 * 获取字典
	 */
	@GetMapping("/dictionary")
	@Cacheable(cacheNames = "blade:getPcsglZd")
	public R dictionary() {
		PcsZdVO data = new PcsZdVO();

		String code = "";

		code = "gajgxz"; // 公安机关性质
		List<ZdVO> gajgxz = pcsInfoService.getDictList(code);
		data.setGajgxz(gajgxz);

		code = "gajgjb"; // 公安机关级别
		List<ZdVO> gajgjb = pcsInfoService.getDictList(code);
		data.setGajgjb(gajgjb);

		code = "bzgg"; // 编制规格
		List<ZdVO> bzgg = pcsInfoService.getDictList(code);
		data.setBzgg(bzgg);

		code = "pcsgllb"; // 派出所管理类别
		List<ZdVO> pcsgllb = pcsInfoService.getDictList(code);
		data.setPcsgllb(pcsgllb);

		code = "pcslb"; // 派出所类别
		List<ZdVO> pcslb = pcsInfoService.getDictList(code);
		data.setPcslb(pcslb);

		code = "pcsdj"; // 派出所等级
		List<ZdVO> pcsdj = pcsInfoService.getDictList(code);
		data.setPcsdj(pcsdj);

		code = "fqsqk"; // 枫桥所情况
		List<ZdVO> fqsqk = pcsInfoService.getDictList(code);
		data.setFqsqk(fqsqk);

		code = "pc"; // 枫桥所情况
		List<ZdVO> pc = pcsInfoService.getDictList(code);
		data.setPc(pc);

		return R.data(data);
	}

	/**
	 * 查询负责人(所长)
	 */
	@GetMapping("/getFzr")
	public R getFzr(@RequestParam Map<String, Object> params) {
		List<User> userList = pcsInfoService.getFzr(params);
		return R.data(userList);
	}

	/**
	 * 查询分局或市局
	 */
	@GetMapping("/getFjSj")
	public R getFjSj(@RequestParam Map<String, Object> params) {
		List<Dept> deptList = pcsInfoService.getFjSj(params);
		return R.data(deptList);
	}

	/**
	 * 根据parentId查询子部门
	 */
	@GetMapping("/getChildDept")
	public R getChildDept(@RequestParam Map<String, Object> params) {
		List<Dept> deptList = pcsInfoService.getChildDept(params);
		return R.data(deptList);
	}


	/**
	 * 查询派出所
	 * 派出所名称deptName
	 */
	@GetMapping("/getAllPcs")
	public R getAllPcs(@RequestParam Map<String, Object> params) {
		List<PcsInfoVO> pcsList = pcsInfoService.getAllPcs(params);
		return R.data(pcsList);
	}

	/**
	 * 查询责任区(社区)
	 * 派出所id
	 */
	@GetMapping("/getSqByPcsid")
	public R getSqByPcsid(@RequestParam Map<String, Object> params) {
		List<PcsInfoVO> sqList = pcsInfoService.getSqByPcsid(params);
		return R.data(sqList);
	}

	/**
	 * 查询警格
	 */
	@GetMapping("/getJgBySqId")
	public R getJgBySqId(@RequestParam Map<String, Object> params) {
		List<PcsInfoVO> jgList = pcsInfoService.getJgBySqId(params);
		return R.data(jgList);
	}

	/**
	 * 查询地址信息
	 */
	@GetMapping("/getDzxx")
	public R getDzxx(@RequestParam Map<String, Object> params, Query query) {
		IPage<DzxxVO> pages = pcsInfoService.getDzxx(Condition.getPage(query), params);
		return R.data(pages);
	}
}
