package com.jwipc.sys.logic.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.dto.SupplierNameConvertDTO;
import com.jwipc.supplier.entity.SupplierNameConvert;
import com.jwipc.supplier.model.po.SupplierNameConvertPO;
import com.jwipc.supplier.model.query.SupplierNameConvertQuery;
import com.jwipc.supplier.service.ISupplierNameConvertService;
import com.jwipc.sys.convert.SupplierNameConvertConvert;
import com.jwipc.sys.logic.SupplierNameConvertLogic;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author qizhicai
 * @version JDK 1.8
 * @date 2024/7/31
 * @description TODO
 */
@Service
@RequiredArgsConstructor
public class SupplierNameConvertLogicImpl implements SupplierNameConvertLogic {

    private final ISupplierNameConvertService supplierNameConvertService;
    private final SupplierNameConvertConvert supplierNameConvertConvert;
    @Override
    public PageView findByPage(SupplierNameConvertQuery query) {

        final Page<SupplierNameConvert> dbPage = PageUtil.toPage(query);

        List<SupplierNameConvertPO> supplierNameConvertPos = supplierNameConvertService.findPageByQuery(dbPage, query);

        return PageUtil.getPV(supplierNameConvertConvert.entity2PageListDTO(supplierNameConvertPos), dbPage.getTotal());
    }

    @Override
    public SupplierNameConvertDTO getById(Long id) {
        SupplierNameConvert byId = supplierNameConvertService.getById(id);
        return supplierNameConvertConvert.entity2DTO(byId);
    }

    @Override
    public void saveOrUpdate(SupplierNameConvertDTO param) {
        if(!Objects.nonNull(param.getId())){
            param.setCreateId(Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());
            param.setCreateTime(LocalDateTime.now());
        }
        param.setUpdateTime(LocalDateTime.now());
        param.setUpdateId(Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());
        supplierNameConvertService.saveOrUpdate(supplierNameConvertConvert.dto2Entity(param));
    }

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


}
