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

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.FireCheckListStateEnum;
import com.zcloud.fire.check.domain.enums.MenuEnum;
import com.zcloud.fire.check.domain.model.CorpInfoE;
import com.zcloud.fire.check.domain.model.DateTimeE;
import com.zcloud.fire.check.domain.model.FireCheckListE;
import com.zcloud.fire.check.persistence.FireCheckPointListDO;
import com.zcloud.fire.check.persistence.dataobject.FireCheckListDO;
import com.zcloud.fire.check.persistence.dataobject.PointDistributionDO;
import com.zcloud.fire.check.persistence.dataobject.RiskDistributionDO;
import com.zcloud.fire.check.persistence.mapper.FireCheckListMapper;
import com.zcloud.fire.check.persistence.repository.CorpInfoRepository;
import com.zcloud.fire.check.persistence.repository.FireCheckListRepository;
import com.zcloud.gbscommon.utils.Query;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-11-28 10:32:09
 */
@Service
@RequiredArgsConstructor
public class FireCheckListRepositoryImpl extends BaseRepositoryImpl<FireCheckListMapper, FireCheckListDO> implements FireCheckListRepository {
    private final FireCheckListMapper fireCheckListMapper;
    private final CorpInfoRepository corpInfoRepository;

    @Override
    public PageResponse<FireCheckListDO> screenList(Map<String, Object> params) {
        IPage<FireCheckListDO> iPage = new Query<FireCheckListDO>().getPage(params);
        FireCheckListE fireCheckListE = new FireCheckListE();
        DateTimeE dateTimeE = fireCheckListE.getCheckRecordInterval(null);
        IPage<FireCheckListDO> result = fireCheckListMapper.screenList(iPage, params, dateTimeE);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public PageResponse<FireCheckListDO> listPage(Map<String, Object> params) {
        IPage<FireCheckListDO> iPage = new Query<FireCheckListDO>().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());
                }
            }
        }
        String menuPerms = "";
        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        IPage<FireCheckListDO> result = fireCheckListMapper.listPage(iPage, params,menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public FireCheckListDO getInfoById(Long id) {
        return fireCheckListMapper.getInfoById(id);
    }

    @Override
    public PageResponse<FireCheckListDO> appCheckList(Map<String, Object> params) {
        IPage<FireCheckListDO> iPage = new Query<FireCheckListDO>().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());
                }
            }
        }
        params.put("currentTime", new Date());
        FireCheckListE fireCheckListE = new FireCheckListE();
        DateTimeE dateTimeE = fireCheckListE.getCheckRecordInterval(null);
        IPage<FireCheckListDO> result = fireCheckListMapper.appCheckList(iPage, params,dateTimeE);

        return PageHelper.pageToResponse(result, result.getRecords());
    }

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

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

    @Override
    public List<FireCheckPointListDO> getCheckPointList(Map<String, Object> params) {
        return fireCheckListMapper.getCheckPointList( params);
    }

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



    @Override
    public PageResponse<FireCheckListDO> checkListRecordList(Map<String, Object> params) {
        IPage<FireCheckListDO> iPage = new Query<FireCheckListDO>().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());
                }*/
            }
        }
        FireCheckListE fireCheckListE = new FireCheckListE();
        DateTimeE dateTimeE = fireCheckListE.getCheckRecordInterval(null);
        String menuPerms = "";
        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        IPage<FireCheckListDO> result = fireCheckListMapper.checkListRecordList(iPage, params,dateTimeE,menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());
    }


    @Override
    public FireCheckListDO getInfoByUUIdState(String fireCheckId, Integer state) {
        QueryWrapper<FireCheckListDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_check_id", fireCheckId);
        queryWrapper.eq("state", state);
        return fireCheckListMapper.selectOne(queryWrapper);
    }

    @Override
    public PageResponse<FireCheckListDO> getCheckListRecord(Map<String, Object> params) {
        IPage<FireCheckListDO> iPage = new Query<FireCheckListDO>().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());
                }*/
            }
        }
        FireCheckListE fireCheckListE = new FireCheckListE();
        DateTimeE dateTimeE = fireCheckListE.getCheckRecordInterval(null);
        String menuPerms = "";
        if (!org.springframework.util.ObjectUtils.isEmpty(params.get("menuPath"))){
            menuPerms = MenuEnum.getMenuKeyByPath(params.get("menuPath").toString());
        }
        IPage<FireCheckListDO> result = fireCheckListMapper.getCheckListRecord(iPage, params,dateTimeE,menuPerms);
        return PageHelper.pageToResponse(result, result.getRecords());

    }


    @Override
    public List<FireCheckListDO> waitFireCheckList(String checkType) {
        QueryWrapper<FireCheckListDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_check_type", checkType);
        queryWrapper.eq("state", FireCheckListStateEnum.ENABLE.getCode());
        List<FireCheckListDO> result = fireCheckListMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public List<FireCheckListDO> overTimeRecord(Map<String, Object> params) {
        List<FireCheckListDO> result = fireCheckListMapper.overTimeRecord(params);
        return result;
    }

    @Override
    public FireCheckListDO getInfoByUUId(String fireCheckId) {
        QueryWrapper<FireCheckListDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_check_id", fireCheckId);
        return fireCheckListMapper.selectOne(queryWrapper);
    }
}

