package vip.xiaonuo.quality.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.quality.entity.QualityReagent;
import vip.xiaonuo.quality.entity.QualityReagentOutStock;
import vip.xiaonuo.quality.mapper.QualityReagentMapper;
import vip.xiaonuo.quality.mapper.QualityReagentOutStockMapper;
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.Calendar;
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;
    @Autowired
    private QualityReagentOutStockMapper outStockMapper;

    @Override
    public List<QualityReagentOutStock> getList(QualityReagentParam reagentParam) {
        String orgId = reagentParam.getOrgId();
        String produce = reagentParam.getProduce();
        Integer timeType = reagentParam.getTimeType();
        Integer number = reagentParam.getNumber();
        QueryWrapper<QualityReagentOutStock> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.like(StringUtils.isNotBlank(produce), "reagent", produce);
        if (0 == timeType) {
            Calendar calendar = Calendar.getInstance();
            if (null == number) {
                calendar.add(Calendar.YEAR, -3);
            } else {
                calendar.add(Calendar.YEAR, -number);
            }
            queryWrapper.between("create_time", calendar.getTime(), new Date());
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
        } else {
            Calendar calendar = Calendar.getInstance();
            if (null == number) {
                calendar.add(Calendar.MONTH, -13);
            } else {
                calendar.add(Calendar.MONTH, -number);
            }
            queryWrapper.between("create_time", calendar.getTime(), new Date());
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-m')");
        }
        queryWrapper.orderByAsc("create_time");
        return outStockMapper.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;
    }

}
