package com.ruoyi.project.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.enums.FileBuzType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.project.business.domain.TbFile;
import com.ruoyi.project.business.domain.TqContractLiftHistory;
import com.ruoyi.project.business.mapper.contract.TqContractLiftHistoryMapper;
import com.ruoyi.project.business.service.ITbFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.business.mapper.contract.TqContractHistoryMapper;
import com.ruoyi.project.business.domain.TqContractHistory;
import com.ruoyi.project.business.service.ITqContractHistoryService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

/**
 * 合同履历Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-16
 */
@Service
public class TqContractHistoryServiceImpl implements ITqContractHistoryService 
{
    @Resource
    private TqContractHistoryMapper tqContractHistoryMapper;

    @Resource
    private TqContractLiftHistoryMapper tqContractLiftHistoryMapper;

    @Resource
    private ITbFileService tbFileService;


    /**
     * 查询合同履历
     * 
     * @param id 合同履历主键
     * @return 合同履历
     */
    @Override
    public TqContractHistory selectTqContractHistoryById(Long id)
    {
        return tqContractHistoryMapper.selectTqContractHistoryById(id);
    }

    @Override
    public TqContractHistory getContractHistory4Contract(Long id)
    {
        TqContractHistory tqContractHistory = tqContractHistoryMapper.getContractHistory4Contract(id);
        if(!ObjectUtils.isEmpty(tqContractHistory)){
            TqContractLiftHistory search  = new TqContractLiftHistory();
            search.setContractNo(tqContractHistory.getContractNo());
            search.setVersion(tqContractHistory.getVersion());

            tqContractHistory.setTqContractLiftHistoryList(tqContractLiftHistoryMapper.selectTqContractLiftHistoryList(search));
        }
        List<TbFile> annex = tbFileService.list(Wrappers.<TbFile>lambdaQuery().eq(TbFile::getBusinessId, id)
                .eq(TbFile::getBusinessType, FileBuzType.CONTRACT_HISTORY.getType()));
        if (Objects.nonNull(tqContractHistory)) {
            tqContractHistory.setFile(annex.stream().map(TbFile::getFilePath).collect(Collectors.joining(",")));
        } else {
            throw new ServiceException(MessageUtils.message("common.dataDeleted"));
        }
        return tqContractHistory;
    }
    /**
     * 查询合同履历列表
     * 
     * @param tqContractHistory 合同履历
     * @return 合同履历
     */
    @Override
    public List<TqContractHistory> selectTqContractHistoryList(TqContractHistory tqContractHistory)
    {
        return tqContractHistoryMapper.selectTqContractHistoryList(tqContractHistory);
    }

    /**
     * 新增合同履历
     * 
     * @param tqContractHistory 合同履历
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTqContractHistory(TqContractHistory tqContractHistory)
    {
        //首先看下是否存在版本
        Long version = tqContractHistoryMapper.selectVersionByContractNo(tqContractHistory.getContractNo());
        version = version !=null? version+1: 1;
        //存在即版本+1  否则初始版本1
        tqContractHistory.setVersion(version);
        //电梯履历
        List<TqContractLiftHistory> tqContractLiftHistoryList = tqContractHistory.getTqContractLiftHistoryList();
        if(!CollectionUtils.isEmpty(tqContractLiftHistoryList)){
            Long finalVersion = version;
            tqContractLiftHistoryList.forEach(item->{
                item.setVersion(finalVersion);
            });
            tqContractLiftHistoryMapper.insertTqContractLiftHistoryBatch(tqContractLiftHistoryList);
        }
        tqContractHistory.setCreateTime(DateUtils.getNowDate());
        int r1 =  tqContractHistoryMapper.insertTqContractHistory(tqContractHistory);
        //附件上传
        boolean r2 =   tbFileService.saveOrUpdateFile(tqContractHistory.getFile(),tqContractHistory.getId(), FileBuzType.CONTRACT_HISTORY.getType());
        return r1;
    }

    /**
     * 修改合同履历
     * 
     * @param tqContractHistory 合同履历
     * @return 结果
     */
    @Override
    public int updateTqContractHistory(TqContractHistory tqContractHistory)
    {
        tqContractHistory.setUpdateTime(DateUtils.getNowDate());
        return tqContractHistoryMapper.updateTqContractHistory(tqContractHistory);
    }

    /**
     * 批量删除合同履历
     * 
     * @param ids 需要删除的合同履历主键
     * @return 结果
     */
    @Override
    public int deleteTqContractHistoryByIds(Long[] ids)
    {
        return tqContractHistoryMapper.deleteTqContractHistoryByIds(ids);
    }

    /**
     * 删除合同履历信息
     * 
     * @param id 合同履历主键
     * @return 结果
     */
    @Override
    public int deleteTqContractHistoryById(Long id)
    {
        return tqContractHistoryMapper.deleteTqContractHistoryById(id);
    }
}
