package com.jwipc.supplier.logic.impl;

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.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.supplier.convert.SupplierAgreementConvert;
import com.jwipc.supplier.dto.SupplierAgreementDTO;
import com.jwipc.supplier.entity.SupplierAgreement;
import com.jwipc.supplier.entity.SupplierAgreementVersion;
import com.jwipc.supplier.logic.SupplierAgreementLogic;
import com.jwipc.supplier.model.po.SupplierAgreementPO;
import com.jwipc.supplier.model.query.SupplierAgreementQuery;
import com.jwipc.supplier.service.ISupplierAgreementService;
import com.jwipc.supplier.service.ISupplierAgreementVersionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @author shenyang.ou
 * @date 2024/2/2 22:27
 * @desc
 */
@Service
@RequiredArgsConstructor
public class SupplierAgreementLogicImpl implements SupplierAgreementLogic {
    private final ISupplierAgreementService supplierAgreementService;
    private final ISupplierAgreementVersionService agreementVersionService;
    private final SupplierAgreementConvert supplierAgreementConvert;
    @Override
    public PageView findByPage(SupplierAgreementQuery query) {
        final Page<SupplierAgreementPO> dbPage = PageUtil.toPage(query);
        List<SupplierAgreementPO> supplierAgreementPOS = supplierAgreementService.findPageByQuery(dbPage, query);
        List<SupplierAgreementDTO> supplierAgreementDTOS = supplierAgreementConvert.po2DTOList(supplierAgreementPOS);
        return PageUtil.getPV(supplierAgreementDTOS, dbPage.getTotal());
    }

    @Override
    public SupplierAgreementDTO getById(Long id) {
        SupplierAgreement supplierAgreement = supplierAgreementService.getById(id);
        return supplierAgreementConvert.entity2DTO(supplierAgreement);
    }

    @Override
    public void saveOrUpdate(SupplierAgreementDTO param) {
        //保存
        if (param.getId() == null) {
            if (ObjectUtil.isNotNull(supplierAgreementService.getOne(new LambdaQueryWrapper<SupplierAgreement>().eq(
                    SupplierAgreement::getAgreementVersion, param.getAgreementVersion()).last(" limit 1")))) {
                throw new DaoException("协议版本重复, 请检查");
            }
            param.setCreateTime(LocalDateTime.now());
            param.setCreateUser(Objects.requireNonNull(LoginUtil.getCurrentUser()).getUserId());
        } else {
            if (ObjectUtil.isNotNull(supplierAgreementService.getOne(new LambdaQueryWrapper<SupplierAgreement>().eq(
                    SupplierAgreement::getAgreementVersion, param.getAgreementVersion()).ne(SupplierAgreement::getId, param.getId()).last(" limit 1")))) {
                throw new DaoException("协议版本重复, 请检查");
            }
            if (ObjectUtil.isNotNull(agreementVersionService.getOne(new LambdaQueryWrapper<SupplierAgreementVersion>().eq(
                    SupplierAgreementVersion::getAgreementVersion, param.getAgreementVersion()).last(" limit 1")))) {
                throw new DaoException("该协议正在使用, 无法更新, 请检查");
            }
        }
        supplierAgreementService.saveOrUpdate(supplierAgreementConvert.dto2Entity(param));
    }


    /**
     * 删除
     *
     * @param id 参数
     */
    @Override
    public void remove(Long id) {
        //判断是否可以删除
        SupplierAgreement supplierAgreement = supplierAgreementService.getById(id);
        if (ObjectUtil.isNotNull(agreementVersionService.getOne(new LambdaQueryWrapper<SupplierAgreementVersion>().eq(
                SupplierAgreementVersion::getAgreementVersion, supplierAgreement.getAgreementVersion()).last(" limit 1")))) {
            throw new DaoException("该协议正在使用, 无法删除, 请检查");
        }
        supplierAgreementService.removeById(id);
    }
}
