package cn.iocoder.yudao.module.pms.dal.mysql.iotopeationfill;

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.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.pms.controller.admin.iotmodeltemplateattrs.vo.IotModelTemplateAttrsRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotopeationfill.vo.IotOpeationFillPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotopeationfill.vo.IotOpeationFillPageVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotopeationfill.vo.IotOpeationFillRespVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotopeationfill.vo.IotOpeationFillSaveReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.maintain.vo.IotMaintainPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.stat.vo.AllOrderResp;
import cn.iocoder.yudao.module.pms.controller.admin.vo.IotDeviceRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.IotDeviceDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotZHBD.DeviceZHBDDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotcountdata.IotCountDataDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicecountdata.IotDeviceCountData;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotdevicerunlog.IotDeviceRunLogDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmodeltemplateattrs.IotModelTemplateAttrsDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotopeationfill.IotOpeationFillDO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotopeationfill.IotOpeationFillOrderDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * 运行记录填报 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface IotOpeationFillMapper extends BaseMapperX<IotOpeationFillDO> {

    default PageResult<IotOpeationFillDO> selectPage(IotOpeationFillPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotOpeationFillDO>()
                .eqIfPresent(IotOpeationFillDO::getDeviceCode, reqVO.getDeviceCode())
                .likeIfPresent(IotOpeationFillDO::getDeviceName, reqVO.getDeviceName())
                .eqIfPresent(IotOpeationFillDO::getDeviceComponent, reqVO.getDeviceComponent())
                .eqIfPresent(IotOpeationFillDO::getDeptId, reqVO.getDeptId())
                .likeIfPresent(IotOpeationFillDO::getOrgName, reqVO.getOrgName())
                .eqIfPresent(IotOpeationFillDO::getProId, reqVO.getProId())
                .likeIfPresent(IotOpeationFillDO::getProName, reqVO.getProName())
                .eqIfPresent(IotOpeationFillDO::getTeamId, reqVO.getTeamId())
                .likeIfPresent(IotOpeationFillDO::getTeamName, reqVO.getTeamName())
                .likeIfPresent(IotOpeationFillDO::getDutyName, reqVO.getDutyName())
                .eqIfPresent(IotOpeationFillDO::getCreDate, reqVO.getCreDate())
                .betweenIfPresent(IotOpeationFillDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotOpeationFillDO::getId));
    }

    /**
     * 根据条件查询 运行记录 列表
     *
     * @param reqVO     查询条件
     * @return 返回符合条件的瑞恒日报记录
     */
    @TenantIgnore
    List<IotOpeationFillDO> selectList(@Param("reqVO") IotOpeationFillPageReqVO reqVO);

    /**
     * 根据条件查询日报记录
     *
     * @param deptId     施工队伍id
     * @param dateTime   当前时间
     * @return 返回符合条件的瑞恒日报记录
     */
    default IotOpeationFillDO selectExistReport(Long deptId, LocalDateTime dateTime) {
        String dateStr = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        // 使用LambdaQueryWrapperX构建查询条件，根据传入的参数动态添加查询条件
        return selectOne(new LambdaQueryWrapperX<IotOpeationFillDO>()
                .eqIfPresent(IotOpeationFillDO::getDeptId, deptId)
                .eq(IotOpeationFillDO::getIsReport, 1)
                .apply("DATE(create_time) = {0}", dateStr)
                .orderByDesc(IotOpeationFillDO::getId)
                .last("LIMIT 1"));
    }

    /**
     * 根据条件查询日报记录 指定部门当年的日报记录
     *
     * @param deptId     施工队伍id
     * @param dateTime   当前时间
     * @return 返回符合条件的瑞恒日报记录
     */
    default List<IotOpeationFillDO> currentYearReports(Long deptId, LocalDateTime dateTime) {
        int year = dateTime.getYear();  // 获取年份

        // 使用LambdaQueryWrapperX构建查询条件，根据传入的参数动态添加查询条件
        return selectList(new LambdaQueryWrapperX<IotOpeationFillDO>()
                .eqIfPresent(IotOpeationFillDO::getDeptId, deptId)
                .eq(IotOpeationFillDO::getIsReport, 1)
                .apply("YEAR(create_time) = {0}", year));
    }

    /**
     * 根据条件查询日报记录 指定部门当年的日报记录
     *
     * @param deptId     施工队伍id
     * @param year   当前时间 年
     * @return 返回符合条件的瑞恒日报记录
     */
    @TenantIgnore
    List<IotOpeationFillDO> selectReportsByYear(@Param("deptId")Long deptId, @Param("year")int year);

    @TenantIgnore
    IotOpeationFillDO selectRyFillReports(@Param("deptId")Long deptId, @Param("reportDateStr")String reportDateStr);

    @TenantIgnore
    IotOpeationFillDO selectRhFillReports(@Param("deptId")Long deptId, @Param("reportDateStr")String reportDateStr);

    @TenantIgnore
    List<IotOpeationFillOrderDO> selectListByCreateTime(IotOpeationFillPageReqVO reqVO);

    @TenantIgnore
    List<IotOpeationFillDO> countFills(IotOpeationFillPageReqVO reqVO);

    @TenantIgnore
    List<IotOpeationFillDO> getFillDevices(@Param("deviceIds")List<Long> deviceIds);



    @TenantIgnore
    int insertFill(List<IotOpeationFillDO> vo);

    /*int insertFillOrder(List<IotOpeationFillDO> vo);*/

    @TenantIgnore
    List<IotOpeationFillDO> fillList(IotOpeationFillPageReqVO vo);
    @TenantIgnore
    List<IotOpeationFillDO> fills(@Param("deviceIds")List<Long> deviceIds);
    @TenantIgnore
    int upFill(@Param("deptId")Long deptId,@Param("deviceIds")List<Long> deviceIds);
    @TenantIgnore
    int upFill1(IotOpeationFillSaveReqVO vo);
    @TenantIgnore
    int upFill2(IotOpeationFillSaveReqVO vo);

    @TenantIgnore
    List<IotOpeationFillDO> fillListByUserId(IotOpeationFillRespVO vo);

    @TenantIgnore
    IPage<IotOpeationFillDO> fillListPage(IPage<IotOpeationFillPageVO> page,@Param("orderId") Long orderId);

    @TenantIgnore
    List<IotOpeationFillDO> getOrderIds(IotOpeationFillRespVO vo);
    @TenantIgnore
    List<IotOpeationFillDO> fillListByDeptId(IotOpeationFillRespVO vo);
    @TenantIgnore
    IotOpeationFillDO devListByOrderId(IotOpeationFillRespVO vo);
    @TenantIgnore
    List<IotOpeationFillDO> rdList();
    @TenantIgnore
    List<IotOpeationFillDO> ryList();
    @TenantIgnore
    List<IotOpeationFillDO> rhList();
    @TenantIgnore
    List<IotOpeationFillDO> pdList1(IotOpeationFillDO vo);
    @TenantIgnore
    List<IotOpeationFillDO> flList(IotOpeationFillDO vo);

    @TenantIgnore
    List<IotOpeationFillDO> pdList(@Param("deviceIds")Collection<Long> deviceIds);

    @TenantIgnore
    List<IotOpeationFillDO> pdListRh(@Param("deviceIds")Collection<Long> deviceIds);

    @TenantIgnore
    List<IotOpeationFillDO> pdList2(@Param("deviceIds")Collection<Long> deviceIds);

    @TenantIgnore
    List<IotOpeationFillOrderDO> allOrder(@Param("localDateTime")LocalDateTime localDateTime,@Param("userNames")Collection<String> userNames);


    List<IotOpeationFillDO> deviceList(IotOpeationFillRespVO vo);
    @TenantIgnore
    PageResult<IotOpeationFillDO> fillList1(IotOpeationFillPageReqVO vo);

    @TenantIgnore
    int insertLog(List<IotDeviceRunLogDO> logDO);
    @TenantIgnore
    int insertLog1(IotDeviceRunLogDO logDO);
    @TenantIgnore
    int updateLog(IotDeviceRunLogDO logDO);
    @TenantIgnore
    int updateSumLog(IotDeviceRunLogDO logDO);

    @TenantIgnore
    int updateFill(IotOpeationFillSaveReqVO vo);

    // 批量查询已存在的日志
    @TenantIgnore
    List<IotDeviceRunLogDO> batchQueryExistingLogs(@Param("list") List<IotDeviceRunLogDO> logList);
    // 批量插入
    @TenantIgnore
    int batchInsertLogs(@Param("list") List<IotDeviceRunLogDO> logList);
    // 批量更新普通日志
    @TenantIgnore
    int batchUpdateLogs(@Param("list") List<IotDeviceRunLogDO> logList);
    // 批量更新累计日志
    @TenantIgnore
    int batchUpdateSumLogs(@Param("list") List<IotDeviceRunLogDO> logList);


    @TenantIgnore
    int batchUpdateFill(@Param("list") List<IotOpeationFillSaveReqVO> list);

    @TenantIgnore
    List<IotOpeationFillRespVO> getFillList(IotOpeationFillRespVO vo);
    @TenantIgnore
    List<IotDeviceRunLogDO> getDeivceFillInfo(IotDeviceRunLogDO vo);




    @TenantIgnore
    List<IotOpeationFillDO> devList(IotOpeationFillDO fillDO);

    @TenantIgnore
    List<IotOpeationFillDO> orderList(IotOpeationFillDO fillDO);

    @TenantIgnore
    List<IotOpeationFillDO> delList(IotOpeationFillDO fillDO);

    @TenantIgnore
    int delRepeat(IotOpeationFillDO fillDO);

    @TenantIgnore
    int delRepeatOrder(IotOpeationFillDO fillDO);


    @TenantIgnore
    int updateFillOrder(IotDeviceRunLogDO vo);
    @TenantIgnore
    int updateFillOrder1(IotDeviceRunLogDO vo);
    @TenantIgnore
    int updateFillOrder2(IotDeviceRunLogDO vo);
    @TenantIgnore
    IPage<IotOpeationFillOrderDO> fillRecords(IPage<IotOpeationFillPageReqVO> page, @Param("deviceId") Long deviceId, @Param("orderName") String orderName,@Param("orderStatus") Integer orderStatus);

    @TenantIgnore
    IPage<IotDeviceRespVO> getFillDevices1(IPage<IotOpeationFillPageReqVO> page,@Param("deviceIds") Collection<Long> deviceIds);

    @TenantIgnore
    List<IotDeviceRespVO> getFillDevices2(@Param("deptIds") Collection<Long> deptIds);

    @TenantIgnore
    List<DeviceZHBDDO> carList();

    @TenantIgnore
    int upLocation(DeviceZHBDDO zhbddo);
    @TenantIgnore
    IotOpeationFillDO getFillById(IotOpeationFillDO fillDO);
    @TenantIgnore
    List<IotCountDataDO> countList(IotCountDataDO dataDO);
    @TenantIgnore
    List<IotCountDataDO> countDeviceList(IotCountDataDO dataDO);
    @TenantIgnore
    IPage<IotDeviceCountData> deviceCountList(IPage<IotOpeationFillPageReqVO> page, @Param("deptId") Long deptId,
                                              @Param("createTime")LocalDateTime[] createTime,@Param("isFill") Integer isFill,@Param("orderName") String orderName);


    @TenantIgnore
    List<IotOpeationFillOrderDO> childList(IotOpeationFillOrderDO fillDO);

    @TenantIgnore
    IotDeviceDO devStatus(IotDeviceDO deviceDO) ;

    @TenantIgnore
    List<IotDeviceDO> selectDevStatusBatch(@Param("teams") List<DeptDO> teams) ;

    @TenantIgnore
    IotOpeationFillDO isReport(IotOpeationFillDO fillDO);
    // 1. 批量查询isReport状态
    @TenantIgnore
    List<IotOpeationFillDO> batchGetIsReport(List<IotOpeationFillDO> queries);

    @TenantIgnore
    IotOpeationFillDO orderDO(IotOpeationFillDO fillDO);
    // 2. 批量查询orderDO
    @TenantIgnore
    List<IotOpeationFillDO> batchGetOrderDO(List<IotOpeationFillDO> queries);

    @TenantIgnore
    IotOpeationFillDO orderDO1(IotOpeationFillDO fillDO);

    @TenantIgnore
    List<IotOpeationFillDO> reportList(IotOpeationFillDO fillDO);

    @TenantIgnore
    List<IotOpeationFillDO> reportList2(IotOpeationFillDO fillDO);


    @TenantIgnore
    List<IotModelTemplateAttrsDO> getAttrsById(IotModelTemplateAttrsRespVO vo);

    // 3. 批量查询属性模板
    @TenantIgnore
    List<IotModelTemplateAttrsDO> getAttrsByIdBatch(@Param("vos") List<IotModelTemplateAttrsRespVO> vos);


    @TenantIgnore
    IotDeviceRunLogDO getLogInfo(IotDeviceRunLogDO vo);
    // 5. 批量查询logInfo
    @TenantIgnore
    List<IotDeviceRunLogDO> batchGetLogInfo(List<IotDeviceRunLogDO> queries);

    @TenantIgnore
    IotDeviceRunLogDO getMaxFillInfo(IotDeviceRunLogDO vo);
    // 6. 批量查询maxFillInfo
    @TenantIgnore
    List<IotDeviceRunLogDO> batchGetMaxFillInfo(List<IotDeviceRunLogDO> queries);


    @TenantIgnore
    List<IotOpeationFillDO> reportList1(IotOpeationFillDO fillDO);

    @TenantIgnore
    IotDeviceRunLogDO reportData(IotDeviceRunLogDO runLogDO);
    // 4. 批量查询reportData
    @TenantIgnore
    List<IotDeviceRunLogDO> batchReportData(List<IotDeviceRunLogDO> queries);

    @TenantIgnore
    IotDeviceRunLogDO maxReportData(IotDeviceRunLogDO runLogDO);

    @TenantIgnore
    // 批量查询历史最大数据
    List<IotDeviceRunLogDO> batchQueryMaxReportData(@Param("list") List<IotDeviceRunLogDO> queryLogs);

    @TenantIgnore
    IotDeviceRunLogDO reportData1(IotDeviceRunLogDO runLogDO);

    @TenantIgnore
    IotDeviceRunLogDO getDesc(IotDeviceRunLogDO runLogDO);
    @TenantIgnore
    List<IotDeviceRunLogDO> batchGetDescByPointNames(@Param("pointNameSet") Set<String> pointNameSet);

    @TenantIgnore
    IotDeviceRunLogDO getTeamType(IotDeviceRunLogDO runLogDO);

    @TenantIgnore
    AdminUserDO getUserInfo(IotDeviceRunLogDO runLogDO);


    @TenantIgnore
    IotDeviceRunLogDO getUserId(IotDeviceRunLogDO runLogDO);

    @TenantIgnore
    List<AllOrderResp> selectStatusNumber(@Param("reqVO") IotMaintainPageReqVO reqVO);
    @TenantIgnore
    List<IotOpeationFillDO> getListByOrderId(@Param("orderId") Long orderId);
}
