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.enums.common.CommonSourceFromEnum;
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.SysRoleExtDataSample;
import cn.superdesk.uniorder.dto.dubbo.sys.role.SysRoleExtDataSampleParam;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysEnterpriseDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysOrganizationDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.sys.SysUserDubboConsumerService;
import cn.superdesk.uniorder.dubbo.consumer.sys.role.SysRoleExtDataSampleDubboConsumerService;
import cn.superdesk.uniorder.common.lamda.AnLamdaUtils;
import cn.superdesk.uniorder.common.numerical.AnStringUtils;
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("/sysRoleExtDataSample")
public class SysRoleExtDataSampleController extends BaseController {

	@Autowired
	private SysOrganizationDubboConsumerService sysOrganizationDubboConsumerService;
	@Autowired
	private SysEnterpriseDubboConsumerService   sysEnterpriseDubboConsumerService;
	@Autowired
	private SysUserDubboConsumerService         sysUserDubboConsumerService;

	@Autowired
	private SysRoleExtDataSampleDubboConsumerService sysRoleExtDataSampleDubboConsumerService;

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

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

		R<List<SysRoleExtDataSample>>      resultEntityList = new R<>();
		R<List<SysRoleExtDataSampleParam>> resultVoList     = new R<>();

		// outOrgId
		// outOrgName


		// TODO 参数合法性判定

		resultEntityList = sysRoleExtDataSampleDubboConsumerService.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());

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

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

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

			vo.setSourceFromName(CommonSourceFromEnum.getBySourceFromCode(vo.getSourceFrom()).getSourceFromName());

			List<Long> searchOutOrgIds = new ArrayList<>();
			searchOutOrgIds.add(vo.getOutOrgId());

			List<Long> searchOutVirtIds = new ArrayList<>();
			searchOutVirtIds.addAll(AnStringUtils.stringArrayConvertToLongList(vo.getOutVirtIds(), ","));

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


			vo.setOutOrgNames(AnStringUtils.stringListConvertToString(sysOrganizationDubboConsumerService.convertOutOrgIds2OrgNames(searchOutOrgIds)));
			vo.setOutVirtNames(AnStringUtils.stringListConvertToString(sysEnterpriseDubboConsumerService.convertOutVirtIds2VirtNames(searchOutVirtIds)));

			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<SysRoleExtDataSample>> getById(@PathVariable("id") long id) {

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

		// TODO 参数合法性判定

		SysRoleExtDataSample entity = sysRoleExtDataSampleDubboConsumerService.findSysRoleExtDataSampleById(id);

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

		result.setData(data);

		return result;
	}

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

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


		// TODO 参数合法性判定

		sysRoleExtDataSampleDubboConsumerService.addSysRoleExtDataSample(getToken(), param);

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

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

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

		sysRoleExtDataSampleDubboConsumerService.updateSysRoleExtDataSample(getToken(), param);

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

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

		// TODO 参数合法性判定

		sysRoleExtDataSampleDubboConsumerService.deleteSysRoleExtDataSample(id);

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

	/**
	 * web/sysRoleExtDataSample/delete_ids?ids=17,18
	 * @param ids
	 * @return
	 */
	@ApiOperation(value = "批量删除")
	@RequestMapping(value = "delete_ids", method = RequestMethod.POST)
	@ResponseBody
	public R<String> deleteIdsSysRoleExtDataSample(
			@RequestParam(value = "ids") long[] ids
	) {

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

		for (long id : ids) {
			sysRoleExtDataSampleDubboConsumerService.deleteSysRoleExtDataSample(id);
		}

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

	}


}
