package org.springblade.modules.gzInfo.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.gzInfo.entity.InfoEntity;
import org.springblade.modules.gzInfo.excel.InfoExcel;
import org.springblade.modules.gzInfo.service.IInfoService;
import org.springblade.modules.gzInfo.vo.InfoVO;
import org.springblade.modules.gzInfo.vo.ZdVo;
import org.springblade.modules.gzInfo.vo.ZdVos;
import org.springblade.modules.gzInfo.vo.ZdVoss;
import org.springblade.modules.gzInfo.wrapper.InfoWrapper;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IDictBizService;
import org.springblade.modules.system.service.IUserService;
import org.springblade.modules.system.vo.FjVO;
import org.springblade.modules.system.vo.PcsVO;
import org.springblade.modules.system.vo.SqVO;
import org.springblade.modules.system.vo.UserVO;
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;

/**
 * gz_info 控制器
 *
 * @author BladeX
 * @since 2023-07-07
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-info/info")
@Api(value = "gz_info", tags = "gz_info接口")
public class InfoController extends BladeController {

	private final IInfoService infoService;
	private final IDictBizService dictBizService;
	private final IUserService userService;
	private final IDeptService deptService;

	/**
	 * gz_info 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入info")
	public R<InfoVO> detail(InfoEntity info) {
		InfoVO detail = infoService.getDetailById(info.getId());
		return R.data(detail);
	}

	/**
	 * gz_info 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入info")
	public R<IPage<InfoVO>> list(@ApiIgnore @RequestParam Map<String, Object> info, Query query) {
		IPage<InfoEntity> pages = infoService.page(Condition.getPage(query), Condition.getQueryWrapper(info, InfoEntity.class));
		for (InfoEntity infoEntity : pages.getRecords()) {
			QueryWrapper<User> qe = new QueryWrapper<>();
			qe.eq("id", infoEntity.getUserId());
			User user = userService.getOne(qe);
			infoEntity.setName(user.getName());
		}
		return R.data(InfoWrapper.build().pageVO(pages));
	}

	/**
	 * gz_info 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入info")
	public R<IPage<InfoVO>> page(@RequestParam Map<String, Object> params, Query query) {
		IPage<InfoVO> pages = infoService.selectInfoPage(Condition.getPage(query), params);
		return R.data(pages);
	}

	/**
	 * gz_info 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入info")
	public R save(@Valid @RequestBody InfoEntity info) {
		//return R.status(infoService.save(info));
		return R.status(infoService.insertGz(info));
	}

	/**
	 * gz_info 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入info")
	public R update(@Valid @RequestBody InfoEntity info) {
		return R.status(infoService.updateById(info));
	}

	/**
	 * gz_info 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入info")
	public R submit(@Valid @RequestBody InfoEntity info) {
		info.setUserId(AuthUtil.getUserId());
//		if (info.getGzlxList()!=null) {
//			for (int i = 0; i < info.getGzlxList().size(); i++) {
//				if (i == 2) {
//					info.setGzlx(info.getGzlxList().get(i));
//				}
//			}
//		}
		return R.status(infoService.saveOrUpdate(info));
	}

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


	/**
	 * 导出数据
	 */
	@GetMapping("/export-info")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "导出数据", notes = "传入info")
	public void exportInfo(@ApiIgnore @RequestParam Map<String, Object> info, BladeUser bladeUser, HttpServletResponse response) {
		QueryWrapper<InfoEntity> queryWrapper = Condition.getQueryWrapper(info, InfoEntity.class);
		//if (!AuthUtil.isAdministrator()) {
		//	queryWrapper.lambda().eq(Info::getTenantId, bladeUser.getTenantId());
		//}
		queryWrapper.lambda().eq(InfoEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
		List<InfoExcel> list = infoService.exportInfo(queryWrapper);
		ExcelUtil.export(response, "gz_info数据" + DateUtil.time(), "gz_info数据表", list, InfoExcel.class);
	}

	/**
	 * 工作类型字典查询-WEB
	 */
//	@GetMapping("selectGzlx")
//	public R selectGzlx(){
//		List<ZdVo> list = dictBizService.selectGzlx();
//		for (ZdVo zdVo:list){
//			List<ZdVos> lists = dictBizService.selectGzlxs(zdVo.getId());
//			zdVo.setChildren(lists);
//			for (ZdVos zdVos:lists){
//				List<ZdVoss> listss = dictBizService.selectGzlxss(zdVos.getId());
//				zdVos.setChildren(listss);
//			}
//		}
//		return R.data(list);
//	}

	/**
	 * 工作类型字典查询-app
	 */
	@GetMapping("/selectGzlx1")
	public R selectGzlx1(int type) {
		List<ZdVo> list = dictBizService.selectGzlx(type);
		return R.data(list);
	}

	/**
	 * 工作类型字典查询-app
	 */
	@GetMapping("/selectGzlx2")
	public R selectGzlx2(Long id, int type) {
		List<ZdVos> list = dictBizService.selectGzlxs(id, type);
		return R.data(list);
	}

	/**
	 * 工作类型字典查询-app
	 */
	@GetMapping("/selectGzlx3")
	public R selectGzlx3(Long id, int type) {
		List<ZdVoss> list = dictBizService.selectGzlxss(id, type);
		return R.data(list);
	}

	/**
	 * 获取派出所列表
	 */
	@GetMapping("/getPcsList")
	public R getPcsList() {
		List<FjVO> fjList = deptService.getfJList(AuthUtil.getDeptId());
		for (FjVO fj : fjList) {
			List<PcsVO> pcsList = deptService.getPcsList(AuthUtil.getDeptId(), fj.getId());
			for (PcsVO pcs : pcsList) {
				List<SqVO> sqList = deptService.getSqList(AuthUtil.getDeptId(), pcs.getId());
				pcs.setSqList(sqList);
			}
			fj.setPcsList(pcsList);
		}
		return R.data(fjList);
	}

	/**
	 * 根据派出所id获取民警列表
	 */
	@GetMapping("/getMjList")
	public R getMjList(@RequestParam Map<String, Object> params) {
		List<UserVO> mjList = userService.getMjList(params);
		return R.data(mjList);
	}

	/**
	 * 根据加分项/减分项列表 type=1 加分项 type=2 减分项
	 */
	@GetMapping("/getXmList")
	public R getXmList(@RequestParam Map<String, Object> params) {
		List<ZdVos> xmList = dictBizService.getXmList(params);
		for (ZdVos zdVos : xmList) {
			List<ZdVoss> zdVoss = dictBizService.getXmList2(zdVos.getId());
			zdVos.setChildren(zdVoss);
		}
		return R.data(xmList);
	}
}
