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 org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.production.entity.ProductionSecond;
import vip.xiaonuo.production.mapper.ProductionEnergyMapper;
import vip.xiaonuo.production.mapper.ProductionSecondMapper;
import vip.xiaonuo.production.param.ProductionSecondParam;
import vip.xiaonuo.production.service.IProductionSecondService;
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 ProductionSecondServiceImpl extends ServiceImpl<ProductionSecondMapper, ProductionSecond> implements IProductionSecondService {

    @Autowired
    private ProductionSecondMapper secondMapper;

    @Override
    public ProductionSecond getSecond(ProductionSecondParam secondParam) {
        String locationName = secondParam.getLocationName();
        String orgId = secondParam.getOrgId();
        Integer time = secondParam.getTime();
        QueryWrapper<ProductionSecond> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId)
                .eq(StringUtils.isNotBlank(locationName), "location_name", locationName);
        queryWrapper.select(" IFNULL(sum(month_standard),'0') monthStandard," +
                "IFNULL(sum(month_actual),'0') month_actual," +
                "IFNULL(sum(exceed),'0') exceed," +
                "convert(IFNULL(avg(qualified_rate),'0'),decimal(10,2)) qualified_rate," +
                "IFNULL(sum(unqualified_batch),'0') unqualifiedBatch");
        if (null == time) {
            queryWrapper.ge("create_time", DateUtil.beginOfMonth(new Date()));
        } else {
            queryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        }
        return secondMapper.selectOne(queryWrapper);
    }
}
