package cn.cslg.pas.service;

import cn.cslg.pas.common.model.vo.ProjectExportVO;
import cn.cslg.pas.common.model.vo.ProjectVO;
import cn.cslg.pas.common.utils.FileUtils;
import cn.cslg.pas.common.utils.SecurityUtils.LoginUtils;
import cn.cslg.pas.common.utils.StringUtils;
import cn.cslg.pas.domain.ProjectExport;
import cn.cslg.pas.domain.User;
import cn.cslg.pas.mapper.ProjectExportMapper;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【os_thematic_export(专题库数据导出记录)】的数据库操作Service
* @createDate 2022-04-24 08:06:14
*/
@Service
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
public class ProjectExportService extends ServiceImpl<ProjectExportMapper, ProjectExport> {

    private final FileUtils fileUtils;
    private final LoginUtils loginUtils;
    private  final  RequestService requestService;
    public IPage<ProjectExport> getPageList(ProjectExportVO params) throws IOException {
        LambdaQueryWrapper<ProjectExport> queryWrapper = new LambdaQueryWrapper<>();
        if (params.getProjectId() != null) {
            queryWrapper.eq(ProjectExport::getProjectId, params.getProjectId());
        }
        queryWrapper.orderByDesc(ProjectExport::getCreateTime);
        IPage<ProjectExport> pageList = this.page(new Page<>(params.getCurrent(), params.getSize()), queryWrapper);
       List<Integer> personIds = pageList.getRecords().stream().map(ProjectExport::getCreateBy).collect(Collectors.toList());
        List<ProjectVO.Personnel> personnelList =new ArrayList<>();
       if(personIds.size()!=0){
           String jsonObject1 = requestService.getPersonnelFromPCS(personIds);
           JSONArray jsonArray = JSON.parseArray(jsonObject1);
           personnelList = jsonArray.toJavaList(ProjectVO.Personnel.class);
       }
            for(ProjectExport data: pageList.getRecords()) {
                data.setCreateName(personnelList.stream().filter(item -> item.getId().equals(data.getCreateBy())).findFirst().orElse(new ProjectVO.Personnel()).getPersonnelName());
            }
        return pageList;
    }

    public Integer add(ProjectExport projectExport) {
        projectExport.setCreateBy(loginUtils.getId());
        projectExport.setCreateTime(new Date());
        projectExport.insert();
        return projectExport.getId();
    }

    public void delete(Integer id) {
        ProjectExport temp = this.getById(id);
        this.removeById(id);
        if (StringUtils.isNotEmpty(temp.getUrl())) {
            FileUtil.del(fileUtils.getSystemPath(temp.getUrl()));
        }
    }
}
