/*
 *      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.cdglinfos.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.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.cdglinfos.entity.GlInfoEntity;
import org.springblade.modules.cdglinfos.service.CdUserRoleGxbService;
import org.springblade.modules.cdglinfos.service.IGlInfoService;
import org.springblade.modules.cdglinfos.vo.GlInfoVO;
import org.springblade.modules.cdglinfos.wrapper.GlInfoWrapper;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.entity.Role;
import org.springblade.modules.system.service.IDeptService;
import org.springblade.modules.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 菜单信息表 控制器
 *
 * @author ldx
 * @since 2023-08-14
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-glinfo/glinfo")
@Api(value = "菜单信息表", tags = "菜单信息表接口")
public class GlInfoController extends BladeController {

	private final IGlInfoService glinfoService;

	private final IDeptService deptService;
	@Autowired
	CdUserRoleGxbService cdUserRoleGxbService;
	@Autowired
	IRoleService roleService;
	/**
	 * 菜单信息表 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入glinfo")
	public R<GlInfoVO> detail(GlInfoEntity glinfo) {
		GlInfoEntity detail = glinfoService.getOne(Condition.getQueryWrapper(glinfo));
		return R.data(GlInfoWrapper.build().entityVO(detail));
	}
	/**
	 * 菜单信息表 分页
	 */

	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入glinfo")
	public R<IPage<GlInfoVO>> list(@ApiIgnore @RequestParam Map<String, Object> glinfo, Query query) {
		IPage<GlInfoEntity> pages = glinfoService.page(Condition.getPage(query), Condition.getQueryWrapper(glinfo, GlInfoEntity.class));
		return R.data(GlInfoWrapper.build().pageVO(pages));
	}
//	public R<IPage<GlInfoVO>> list(@ApiIgnore  GlInfoEntity glinfo, Query query) {
//		Map<String, Object> params = JsonUtil.readMap(JsonUtil.toJson(glinfo), String.class, Object.class);
//		IPage<GlInfoEntity> pages = glinfoService.page(Condition.getPage(query), Condition.getQueryWrapper(params, GlInfoEntity.class));
//		return R.data(GlInfoWrapper.build().pageVO(pages));
//	}



	/**
	 * 菜单信息表 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入glinfo")
	public R<IPage<GlInfoVO>> page(GlInfoVO glinfo, Query query) {
		IPage<GlInfoVO> pages = glinfoService.selectGlInfoPage(Condition.getPage(query), glinfo);
		return R.data(pages);
	}

	/**
	 * 菜单信息表 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入glinfo")
	public R save(@Valid @RequestBody GlInfoEntity glinfo) {
		return R.status(glinfoService.save(glinfo));
	}

	/**
	 * 菜单信息表 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入glinfo")
	public R update(@Valid @RequestBody GlInfoEntity glinfo) {
		return R.status(glinfoService.updateById(glinfo));
	}

	/**
	 * 菜单信息表 新增或修改
	 */

	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入glinfo")
	public R submit(@Valid @RequestBody GlInfoEntity glinfo) {
		String[] roleArr = glinfo.getRoleName();
		StringBuilder builder = new StringBuilder();
		if(roleArr != null && roleArr.length>0){
			for (int i = 0; i < roleArr.length; i++) {
				QueryWrapper<Role> qe = new QueryWrapper<>();
				qe.eq("id",Long.parseLong(roleArr[i]));
				Role one = roleService.getOne(qe);
				builder.append(one.getRoleName()+",");
			}
			String roleStr = builder.toString();
			glinfo.setRoles(roleStr.substring(0, roleStr.length()-1));
		}
		glinfoService.saveOrUpdate(glinfo);
		return R.status(true);
	}
//	@ApiEncrypt(CryptoType.AES)
//	@ApiDecrypt(CryptoType.AES)
	@GetMapping("getAppByUserRole")
	public R<List<GlInfoEntity>> getAppByUserRole(@RequestParam Map<String, String> params){
		Dept dept = deptService.getById(Long.valueOf(AuthUtil.getDeptId().split(",")[0]));
		return R.data(glinfoService.getAppByUserRole(dept.getAncestors(), AuthUtil.getUser().getRoleName(), params.get("appName")));
	}

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


}
