package com.gxwebsoft.oa.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.oa.entity.Product;
import com.gxwebsoft.oa.mapper.ProductMapper;
import com.gxwebsoft.oa.param.ProductParam;
import com.gxwebsoft.oa.service.CompanyService;
import com.gxwebsoft.oa.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 产品记录表Service实现
 *
 * @author 科技小王子
 * @since 2023-12-14 14:18:01
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
  @Resource
  private CompanyService companyService;

    @Override
    public PageResult<Product> pageRel(ProductParam param) {
        PageParam<Product, ProductParam> page = new PageParam<>(param);
        page.setDefaultOrder("sort_number asc, create_time desc");
        List<Product> list = baseMapper.selectPageRel(page, param);
        list.forEach(d -> {
          d.setCompany(companyService.getById(d.getCompanyId()));
        });
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<Product> listRel(ProductParam param) {
        List<Product> list = baseMapper.selectListRel(param);
        // 排序
        PageParam<Product, ProductParam> page = new PageParam<>();
        page.setDefaultOrder("sort_number asc, create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public Product getByIdRel(Integer productId) {
        ProductParam param = new ProductParam();
        param.setProductId(productId);
        Product product = param.getOne(baseMapper.selectListRel(param));
        if(product != null){
          product.setCompany(companyService.getById(product.getCompanyId()));
        }
      return product;
    }

}
