package com.fjhx.sale.service.contract.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.sale.entity.contract.po.Contract;
import com.fjhx.sale.entity.contract.po.ContractDocumentary;
import com.fjhx.sale.mapper.contract.ContractDocumentaryMapper;
import com.fjhx.sale.service.contract.ContractDocumentaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.sale.entity.contract.vo.ContractDocumentaryVo;
import com.fjhx.sale.entity.contract.dto.ContractDocumentarySelectDto;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.fjhx.sale.entity.contract.dto.ContractDocumentaryDto;
import cn.hutool.core.bean.BeanUtil;


/**
 * <p>
 * 外销跟单表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-08-30
 */
@Service
public class ContractDocumentaryServiceImpl extends ServiceImpl<ContractDocumentaryMapper, ContractDocumentary> implements ContractDocumentaryService {

    /**
     * 分页
     * @param dto
     * @return
     */
    @Override
    public Page<ContractDocumentaryVo> getPage(ContractDocumentarySelectDto dto) {
        IWrapper<ContractDocumentary> wrapper = getWrapper();
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getContractId()),"contract_id",dto.getContractId());
        wrapper.eq(ObjectUtil.isNotEmpty(dto.getDocumentaryType()),"documentary_type",dto.getDocumentaryType());
        wrapper.orderByDesc("cd", ContractDocumentary::getId);
        Page<ContractDocumentaryVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public ContractDocumentaryVo detail(Long id) {
        ContractDocumentary ContractDocumentary = this.getById(id);
        ContractDocumentaryVo result = BeanUtil.toBean(ContractDocumentary, ContractDocumentaryVo.class);
        return result;
    }

    /**
     * 添加
     * @param contractDocumentaryDto
     */
    @Override
    @DSTransactional
    public void add(ContractDocumentaryDto contractDocumentaryDto) {
        this.save(contractDocumentaryDto);
        ObsFileUtil.saveFile(contractDocumentaryDto.getFileList(),contractDocumentaryDto.getId());
    }

    @Override
    public void edit(ContractDocumentaryDto contractDocumentaryDto) {
        this.updateById(contractDocumentaryDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
