package com.lz.modules.performance.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.lz.common.utils.PageUtils;
import com.lz.modules.app.entity.StaffEntity;
import com.lz.modules.app.entity.StaffOccupationEntity;
import com.lz.modules.app.model.TaskModel;
import com.lz.modules.app.service.StaffOccupationService;
import com.lz.modules.app.service.StaffService;
import com.lz.modules.performance.dao.ResultTaskMapper;
import com.lz.modules.performance.dao.TaskProcessRecordMapper;
import com.lz.modules.performance.dto.ResultTaskDto;
import com.lz.modules.performance.dto.TaskProcessRecordDto;
import com.lz.modules.performance.entity.ResultTask;
import com.lz.modules.performance.entity.TaskProcessRecord;
import com.lz.modules.performance.enums.ProcessRecordEnum;
import com.lz.modules.performance.req.ChangeTaskListReq;
import com.lz.modules.performance.service.ResultTaskService;
import com.lz.modules.performance.service.TaskProcessRecordService;
import com.lz.modules.sys.entity.app.ResultDetail;
import com.lz.modules.sys.service.app.ResultDetailService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* <p>
* 任务进度更新表 服务类
* </p>
*
* @author quyixiao
* @since 2020-12-08
*/

@Service
public class TaskProcessRecordServiceImpl extends ServiceImpl<TaskProcessRecordMapper, TaskProcessRecord> implements TaskProcessRecordService {


    @Autowired
	private TaskProcessRecordMapper taskProcessRecordMapper;

    @Autowired
	private ResultTaskService resultTaskService;

    @Autowired
	private ResultDetailService resultDetailService;

    @Autowired
	private StaffService staffService;
    @Autowired
	private StaffOccupationService staffOccupationService;
    @Autowired
	private ResultTaskMapper resultTaskMapper;



	@Override
	public TaskProcessRecord selectTaskProcessRecordById(Long id){
		return taskProcessRecordMapper.selectTaskProcessRecordById(id);
	}



	@Override
	public Long insertTaskProcessRecord(TaskProcessRecord taskProcessRecord){
		return taskProcessRecordMapper.insertTaskProcessRecord(taskProcessRecord);
	}



	@Override
	public int updateTaskProcessRecordById(TaskProcessRecord taskProcessRecord){
		return taskProcessRecordMapper.updateTaskProcessRecordById(taskProcessRecord);
	}



	@Override
	public int updateCoverTaskProcessRecordById(TaskProcessRecord taskProcessRecord){
		return taskProcessRecordMapper.updateCoverTaskProcessRecordById(taskProcessRecord);
	}



	@Override
	public int deleteTaskProcessRecordById(Long id){
		return taskProcessRecordMapper.deleteTaskProcessRecordById(id);
	}

	@Override
	public PageUtils selectTaskProcessRecordsByTaskId(ChangeTaskListReq req) {
		Long taskId = req.getTaskId();
		Long detailId = req.getDetailId();
		PageUtils pageUtils = new PageUtils();
		if(detailId!=null){
			List<ResultTaskDto> resultTaskDtos = resultTaskMapper.selectAllResultTaskDtosByDetailId(detailId);
			if(CollectionUtils.isNotEmpty(resultTaskDtos)){
				List<Long> collect = resultTaskDtos.stream().map(resultTaskDto -> resultTaskDto.getId()).collect(Collectors.toList());
				pageUtils = PageUtils.startPage(req.getCurrPage(),req.getPageSize()).doSelect(
						page -> taskProcessRecordMapper.selectTaskProcessRecordsByTaskIdsAndType(page, collect,req.getUseType())
				);
			}

		}
		else {
			pageUtils = PageUtils.startPage(req.getCurrPage(),req.getPageSize()).doSelect(
					page -> taskProcessRecordMapper.selectTaskProcessRecordsByTaskId(page, taskId,req.getUseType())
			);
		}

		List list = pageUtils.getList();
		if(CollectionUtils.isNotEmpty(list)){
			List<TaskProcessRecordDto> dtos = list;
			if(detailId == null){
                detailId = dtos.get(0).getDetailId();
			}
			ResultDetail resultDetail = resultDetailService.selectResultDetailById(detailId);
			StaffEntity staffEntity = staffService.selectStaffById(resultDetail.getStaffId());
			StaffOccupationEntity staffOccupationByStaffId = staffOccupationService.getStaffOccupationByStaffId(resultDetail.getStaffId());


			for(TaskProcessRecordDto dto:dtos){
				ResultTask resultTask = resultTaskService.selectResultTaskById(dto.getTaskId());
				Optional.ofNullable(resultTask).ifPresent(resultTask1 -> dto.setTaskName(resultTask1.getName()));
				if(dto.getUseType()==0){
					dto.setTypeDesc(ProcessRecordEnum.findByType(dto.getType()).getDesc());
				}
				Optional.ofNullable(staffEntity).ifPresent(staffEntity1 -> {
                    dto.setAvatar(staffEntity1.getAvatar());
                    dto.setStaffName(staffEntity1.getName());
                });
				Optional.ofNullable(staffOccupationByStaffId).ifPresent(staffOccupationEntity -> dto.setPosition(staffOccupationEntity.getPosition()));

				if (dto.getUseType()==1 ) {
					if(dto.getStaffId().equals(req.getStaffId())){
						dto.setIsSelf(1);
					}
					staffEntity = staffService.selectStaffById(dto.getStaffId());
                    Optional.ofNullable(staffEntity).ifPresent(staffEntity12 -> {
                        dto.setAvatar(staffEntity12.getAvatar());
                        dto.setStaffName(staffEntity12.getName());
						StaffOccupationEntity staffOccupation= staffOccupationService.getStaffOccupationByStaffId(staffEntity12.getId());
						Optional.ofNullable(staffOccupation).ifPresent(staffOccupationEntity -> dto.setPosition(staffOccupationEntity.getPosition()));
                    });

				}
			}

		}
		return pageUtils;
	}

	@Override
	public int deleteTaskProcessRecordsByTaskId(Long taskId){
		return taskProcessRecordMapper.deleteTaskProcessRecordsByTaskId(taskId);
	}

    @Override
    public List<TaskProcessRecord> selectByCondition(IPage page, TaskModel taskModel) {
		return taskProcessRecordMapper.selectByCondition(page,taskModel);
    }

    @Override
	public TaskProcessRecord selectCommentTaskProcessRecordByDetailId(Long detailId){
		return taskProcessRecordMapper.selectCommentTaskProcessRecordByDetailId(detailId);
	}

	@Override
	public TaskProcessRecord selectCommentTaskProcessRecordByTaskId(Long taskId){
		return taskProcessRecordMapper.selectCommentTaskProcessRecordByTaskId(taskId);
	}
}
