package vip.xiaonuo.production.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.production.entity.ProductionConsume;
import vip.xiaonuo.production.mapper.ProductionConsumeMapper;
import vip.xiaonuo.production.param.ProductionConsumeParam;
import vip.xiaonuo.production.service.IProductionConsumeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 生产管控单耗 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-12
 */
@Service
public class ProductionConsumeServiceImpl extends ServiceImpl<ProductionConsumeMapper, ProductionConsume> implements IProductionConsumeService {

    @Autowired
    private ProductionConsumeMapper consumeMapper;

    @Override
    public Page<ProductionConsume> getList(ProductionConsumeParam consumeParam) {
        String orgId = consumeParam.getOrgId();
        String locationName = consumeParam.getLocationName();
        String produceName = consumeParam.getProduceName();
        String time = consumeParam.getTime();
        QueryWrapper<ProductionConsume> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId)
                .eq(StringUtils.isNotBlank(locationName), "location_name", locationName)
                .eq(StringUtils.isNotBlank(produceName), "material", produceName);
        Page<ProductionConsume> page = new Page(consumeParam.getPageNum(), consumeParam.getPageSize());
        if (null == time) {
            queryWrapper.ge("create_time", DateUtil.beginOfYear(new Date()));
        } else {
            queryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        }
        queryWrapper.orderByDesc("create_time");
        return consumeMapper.selectPage(page, queryWrapper);
    }
}
