package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaReportDetailBo;
import com.ruoyi.oa.domain.vo.OaReportDetailVo;
import com.ruoyi.oa.domain.OaReportDetail;
import com.ruoyi.oa.mapper.OaReportDetailMapper;
import com.ruoyi.oa.service.IOaReportDetailService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设计项目汇报详情Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-13
 */
@RequiredArgsConstructor
@Service
public class OaReportDetailServiceImpl implements IOaReportDetailService {

    private final OaReportDetailMapper baseMapper;

    /**
     * 查询设计项目汇报详情
     */
    @Override
    public OaReportDetailVo queryById(Long id){

        return baseMapper.selectVoByIdPlus(id);
    }

    /**
     * 查询设计项目汇报详情列表
     */
    @Override
    public TableDataInfo<OaReportDetailVo> queryPageList(OaReportDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaReportDetail> lqw = buildQueryWrapper(bo);
        Page<OaReportDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        for(OaReportDetailVo item : result.getRecords()) {
            String ossIds = item.getOssIds();
            // 分离拿出ossIds
            String[] ossIdArray = new String[0];
            if (StringUtils.isNotBlank(ossIds)) {
                ossIdArray = ossIds.split(",");
            }
            // 准备images数组
            List<String> images = new ArrayList<>();
            // 遍历ossIdArray，添加到images中
            for (String ossId : ossIdArray) {
                // 查出图片地址
                String url = baseMapper.selectImageUrlByOssId(ossId);
                images.add(url);
            }
            // 设置images属性,将images打成，分割的字符串
            item.setImages(String.join(",", images));
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询设计项目汇报详情列表
     */
    @Override
    public List<OaReportDetailVo> queryList(OaReportDetailBo bo) {
        LambdaQueryWrapper<OaReportDetail> lqw = buildQueryWrapper(bo);
        List<OaReportDetailVo> result = baseMapper.selectVoList(lqw);
        for(OaReportDetailVo item : result) {
            String ossIds = item.getOssIds();
            // 分离拿出ossIds
            String[] ossIdArray = new String[0];
            if (StringUtils.isNotBlank(ossIds)) {
                ossIdArray = ossIds.split(",");
            }
            // 准备images数组
            List<String> images = new ArrayList<>();
            // 遍历ossIdArray，添加到images中
            for (String ossId : ossIdArray) {
                // 查出图片地址
                String url = baseMapper.selectImageUrlByOssId(ossId);
                images.add(url);
            }
            // 设置images属性,将images打成，分割的字符串
            item.setImages(String.join(",", images));
        }
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaReportDetail> buildQueryWrapper(OaReportDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaReportDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSummaryId() != null, OaReportDetail::getSummaryId, bo.getSummaryId());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceCode()), OaReportDetail::getDeviceCode, bo.getDeviceCode());
        lqw.eq(StringUtils.isNotBlank(bo.getCategory()), OaReportDetail::getCategory, bo.getCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceDescription()), OaReportDetail::getDeviceDescription, bo.getDeviceDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getReportDetail()), OaReportDetail::getReportDetail, bo.getReportDetail());
        lqw.eq(StringUtils.isNotBlank(bo.getOssIds()), OaReportDetail::getOssIds, bo.getOssIds());
        return lqw;
    }

    /**
     * 新增设计项目汇报详情
     */
    @Override
    public Boolean insertByBo(OaReportDetailBo bo) {
        OaReportDetail add = BeanUtil.toBean(bo, OaReportDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改设计项目汇报详情
     */
    @Override
    public Boolean updateByBo(OaReportDetailBo bo) {
        OaReportDetail update = BeanUtil.toBean(bo, OaReportDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(OaReportDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除设计项目汇报详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
