package com.qunzhixinxi.hnqz.admin.service.impl;/*
 *    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
 */

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.WmScorePackage;
import com.qunzhixinxi.hnqz.admin.entity.WmScoreTaskType;
import com.qunzhixinxi.hnqz.admin.entity.WmTaskType;
import com.qunzhixinxi.hnqz.admin.entity.WmTaskTypeTree;
import com.qunzhixinxi.hnqz.admin.mapper.SysDictItemMapper;
import com.qunzhixinxi.hnqz.admin.mapper.WmScoreTaskTypeMapper;
import com.qunzhixinxi.hnqz.admin.mapper.WmTaskTypeMapper;
import com.qunzhixinxi.hnqz.admin.service.WmScoreTaskTypeService;
import com.qunzhixinxi.hnqz.admin.service.WmTaskTypeService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
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 pigx code generator
 * @date 2021-01-04 17:13:30
 */
@Service
@AllArgsConstructor
public class WmScoreTaskTypeServiceImpl extends ServiceImpl<WmScoreTaskTypeMapper, WmScoreTaskType> implements WmScoreTaskTypeService {
	private final WmScoreTaskTypeMapper wmScoreTaskTypeMapper;
	private final WmTaskTypeService wmTaskTypeService;

	@Override
	public int batchInsertType(List<WmScoreTaskType> wmScoreTaskTypeList) {
		return wmScoreTaskTypeMapper.batchInsertType(wmScoreTaskTypeList);
	}

	@Override
	public List<WmScoreTaskType> listWmScoreTaskTypes(WmScoreTaskType wmScoreTaskType) {
		return wmScoreTaskTypeMapper.listWmScoreTaskTypes(wmScoreTaskType);
	}

	@Override
	public List<String> selectByScoreId(WmScorePackage wmScorePackage) {
		WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
		wmScoreTaskType.setScoreId(wmScorePackage.getId());
		List<WmScoreTaskType> wmScoreTaskTypeList =wmScoreTaskTypeMapper.selectByScoreId(wmScoreTaskType);
		List<String> ids = wmScoreTaskTypeList.stream().map(p -> p.getTaskTypeId()).collect(Collectors.toList());
		return ids;
	}

	@Override
	public R selectByScorePackageId(WmScorePackage wmScorePackage) {
		WmScoreTaskType wmScoreTaskType = new WmScoreTaskType();
		wmScoreTaskType.setScoreId(wmScorePackage.getId());
		List<WmScoreTaskType> wmScoreTaskTypeList =wmScoreTaskTypeMapper.selectByScoreId(wmScoreTaskType);

		List<String> ids = wmScoreTaskTypeList.stream().map(p -> p.getTaskTypeId()).collect(Collectors.toList());
		List<WmTaskType> typeList = wmTaskTypeService.listByIds(ids);
		Map<String,String> resultMap = new HashMap<>();

		List<Map<String,String>> list = new ArrayList<>();
		for (WmTaskType type : typeList) {
			if(StringUtils.isEmpty(type.getParentId())){
				continue;
			}
			if(resultMap.containsKey(type.getParentId())){
				String typeName = resultMap.get(type.getParentId());
				typeName = typeName +"；"+ type.getTaskTypeName();
				resultMap.put(type.getParentId(),typeName);
			}else {
				resultMap.put(type.getParentId(),type.getTaskTypeName());
//				list.add(resultMap);
			}
		}
//		resultMap.forEach((k,v)->item.put("code",k));
		list = resultMap.entrySet().stream().map(e ->{
			Map<String,String> item = new HashMap<>();
			item.put("code",e.getKey());
			item.put("label",e.getValue());
			return item;

		}).collect(Collectors.toList());

		return R.ok(list);
	}

	@Override
	public int deleteByScoreId(String id) {
		return wmScoreTaskTypeMapper.deleteByScoreId(id);
	}


	@Override
	public List<WmScoreTaskType>  getListByScoreId(WmScoreTaskType wmScoreTaskType) {
		return wmScoreTaskTypeMapper.getScoreTaskTypeByScoreId(wmScoreTaskType.getScoreId());
	}
}
