package vip.xiaonuo.sale.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.sale.entity.SaleDetail;
import vip.xiaonuo.sale.mapper.SaleDetailMapper;
import vip.xiaonuo.sale.param.SaleDetailPageParam;
import vip.xiaonuo.sale.param.SaleDetailParam;
import vip.xiaonuo.sale.service.ISaleDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 销售信息明细表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-13
 */
@Service
public class SaleDetailServiceImpl extends ServiceImpl<SaleDetailMapper, SaleDetail> implements ISaleDetailService {

    @Autowired
    private SaleDetailMapper detailMapper;

    @Override
    public List<SaleDetail> getList(SaleDetailParam detailParam) {
        String orgId = detailParam.getOrgId();
        String produceName = detailParam.getProduceName();
        QueryWrapper<SaleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId)
                .eq(StringUtils.isNotBlank(produceName), "produce_name", produceName);
        queryWrapper.select(" IFNULL(sum(sale_money),'0') saleMoney,customer customer");
        queryWrapper.groupBy("customer");
        return detailMapper.selectList(queryWrapper);
    }

    @Override
    public Page<SaleDetail> getList2(SaleDetailPageParam detailPageParam) {
        String orgId = detailPageParam.getOrgId();
        Page<SaleDetail> page = new Page<>(detailPageParam.getPageNum(), detailPageParam.getPageSize());
        QueryWrapper<SaleDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.orderByDesc("create_time");
        return detailMapper.selectPage(page, queryWrapper);
    }

}
