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.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.mes.controller.admin.producereport.vo.ProduceReportDetailPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.producereport.vo.ReportStatusEnum;
import cn.iocoder.yudao.module.mes.dal.dataobject.producereport.ProduceReportDetailDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
import java.util.Map;

/**
 * 生产报工明细 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface ProduceReportDetailMapper extends BaseMapperX<ProduceReportDetailDO> {

    default PageResult<ProduceReportDetailDO> selectPage(ProduceReportDetailPageReqVO reqVO) {
        List<Integer> status =null;
        if(reqVO.getReportStatus()!=null && reqVO.getReportStatus() == 100){
            status = ReportStatusEnum.getEnableEditStatus();
            reqVO.setReportStatus(null);
        }
        return selectPage(reqVO, new LambdaQueryWrapperX<ProduceReportDetailDO>()
                .eqIfPresent(ProduceReportDetailDO::getReportId, reqVO.getReportId())
                .eqIfPresent(ProduceReportDetailDO::getPlanId, reqVO.getPlanId())
                .eqIfPresent(ProduceReportDetailDO::getProductId, reqVO.getProductId())
                .eqIfPresent(ProduceReportDetailDO::getUserId, reqVO.getUserId())
                .eqIfPresent(ProduceReportDetailDO::getOrgId, reqVO.getOrgId())
                .eqIfPresent(ProduceReportDetailDO::getQualityRate, reqVO.getQualityRate())
                .eqIfPresent(ProduceReportDetailDO::getWasteReason, reqVO.getWasteReason())
                .eqIfPresent(ProduceReportDetailDO::getRemark, reqVO.getRemark())
                .eqIfPresent(ProduceReportDetailDO::getIsEnable, reqVO.getIsEnable())

                .eqIfPresent(ProduceReportDetailDO::getReportType, reqVO.getReportType())
                .eqIfPresent(ProduceReportDetailDO::getOrgType, reqVO.getOrgType())
                .eqIfPresent(ProduceReportDetailDO::getGroupType, reqVO.getGroupType())

                .betweenIfPresent(ProduceReportDetailDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(ProduceReportDetailDO::getQualityNumber, reqVO.getQualityNumber())
                .eqIfPresent(ProduceReportDetailDO::getWasteNumber, reqVO.getWasteNumber())
                .eqIfPresent(ProduceReportDetailDO::getTotalNumber, reqVO.getTotalNumber())
                .betweenIfPresent(ProduceReportDetailDO::getReportDate, reqVO.getReportDate())
                .eqIfPresent(ProduceReportDetailDO::getReportStatus, reqVO.getReportStatus())
                .inIfPresent(ProduceReportDetailDO::getReportStatus, status)
                .orderByDesc(ProduceReportDetailDO::getId));
    }

    default PageResult<ProduceReportDetailDO> selectPage(PageParam reqVO, Long reportId) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ProduceReportDetailDO>()
                .eq(ProduceReportDetailDO::getReportId, reportId)
                .orderByDesc(ProduceReportDetailDO::getId));
    }
    default List<ProduceReportDetailDO> selectListByReportId(Long reportId) {
        return selectList(ProduceReportDetailDO::getReportId, reportId);
    }

    default int deleteByReportId(Long reportId) {
        return delete(ProduceReportDetailDO::getReportId, reportId);
    }

    default Long sumByPlanAndOrgType(Long planId,String orgType) {
        // SQL sum 查询
        List<Map<String, Object>> result = selectMaps(new QueryWrapper<ProduceReportDetailDO>()
                .select("SUM(quality_number) AS quality_number,SUM(waste_number) AS waste_number, SUM(total_number) AS total_number ")
                .eq(planId != null, "plan_id", planId)
                .eq(StringUtils.isNotEmpty(orgType), "org_type", orgType));
        // 获得数量
        if (CollUtil.isEmpty(result)) {
            return 0L;
        }
        return MapUtil.getLong(result.get(0), "quality_number", 0L);
    }
}