package com.jwipc.order.logic.impl;

import cn.hutool.core.util.StrUtil;
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.PurchaseOrderBomConvert;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.order.dto.PurchaseOrderBomDTO;
import com.jwipc.order.entity.PurchaseOrderBom;
import com.jwipc.order.logic.PurchaseOrderBomLogic;
import com.jwipc.order.model.po.PurchaseOrderBomPO;
import com.jwipc.order.model.query.PurchaseOrderBomQuery;
import com.jwipc.order.service.IPurchaseOrderBomService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
 * <p> </p>
 *
 * @author BOGON
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@RequiredArgsConstructor
public class PurchaseOrderBomLogicImpl implements PurchaseOrderBomLogic {
    private final IPurchaseOrderBomService purchaseOrderBomService;
    private final PurchaseOrderBomConvert purchaseOrderBomConvert;

    @Override
    public PageView findByPage(PurchaseOrderBomQuery query) {
        final Page<PurchaseOrderBom> dbPage = PageUtil.toPage(query);

        List<PurchaseOrderBomPO> purchaseOrderBomPOS = purchaseOrderBomService.findPageByQuery(dbPage, query);

        return PageUtil.getPV(purchaseOrderBomConvert.po2PageListDTO(purchaseOrderBomPOS), dbPage.getTotal());
    }

    @Override
    public PurchaseOrderBomDTO getById(Long id) {
        if (id != null) {
            PurchaseOrderBomPO purchaseOrderBomPO = purchaseOrderBomService.getDataById(id);
            return purchaseOrderBomConvert.po2DTO(purchaseOrderBomPO);
        } else {
            return new PurchaseOrderBomDTO();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(PurchaseOrderBomDTO param) {

        PurchaseOrderBom getOneData = purchaseOrderBomService.getOne(new LambdaQueryWrapper<PurchaseOrderBom>()
                .eq(PurchaseOrderBom::getMaterialCode, param.getMaterialCode())
                .eq(PurchaseOrderBom::getComponentCode, param.getComponentCode())
                .eq(PurchaseOrderBom::getFactoryCode, param.getFactoryCode()));

        //保存
        if (Objects.isNull(param.getId())) {
            if (Objects.nonNull(getOneData)) {
                throw new DaoException(StrUtil.format("套件物料编码：{},子件物料编码：{},工厂：{},在系统中不存在", param.getMaterialCode(), param.getComponentCode(), param.getFactoryCode()));
            }
            param.setCreateTime(LocalDateTime.now());
        } else {
            if (Objects.nonNull(getOneData) && getOneData.getId().compareTo(param.getId()) != 0) {
                throw new DaoException(StrUtil.format("套件物料编码：{},子件物料编码：{},工厂：{},在系统中不存在", param.getMaterialCode(), param.getComponentCode(), param.getFactoryCode()));
            }
            param.setUpdateTime(LocalDateTime.now());
        }


        purchaseOrderBomService.saveOrUpdate(purchaseOrderBomConvert.param2Entity(param));
    }

    @Override
    public void remove(Long id) {
        purchaseOrderBomService.remove(new LambdaQueryWrapper<PurchaseOrderBom>().eq(PurchaseOrderBom::getId, id));
    }

//    @Override
//    public List<PurchaseOrgDTO> listAll(PurchaseOrgQuery query) {
//        List<PurchaseOrgPO> purchaseOrgPOS = purchaseOrgService.findPageByQuery(null, query);
//        return purchaseOrgConvert.po2PageListDTO(purchaseOrgPOS);
//    }
}