package com.hotwj.platform.securityManagement.violationInterview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotwj.platform.noticeManagerment.systemNotification.domain.bo.HotSystemNotificationGroupBo;
import com.hotwj.platform.noticeManagerment.systemNotification.service.IHotSystemNotificationService;
import com.hotwj.platform.securityManagement.violationInfo.domain.HotViolationInfo;
import com.hotwj.platform.securityManagement.violationInfo.mapper.HotViolationInfoMapper;
import com.hotwj.platform.securityManagement.violationInterview.domain.HotViolationInterview;
import com.hotwj.platform.securityManagement.violationInterview.domain.bo.HotViolationInterviewBo;
import com.hotwj.platform.securityManagement.violationInterview.domain.vo.HotViolationInterviewVo;
import com.hotwj.platform.securityManagement.violationInterview.mapper.HotViolationInterviewMapper;
import com.hotwj.platform.securityManagement.violationInterview.service.IHotViolationInterviewService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

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

/**
 * 违规约谈记录Service业务层处理
 *
 * @author shihongwei
 * @date 2025-12-17
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotViolationInterviewServiceImpl implements IHotViolationInterviewService {

    private final HotViolationInterviewMapper baseMapper;
    private final HotViolationInfoMapper violationInfoMapper;
    private final IHotSystemNotificationService notificationService;

    /**
     * 查询违规约谈记录
     *
     * @param id 主键
     * @return 违规约谈记录
     */
    @Override
    public HotViolationInterviewVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询违规约谈记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 违规约谈记录分页列表
     */
    @Override
    public TableDataInfo<HotViolationInterviewVo> queryPageList(HotViolationInterviewBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotViolationInterview> lqw = buildQueryWrapper(bo);
        Page<HotViolationInterviewVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的违规约谈记录列表
     *
     * @param bo 查询条件
     * @return 违规约谈记录列表
     */
    @Override
    public List<HotViolationInterviewVo> queryList(HotViolationInterviewBo bo) {
        LambdaQueryWrapper<HotViolationInterview> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotViolationInterview> buildQueryWrapper(HotViolationInterviewBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotViolationInterview> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotViolationInterview::getId);
        lqw.eq(bo.getViolationId() != null, HotViolationInterview::getViolationId, bo.getViolationId());
        lqw.eq(bo.getCompanyId() != null, HotViolationInterview::getCompanyId, bo.getCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getInterviewee()), HotViolationInterview::getInterviewee, bo.getInterviewee());
        lqw.eq(bo.getInterviewTime() != null, HotViolationInterview::getInterviewTime, bo.getInterviewTime());
        lqw.eq(StringUtils.isNotBlank(bo.getInterviewLocation()), HotViolationInterview::getInterviewLocation, bo.getInterviewLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getRecorder()), HotViolationInterview::getRecorder, bo.getRecorder());
        lqw.eq(StringUtils.isNotBlank(bo.getInterviewer()), HotViolationInterview::getInterviewer, bo.getInterviewer());
        lqw.eq(StringUtils.isNotBlank(bo.getInterviewReason()), HotViolationInterview::getInterviewReason, bo.getInterviewReason());
        lqw.eq(StringUtils.isNotBlank(bo.getConversationContent()), HotViolationInterview::getConversationContent, bo.getConversationContent());
        lqw.eq(StringUtils.isNotBlank(bo.getCommitmentLetter()), HotViolationInterview::getCommitmentLetter, bo.getCommitmentLetter());
        lqw.eq(StringUtils.isNotBlank(bo.getInterviewPhotos()), HotViolationInterview::getInterviewPhotos, bo.getInterviewPhotos());
        lqw.eq(StringUtils.isNotBlank(bo.getInterviewerSignature()), HotViolationInterview::getInterviewerSignature, bo.getInterviewerSignature());
        lqw.eq(StringUtils.isNotBlank(bo.getIntervieweeSignature()), HotViolationInterview::getIntervieweeSignature, bo.getIntervieweeSignature());
        lqw.eq(bo.getIsDeleted() != null, HotViolationInterview::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增违规约谈记录
     *
     * @param bo 违规约谈记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotViolationInterviewBo bo) {
        HotViolationInterview add = MapstructUtils.convert(bo, HotViolationInterview.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            if (add.getViolationId() != null) {
                HotViolationInfo violation = violationInfoMapper.selectById(add.getViolationId());
                String driverId = violation != null ? violation.getDriverId() : null;
                if (StringUtils.isNotBlank(driverId)) {
                    HotSystemNotificationGroupBo bos = new HotSystemNotificationGroupBo();
                    bos.setLevel("普通");
                    bos.setContent("您有一条约谈记录，请及时查看。");
                    bos.setSourceType("约谈");
                    bos.setSenderType("SYSTEM");
                    bos.setReceiverType("驾驶员");
                    bos.setReceiverIds(java.util.Collections.singletonList(driverId));
                    bos.setIsDeleted(0L);
                    try {
                        notificationService.insertByBo(bos);
                    } catch (Exception e) {
                        log.warn("发送约谈新增通知失败 violationId={} driverId={}", add.getViolationId(), driverId, e);
                    }
                }
            }
        }
        return flag;
    }

    /**
     * 修改违规约谈记录
     *
     * @param bo 违规约谈记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotViolationInterviewBo bo) {
        HotViolationInterview before = null;
        if (bo.getId() != null) {
            before = baseMapper.selectById(bo.getId());
        }
        HotViolationInterview update = MapstructUtils.convert(bo, HotViolationInterview.class);
        validEntityBeforeSave(update);
        boolean ok = baseMapper.updateById(update) > 0;
        if (ok) {
            Long violationId = update.getViolationId() != null ? update.getViolationId() : (before != null ? before.getViolationId() : null);
            if (violationId != null) {
                HotViolationInfo violation = violationInfoMapper.selectById(violationId);
                String driverId = violation != null ? violation.getDriverId() : null;
                if (org.dromara.common.core.utils.StringUtils.isNotBlank(driverId)) {
                    HotSystemNotificationGroupBo bos = new HotSystemNotificationGroupBo();
                    bos.setLevel("普通");
                    bos.setContent("您的约谈记录已更新，请及时查看。");
                    bos.setSourceType("约谈");
                    bos.setSenderType("SYSTEM");
                    bos.setReceiverType("驾驶员");
                    bos.setReceiverIds(java.util.Collections.singletonList(driverId));
                    bos.setIsDeleted(0L);
                    try {
                        notificationService.insertByBo(bos);
                    } catch (Exception e) {
                        log.warn("发送约谈修改通知失败 violationId={} driverId={}", violationId, driverId, e);
                    }
                }
            }
        }
        return ok;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotViolationInterview entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除违规约谈记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
