package vip.xiaonuo.sale.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.sale.entity.SalePrice;
import vip.xiaonuo.sale.mapper.SalePriceMapper;
import vip.xiaonuo.sale.param.SalePriceParam;
import vip.xiaonuo.sale.service.ISalePriceService;
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 SalePriceServiceImpl extends ServiceImpl<SalePriceMapper, SalePrice> implements ISalePriceService {

    @Autowired
    private SalePriceMapper priceMapper;

    @Override
    public List<SalePrice> getList(SalePriceParam priceParam) {
        String orgId = priceParam.getOrgId();
        String produceName = priceParam.getProduceName();
        Integer timeType = priceParam.getTimeType();
        QueryWrapper<SalePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId).eq(StringUtils.isNotBlank(produceName), "produce_name", produceName);
        queryWrapper.select(" convert(IFNULL(avg(price),'0'),decimal(10,2)) price,create_time createTime");
        if (0 == timeType) {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
        } else {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        }
        queryWrapper.orderByAsc("create_time");
        return priceMapper.selectList(queryWrapper);
    }

}
