package com.qunzhixinxi.hnqz.admin.controller;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDeptRelation;
import com.qunzhixinxi.hnqz.admin.entity.WmDeptTaskTypeConfig;
import com.qunzhixinxi.hnqz.admin.entity.WmTask;
import com.qunzhixinxi.hnqz.admin.entity.dto.WmDeptTaskTypeConfigDTO;
import com.qunzhixinxi.hnqz.admin.entity.dto.WmDeptTaskTypeConfigJsonDTO;
import com.qunzhixinxi.hnqz.admin.service.SysDeptRelationService;
import com.qunzhixinxi.hnqz.admin.service.WmDeptTaskTypeConfigService;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.log.annotation.SysLog;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 部门任务类型配置控制层
 *
 * @author lixuesong
 * @date 2021年12月03日 11:03
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/dept-task-type-config")
public class WmDeptTaskTypeConfigController {

	private WmDeptTaskTypeConfigService wmDeptTaskTypeConfigService;

	private SysDeptRelationService sysDeptRelationService;

	/**
	 * 保存任务类型配置
	 *
	 * @param wmDeptTaskTypeConfigDTO
	 * @return
	 */
	@SysLog("保存任务类型配置")
	@PostMapping("/save")
	public R<?> saveDeptTaskTypeConfig(@Validated @RequestBody WmDeptTaskTypeConfigDTO wmDeptTaskTypeConfigDTO) {
		wmDeptTaskTypeConfigService.saveDeptTaskTypeConfig(wmDeptTaskTypeConfigDTO);
		return R.ok();
	}

	/**
	 * 查询任务类型配置
	 *
	 * @param deptId
	 * @return
	 */
	@GetMapping("/{deptId}")
	public R<WmDeptTaskTypeConfigDTO> getDeptTaskTypeConfig(@PathVariable("deptId") Integer deptId) {
		return R.ok(wmDeptTaskTypeConfigService.getDeptTaskTypeConfig(deptId));
	}

	/**
	 * 是否配置了企业的任务类型配置
	 *
	 * @return
	 */
	@GetMapping("/is-config")
	public R<?> isConfig() {
		Map<String, Object> resultMap = new HashMap<>();
		Integer deptId = SecurityUtils.getUser().getDeptId();
		AtomicBoolean configed = new AtomicBoolean(false);
		// 本级dept对应的任务类型
		WmDeptTaskTypeConfig deptTaskTypeConfig = wmDeptTaskTypeConfigService.getOne(Wrappers.<WmDeptTaskTypeConfig>lambdaQuery()
				.eq(WmDeptTaskTypeConfig::getDeptId, deptId));
		if (deptTaskTypeConfig != null) {
			List<WmDeptTaskTypeConfigJsonDTO> configJsonDTOList =
					JSON.parseArray(deptTaskTypeConfig.getTaskTypeJson(), WmDeptTaskTypeConfigJsonDTO.class);
			configJsonDTOList.forEach(configJsonDTO -> {
				if (configJsonDTO.getQualifiedTask()) {
					configed.set(true);
				}
			});
		}
		// 查询上级的deptId
		SysDeptRelation ancestorDeptRelation = sysDeptRelationService.getOne(Wrappers.<SysDeptRelation>lambdaQuery()
				.eq(SysDeptRelation::getDescendant, deptId)
				.ne(SysDeptRelation::getAncestor, deptId)
				.ne(SysDeptRelation::getAncestor, 1));
		// 上级dept对应任务类型配置
		if (ancestorDeptRelation != null) {
			WmDeptTaskTypeConfig ancestorDeptTaskTypeConfig = wmDeptTaskTypeConfigService.getOne(Wrappers.<WmDeptTaskTypeConfig>lambdaQuery()
					.eq(WmDeptTaskTypeConfig::getDeptId, ancestorDeptRelation.getAncestor()));
			if (ancestorDeptTaskTypeConfig != null) {
				List<WmDeptTaskTypeConfigJsonDTO> ancestorConfigJsonDTOList =
						JSON.parseArray(ancestorDeptTaskTypeConfig.getTaskTypeJson(), WmDeptTaskTypeConfigJsonDTO.class);
				ancestorConfigJsonDTOList.stream()
						.forEach(configJsonDTO -> {
							if (configJsonDTO.getQualifiedTask() && configJsonDTO.getAssociateLower()) {
								configed.set(true);
							}
						});
			}
		}
		resultMap.put("config", configed.get());
		return R.ok(resultMap);
	}

	/**
	 * 根据企业的任务类型配置查询任务列表
	 *
	 * @param page         分页参数
	 * @param taskTypeName 任务类型名称
	 * @param taskStatus   任务状态
	 * @return
	 */
	@GetMapping("/list-task")
	public R<IPage<WmTask>> listTaskByDeptTaskType(Page<WmTask> page, String taskTypeName, String[] taskStatus) {
		if (ArrayUtil.isEmpty(taskStatus)) {
			return R.failed("taskStatus不能为空");
		}

		return R.ok(wmDeptTaskTypeConfigService.listTaskByConfig(page, taskTypeName, taskStatus));
	}

}
