package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.helper.LoginHelper;
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 com.ruoyi.oa.domain.bo.SysOaClaimDetailBo;
import com.ruoyi.oa.domain.bo.SysOaFileBo;
import com.ruoyi.oa.service.ISysOaClaimDetailService;
import com.ruoyi.system.service.ISysOssService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.SysOaClaimBo;
import com.ruoyi.oa.domain.vo.SysOaClaimVo;
import com.ruoyi.oa.domain.SysOaClaim;
import com.ruoyi.oa.mapper.SysOaClaimMapper;
import com.ruoyi.oa.service.ISysOaClaimService;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 差旅费报销Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-21
 */
@RequiredArgsConstructor
@Service
public class SysOaClaimServiceImpl implements ISysOaClaimService {

    private final SysOaClaimMapper baseMapper;

    @Autowired
    private ISysOaClaimDetailService sysOaClaimDetailService;

    @Autowired
    private ISysOssService ossService;


    /**
     * 查询差旅费报销
     */
    @Override
    public SysOaClaimVo queryById(Long claimId){
        return baseMapper.selectSysOaClaimVoById(claimId);
    }

    /**
     * 根据实例id返回vo
     * @param procInsId 实例id
     * @return vo
     */
    @Override
    public SysOaClaimVo queryByProcInsId(String procInsId) {
        return baseMapper.selectSysOaClaimVoByProcInsId(procInsId);
    }

    @Override
    public TableDataInfo<SysOaClaimVo> queryPageListByMoney(SysOaClaimBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        LambdaQueryWrapper<SysOaClaim> lqw = buildQueryWrapper(bo);
        lqw.isNull(SysOaClaim::getEndTime);
        Page<SysOaClaimVo> result = baseMapper.selectPageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);

    }

    @Override
    public TableDataInfo<SysOaClaimVo> queryPageListByClaim(SysOaClaimBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        LambdaQueryWrapper<SysOaClaim> lqw = buildQueryWrapper(bo);
        lqw.isNotNull(SysOaClaim::getEndTime);
        Page<SysOaClaimVo> result = baseMapper.selectPageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询差旅费报销列表
     */
    @Override
    public TableDataInfo<SysOaClaimVo> queryPageList(SysOaClaimBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getUserId());
        LambdaQueryWrapper<SysOaClaim> lqw = buildQueryWrapper(bo);
        Page<SysOaClaimVo> result = baseMapper.selectPageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询差旅费报销列表
     */
    @Override
    public List<SysOaClaimVo> queryList(SysOaClaimBo bo) {
        LambdaQueryWrapper<SysOaClaim> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysOaClaim> buildQueryWrapper(SysOaClaimBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOaClaim> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, SysOaClaim::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getFileIds()), SysOaClaim::getFileIds, bo.getFileIds());
        lqw.eq(bo.getStartTime() != null, SysOaClaim::getStartTime, bo.getStartTime());
        lqw.eq(bo.getTripDays() != null, SysOaClaim::getTripDays, bo.getTripDays());
        lqw.eq(bo.getCost() != null, SysOaClaim::getCost, bo.getCost());
        lqw.eq(bo.getDetailNumber() != null, SysOaClaim::getDetailNumber, bo.getDetailNumber());
        lqw.eq(bo.getProjectId() != null, SysOaClaim::getProjectId, bo.getProjectId());
        lqw.eq(StringUtils.isNotBlank(bo.getProcInsId()), SysOaClaim::getProcInsId, bo.getProcInsId());
        lqw.eq(bo.getCompletedTime() != null, SysOaClaim::getCompletedTime, bo.getCompletedTime());
        lqw.eq(SysOaClaim::getDelFlag, 0L);
        return lqw;
    }

    /**
     * 新增差旅费报销
     */
    @Override
    public Boolean insertByBo(SysOaClaimBo bo) {
        bo.setUserId(LoginHelper.getUserId());
        SysOaClaim add = BeanUtil.toBean(bo, SysOaClaim.class);
        validEntityBeforeSave(add);

        // 处理文件
        List<String> fileUrls = bo.getFileList().stream().map(SysOaFileBo::getFileUrl).collect(Collectors.toList());
        String fileIdsString = ossService.insertFiles(fileUrls);
        add.setFileIds(fileIdsString);
        add.setUserId(LoginHelper.getUserId());
        boolean flag = baseMapper.insert(add) > 0;
        if(bo.getClaimDetailList() != null && !bo.getClaimDetailList().isEmpty()) {
            for (SysOaClaimDetailBo sysOaClaimDetail : bo.getClaimDetailList()) {
                sysOaClaimDetail.setClaimId(add.getClaimId());
                sysOaClaimDetailService.insertByBo(sysOaClaimDetail);
            }
        }
        if (flag) {
            bo.setClaimId(add.getClaimId());
        }
        return flag;
    }

    /**
     * 修改差旅费报销
     */
    @Override
    public Boolean updateByBo(SysOaClaimBo bo) {
        SysOaClaim update = BeanUtil.toBean(bo, SysOaClaim.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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


}
