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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.sale.entity.serviceContract.po.ServiceContractRecord;
import com.fjhx.sale.mapper.serviceContract.ServiceContractRecordMapper;
import com.fjhx.sale.service.serviceContract.ServiceContractRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.BasePo;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.sale.entity.serviceContract.vo.ServiceContractRecordVo;
import com.fjhx.sale.entity.serviceContract.dto.ServiceContractRecordSelectDto;
import com.fjhx.sale.entity.serviceContract.dto.ServiceContractRecordDto;
import cn.hutool.core.bean.BeanUtil;

import java.util.List;


/**
 * <p>
 * 服务合同记录表 服务实现类
 * </p>
 *
 * @author
 * @since 2023-04-18
 */
@Service
public class ServiceContractRecordServiceImpl extends ServiceImpl<ServiceContractRecordMapper, ServiceContractRecord> implements ServiceContractRecordService {

    @Override
    public Page<ServiceContractRecordVo> getPage(ServiceContractRecordSelectDto dto) {
        LambdaQueryWrapper<ServiceContractRecord> wrapper = Wrappers.<ServiceContractRecord>lambdaQuery();
        wrapper.orderByDesc(ServiceContractRecord::getServiceTime);
        wrapper.eq(ServiceContractRecord::getServiceContractId,dto.getServiceContractId());
        Page<ServiceContractRecordVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<ServiceContractRecordVo> records = page.getRecords();
        // 赋值用户名称
        UserUtil.assignmentNickName(records, BasePo::getCreateUser,ServiceContractRecordVo::setUserName);
        return page;
    }

    @Override
    public ServiceContractRecordVo detail(Long id) {
        ServiceContractRecord ServiceContractRecord = this.getById(id);
        ServiceContractRecordVo result = BeanUtil.toBean(ServiceContractRecord, ServiceContractRecordVo.class);

        return result;
    }

    @Override
    public void add(ServiceContractRecordDto serviceContractRecordDto) {
        this.save(serviceContractRecordDto);

        //添加附件信息
        ObsFileUtil.saveFile(serviceContractRecordDto.getFileList(),serviceContractRecordDto.getId());

    }

}
