package com.zcloud.fire.check.persistence.repository.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.dto.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.zcloud.fire.check.domain.enums.CommonFlagEnum;
import com.zcloud.fire.check.persistence.dataobject.DataStatisticDO;
import com.zcloud.fire.check.persistence.dataobject.FireCheckRecordDO;
import com.zcloud.fire.check.persistence.dataobject.FireCheckRecordStatisticDO;
import com.zcloud.fire.check.persistence.mapper.FireCheckRecordMapper;
import com.zcloud.fire.check.persistence.repository.CorpInfoRepository;
import com.zcloud.fire.check.persistence.repository.FireCheckRecordRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-12-12 11:10:59
 */
@Service
@RequiredArgsConstructor
public class FireCheckRecordRepositoryImpl extends BaseRepositoryImpl<FireCheckRecordMapper, FireCheckRecordDO> implements FireCheckRecordRepository {
    private final FireCheckRecordMapper fireCheckRecordMapper;
    private final CorpInfoRepository corpInfoRepository;
    @Override
    public PageResponse<FireCheckRecordDO> listPage(Map<String, Object> params) {
        IPage<FireCheckRecordDO> iPage = new Query<FireCheckRecordDO>().getPage(params);
        QueryWrapper<FireCheckRecordDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params);
        queryWrapper.orderByDesc("create_time");
        IPage<FireCheckRecordDO> result = fireCheckRecordMapper.selectPage(iPage, queryWrapper);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public FireCheckRecordDO getNoFinishedInfoId(String fireCheckId) {
        QueryWrapper<FireCheckRecordDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_check_id", fireCheckId);
        queryWrapper.eq("finished_flag", CommonFlagEnum.NO.getCode());
        queryWrapper.le("period_start", LocalDate.now().toString());
        queryWrapper.ge("period_end", LocalDate.now().toString());

        queryWrapper.orderByDesc("create_time");
        List<FireCheckRecordDO> fireCheckRecordDOList = fireCheckRecordMapper.selectList(queryWrapper);
        if(CollUtil.isNotEmpty(fireCheckRecordDOList)){
            return fireCheckRecordDOList.get(0);
        }
        return null;
    }

    @Override
    public PageResponse<FireCheckRecordDO> getCheckRecordList(Map<String, Object> params) {
        IPage<FireCheckRecordDO> iPage = new Query<FireCheckRecordDO>().getPage(params);
        if (params.get("corpinfoId") != null && StringUtils.isNotEmpty(params.get("corpinfoId").toString())) {
            params.put("corpinfoId", params.get("corpinfoId"));
        } else {
            if(!corpInfoRepository.isSupper()){
                params.put("corpinfoId", AuthContext.getTenantId());
               /* if(!CorpInfoE.isCorp()){
                    //是个人，只能查询自己的清单
                    params.put("userId", AuthContext.getUserId());
                }*/
            }
        }
        IPage<FireCheckRecordDO> result = fireCheckRecordMapper.getCheckRecordList(iPage, params);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public FireCheckRecordDO getCheckRecordInfo(String fireRecordId) {
        return fireCheckRecordMapper.getCheckRecordInfo(fireRecordId);
    }

    @Override
    public List<FireCheckRecordStatisticDO> statisticListByFireCheckIdList(List<String> fireCheckIdList) {
       return fireCheckRecordMapper.statisticListByFireCheckIdList(fireCheckIdList);
    }

    @Override
    public List<DataStatisticDO> DataStatisticDO(List<Long> corpinfoIds) {
        return fireCheckRecordMapper.DataStatisticDO(corpinfoIds);
    }

    @Override
    public PageResponse<FireCheckRecordDO> getCheckRecordPageH5(Map<String, Object> params) {
        IPage<FireCheckRecordDO> iPage = new Query<FireCheckRecordDO>().getPage(params);
        IPage<FireCheckRecordDO> result = fireCheckRecordMapper.getCheckRecordPageH5(iPage, params);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public PageResponse<FireCheckRecordDO> getCheckRecordPageH5New(Map<String, Object> params) {
        IPage<FireCheckRecordDO> iPage = new Query<FireCheckRecordDO>().getPage(params);
        IPage<FireCheckRecordDO> result = fireCheckRecordMapper.getCheckRecordPageH5New(iPage, params);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public FireCheckRecordDO getCheckRecordInfoByFireRecordId(String fireRecordId) {
      QueryWrapper<FireCheckRecordDO> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("fire_record_id", fireRecordId);
      return fireCheckRecordMapper.selectOne(queryWrapper);
    }


}

