package cn.superdesk.uniorder.controller.organisation;

import cn.superdesk.libs.common.json.JsonUtils;
import cn.superdesk.uniorder.common.enums.common.CommonEfficacyEnum;
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.organisation.SysClique;
import cn.superdesk.uniorder.dto.param.organisation.SysCliqueFindByTreeCriteria;
import cn.superdesk.uniorder.dto.param.organisation.SysCliqueParam;
import cn.superdesk.uniorder.dto.vo._function.tree.SysCliqueTreeModel;
import cn.superdesk.uniorder.dubbo.consumer.organisation.SysCliqueDubboConsumerService;
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;


/**
 * @Description: 集团
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-04-28 14:08:25
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-04-28 14:08:25
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "集团分公司-集团分公司(数据来源-服务之窗)", tags = {"集团分公司-集团分公司(数据来源-服务之窗)"})
@Slf4j
@RestController
@RequestMapping("/sysClique")
public class SysCliqueController extends BaseController {

	@Autowired
	private SysCliqueDubboConsumerService sysCliqueDubboConsumerService;

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

		log.info("pageQuerySysClique:\n{}", JsonUtils.toJson(param));

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

		// TODO 参数合法性判定

		result = sysCliqueDubboConsumerService.pageQuery(param.getConditions(), param.getPageNo(), param.getPageSize());

		return result;
	}

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

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

		// TODO 参数合法性判定

		SysClique entity = sysCliqueDubboConsumerService.findSysCliqueById(id);

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

		result.setData(data);

		return result;
	}

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

		log.info("addSysClique:\n{}", JsonUtils.toJson(param));


		// TODO 参数合法性判定

		sysCliqueDubboConsumerService.addSysClique(param);

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

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

		log.info("updateSysClique:\n{}", JsonUtils.toJson(param));

		sysCliqueDubboConsumerService.updateSysClique(param);

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

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

		// TODO 参数合法性判定

		sysCliqueDubboConsumerService.deleteSysClique(id);

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

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

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

		for (long id : ids) {
			sysCliqueDubboConsumerService.deleteSysClique(id);
		}

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

	}


	/**
	 * http://192.168.6.223:8089/web/sysClique/findByTree
	 *
	 * @return
	 */
	@ApiOperation(value = "集团分公司-树型数据-element ui tree")
	@RequestMapping(value = "findByTree", method = RequestMethod.POST)
	@ResponseBody
	public R<SysCliqueTreeModel> findByTree(@RequestBody SysCliqueFindByTreeCriteria criteria) {

		if (null == criteria.getEfficacy()) {
			criteria.setEfficacy(CommonEfficacyEnum.有效的.getKey());
		}

		List<SysCliqueTreeModel> result002Data = sysCliqueDubboConsumerService.findByTree002(criteria);
		List<SysCliqueTreeModel> result003Data = sysCliqueDubboConsumerService.findByTree003(criteria);
		List<SysCliqueTreeModel> result004Data = sysCliqueDubboConsumerService.findByTree004(criteria);

		Map<Long, List<SysCliqueTreeModel>> model004Map = SysCliqueTreeModel.buildChildren(result004Data);


//		把企业挂在项目上
		log.info("model004Map {}", JsonUtils.toJson(model004Map));
		result003Data.forEach(item -> {
			log.info("result003Data item {}", JsonUtils.toJson(item));
			if (null != model004Map.get(item.getId())) {
				item.addAllChild(model004Map.get(item.getId()));
			}
		});

		Map<Long, List<SysCliqueTreeModel>> model003Map = SysCliqueTreeModel.buildChildren(result003Data);

		// TODO 待验证 这里应该是个完整的层级吧 已经证明是对的
		log.info("model003Map {}", JsonUtils.toJson(model003Map));

		// 这只是 集团分公司级别 但这个是多层的 层次不定
		SysCliqueTreeModel model002Root = SysCliqueTreeModel.buildRoot(result002Data);

		// TODO 待验证 把002 与 003 合成

		List<SysCliqueTreeModel> model002RootLinear = new ArrayList<>();
		SysCliqueTreeModel.recursiveTraversal(model002Root, model002RootLinear);

		model002RootLinear.forEach(item -> {
			log.info("result003Data item {}", JsonUtils.toJson(item));
			if (null != model003Map.get(item.getId())) {
				item.addAllChild(model003Map.get(item.getId()));
			}
		});


		R<SysCliqueTreeModel> result = new R<>();

		result.setSuccess(true);
		result.setCode(200);
		result.setData(model002Root);


		return result;

	}


}
