/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.feesapi.dzht.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.common.exception.FeesErrorCode;
import com.tofly.feesapi.common.exception.FeesException;
import com.tofly.feesapi.config.FilePathBean;
import com.tofly.feesapi.dzht.entity.HtTemplate;
import com.tofly.feesapi.dzht.service.HtTemplateService;
import com.tofly.feesapi.file.entity.File;
import com.tofly.feesapi.file.service.IFileInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tofly.feesapi.dzht.mapper.HtTemplateMapper;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 合同模版
 *
 * @author admin
 * @date Tue Nov 19 00:00:00 GMT+08:00 2024
 */
@Slf4j
@Service("htTemplateService")
public class HtTemplateServiceImpl extends ServiceImpl<HtTemplateMapper, HtTemplate> implements HtTemplateService {
    @Autowired
    private IFileInfoService iFileInfoService;
    @Autowired
    private FilePathBean filePathBean;

    @Override
    public Page<HtTemplate> getPage(Page page, HtTemplate htTemplate) {
        Page<HtTemplate> pageResult = this.page(page, Wrappers.query(htTemplate));
        return pageResult;
    }

    /**
     * 根据ID合同模版查询
     *
     * @param id
     * @return
     */
    @Override
    public HtTemplate getHtTemplateById(String id) {
        HtTemplate htTemplate = this.getById(id);
        return htTemplate;
    }

    /**
     * 合同模版保存
     *
     * @param htTemplate
     * @return
     */
    @Override
    public Boolean HtSave(HtTemplate htTemplate) {
        try {
            this.save(htTemplate);
            if (StringUtil.isNotNull(htTemplate.getMultipartFile())) {
                File file = iFileInfoService.saveFile(htTemplate.getMultipartFile(), filePathBean.getHtmbfile(), htTemplate.getId(), "HT_TEMPLATE");
                htTemplate.setMbdz(file.getFileNewFn());
            }
            return this.updateById(htTemplate);
        } catch (Exception e) {
            log.info("合同模版保存失败:" + e.getMessage());
            throw new FeesException("合同模版保存失败", FeesErrorCode.BUSINESS_ERROR);
        }
    }

    /**
     * 合同模版修改
     *
     * @param htTemplate
     * @return
     */
    @Override
    public Boolean HtUpdate(HtTemplate htTemplate) {
        try {
            if (StringUtil.isEmpty(htTemplate.getId())) {
                throw new FeesException("合同模版ID不能为空", FeesErrorCode.BUSINESS_ERROR);
            }

            iFileInfoService.removeFileListByTableId(htTemplate.getId(), "HT_TEMPLATE");
            if (StringUtil.isNotNull(htTemplate.getMultipartFile())) {
                File file = iFileInfoService.saveFile(htTemplate.getMultipartFile(), filePathBean.getHtmbfile(), htTemplate.getId(), "HT_TEMPLATE");
                htTemplate.setMbdz(file.getFileNewFn());
            }
            return this.updateById(htTemplate);
        } catch (Exception e) {
            log.info("合同模版修改失败:" + e.getMessage());
            throw new FeesException("合同模版修改失败", FeesErrorCode.BUSINESS_ERROR);
        }
    }

    /**
     * 验证合同模版是否使用
     *
     * @param ids 合同模版id集合
     * @return
     */
    @Override
    public Boolean yzhtsfsy(String ids) {
        List<File> fileList = iFileInfoService.getFileList(
                new QueryWrapper<File>().lambda()
                        .in(File::getTableBh, Arrays.stream(ids.split(",")))
                        .eq(File::getTableName, "HT_TEMPLATE")
        );
        if (fileList.size() > 0) {
            return true;
        }
        return false;
    }
}
