/*
 *    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.daemon.quartz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.daemon.quartz.entity.WmTaskType;
import com.qunzhixinxi.hnqz.daemon.quartz.mapper.WmTaskTypeMapper;
import com.qunzhixinxi.hnqz.daemon.quartz.service.WmTaskTypeService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

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

	@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;
	}

	/**
	 * 根据平台积分规则，修改各企业积分规则
	 * @return
	 * @param para 0-覆盖所有企业积分值，1-不覆盖符合积分规则范围的企业的
	 */
	@Override
	public int changeTaskTypeInfo(String para) {
		// 这里能查询的前提是：在平台积分规则点击了停用或编辑的数据（点击停用或编辑后会新增一条status=1的相同数据，这些数据实际应该是临时数据，后边会删除）
		WmTaskType wmTaskType = new WmTaskType();
		wmTaskType.setTaskTypeLevel("1");
		wmTaskType.setStatus("1");
		List<WmTaskType> wmTaskTypes = super.list(Wrappers.query(wmTaskType));

		// 遍历平台需要修改的积分规则
		for (WmTaskType wmT : wmTaskTypes) {
			// 根据积分规则名称，查询各个企业的积分规则
			WmTaskType wmTaskType1 = new WmTaskType();
			wmTaskType1.setTaskTypeName(wmT.getTaskTypeName());
			wmTaskType1.setTaskTypeLevel("2");
			List<WmTaskType> wmTaskTypeList = super.list(Wrappers.query(wmTaskType1));
			// 修改各企业的积分规则
			LocalDateTime now = LocalDateTime.now();
			for (WmTaskType wmTaskType2 : wmTaskTypeList) {
				WmTaskType updateTaskType = new WmTaskType();
				updateTaskType.setId(wmTaskType2.getId());
				if ("1".equals(para)) {
					// 如果企业现有的积分规则符合新的积分规则范围，则不做覆盖修改处理，否则会修改为平台的预设值
					if (wmTaskType2.getScore() > wmT.getTaskScoreEnd() || wmTaskType2.getScore() < wmT.getTaskScoreStart()) {
						updateTaskType.setScore(wmT.getScore());
					}
				} else if ("0".equals(para)) {
					// para为0时，则直接覆盖企业积分值
					updateTaskType.setScore(wmT.getScore());
				}
				updateTaskType.setTaskScoreStart(wmT.getTaskScoreStart());
				updateTaskType.setTaskScoreEnd(wmT.getTaskScoreEnd());
				updateTaskType.setUpdateTime(now);
				updateTaskType.setUpdateUser(0);
				this.updateById(updateTaskType);
			}

			// 如果baseId有对应的父级数据，则更新（原有逻辑）
			WmTaskType wmTaskTypeBase = super.getById(wmT.getBaseId());
			if (wmTaskTypeBase != null) {
				wmTaskTypeBase.setScore(wmT.getScore());
				wmTaskTypeBase.setTaskScoreEnd(wmT.getTaskScoreEnd());
				wmTaskTypeBase.setTaskScoreStart(wmT.getTaskScoreStart());
				wmTaskTypeBase.setUpdateTime(now);
				wmTaskTypeBase.setUpdateUser(0);
				this.updateById(wmTaskTypeBase);
			}
		}

		// 移除这些数据（status=1的，即设置为停用的数据）
		this.remove(Wrappers.query(wmTaskType));

		return 0;
	}
}
