package com.fjhx.attachment.service.impl;

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.fjhx.attachment.mapper.StockAttachmentMapper;
import com.fjhx.attachment.service.StockAttachmentService;
import com.fjhx.entity.attachment.StockAttachment;
import com.fjhx.params.stock.StockAttachmentVo;
import com.fjhx.utils.WrapperUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 业务附件表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-07-12
 */
@Service
public class StockAttachmentServiceImpl extends ServiceImpl<StockAttachmentMapper, StockAttachment> implements StockAttachmentService {

    @Override
    public Page<StockAttachment> getPage(Map<String, String> condition) {

        QueryWrapper<StockAttachment> wrapper = Wrappers.query();

        WrapperUtil.init(condition, wrapper)
                .eqTenantId()
                .createTimeDesc();

        Page<StockAttachment> page = page(condition, wrapper);
        return page;
    }

    /**
     * 添加
     * @param stockAttachmentVo
     */
    @Transactional(rollbackFor = Exception.class)
    @TenantIgnore
    @Override
    public void add(StockAttachmentVo stockAttachmentVo) {
        if(StringUtil.isEmpty(stockAttachmentVo.getBusiId())||
        StringUtil.isEmpty(stockAttachmentVo.getPath())){
            throw new ServiceException("参数异常");
        }
        stockAttachmentVo.setCreatedTime(new Date());
        save(stockAttachmentVo);
    }

    /**
     * 修改
     * @param stockAttachmentVo
     */
    @Transactional(rollbackFor = Exception.class)
    @TenantIgnore
    @Override
    public void edit(StockAttachmentVo stockAttachmentVo) {
        if(StringUtil.isEmpty(stockAttachmentVo.getId())){
            throw new ServiceException("参数异常");
        }
        updateById(stockAttachmentVo);
    }

    /**
     * 删除
     * @param stockAttachmentVo
     */
    @Transactional(rollbackFor = Exception.class)
    @TenantIgnore
    @Override
    public void delete(StockAttachmentVo stockAttachmentVo) {
        if(StringUtil.isEmpty(stockAttachmentVo.getId())){
            throw new ServiceException("参数异常");
        }
        removeById(stockAttachmentVo.getId());
    }

    /**
     * 根据业务ID查询文件
     * @param busi
     * @return
     */
    @TenantIgnore
    @Override
    public List<StockAttachment> getListByBusi(String busi) {
        if(StringUtil.isEmpty(busi)){
            throw new ServiceException("参数异常");
        }
        return list(Wrappers.<StockAttachment>query().lambda().eq(StockAttachment::getBusiId,busi));
    }

}
