package com.jeeplus.test.cw.projectReportArchive.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
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.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.domain.User;
import com.jeeplus.sys.mapper.UserMapper;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.cw.projectRecords.domain.CwProjectRecords;
import com.jeeplus.test.cw.projectRecords.mapper.CwProjectRecordsMapper;
import com.jeeplus.test.cw.projectReport.domain.CwProjectInfoData;
import com.jeeplus.test.cw.projectReport.mapper.CwProjectInfoMapper;
import com.jeeplus.test.cw.projectReportArchive.domain.CwProjectReportArchive;
import com.jeeplus.test.cw.projectReportArchive.domain.CwProjectReportArchiveDetail;
import com.jeeplus.test.cw.projectReportArchive.domain.CwProjectReportArchiveReport;
import com.jeeplus.test.cw.projectReportArchive.mapper.CwProjectReportArchiveDetailMapper;
import com.jeeplus.test.cw.projectReportArchive.mapper.CwProjectReportArchiveFileMapper;
import com.jeeplus.test.cw.projectReportArchive.mapper.CwProjectReportArchiveMapper;
import com.jeeplus.test.cw.projectReportArchive.mapper.CwProjectReportArchiveReportMapper;
import com.jeeplus.test.cw.projectReportArchive.service.dto.CwProjectReportArchiveDetailDTO;
import com.jeeplus.test.cw.projectReportArchive.service.mapstruct.CwProjectReportArchiveDetailWrapper;
import com.jeeplus.test.cw.projectReportArchive.service.mapstruct.CwProjectReportArchiveWrapper;
import com.jeeplus.test.mould.service.SerialnumTplService;
import com.jeeplus.test.oss.mapper.OssServiceMapper;
import com.jeeplus.test.oss.service.OssService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class CwProjectReportArchiveDetailService extends ServiceImpl<CwProjectReportArchiveDetailMapper, CwProjectReportArchiveDetail> {

    @Resource
    private CwProjectReportArchiveDetailMapper cwProjectReportArchiveDetailMapper;
    @Resource
    private CwProjectReportArchiveReportMapper cwProjectReportArchiveReportMapper;
    @Resource
    private CwProjectRecordsMapper cwProjectRecordsMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CwProjectInfoMapper cwProjectInfoMapper;

    /**
     * 项目归档列表信息
     * @param page
     * @param cwProjectReportArchiveDetailDTO
     * @return
     * @throws Exception
     */
    public IPage<CwProjectReportArchiveDetailDTO> findList(Page<CwProjectReportArchiveDetailDTO> page, CwProjectReportArchiveDetailDTO cwProjectReportArchiveDetailDTO) throws Exception{
        QueryWrapper<CwProjectReportArchiveDetail> queryWrapper = QueryWrapperGenerator.buildQueryCondition ( CwProjectReportArchiveDetailWrapper.INSTANCE.toEntity(cwProjectReportArchiveDetailDTO), CwProjectReportArchiveDetail.class );
        queryWrapper.eq("a.del_flag","0");
        queryWrapper.eq("pr.del_flag","0");
        if (ObjectUtil.isNotEmpty(cwProjectReportArchiveDetailDTO)) {
            if (ObjectUtil.isNotEmpty(cwProjectReportArchiveDetailDTO.getCwProjectRecordsDTO())){
                // 项目名称
                if (StringUtils.isNotBlank(cwProjectReportArchiveDetailDTO.getCwProjectRecordsDTO().getProjectName())){
                    List<CwProjectRecords> cwProjectRecords = cwProjectRecordsMapper.selectList(new QueryWrapper<CwProjectRecords>().lambda().like(CwProjectRecords::getProjectName, cwProjectReportArchiveDetailDTO.getCwProjectRecordsDTO().getProjectName()));
                    List<String> ids = cwProjectRecords.stream().distinct().map(CwProjectRecords::getId).collect(Collectors.toList());
                    if(CollectionUtil.isNotEmpty(ids)) {
                        queryWrapper.in("p.id",ids);
                    }else{
                        return new Page<>();
                    }
                }
                // 项目经理
                if (StringUtils.isNotBlank(cwProjectReportArchiveDetailDTO.getCwProjectRecordsDTO().getProjectMasterId())){
                    queryWrapper.eq("p.project_master_id",cwProjectReportArchiveDetailDTO.getCwProjectRecordsDTO().getProjectMasterId());
                }
                // 创建时间
                if (ArrayUtil.isNotEmpty(cwProjectReportArchiveDetailDTO.getReportDates())){
                    queryWrapper.between("a.report_date", cwProjectReportArchiveDetailDTO.getReportDates()[0], cwProjectReportArchiveDetailDTO.getReportDates()[1]);
                }
                // 归档时间
                if (ArrayUtil.isNotEmpty(cwProjectReportArchiveDetailDTO.getAuditDates())){
                    queryWrapper.between("pra.audit_date", cwProjectReportArchiveDetailDTO.getAuditDates()[0], cwProjectReportArchiveDetailDTO.getAuditDates()[1]);
                }
                // 报告文号
                if (StringUtils.isNotBlank(cwProjectReportArchiveDetailDTO.getReportNo())){
                    queryWrapper.like("a.report_no",cwProjectReportArchiveDetailDTO.getReportNo());
                }
                // 案卷号
                if (StringUtils.isNotBlank(cwProjectReportArchiveDetailDTO.getNumber())){
                    queryWrapper.like("pra.number",cwProjectReportArchiveDetailDTO.getNumber());
                }
            }
        }
        IPage<CwProjectReportArchiveDetailDTO> list = cwProjectReportArchiveDetailMapper.findList(page, queryWrapper,cwProjectReportArchiveDetailDTO);
        List<CwProjectReportArchiveDetailDTO> records = list.getRecords();
        if (null != records && records.size()>0){
            //循环查询出来的数据
            for (CwProjectReportArchiveDetailDTO info : records) {
                //如果归档日期为空，则表示该报告还未归档
                if(null == info.getAuditDate()){
                    //如果归档日期差大于60天，表示超期未归档
                    if(info.getArchiveDay()>60){
                        info.setStatus("超期未归档");
                        long n = (info.getArchiveDay()) / 60;
                        String money = Long.toString(n * 200);
                        info.setOverdueDay(String.valueOf(info.getArchiveDay() - 60));
                        info.setFineMoney(money);
                    }else{
                        //如果归档日期差小于等于60天，表示暂时未归档
                        info.setStatus("暂时未归档");
                        info.setOverdueDay(String.valueOf(info.getArchiveDay() - 60));
                        info.setFineMoney("0");
                    }
                } else {
                    //如果归档日期不为空，则表示该报告已归档

                    //如果归档日期差大于60天，表示已超期归档
                    if(info.getArchiveDay()>60){
                        info.setStatus("已超期归档");
                        long n = (info.getArchiveDay()) / 60;
                        String money = Long.toString(n * 200);
                        info.setOverdueDay(String.valueOf(info.getArchiveDay() - 60));
                        info.setFineMoney(money);
                    }else{
                        //如果归档日期差小于等于60天，表示已按时归档
                        info.setStatus("已按时归档");
                        info.setOverdueDay(String.valueOf(info.getArchiveDay() - 60));
                        info.setFineMoney("0");
                    }
                }
            }
        }
        return list;
    }
}
