package com.fjhx.purchase.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.purchase.entity.purchase.dto.PurchaseDetailDto;
import com.fjhx.purchase.entity.purchase.dto.PurchaseDetailSelectDto;
import com.fjhx.purchase.entity.purchase.po.PurchaseDetail;
import com.fjhx.purchase.entity.purchase.vo.PurchaseDetailVo;
import com.fjhx.purchase.mapper.purchase.PurchaseDetailMapper;
import com.fjhx.purchase.service.purchase.PurchaseDetailService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 采购单明细 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-04-06
 */
//@DS(SourceConstant.PURCHASE)
@Service
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailMapper, PurchaseDetail> implements PurchaseDetailService {

    @Override
    public Page<PurchaseDetailVo> getPage(PurchaseDetailSelectDto dto) {
        IWrapper<PurchaseDetail> wrapper = getWrapper();
        wrapper.orderByDesc("pd", PurchaseDetail::getId);
        Page<PurchaseDetailVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }

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

    @Override
    public void add(PurchaseDetailDto purchaseDetailDto) {
        this.save(purchaseDetailDto);
    }

    /**
     * 编辑
     * @param purchaseDetailDto
     */
    @Override
    public void edit(PurchaseDetailDto purchaseDetailDto) {
        this.updateById(purchaseDetailDto);
    }

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

    /**
     * 获取外销合同总采购量
     * @param dataResourceIds
     * @return
     */
    @Override
    public List<PurchaseDetailVo> getSumCountInDataResourceId(List<Long> dataResourceIds) {
        return baseMapper.getSumCountInDataResourceId(dataResourceIds);
    }

    /**
     * 获取外销合同总采购量审批通过的
     *
     * @param dataResourceIds
     * @return
     */
    @Override
    public List<PurchaseDetailVo> getSumCountInDataResourceIds(List<Long> dataResourceIds) {
        return baseMapper.getSumCountInDataResourceIds(dataResourceIds);
    }

    @Override
    public List<PurchaseDetailVo> getPurchaseCountListBySubscribeDetailIds(List<Long> subscribeDetailIds) {
        IWrapper<PurchaseDetail> wrapper = getWrapper();
        wrapper.in("p.status", 10, 30, 99);
        wrapper.in(PurchaseDetail::getSubscribeDetailId, subscribeDetailIds);
        return baseMapper.getPurchaseDetailList(wrapper);
    }

}
