package vip.xiaonuo.quality.service.impl;

import cn.hutool.core.date.DateUtil;
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.quality.entity.QualityReagent;
import vip.xiaonuo.quality.mapper.QualityReagentMapper;
import vip.xiaonuo.quality.param.QualityReagentParam;
import vip.xiaonuo.quality.service.IQualityReagentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 质量试剂使用情况 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-11
 */
@Service
public class QualityReagentServiceImpl extends ServiceImpl<QualityReagentMapper, QualityReagent> implements IQualityReagentService {

    @Autowired
    private QualityReagentMapper reagentMapper;
    @Override
    public List<QualityReagent> getList(QualityReagentParam reagentParam) {
        String orgId = reagentParam.getOrgId();
        String produce = reagentParam.getProduce();
        Integer timeType = reagentParam.getTimeType();
        QueryWrapper<QualityReagent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.eq(StringUtils.isNotBlank(produce), "regent_name", produce);
        if (0 == timeType) {
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
        } else {
            queryWrapper.ge("create_time", DateUtil.beginOfYear(new Date()));
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-m')");
        }
        queryWrapper.orderByAsc("create_time");
        return reagentMapper.selectList(queryWrapper);
    }

}
