/*
 *    Copyright (c) 2018-2025, hnqz 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 pig4cloud.com 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: hnqz
 */
package com.qunzhixinxi.hnqz.admin.service.impl;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDictItem;
import com.qunzhixinxi.hnqz.admin.entity.WmTaskType;
import com.qunzhixinxi.hnqz.admin.entity.WmTaskTypeTree;
import com.qunzhixinxi.hnqz.admin.entity.output.WmTaskTypeDeptOutput;
import com.qunzhixinxi.hnqz.admin.mapper.SysDictItemMapper;
import com.qunzhixinxi.hnqz.admin.mapper.WmTaskTypeMapper;
import com.qunzhixinxi.hnqz.admin.service.WmTaskTypeService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * 任务类型打分表
 *
 * @author gaoyang
 * @date 2020-07-01 20:20:05
 */
@Service
@AllArgsConstructor
public class WmTaskTypeServiceImpl extends ServiceImpl<WmTaskTypeMapper, WmTaskType> implements WmTaskTypeService {
	private final WmTaskTypeMapper wmTaskTypeMapper;
	private final SysDictItemMapper sysDictItemMapper;

	@Override
	public List<WmTaskTypeDeptOutput> getTaskTypeDeptList(WmTaskType query) {
		return wmTaskTypeMapper.getTaskTypeDeptList(query);
	}

	@Override
	public int getWmTaskTypeByEnt(WmTaskType wmTaskType) {
		wmTaskType.setDelFlag("0");
		wmTaskType.setEnableFlag("0");
		List<WmTaskType> list = super.list(Wrappers.query(wmTaskType));
		if (list.size() > 0) {
			if (list.size() > 0) {
				return list.get(0).getScore();
			}
		} else {
			return 0;
		}
		return 0;
	}

	@Override
	public List<WmTaskType> selectByRuleId(WmTaskType wmTaskType) {
		return wmTaskTypeMapper.selectByRuleId(wmTaskType);
	}

	/**
	 * 查询有效任务类型树
	 *
	 * @return
	 */
	@Override
	public List<WmTaskTypeTree> getTaskTree(String description) {
		List<SysDictItem> dictList=new ArrayList<>();
		if(StringUtil.isEmpty(description)){
			dictList=sysDictItemMapper.selectList(
					Wrappers.<SysDictItem>lambdaQuery().eq(SysDictItem::getType,"score_task_type")
							.orderByDesc(SysDictItem::getValue));
		}else {
			dictList=sysDictItemMapper.selectList(
					Wrappers.<SysDictItem>lambdaQuery().eq(SysDictItem::getType,"score_task_type")
							.eq(SysDictItem::getDescription,description)
							.orderByDesc(SysDictItem::getValue));
		}

		List<WmTaskTypeTree> treeList=new ArrayList<>();
		for (SysDictItem item:dictList){
			WmTaskTypeTree tree=new WmTaskTypeTree();
			tree.setTaskTypeId(item.getValue());
			tree.setTaskTypeName(item.getLabel());
			tree.setType(item.getDescription());
			WmTaskType wmTaskType=new WmTaskType();
			wmTaskType.setParentId(item.getValue());
			tree.setChildren(wmTaskTypeMapper.getWmTaskTypeYX(wmTaskType));
			treeList.add(tree);
		}
		return treeList;
	}

	/**
	 * 查询有效任务类型树
	 * @return
	 */
	public R getTaskTypeTree(String description){
		List<SysDictItem> dictList=new ArrayList<>();
		List<String> keys = new ArrayList<>();
		if(StringUtil.isEmpty(description)){
			dictList=sysDictItemMapper.selectList(
					Wrappers.<SysDictItem>lambdaQuery().eq(SysDictItem::getType,"score_task_type")
							.orderByAsc(SysDictItem::getValue));
		}else {
			dictList=sysDictItemMapper.selectList(
					Wrappers.<SysDictItem>lambdaQuery().eq(SysDictItem::getType,"score_task_type")
							.eq(SysDictItem::getDescription,description)
							.orderByAsc(SysDictItem::getValue));
		}

		List<WmTaskTypeTree> treeList=new ArrayList<>();
		keys.addAll(dictList.stream().map(SysDictItem::getValue).collect(Collectors.toList()));
		for (SysDictItem item:dictList){
			WmTaskTypeTree tree=new WmTaskTypeTree();
			tree.setTaskTypeId(item.getValue());
			tree.setTaskTypeName(item.getLabel());
			tree.setType(item.getDescription());
			WmTaskType wmTaskType=new WmTaskType();
			wmTaskType.setParentId(item.getValue());
			List<WmTaskTypeTree> children = wmTaskTypeMapper.getWmTaskTypeYX(wmTaskType);
			keys.addAll(children.stream().map(WmTaskTypeTree::getTaskTypeId).collect(Collectors.toList()));
			tree.setChildren(children);
			treeList.add(tree);
		}

		Map<String,Object> resultMap = new HashMap<>();
		resultMap.put("treeList",treeList);
		resultMap.put("keys",keys);
		return R.ok(resultMap);
	}
}
