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

import com.lz.common.utils.DateUtils;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.StringUtil;
import com.lz.modules.app.entity.StaffEntity;
import com.lz.modules.app.service.StaffService;
import com.lz.modules.flow.service.FlowChangeService;
import com.lz.modules.flow.service.FlowRecordService;
import com.lz.modules.performance.dto.TaskListDto;
import com.lz.modules.performance.req.AssessTaskReq;
import com.lz.modules.performance.res.ChartStatisticalRes;
import com.lz.modules.performance.res.TaskListRes;
import com.lz.modules.performance.service.AssessService;
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;

/**
 * @Author: djc
 * @Desc:
 * @Date: 2020/10/14 14:59
 */
@Service("assessService")
public class AssessServiceImpl implements AssessService {
    @Autowired
    private FlowRecordService flowRecordService;
    @Autowired
    private StaffService staffService;
    @Autowired
    private FlowChangeService flowChangeService;

    @Override
    public PageUtils userTaskList(AssessTaskReq req,Long userId) {
        PageUtils data = new PageUtils();
        List<TaskListRes> list = new ArrayList<>();
        PageUtils pageUtils = flowRecordService.selectTaskListByApprovalStaffId(req,userId);
        if(CollectionUtils.isEmpty(pageUtils.getList())){
            return data;
        }
        List<TaskListDto> dtos = pageUtils.getList();
        dtos.stream().forEach(taskListDto -> {
            TaskListRes res = new TaskListRes();
            StaffEntity staffEntity = staffService.selectStaffById(taskListDto.getStaffId());
            res.setAvatar(Optional.ofNullable(staffEntity.getAvatar()).orElse(StringUtil.EMPTY));
            String name = staffEntity.getName();
            if(taskListDto.getStaffId().equals(taskListDto.getCurrentApprovalStaffId())){
                name = "您";
            }
            //根据状态拼接文案
            res.setTitle(name + "的" + taskListDto.getMonthTime() + "");
            res.setTime(DateUtils.format(taskListDto.getGmtModified(),DateUtils.DATE_TIME_PATTERN));
            //跳转绩效详情页
            res.setRecordId(taskListDto.getRecordId());
            list.add(res);
        });
        data.setTotalCount(pageUtils.getTotalCount());
        data.setTotalPage(pageUtils.getTotalPage());
        data.setList(list);
        data.setPageSize(req.getPageSize());
        data.setCurrPage(req.getCurrPage());
        return data;
    }

}
