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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jjb.saas.framework.repository.common.PageHelper;
import com.jjb.saas.framework.repository.repo.impl.BaseRepositoryImpl;
import com.zcloud.fire.check.domain.enums.HiddenCheckTypeEnum;
import com.zcloud.fire.check.domain.model.FireCheckRecordHiddenE;
import com.zcloud.fire.check.persistence.dataobject.FireCheckRecordHiddenDO;
import com.zcloud.fire.check.persistence.mapper.FireCheckRecordHiddenMapper;
import com.zcloud.fire.check.persistence.repository.FireCheckRecordHiddenRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import com.zcloud.gbscommon.utils.Query;
import com.zcloud.gbscommon.zcloudhidden.face.ZcloudHiddenFacade;
import com.zcloud.gbscommon.zcloudhidden.request.HiddenAddCmd;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

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

/**
 * web-infrastructure
 *
 * @Author zhaokai
 * @Date 2025-12-12 11:11:16
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class FireCheckRecordHiddenRepositoryImpl extends BaseRepositoryImpl<FireCheckRecordHiddenMapper, FireCheckRecordHiddenDO> implements FireCheckRecordHiddenRepository {
    private final FireCheckRecordHiddenMapper fireCheckRecordHiddenMapper;
    @DubboReference
    private ZcloudHiddenFacade zcloudHiddenFacade;
    @Override
    public PageResponse<FireCheckRecordHiddenDO> listPage(Map<String, Object> params) {
        IPage<FireCheckRecordHiddenDO> iPage = new Query<FireCheckRecordHiddenDO>().getPage(params);
        QueryWrapper<FireCheckRecordHiddenDO> queryWrapper = new QueryWrapper<>();
        queryWrapper = PageQueryHelper.createPageQueryWrapper(queryWrapper, params);
        queryWrapper.orderByDesc("create_time");
        IPage<FireCheckRecordHiddenDO> result = fireCheckRecordHiddenMapper.selectPage(iPage, queryWrapper);
        return PageHelper.pageToResponse(result, result.getRecords());
    }

    @Override
    public List<FireCheckRecordHiddenDO> getChecKRecordDeviceList(List<String> fireRecordDeviceIdList) {
        QueryWrapper<FireCheckRecordHiddenDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("fire_record_device_id", fireRecordDeviceIdList);
        queryWrapper.eq("hidden_check_type", HiddenCheckTypeEnum.CHECK_ITEM_HIDDEN.getCode());
        return fireCheckRecordHiddenMapper.selectList(queryWrapper);
    }

    @Override
    public List<FireCheckRecordHiddenDO> getChecKRecordHiddenList(String fireRecordId,Integer hiddenCheckType) {
        QueryWrapper<FireCheckRecordHiddenDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_record_id", fireRecordId);
        if(hiddenCheckType != null){
            queryWrapper.eq("hidden_check_type", hiddenCheckType);
        }
        return fireCheckRecordHiddenMapper.selectList(queryWrapper);
    }

    @Override
    public List<FireCheckRecordHiddenDO> getChecKRecordHiddenListByRecordIdList(List<String> fireRecordIdList, Integer hiddenCheckType) {
        QueryWrapper<FireCheckRecordHiddenDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("fire_record_id", fireRecordIdList);
        if(hiddenCheckType != null){
            queryWrapper.eq("hidden_check_type", hiddenCheckType);
        }
        return fireCheckRecordHiddenMapper.selectList(queryWrapper);
    }

    @Override
    public FireCheckRecordHiddenDO listByFireCheckRecordDeviceId(String fireRecordDeviceId) {
        QueryWrapper<FireCheckRecordHiddenDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_record_device_id", fireRecordDeviceId);
        queryWrapper.eq("hidden_check_type", HiddenCheckTypeEnum.CHECK_ITEM_HIDDEN.getCode());
        return fireCheckRecordHiddenMapper.selectOne(queryWrapper);
    }

    @Override
    public void add(FireCheckRecordHiddenDO fireCheckRecordHiddenDO, HiddenAddCmd hiddenAddCmd) {
        save(fireCheckRecordHiddenDO);
        // 调用dubbo接口新增隐患信息
        try {
            log.info("新增隐患记录，checkrecord:{},参数：{}", fireCheckRecordHiddenDO.getFireRecordId(), JSONUtil.toJsonStr(hiddenAddCmd));
            SingleResponse singleResponse = zcloudHiddenFacade.addHidden(hiddenAddCmd);
            log.info("新增隐患记录，checkrecord:{},隐患id：{}，返回结果：{}", fireCheckRecordHiddenDO.getFireRecordId(), hiddenAddCmd.getHiddenId(), JSONUtil.toJsonStr(singleResponse));
        } catch (Exception e) {
            log.error("新增隐患记录，checkrecord:{},隐患id：{}，返回结果：{}", fireCheckRecordHiddenDO.getFireRecordId(), JSONUtil.toJsonStr(hiddenAddCmd),  ExceptionUtils.getStackTrace(e));
            throw new BizException("新增隐患信息失败:{}",e.getMessage());
        }
    }

    @Override
    public void updateHidden(FireCheckRecordHiddenDO fireCheckRecordHiddenDO, HiddenAddCmd hiddenAddCmd) {
        updateById(fireCheckRecordHiddenDO);
        // 调用dubbo接口更新隐患信息
        try {
            log.info("更新隐患记录，checkrecord:{},参数：{}", fireCheckRecordHiddenDO.getFireRecordId(), JSONUtil.toJsonStr(hiddenAddCmd));
            SingleResponse singleResponse = zcloudHiddenFacade.updateHidden(hiddenAddCmd);
        }catch (Exception e){
            log.error("修改隐患记录，checkrecord:{},隐患id：{}，返回结果：{}", fireCheckRecordHiddenDO.getFireRecordId(), JSONUtil.toJsonStr(hiddenAddCmd),  ExceptionUtils.getStackTrace(e));
            throw new BizException("修改隐患信息失败:{}",e.getMessage());
        }
    }

    @Override
    public void deletedFireCheckRecordHiddenById(FireCheckRecordHiddenDO fireCheckRecordHiddenDO) {
        removeById(fireCheckRecordHiddenDO);

        HiddenAddCmd hiddenDeleteCmd = new HiddenAddCmd();
        hiddenDeleteCmd.setHiddenId(fireCheckRecordHiddenDO.getHiddenId());
        try {
            log.info("删除隐患记录，checkrecord:{},参数：{}", fireCheckRecordHiddenDO.getFireRecordId(), JSONUtil.toJsonStr(hiddenDeleteCmd));
            SingleResponse singleResponse = zcloudHiddenFacade.deleteHidden(hiddenDeleteCmd);
            log.info("删除隐患记录，checkrecord:{},隐患id：{}，返回结果：{}", fireCheckRecordHiddenDO.getFireRecordId(), fireCheckRecordHiddenDO.getHiddenId(), JSONUtil.toJsonStr(singleResponse));
        } catch (Exception e) {
            log.error("删除隐患记录，checkrecord:{},隐患id：{}，返回结果：{}", fireCheckRecordHiddenDO.getFireRecordId(), JSONUtil.toJsonStr(hiddenDeleteCmd),  ExceptionUtils.getStackTrace(e));
            throw new BizException("删除隐患信息失败:{}",e.getMessage());
        }
    }

    @Override
    public void updateHiddenStatus(String fireRecordId, Boolean hiddenStatus) {
        QueryWrapper<FireCheckRecordHiddenDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_record_id", fireRecordId);
        List<FireCheckRecordHiddenDO> fireCheckRecordHiddenDOList = fireCheckRecordHiddenMapper.selectList(queryWrapper);
        if(CollUtil.isNotEmpty(fireCheckRecordHiddenDOList) || hiddenStatus){
            HiddenAddCmd updateHiddenStatusCmd = new HiddenAddCmd();
            updateHiddenStatusCmd.setForeignKey(fireRecordId);
            updateHiddenStatusCmd.setState(100);
            log.info("更新隐患状态，请求参数：{}", JSONUtil.toJsonStr(updateHiddenStatusCmd));
            SingleResponse singleResponse = zcloudHiddenFacade.updateHiddenStatus(updateHiddenStatusCmd);
            log.info("更新隐患状态，返回结果：{}", JSONUtil.toJsonStr(singleResponse));
        }
    }

    @Override
    public List<FireCheckRecordHiddenDO> getHiddenListByFireCheckId(String fireCheckId, Integer code) {
        QueryWrapper<FireCheckRecordHiddenDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_check_id", fireCheckId);
        queryWrapper.eq("hidden_check_type", code);
        return fireCheckRecordHiddenMapper.selectList(queryWrapper);
    }

    @Override
    public List<FireCheckRecordHiddenDO> getHiddenList(String fireRecordId) {
        QueryWrapper<FireCheckRecordHiddenDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_record_id", fireRecordId);
        return fireCheckRecordHiddenMapper.selectList(queryWrapper);
    }

    @Override
    public List<FireCheckRecordHiddenDO> getByOtherCheckRecordId(String fireRecordId) {
        QueryWrapper<FireCheckRecordHiddenDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fire_record_id", fireRecordId);
        queryWrapper.eq("hidden_check_type", HiddenCheckTypeEnum.OTHER_HIDDEN.getCode());
        return fireCheckRecordHiddenMapper.selectList(queryWrapper);
    }
}

