package cn.iocoder.yudao.module.mes.dal.mysql.producereport;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mes.dal.dataobject.producereport.ViewReportUserDateSummary;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 报工工人日期汇总 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface ViewReportUserDateMapper extends BaseMapperX<ViewReportUserDateSummary> {

    default List<ViewReportUserDateSummary> selectBy(Long userId, String reportDay, String[]reportDays) {
        return selectList(new LambdaQueryWrapperX<ViewReportUserDateSummary>()
                .eqIfPresent(ViewReportUserDateSummary::getUserId, userId)
                .eqIfPresent(ViewReportUserDateSummary::getReportDay, reportDay)
                .betweenIfPresent(ViewReportUserDateSummary::getReportDay, reportDays)
                .orderByDesc(ViewReportUserDateSummary::getReportDay));
    }
    default ViewReportUserDateSummary selectSum(Long userId, String reportDay1,String reportDay2) {
        // SQL sum 查询
        List<Map<String, Object>> result = selectMaps(new QueryWrapper<ViewReportUserDateSummary>()
                .select("SUM(sumNumber) AS sumCount1, SUM(totalQualityNumber) AS sumCount2, SUM(totalWasteNumber) AS sumCount3")
                .eq(userId!=null, "user_id", userId)
                .between(reportDay1 != null && reportDay2!=null,"report_day", reportDay1, reportDay2));
        // 获得数量
        ViewReportUserDateSummary data = new ViewReportUserDateSummary().setUserId(userId).setSumNumber(BigDecimal.ZERO)
                .setTotalQualityNumber(BigDecimal.ZERO).setTotalWasteNumber(BigDecimal.ZERO);
        if (!CollUtil.isEmpty(result)) {
            BigDecimal sum = BigDecimal.valueOf(MapUtil.getLong(result.get(0), "sumCount1", 0L));
            data.setSumNumber(sum);
            BigDecimal sum2 = BigDecimal.valueOf(MapUtil.getLong(result.get(0), "sumCount2", 0L));
            data.setTotalQualityNumber(sum2);
            BigDecimal sum3 = BigDecimal.valueOf(MapUtil.getLong(result.get(0), "sumCount3", 0L));
            data.setTotalWasteNumber(sum3);
        }
        return data;
    }
}