/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2022/8/22
 */
package org.springblade.modules.baseinfo.org.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springblade.constant.BusinessConstant;
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.BladeUser;
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.baseinfo.org.dto.OrgInfoDTO;
import org.springblade.modules.baseinfo.org.entity.DeptRegionEntity;
import org.springblade.modules.baseinfo.org.service.IDeptRegionService;
import org.springblade.modules.baseinfo.org.vo.OrgInfoVO;
import org.springblade.modules.baseinfo.org.wrapper.OrgInfoWrapper;
import org.springblade.modules.system.entity.Dept;
import org.springblade.modules.system.service.IDeptService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


/***
 * Date:2022/8/22
 * Title: 组织机构管理
 * Description：系统管理员管理盟市，盟市管理员管理运维单位
 * @author swp
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@RestController
@RequestMapping("galaxy-business/baseinfo/org")
@AllArgsConstructor
@Api(value = "运维单位管理", tags = "运维单位管理")
public class BaseInfoOrgController extends BladeController {
	private final IDeptService deptService;
	private final IDeptRegionService deptRegionService;

	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "分页")
	public R<List<OrgInfoVO>> list() {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName())) {
			LambdaQueryWrapper<Dept> wrapper = Wrappers.<Dept>query().lambda();
			wrapper.eq(Dept::getIsDeleted, 0);
			wrapper.eq(Dept::getParentId, Func.toLong(user.getDeptId()));
			List<Dept> pages = deptService.list(wrapper);
			return R.data(OrgInfoWrapper.build().listVO(pages));
		} else {
			return R.data(null);
		}
	}

	/**
	 * 多表联合查询自定义分页
	 */
	@GetMapping("/page")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "deptName", value = "机构名称", paramType = "query", dataType = "string"),
	})
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "分页")
	public R<IPage<OrgInfoVO>> page(@ApiIgnore Dept entity, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName()) || BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName())) {
			LambdaQueryWrapper<Dept> wrapper = Wrappers.<Dept>query().lambda();
			wrapper.eq(Dept::getIsDeleted, 0);
			wrapper.eq(Dept::getParentId, Func.toLong(user.getDeptId()));
			if (entity.getDeptName() != null && entity.getDeptName().length() > 0) {
				wrapper.like(Dept::getDeptName, entity.getDeptName());
			}
			IPage<Dept> pages = deptService.page(Condition.getPage(query), wrapper);
			return R.data(OrgInfoWrapper.build().pageVO(pages));
		} else {
			return R.data(null);
		}
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "详情", notes = "传入 Dept")
	public R<Dept> detail(Dept dept) {
		Dept vo = deptService.getById(dept.getId());
		return R.data(vo);
	}


	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "新增", notes = "传入 Dept")
	@Transactional
	public R save(@RequestBody OrgInfoDTO dept) {
		BladeUser user = AuthUtil.getUser();
		dept.setFullName(dept.getDeptName());
		dept.setParentId(Func.toLong(user.getDeptId()));
		Dept parentDept = deptService.getById(Func.toLong(user.getDeptId()));
		dept.setAncestors(parentDept.getAncestors() + "," + parentDept.getId());
		if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
			dept.setDeptCategory(2);
		} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName())) {
			dept.setDeptCategory(1);
		}
		if (deptService.save(dept)) {
			DeptRegionEntity deptRegionEntity = new DeptRegionEntity();
			deptRegionEntity.setDeptId(dept.getId());
			if (BusinessConstant.ROLE_SYS_ADMIN.equals(user.getRoleName())) {
				deptRegionEntity.setAdcd(dept.getAdcd());
			} else if (BusinessConstant.ROLE_ORG_ADMIN.equals(user.getRoleName())) {
				LambdaQueryWrapper<DeptRegionEntity> deptRegionEntityLambdaQueryWrapper = Wrappers.<DeptRegionEntity>query().lambda();
				deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getIsDeleted, 0);
				deptRegionEntityLambdaQueryWrapper.eq(DeptRegionEntity::getDeptId, Func.toLong(user.getDeptId()));
				DeptRegionEntity orgAdmindRegion = deptRegionService.getOne(deptRegionEntityLambdaQueryWrapper);
				deptRegionEntity.setAdcd(orgAdmindRegion.getAdcd());
			}
			if (deptRegionService.save(deptRegionEntity)) {
				return R.status(true);
			} else {
				return R.status(false);
			}
		} else {
			return R.status(false);
		}
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "修改", notes = "传入 Dept")
	public R update(@RequestBody Dept dept) {
		return R.status(deptService.updateById(dept));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入 Dept")
	public R submit(@RequestBody Dept dept) {
		return R.status(deptService.saveOrUpdate(dept));
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ID数组")
	public R remove(@ApiParam(value = "主键集合") @RequestParam String ids) {
		if (null == ids) {
			return R.status(false);
		}
		List<Long> list = Func.toLongList(ids);
		for (Long id : list) {
			deptService.removeById(id);
		}
		return R.status(true);
	}
}
