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

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.fjhx.common.constant.SourceConstant;
import com.fjhx.sale.entity.sample.dto.SampleProductDto;
import com.fjhx.sale.entity.sample.dto.SampleProductSelectDto;
import com.fjhx.sale.entity.sample.po.SampleProduct;
import com.fjhx.sale.entity.sample.vo.SampleProductVo;
import com.fjhx.item.service.product.ProductClassifyService;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.sale.mapper.sample.SampleProductMapper;
import com.fjhx.sale.service.sample.SampleProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.wrapper.SqlField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.wrapper.IWrapper;
import cn.hutool.core.bean.BeanUtil;

import java.math.BigDecimal;
import java.util.List;


/**
 * <p>
 * 样品单-产品 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-05-05
 */
@Service
public class SampleProductServiceImpl extends ServiceImpl<SampleProductMapper, SampleProduct> implements SampleProductService {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductClassifyService productClassifyService;

    @Override
    public Page<SampleProductVo> getPage(SampleProductSelectDto dto) {
        IWrapper<SampleProduct> wrapper = getWrapper();
        wrapper.orderByDesc("t1", SampleProduct::getCreateTime);
        wrapper.gt("t1",SampleProduct::getExpendQuantity, BigDecimal.ZERO);
        if(StringUtils.isNotEmpty(dto.getKeyword())){
            wrapper.keyword(dto.getKeyword(),new SqlField("t2.`code`"),new SqlField("t2.`user_name`"));
        }
        Page<SampleProductVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        List<SampleProductVo> list = page.getRecords();
        if(CollectionUtils.isNotEmpty(list)){
            // 赋值产品属性
            productInfoService.attributeAssign(list, SampleProductVo::getProductId, (item, product) -> {
                item.setProductCode(product.getCode());
                item.setProductUnit(product.getUnit());
                item.setProductType(product.getType());
                item.setProductName(product.getName());
                item.setProductSpec(product.getSpec());
                item.setProductCategoryId(product.getProductClassifyId());
            });
            // 赋值产品分类名称
            productClassifyService.attributeAssign(list, SampleProductVo::getProductCategoryId, (item, productClassify) -> {
                item.setProductCategory(productClassify.getName());
            });
        }
        return page;
    }

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

    @Override
    public void add(SampleProductDto sampleProductDto) {
        this.save(sampleProductDto);
    }

    @Override
    public void edit(SampleProductDto sampleProductDto) {
        this.updateById(sampleProductDto);
    }

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

}
