package com.jeeplus.test.wdt.disposeRubbish.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.jeeplus.sys.service.dto.UserDTO;
import com.jeeplus.test.garbageClearance.domain.LookOver;
import com.jeeplus.test.garbageClearance.service.LookOverService;
import com.jeeplus.test.oss.domain.WorkAttachment;
import com.jeeplus.test.oss.domain.WorkAttachmentInfo;
import com.jeeplus.test.oss.service.OssService;
import com.jeeplus.test.wdt.disposeRubbish.domain.DisposeRubbish;
import com.jeeplus.test.wdt.disposeRubbish.domain.WorkOverNo;
import com.jeeplus.test.wdt.disposeRubbish.mapper.DisposeRubbishMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * @author: 徐滕
 * @version: 2024-09-02 09:51
 */
@Service
@Transactional
public class DisposeRubbishService extends ServiceImpl<DisposeRubbishMapper, DisposeRubbish> {

    @Autowired
    private OssService ossService;
    @Autowired
    private LookOverService lookOverService;

    public String getMaxNo() {
        // 创建查询条件构造器
        QueryWrapper<DisposeRubbish> queryWrapper = new QueryWrapper<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        queryWrapper.in("year",year);
        // 按no字段降序排列
        queryWrapper.orderByDesc("number");
        queryWrapper.last("limit 1");

        // 只获取第一条记录
        DisposeRubbish disposeRubbish = baseMapper.selectOne(queryWrapper);

        // 返回最大的no值
        return disposeRubbish != null ? disposeRubbish.getNo() : null;
    }

    public DisposeRubbish selectOne() {
        // 创建查询条件构造器
        QueryWrapper<DisposeRubbish> queryWrapper = new QueryWrapper<>();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        queryWrapper.in("year",year);
        // 按no字段降序排列
        queryWrapper.orderByDesc("number");
        queryWrapper.last("limit 1");

        // 只获取第一条记录
        DisposeRubbish disposeRubbish = baseMapper.selectOne(queryWrapper);

        // 返回最大的no值
        return disposeRubbish;
    }

    public UserDTO getUserInfoByOffId(String officeId) {
        return baseMapper.getUserInfoByOffId(officeId);
    }

    /**
     * 根据id获取巡视工单信息
     * @param id
     * @return
     */
    public DisposeRubbish selectById(String id) {
        DisposeRubbish disposeRubbish = baseMapper.selectById(id);
        if(StringUtils.isNotBlank(disposeRubbish.getProgramId())){
            // 使用 split() 方法将字符串分割为数组
            String[] array = disposeRubbish.getProgramId().split(",");
            // 将数组转换为 List
            List<String> programIdList = Arrays.asList(array);
            List<WorkAttachment> fileList1 = ossService.findFileList(programIdList, "before");
            disposeRubbish.setFileList1(fileList1);
            List<WorkAttachment> fileList2 = ossService.findFileList(programIdList, "between");
            disposeRubbish.setFileList2(fileList2);
            List<WorkAttachment> fileList3 = ossService.findFileList(programIdList, "after");
            disposeRubbish.setFileList3(fileList3);
            List<WorkAttachment> fileList4 = ossService.findFileList(id, "weigh");
            disposeRubbish.setFileList4(fileList4);

            List<WorkOverNo> workOverNoList = Lists.newArrayList();
            for (String workOverId : programIdList) {
                LookOver overServiceById = lookOverService.getById(workOverId);
                if(null != overServiceById && StringUtils.isNotBlank(overServiceById.getNo())){
                    WorkOverNo workOverNo = new WorkOverNo();
                    workOverNo.setId(overServiceById.getId());
                    workOverNo.setNo(overServiceById.getNo());
                    //workOverNo.setNo("XS-J" + overServiceById.getNo());
                    workOverNoList.add(workOverNo);
                }
            }
            disposeRubbish.setWorkOverNoList(workOverNoList);
        }
        // 根据id 从work_attachment 表中获取图片信息
        return disposeRubbish;
    }
    /**
     * 根据id获取巡视工单信息
     * @param procInsId
     * @return
     */
    public List<DisposeRubbish> getByProcInsId(List<String> procInsIdList) {
        return baseMapper.getByProcInsId(procInsIdList);
    }

    /**
     * 根据id获取巡视工单信息
     * @param programIdList
     * @return
     */
    public List<WorkAttachment> getByWorkOverId(List<String> programIdList) {
        // 根据id 从work_attachment 表中获取图片信息
        List<WorkAttachment> fileList1 = ossService.findFileList(programIdList, "before");
        return fileList1;
    }

}
