package cn.superdesk.uniorder.controller.sys.role;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.sys.SysUser;
import cn.superdesk.uniorder.dao.entity.sys.role.SysRoleExtData;
import cn.superdesk.uniorder.dto.dubbo.sys.role.SysRoleExtDataParam;
import cn.superdesk.uniorder.dubbo.consumer.sys.SysUserDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.sys.role.SysRoleExtDataDubboConsumerService;
import cn.superdesk.uniorder.common.lamda.AnLamdaUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Description: 角色定义-扩展-数据权限角色
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2020-08-26 14:15:28
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020-08-26 14:15:28
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "系统权限-角色(数据权限)-管理", tags = {"系统权限-角色(数据权限)-管理"})
@Slf4j
@RestController
@RequestMapping("/sysRoleExtData")
public class SysRoleExtDataController extends BaseController {

	@Autowired
	private SysUserDubboConsumerService        sysUserDubboConsumerService;
	@Autowired
	private SysRoleExtDataDubboConsumerService sysRoleExtDataDubboConsumerService;

	@ApiOperation(value = "分页查询")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public R<List<SysRoleExtDataParam>> pageQuerySysRoleExtDatas(@RequestBody PageQueryParam param) {

		log.info("pageQuerySysRoleExtData:\n{}", AnJsonUtils.toJson(param));

		R<List<SysRoleExtData>>      resultEntityList = new R<>();
		R<List<SysRoleExtDataParam>> resultVoList     = new R<>();

//		searchKeyWord // 数据权限名称 // 数据权限描述

		// TODO 参数合法性判定

		resultEntityList = sysRoleExtDataDubboConsumerService.pageQuery(param.getConditions(), param.getPageNo(), param.getPageSize());

		List<Long> sysUserIds = resultEntityList.getData().stream().filter(AnLamdaUtils.distinctByKey(o -> o.getCreateUserId())).map(o -> o.getCreateUserId()).collect(Collectors.toList());

		log.info("sysUserIds : {}", AnJsonUtils.toPrettyJson(sysUserIds));

		Map<Long, SysUser> sysUserMap = sysUserDubboConsumerService.convertSysUserIds2MapSysUserId2Entity(sysUserIds);

		// 转换
		List<SysRoleExtDataParam> data = new ArrayList<>();
		for (SysRoleExtData entity : resultEntityList.getData()) {

			// 大部分 都能直接 过去了
			SysRoleExtDataParam vo = AnBeanUtils.copy(entity, SysRoleExtDataParam.class);


			if (sysUserMap.get(vo.getCreateUserId()) != null) {
				vo.setCreateUserName(sysUserMap.get(vo.getCreateUserId()).getFullname());
			} else if (entity.getCreateUserId().equals(1L)) {
				vo.setCreateUserName("系统");
			}


			data.add(vo);

		}

		resultVoList.setCurrentPage(resultEntityList.getCurrentPage());
		resultVoList.setPageSize(resultEntityList.getPageSize());
		resultVoList.setCount(resultEntityList.getCount());
		resultVoList.setData(data);

		return resultVoList;
	}

	@ApiOperation(value = "按id查询")
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public R<List<SysRoleExtData>> getById(@PathVariable("id") long id) {

		R<List<SysRoleExtData>> result = new R<>();

		// TODO 参数合法性判定

		SysRoleExtData entity = sysRoleExtDataDubboConsumerService.findSysRoleExtDataById(id);

		List<SysRoleExtData> data = new ArrayList<>();
		data.add(entity);

		result.setData(data);

		return result;
	}

	@ApiOperation(value = "新增")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public R<String> addSysRoleExtData(@RequestBody SysRoleExtDataParam param) {

		log.info("addSysRoleExtData:\n{}", AnJsonUtils.toJson(param));


		// TODO 参数合法性判定

		sysRoleExtDataDubboConsumerService.addSysRoleExtData(getToken(), param);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "更新")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public R<String> updateSysRoleExtData(@RequestBody SysRoleExtDataParam param) {

		log.info("updateSysRoleExtData:\n{}", AnJsonUtils.toJson(param));

		sysRoleExtDataDubboConsumerService.updateSysRoleExtData(getToken(), param);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "删除")
	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public R<String> deleteSysRoleExtData(@PathVariable("id") long id) {

		// TODO 参数合法性判定

		sysRoleExtDataDubboConsumerService.deleteSysRoleExtData(id);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "批量删除")
	@RequestMapping(value = "delete_ids", method = RequestMethod.POST)
	@ResponseBody
	public R<String> deleteIdsSysRoleExtData(@RequestParam(value = "ids") long[] ids) {

		log.info(AnJsonUtils.toJson(ids));

		for (long id : ids) {
			sysRoleExtDataDubboConsumerService.deleteSysRoleExtData(id);
		}

		return new R<>("200", "操作成功", true);

	}


}
