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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.quality.entity.QualityFenxibaogao;
import vip.xiaonuo.quality.entity.QualityManufacturer;
import vip.xiaonuo.quality.entity.QualityReagent;
import vip.xiaonuo.quality.mapper.QualityReagentMapper;
import vip.xiaonuo.quality.param.QualityManufacturerParam;
import vip.xiaonuo.quality.param.QualityReagentParam;
import vip.xiaonuo.quality.param.vo.ShiJiParam;
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), "reagent_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);
    }

    @Override
    public Page<QualityReagent> listPage(QualityManufacturerParam manufacturerParam) {
        String orgId = manufacturerParam.getOrgId();
        Page<QualityReagent> page = new Page<>(manufacturerParam.getPageNum(), manufacturerParam.getPageSize());
        QueryWrapper<QualityReagent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.orderByDesc("create_time");
        return reagentMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<QualityReagent> getShiJi(ShiJiParam shiJiParam) {
        QueryWrapper<QualityReagent> wrapper = new QueryWrapper<>();
        wrapper.select("reagent_name ");
        wrapper.eq(StringUtils.isNotBlank(shiJiParam.getOrgId()),"org_id",shiJiParam.getOrgId()).groupBy("reagent_name");
        List<QualityReagent> list = this.list(wrapper);
        return list;
    }


}
