/*
 *    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.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.admin.api.entity.SysDept;
import com.qunzhixinxi.hnqz.admin.api.entity.SysUser;
import com.qunzhixinxi.hnqz.admin.entity.*;
import com.qunzhixinxi.hnqz.admin.mapper.WmWkArticleShareMapper;
import com.qunzhixinxi.hnqz.admin.service.*;
import com.qunzhixinxi.hnqz.common.core.util.R;
import com.qunzhixinxi.hnqz.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 文章分享
 *
 * @author pigx code generator
 * @date 2020-07-09 22:34:30
 */
@Service
@AllArgsConstructor
public class WmWkArticleShareServiceImpl extends ServiceImpl<WmWkArticleShareMapper, WmWkArticleShare> implements WmWkArticleShareService {

	private final SysUserService sysUserService;
	private final SysDeptService sysDeptService;
	private final WmTaskTypeService wmTaskTypeService;
	private final WmScorePackageService wmScorePackageService;
	private final WmTaskService wmTaskService;

	@Override
	public R saveWmWkArticleShare(String artileId, String shareUserId, String openUserId,int type) {
		WmWkArticleShare wmWkArticleShare=new WmWkArticleShare();
		wmWkArticleShare.setArctileId(artileId);
		wmWkArticleShare.setCreateTime(LocalDateTime.now());
		wmWkArticleShare.setOpenUser(openUserId);
		wmWkArticleShare.setShareType("1");
		wmWkArticleShare.setShareUserid(SecurityUtils.getUser().getId()+"");

		String taskTypeId="";
		if(this.save(wmWkArticleShare)) {
			List<SysUser> sysUserList=sysUserService.getUserByPlatId(SecurityUtils.getUser().getPlatId());
			if(sysUserList.size()>0)
			{
				for (SysUser sysU:
						sysUserList) {
					WmTask wmTask = new WmTask();
					wmTask.setTaskFrom("1");
					wmTask.setTaskUserId(sysU.getUserId()+"");  //任务提交人
					if(1==type)
					{
						taskTypeId="10";
						wmTask.setTaskTypeId("10");                           //任务类型
					}else if(2==type)
					{
						taskTypeId="7";
						wmTask.setTaskTypeId("7");                           //任务类型
					}
					wmTask.setLookintoDate(LocalDateTime.now());       //调研时间
					wmTask.setTaskStatus("1");
					wmTask.setSubmitStatus("0");
					wmTask.setDrugEntId(sysU.getDrugEntId());//任务状态

					SysDept sysDept=sysDeptService.getById(sysU.getDeptId());
					WmTaskType wmTaskType=new WmTaskType();
					wmTaskType.setDurgentId(sysU.getDrugEntId());
					WmTaskType wmTaskType1=wmTaskTypeService.getById(taskTypeId);
					wmTaskType.setTaskTypeName(wmTaskType1.getTaskTypeName());
					wmTaskType.setTaskEntId(sysDept.getLevel2Id()+"");
					int socre=wmTaskTypeService.getWmTaskTypeByEnt(wmTaskType);
					System.out.println("获取任务得分："+socre);
					if(socre<0)
					{
						return R.failed("获取任务积分失败");
					}
					wmTask.setScore(socre);

					WmScorePackage wmScorePackage=new WmScorePackage();
					if(SecurityUtils.getRoles().contains(6))
					{
						wmScorePackage.setZbId(sysU.getUserId()+"");
					}else{
						wmScorePackage.setXxdbId(sysU.getUserId()+"");
					}

					wmScorePackage.setDrugEntId(sysU.getDrugEntId());
					WmScorePackage tWmScorePackage=wmScorePackageService.getPackageScoreByTaskInfo(wmScorePackage);
					System.out.println("获取积分包ID："+tWmScorePackage);
					if(null==tWmScorePackage)
					{
						//return R.failed("获取积分包失败");
					}else{
						wmTask.setScorePackageId(tWmScorePackage.getId());
						wmTask.setTaskRuleId(tWmScorePackage.getTaskRuleId());
					}

					//wmTask.setTaskContentId(wmTaskContent.getId()+"");

					if(SecurityUtils.getRoles().contains(5))
					{
						wmTask.setTaskUserType("5");
					}

					wmTask.setCreateTime(LocalDateTime.now());

					if(SecurityUtils.getRoles().contains(6))
					{
						wmTask.setTaskUserType("6");
					}

					wmTask.setDeptId(sysU.getDeptId()+"");
					wmTask.setSubmitStatus("0");
					boolean isSave=wmTaskService.save(wmTask);   //保存任务

					//判断任务是否完成
					if(isSave&&null!=tWmScorePackage)
					{
						WmScorePackage wmScorePackage1=wmScorePackageService.getById(tWmScorePackage.getId());
						if(null!=wmScorePackage1)
						{
							WmTask wmTask1=new WmTask();
							wmTask1.setScorePackageId(wmScorePackage1.getId());
							wmTask1.setDrugEntId(sysU.getDrugEntId());
							List<WmTask> wmTaskList=wmTaskService.list(Wrappers.query(wmTask1));
							int totalCount=0;
							for (WmTask wmtask:wmTaskList) {
								totalCount+=wmtask.getScore();
							}

							if(totalCount>=wmScorePackage1.getScore()){
								wmScorePackage1.setPackageFinishStatus("1");
							}
							wmScorePackageService.updateById(wmScorePackage1);
						}
					}else{
						return R.failed("任务保存失败");
					}
					return R.ok();
				}
			}
		}
		return R.ok();
	}
}
