package com.jwipc.order.logic.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.exception.DaoException;
import com.jwipc.order.convert.AppointmentDockerConvert;
import com.jwipc.order.dto.AppointmentDockerDTO;
import com.jwipc.order.dto.AppointmentSupplierDTO;
import com.jwipc.order.entity.AppointmentDocker;
import com.jwipc.order.entity.AppointmentSupplier;
import com.jwipc.order.logic.AppointmentDockerLogic;
import com.jwipc.order.model.po.AppointmentSupplierPO;
import com.jwipc.order.model.query.AppointmentDockerQuery;
import com.jwipc.order.service.IAppointmentDockerService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author wt
 * @version 1.0
 * @since 2024/3/5
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class AppointmentDockerLogicImpl implements AppointmentDockerLogic {

    private final IAppointmentDockerService appointmentDockerService;
    private final AppointmentDockerConvert appointmentDockerConvert;

    @Override
    public List<AppointmentDockerDTO> listAll(AppointmentDockerQuery query) {
        List<AppointmentDocker> appointmentDockerList = appointmentDockerService.queryByPage(null,query);
        return appointmentDockerConvert.EntityToDtoList(appointmentDockerList);
    }

    @Override
    public PageView getList(AppointmentDockerQuery query) {
        final Page<AppointmentDocker> dbPage = PageUtil.toPage(query);
        List<AppointmentDocker> poList = appointmentDockerService.queryByPage(dbPage,query);

        List<AppointmentDockerDTO> dtoList = appointmentDockerConvert.entrty2PageListDTO(poList);
        return PageUtil.getPV(dtoList, dbPage.getTotal());
    }

    @Override
    public AppointmentDockerDTO selectById(String id) {
        AppointmentDocker appointmentDocker = appointmentDockerService.getById(id);
        return appointmentDockerConvert.entityToDto(appointmentDocker);
    }

    @Override
    public void createOrUpdate(AppointmentDockerDTO parseObject) {
        Long id = parseObject.getId();
        if (ObjectUtil.isNull(id)) {
            List<AppointmentDocker> dayoffList = appointmentDockerService.list(new LambdaQueryWrapper<AppointmentDocker>()
                    .eq(AppointmentDocker::getDockerCode, parseObject.getDockerCode())
                    .eq(AppointmentDocker::getFactoryCode, parseObject.getFactoryCode()));
            if (CollectionUtil.isNotEmpty(dayoffList)) {
                throw new DaoException("工厂码头已经存在");
            }
        }
        AppointmentDocker appointmentDocker = appointmentDockerConvert.dtoToEntity(parseObject);
        boolean b = appointmentDockerService.saveOrUpdate(appointmentDocker);
        if(!b){
            throw new DaoException("保存失败，请联系管理员");
        }
    }

    @Override
    public void deleteById(Long id) {
        boolean b = appointmentDockerService.removeById(id);
        if(!b){
            throw new DaoException("删除失败，请联系管理员");
        }
    }
}
