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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import liquibase.pro.packaged.S;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
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.springblade.modules.zbdsTable.entity.ZbdsTableEntity;
import org.springblade.modules.zbdsTable.service.IZbdsTableService;
import org.springblade.modules.zblsTable.entity.ZblsTableEntity;
import org.springblade.modules.zblsTable.service.IZblsTableService;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.zbTable.entity.ZbTableEntity;
import org.springblade.modules.zbTable.vo.ZbTableVO;
import org.springblade.modules.zbTable.wrapper.ZbTableWrapper;
import org.springblade.modules.zbTable.service.IZbTableService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.Date;
import java.util.List;

/**
 * 装备列表 控制器
 *
 * @author BladeX
 * @since 2024-06-04
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-table/table")
@Api(value = "装备列表", tags = "装备列表接口")
public class ZbTableController extends BladeController {

	private final IZbTableService zbTableService;

	private final IZblsTableService zblsTableService;

	private final IDictBizService dictBizService;

	private final IDeptService deptService;

	private final IZbdsTableService zbdsTableService;

	/**
	 * 装备列表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入table")
	public R<ZbTableVO> detail(ZbTableEntity table) {
		ZbTableEntity detail = zbTableService.getOne(Condition.getQueryWrapper(table));
		return R.data(ZbTableWrapper.build().entityVO(detail));
	}

	/**
	 * 装备列表 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入table")
	public R<IPage<ZbTableVO>> list(ZbTableEntity table, Query query) {
		IPage<ZbTableEntity> pages = zbTableService.page(Condition.getPage(query), Condition.getQueryWrapper(table));
		return R.data(ZbTableWrapper.build().pageVO(pages));
	}

	/**
	 * 装备列表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入table")
	public R<IPage<ZbTableEntity>> page(ZbTableEntity table, Query query) {
		IPage<ZbTableEntity> pages = zbTableService.selectTablePage(Condition.getPage(query), table);
		/*for (ZbTableEntity zbTableVO:pages.getRecords()) {
			String codeyt = "zbyt";
			String codezt = "zbzt";
			String zbyt = dictBizService.getCode(codeyt, zbTableVO.getZbyt());
			String zbzt = dictBizService.getCode(codezt, zbTableVO.getZbzt());
			zbTableVO.setZbyt(zbyt);
			zbTableVO.setZbzt(zbzt);
		}*/
		return R.data(pages);
	}

	/**
	 * 装备下发
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入table")
	public R save(@Valid @RequestBody ZbTableEntity table) {
		ZbTableEntity zb = zbTableService.selectZbByZbbh(table.getZbbh());
		if (zb != null) {
			if (zb.getZbzt().equals("01") || zb.getZbzt().equals("02")) {
				return R.data("该装备正在使用中");
			}
			if (zb.getZbzt().equals("05")) {
				return R.data("该装备已丢失");
			}
		}
		table.setZbzt("01");
		ZblsTableEntity zblsTable = new ZblsTableEntity();
		zblsTable.setZblx(table.getZblx());
		zblsTable.setZbbh(table.getZbbh());
		zblsTable.setZbyt(table.getZbyt());
		zblsTable.setZbzt(table.getZbzt());
		zblsTable.setLqrxm(table.getLqrxm());
		zblsTable.setXb(table.getXb());
		zblsTable.setSfzh(table.getSfzh());
		zblsTable.setJh(table.getJh());
		zblsTable.setLxdh(table.getLxdh());
		zblsTable.setLqsj(table.getLqsj());
		zblsTable.setGhsj(table.getGhsj());
		zblsTableService.save(zblsTable);
		return R.data("操作成功");
	}

	/**
	 * 装备新增
	 */
	@PostMapping("saveZb")
	public R saveZb(@RequestBody ZbTableEntity table) {
		ZbTableEntity zb = zbTableService.selectZbbh(table.getZbbh());
		if (zb != null) {
			return R.fail("装备编号已经存在，请重新输入");
		}
		Dept dept = deptService.getById(Long.valueOf(table.getDeptId()));
		if (dept.getDeptCategory() == 2) {
			table.setSssjBm(Long.valueOf(table.getDeptId()));
			table.setSssjMc(dept.getDeptName());
		}
		if (dept.getDeptCategory() == 3) {
			Dept sj = deptService.getById(dept.getParentId());
			table.setSsfjBm(Long.valueOf(table.getDeptId()));
			table.setSsfjMc(dept.getDeptName());
			table.setSssjBm(sj.getId());
			table.setSssjMc(sj.getDeptName());
		}
		if (dept.getDeptCategory() == 4) {
			Dept fj = deptService.getById(dept.getParentId());
			Dept sj = deptService.getById(fj.getParentId());
			table.setSspcsBm(Long.valueOf(table.getDeptId()));
			table.setSspcsMc(dept.getDeptName());
			table.setSsfjBm(fj.getId());
			table.setSsfjMc(fj.getDeptName());
			table.setSssjBm(sj.getId());
			table.setSssjMc(sj.getDeptName());
		}
		return R.data(zbTableService.save(table));
	}

	/**
	 * 装备列表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入table")
	public R update(@Valid @RequestBody ZbTableEntity table) {
		if (table.getZbzt().equals("03") || table.getZbzt().equals("04")) {
			table.setGhsj(new Date());
		}
		/*ZbTableEntity zb = zbTableService.selectZbByZbbh(table.getZbbh());
		if (zb != null && !table.getZbzt().equals("04")) {
			if (zb.getZbzt().equals("01") || zb.getZbzt().equals("02")) {
				return R.data("该装备正在使用中");
			}
		}*/
		if (table.getDeptId() != null && !table.getDeptId().equals("")) {
			Dept dept = deptService.getById(Long.valueOf(table.getDeptId()));
			if (dept.getDeptCategory() == 2) {
				table.setSssjBm(Long.valueOf(table.getDeptId()));
				table.setSssjMc(dept.getDeptName());
			}
			if (dept.getDeptCategory() == 3) {
				Dept sj = deptService.getById(dept.getParentId());
				table.setSsfjBm(Long.valueOf(table.getDeptId()));
				table.setSsfjMc(dept.getDeptName());
				table.setSssjBm(sj.getId());
				table.setSssjMc(sj.getDeptName());
			}
			if (dept.getDeptCategory() == 4) {
				Dept fj = deptService.getById(dept.getParentId());
				Dept sj = deptService.getById(fj.getParentId());
				table.setSspcsBm(Long.valueOf(table.getDeptId()));
				table.setSspcsMc(dept.getDeptName());
				table.setSsfjBm(fj.getId());
				table.setSsfjMc(fj.getDeptName());
				table.setSssjBm(sj.getId());
				table.setSssjMc(sj.getDeptName());
			}
		}
		zbTableService.updateById(table);
		if (!table.getZbzt().equals("") && !table.getZbzt().equals(null)) {
			ZblsTableEntity zblsTable = new ZblsTableEntity();
			zblsTable.setZblx(table.getZblx());
			zblsTable.setZbbh(table.getZbbh());
			zblsTable.setZbyt(table.getZbyt());
			zblsTable.setZbzt(table.getZbzt());
			zblsTable.setLqrxm(table.getLqrxm());
			zblsTable.setXb(table.getXb());
			zblsTable.setSfzh(table.getSfzh());
			zblsTable.setJh(table.getJh());
			zblsTable.setLxdh(table.getLxdh());
			zblsTable.setLqsj(table.getLqsj());
			zblsTable.setGhsj(table.getGhsj());
			zblsTableService.save(zblsTable);
		}
		return R.data("操作成功");
	}

	/**
	 * 装备列表 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入table")
	public R submit(@Valid @RequestBody ZbTableEntity table) {
		return R.status(zbTableService.saveOrUpdate(table));
	}

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

	/**
	 * 查看历史记录
	 */
	@GetMapping("selectLsjl")
	public R selectLsjl(String zbbh) {
		List<ZblsTableEntity> list = zbTableService.selectLsjl(zbbh);
		ZblsTableEntity lsjl = zbTableService.getLsjlByLqsj(zbbh);
		String json = JSONArray.toJSONString(list);
		if (lsjl != null) {
			if (!json.contains(lsjl.getId().toString())) {
				list.add(lsjl);
			}
		}
		return R.data(list);
	}

	/**
	 * 获取装备用途字典
	 */
	@GetMapping("getZbytZd")
	public R getZbytZd() {
		return R.data(dictBizService.getZbytZd());
	}

	/**
	 * 获取装备类型字典
	 */
	@GetMapping("getZblxZd")
	public R getZblxZd() {
		return R.data(dictBizService.getZblxZd());
	}

	/**
	 * 地图返回
	 */
	@GetMapping("selectDt")
	public R selectDt(ZbTableEntity zbTable) {
		return R.data(zbTableService.selectDt(zbTable));
	}

	/**
	 * 点击地图点位获取当前点位的所有枪支信息
	 */
	@GetMapping("getZbByDw")
	public R getZbByDw(String[] id) {
		return R.data(zbTableService.getZbByDw(id));
	}

	/**
	 * 装备归还
	 */
	@PostMapping("getZbgh")
	public R getZbgh(ZblsTableEntity zbTable) {
		zbTable.setZbzt("04");
		zbTable.setGhsj(new Date());
		return R.data(zblsTableService.save(zbTable));
	}

	/**
	 * 获取当前装备的最新点位
	 */
	@GetMapping("getZbzxdw")
	public R getZbzxdw(String zbbh) {
		return R.data(zbTableService.getZbzxdw(zbbh));
	}

	/**
	 * 装备丢失
	 */
	@PostMapping("getZbds")
	public R getZbds(ZblsTableEntity zbTable) {
		ZbdsTableEntity zbdsTable = new ZbdsTableEntity();
		zbdsTable.setZbbh(zbTable.getZbbh());
		zbdsTable.setZhdw(zbTable.getZhdw());
		zbdsTable.setDsdd(zbTable.getDsdd());
		zbdsTableService.save(zbdsTable);
		zbTable.setZbzt("05");
		zblsTableService.save(zbTable);
		return R.data("操作成功");
	}

	/**
	 * 装备下发获取当前派出所的所有装备
	 */
	@GetMapping("selectZbByPcs")
	public R selectZbByPcs(ZbTableEntity zbTable) {
		return R.data(zbTableService.selectZbByPcs(zbTable));
	}
}
