package cn.iocoder.yudao.module.identity.dal.mysql.scanrecord;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.identity.dal.dataobject.scanrecord.ScanRecordDO;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.identity.controller.admin.scanrecord.vo.*;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 扫码记录 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface ScanRecordMapper extends BaseMapperX<ScanRecordDO> {

    default PageResult<ScanRecordDO> selectPage(ScanRecordPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ScanRecordDO>()
                .eqIfPresent(ScanRecordDO::getGenerateRecordId, reqVO.getGenerateRecordId())
                .eqIfPresent(ScanRecordDO::getIdentityUrl, reqVO.getIdentityUrl())
                .eqIfPresent(ScanRecordDO::getBatchId, reqVO.getBatchId())
                .eqIfPresent(ScanRecordDO::getGoodsId, reqVO.getGoodsId())
                .likeIfPresent(ScanRecordDO::getGoodsName, reqVO.getGoodsName())
                .eqIfPresent(ScanRecordDO::getScanIp, reqVO.getScanIp())
                .eqIfPresent(ScanRecordDO::getBatchType, reqVO.getBatchType())
                .eqIfPresent(ScanRecordDO::getScanArea, reqVO.getScanArea())
                .betweenIfPresent(ScanRecordDO::getScanTime, reqVO.getScanTime())
                .eqIfPresent(ScanRecordDO::getScanLocation, reqVO.getScanLocation())
                .eqIfPresent(ScanRecordDO::getBelongBusinessId, reqVO.getBelongBusinessId())
                .likeIfPresent(ScanRecordDO::getBelongBusinessName, reqVO.getBelongBusinessName())
                .orderByDesc(ScanRecordDO::getId));
    }

    default List<ScanRecordDO> selectList(ScanRecordExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<ScanRecordDO>()
                .eqIfPresent(ScanRecordDO::getGenerateRecordId, reqVO.getGenerateRecordId())
                .eqIfPresent(ScanRecordDO::getIdentityUrl, reqVO.getIdentityUrl())
                .eqIfPresent(ScanRecordDO::getBatchId, reqVO.getBatchId())
                .eqIfPresent(ScanRecordDO::getGoodsId, reqVO.getGoodsId())
                .likeIfPresent(ScanRecordDO::getGoodsName, reqVO.getGoodsName())
                .eqIfPresent(ScanRecordDO::getScanIp, reqVO.getScanIp())
                .eqIfPresent(ScanRecordDO::getBatchType, reqVO.getBatchType())
                .eqIfPresent(ScanRecordDO::getScanArea, reqVO.getScanArea())
                .betweenIfPresent(ScanRecordDO::getScanTime, reqVO.getScanTime())
                .eqIfPresent(ScanRecordDO::getScanLocation, reqVO.getScanLocation())
                .eqIfPresent(ScanRecordDO::getBelongBusinessId, reqVO.getBelongBusinessId())
                .likeIfPresent(ScanRecordDO::getBelongBusinessName, reqVO.getBelongBusinessName())
                .orderByDesc(ScanRecordDO::getId));
    }

    @Select("SELECT COUNT(*) AS scan_count FROM identity_scan_record WHERE belong_business_id = #{belongBusinessId} AND goods_id = #{goodsId} AND scan_time > #{staterTime} AND scan_time < #{endTime} GROUP BY scan_time")
    List<Map<String, Object>> selectCountByGroup(@Param(value = "belongBusinessId") Long belongBusinessId,
                                                 @Param(value = "goodsId") Long goodsId,
                                                 @Param(value = "staterTime") Date staterTime,//这两个参数传过来的时候要记得转成date类型的
                                                 @Param(value = "endTime") Date endTime);//这两个参数传过来的时候要记得转成date类型的，
}
