package com.fjhx.sale.service.purchase.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.item.service.product.ProductClassifyService;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.sale.entity.purchase.dto.EhsdPurchaseProductDto;
import com.fjhx.sale.entity.purchase.dto.EhsdPurchaseProductSelectDto;
import com.fjhx.sale.entity.purchase.po.EhsdPurchaseProduct;
import com.fjhx.sale.entity.purchase.vo.EhsdPurchaseProductVo;
import com.fjhx.sale.mapper.purchase.EhsdPurchaseProductMapper;
import com.fjhx.sale.service.purchase.EhsdPurchaseProductService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * ehsd采购产品表 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-17
 */
@Service
public class EhsdPurchaseProductServiceImpl extends ServiceImpl<EhsdPurchaseProductMapper, EhsdPurchaseProduct> implements EhsdPurchaseProductService {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductClassifyService productClassifyService;

    @Override
    public Page<EhsdPurchaseProductVo> getPage(EhsdPurchaseProductSelectDto dto) {
        IWrapper<EhsdPurchaseProduct> wrapper = getWrapper();

        wrapper.orderByDesc("epp", EhsdPurchaseProduct::getId);
        Page<EhsdPurchaseProductVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

    @Override
    public EhsdPurchaseProductVo detail(Long id) {
        EhsdPurchaseProduct EhsdPurchaseProduct = this.getById(id);
        EhsdPurchaseProductVo result = BeanUtil.toBean(EhsdPurchaseProduct, EhsdPurchaseProductVo.class);
        return result;
    }

    @Override
    public void add(EhsdPurchaseProductDto ehsdPurchaseProductDto) {
        this.save(ehsdPurchaseProductDto);
    }

    @Override
    public void edit(EhsdPurchaseProductDto ehsdPurchaseProductDto) {
        this.updateById(ehsdPurchaseProductDto);
    }

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

    /**
     * 查询采购产品总金额，通过数据来源ID
     * @param contractIds
     * @return
     */
    @Override
    public List<EhsdPurchaseProductVo> getGroupSumAmount(List<Long> contractIds) {
        return baseMapper.getGroupSumAmount(contractIds);
    }

    /**
     * 查询采购产品总，通过数据来源ID
     * @param contractIds
     * @return
     */
    @Override
    public List<EhsdPurchaseProductVo> getPurchaseByContractId(List<Long> contractIds) {
        List<EhsdPurchaseProductVo> list = baseMapper.getPurchaseByContractId(contractIds);
        // 赋值产品属性
        productInfoService.attributeAssign(list, EhsdPurchaseProductVo::getProductId, (item, product) -> {
            item.setProductCategoryId(product.getProductClassifyId());
        });
        // 赋值产品分类名称
        productClassifyService.attributeAssign(list, EhsdPurchaseProductVo::getProductCategoryId, (item, productClassify) -> {
            item.setProductCategoryIds(productClassify.getParentIdSet());
        });
        return list;
    }

    /**
     * 根据合同ID查询采购产品列表
     * @param contractId
     * @return
     */
    @Override
    public List<EhsdPurchaseProductVo> getListByContractId(Long contractId) {
        return baseMapper.getListByContractId(contractId);
    }

    /**
     * 根据采购合同明细id获取采购明细数据
     */
    @Override
    public List<EhsdPurchaseProduct>getPurchaseProductByContractProductIds(List<Long> dataResourceIds){
        return baseMapper.getPurchaseProductByContractProductIds(dataResourceIds);
    }


}
