package com.jeeplus.finance.projectReportArchive.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.finance.projectRecords.domain.CwProjectRecords;
import com.jeeplus.finance.projectRecords.mapper.CwProjectRecordsMapper;
import com.jeeplus.finance.projectReport.domain.*;
import com.jeeplus.finance.projectReport.mapper.CwProjectInfoMapper;
import com.jeeplus.finance.projectReport.mapper.CwProjectReportMapper;
import com.jeeplus.finance.projectReport.service.dto.ProjectReportWorkAttachmentDTO;
import com.jeeplus.finance.projectReportArchive.domain.CwProjectReportArchive;
import com.jeeplus.finance.projectReportArchive.domain.CwProjectReportArchiveApply;
import com.jeeplus.finance.projectReportArchive.domain.CwProjectReportArchiveFile;
import com.jeeplus.finance.projectReportArchive.mapper.CwProjectReportArchiveApplyMapper;
import com.jeeplus.finance.projectReportArchive.mapper.CwProjectReportArchiveFileMapper;
import com.jeeplus.finance.projectReportArchive.mapper.CwProjectReportArchiveMapper;
import com.jeeplus.finance.projectReportArchive.mapper.CwProjectReportArchiveReportMapper;
import com.jeeplus.finance.projectReportArchive.service.dto.*;
import com.jeeplus.finance.projectReportArchive.service.mapstruct.CwProjectReportArchiveApplyWrapper;
import com.jeeplus.finance.projectReportArchive.service.mapstruct.CwProjectReportArchiveWrapper;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.sys.domain.WorkAttachmentInfo;
import com.jeeplus.sys.feign.IRoleApi;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.feign.IWorkAttachmentApi;
import com.jeeplus.sys.service.dto.RoleDTO;
import com.jeeplus.sys.service.dto.UserDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
@Transactional
public class CwProjectReportArchiveApplyService extends ServiceImpl<CwProjectReportArchiveApplyMapper, CwProjectReportArchiveApply> {

    @Resource
    private CwProjectReportArchiveApplyMapper cwProjectReportArchiveApplyMapper;

    @Resource
    private CwProjectReportMapper reportMapper;

    @Resource
    private CwProjectInfoMapper infoMapper;

    @Resource
    private CwProjectReportMapper cwProjectReportMapper;

    @Resource
    private IFlowableApi flowTaskService;


    public CwProjectReportArchiveApplyDTO queryReportById(String id) {

        CwProjectReportArchiveApplyDTO reportData = cwProjectReportArchiveApplyMapper.queryReportById(id);
        if (null == reportData) {
            reportData = cwProjectReportArchiveApplyMapper.queryReportByReportId(id);
        }
        if(reportData != null) {
            if(StringUtils.isNotBlank(reportData.getProjectId())){
                //查询项目对应的项目经理是谁（排除张宁、孙文宝、范百顺、潘中以外的另一个项目经理（只取一个））
                String projectManagerByProjectId = cwProjectReportArchiveApplyMapper.getProjectManagerByProjectId(reportData.getProjectId());
                reportData.setApplyAuditById(projectManagerByProjectId);
            }
        }

        return reportData;
    }

    public CwProjectReportArchiveApplyDTO queryReportByReportId(String reportId) {

        CwProjectReportArchiveApplyDTO reportData = cwProjectReportArchiveApplyMapper.queryReportByReportId(reportId);

        return reportData;
    }



    /**
     * 保存项目归档信息以及其他相关信息
     * @param cwProjectReportArchiveDTO
     * @return
     * @throws Exception
     */
    public String applySave(CwProjectReportArchiveApplyDTO cwProjectReportArchiveDTO) throws Exception{
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        cwProjectReportArchiveDTO.setCreateBy(userDTO);
        CwProjectReportArchiveApply cwProjectReportArchive = CwProjectReportArchiveApplyWrapper.INSTANCE.toEntity(cwProjectReportArchiveDTO);

        if(StringUtils.isNotBlank(cwProjectReportArchive.getId())){ //修改
            cwProjectReportArchiveApplyMapper.updateApplyInfoById(cwProjectReportArchive);
        }else{  //新增
            cwProjectReportArchiveApplyMapper.insert(cwProjectReportArchive);
        }

        return cwProjectReportArchive.getId();
    }



    /**
     * 修改状态
     * @param cwProjectReportArchiveDTO
     * @return
     */
    public String updateStatusById(CwProjectReportArchiveApplyDTO cwProjectReportArchiveDTO) {
        CwProjectReportArchiveApply cwProjectReportArchive = CwProjectReportArchiveApplyWrapper.INSTANCE.toEntity(cwProjectReportArchiveDTO);
        this.update(cwProjectReportArchive, new QueryWrapper<CwProjectReportArchiveApply>().lambda().eq(CwProjectReportArchiveApply::getId, cwProjectReportArchive.getId()));
        return "操作成功";
    }
}
