package com.jwipc.order.logic.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.exception.DaoException;
import com.jwipc.order.convert.AppointmentSupplierConvert;
import com.jwipc.order.dto.AppointmentSupplierDTO;
import com.jwipc.order.entity.AppointmentSupplier;
import com.jwipc.order.logic.AppointmentSupplierLogic;
import com.jwipc.order.model.po.AppointmentSupplierPO;
import com.jwipc.order.model.query.AppointmentSupplierQuery;
import com.jwipc.order.service.IAppointmentSupplierService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 *  送货单预约供应商配置
 * </p>
 *
 * @author wt
 * @version 1.0
 * @since 2024/3/4
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AppointmentSupplierLogicImpl implements AppointmentSupplierLogic {

    private final IAppointmentSupplierService appointmentSupplierService;
    private final AppointmentSupplierConvert appointmentSupplierConvert;
    @Override
    public PageView getList(AppointmentSupplierQuery query) {
        final Page<AppointmentSupplier> dbPage = PageUtil.toPage(query);
        List<AppointmentSupplierPO> poList = appointmentSupplierService.findPageByQuery(dbPage,query);

        List<AppointmentSupplierDTO> dtoList = appointmentSupplierConvert.po2PageListDTO(poList);
        return PageUtil.getPV(dtoList, dbPage.getTotal());
    }

    @Override
    public AppointmentSupplierDTO getById(Long id) {
        AppointmentSupplier po = appointmentSupplierService.getById(id);
        return appointmentSupplierConvert.EntityToDTO(po);
    }

    @Override
    public void saveData(AppointmentSupplierDTO dto) {
        AppointmentSupplier appointmentSupplier = appointmentSupplierConvert.DTO2Entity(dto);
        if(dto.getId() == null){
            appointmentSupplier.setCreateId(LoginUtil.getUserId());
            appointmentSupplier.setCreateTime(LocalDateTime.now());
        }
        appointmentSupplierService.saveOrUpdate(appointmentSupplier);
    }

    @Override
    public void remove(Long id) {
        AppointmentSupplier appointmentSupplier = new AppointmentSupplier();
        appointmentSupplier.setId(id);
        appointmentSupplier.setDeleteFlag(1);
        boolean b = appointmentSupplierService.updateById(appointmentSupplier);
        if(!b){
            throw new DaoException("删除失败,请联系管理员");
        }
    }

    @Override
    public void updateStatus(Long id) {
        AppointmentSupplier appointmentSupplier = appointmentSupplierService.getById(id);
        Integer status = appointmentSupplier.getStatus();
        appointmentSupplier.setStatus(status == 0 ? 1 : 0);
        boolean b = appointmentSupplierService.updateById(appointmentSupplier);
        if(!b){
            throw new DaoException("修改失败,请联系管理员");
        }
    }
}
