package com.hotwj.platform.driverManagement.driverAnnualAssessment.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.driverManagement.driverAnnualAssessment.domain.HotDriverAnnualAssessment;
import com.hotwj.platform.driverManagement.driverAnnualAssessment.domain.bo.HotDriverAnnualAssessmentBo;
import com.hotwj.platform.driverManagement.driverAnnualAssessment.domain.vo.HotDriverAnnualAssessmentVo;
import com.hotwj.platform.driverManagement.driverAnnualAssessment.mapper.HotDriverAnnualAssessmentMapper;
import com.hotwj.platform.driverManagement.driverAnnualAssessment.service.IHotDriverAnnualAssessmentService;
import com.hotwj.platform.noticeManagerment.systemNotification.domain.bo.HotSystemNotificationGroupBo;
import com.hotwj.platform.noticeManagerment.systemNotification.service.IHotSystemNotificationService;
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-16
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotDriverAnnualAssessmentServiceImpl implements IHotDriverAnnualAssessmentService {

    private final HotDriverAnnualAssessmentMapper baseMapper;
    private final IHotSystemNotificationService notificationService;

    /**
     * 查询驾驶员年度考核
     *
     * @param id 主键
     * @return 驾驶员年度考核
     */
    @Override
    public HotDriverAnnualAssessmentVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询驾驶员年度考核列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 驾驶员年度考核分页列表
     */
    @Override
    public TableDataInfo<HotDriverAnnualAssessmentVo> queryPageList(HotDriverAnnualAssessmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotDriverAnnualAssessment> lqw = buildQueryWrapper(bo);
        Page<HotDriverAnnualAssessmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的驾驶员年度考核列表
     *
     * @param bo 查询条件
     * @return 驾驶员年度考核列表
     */
    @Override
    public List<HotDriverAnnualAssessmentVo> queryList(HotDriverAnnualAssessmentBo bo) {
        LambdaQueryWrapper<HotDriverAnnualAssessment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotDriverAnnualAssessment> buildQueryWrapper(HotDriverAnnualAssessmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotDriverAnnualAssessment> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(HotDriverAnnualAssessment::getAssessmentTime);
        lqw.eq(bo.getCompanyId() != null, HotDriverAnnualAssessment::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getDriverId() != null, HotDriverAnnualAssessment::getDriverId, bo.getDriverId());
        lqw.eq(StringUtils.isNotBlank(bo.getAssessmentContent()), HotDriverAnnualAssessment::getAssessmentContent, bo.getAssessmentContent());
        lqw.eq(StringUtils.isNotBlank(bo.getAssessmentResult()), HotDriverAnnualAssessment::getAssessmentResult, bo.getAssessmentResult());
        lqw.eq(bo.getAssessmentTime() != null, HotDriverAnnualAssessment::getAssessmentTime, bo.getAssessmentTime());
        lqw.eq(bo.getEvaluatorSignatureId() != null, HotDriverAnnualAssessment::getEvaluatorSignatureId, bo.getEvaluatorSignatureId());
        lqw.eq(bo.getAssessedSignatureId() != null, HotDriverAnnualAssessment::getAssessedSignatureId, bo.getAssessedSignatureId());
        lqw.eq(bo.getIsDeleted() != null, HotDriverAnnualAssessment::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增驾驶员年度考核
     *
     * @param bo 驾驶员年度考核
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotDriverAnnualAssessmentBo bo) {
        HotDriverAnnualAssessment add = MapstructUtils.convert(bo, HotDriverAnnualAssessment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
            if (StringUtils.isNotBlank(add.getDriverId())) {
                String result = StringUtils.blankToDefault(add.getAssessmentResult(), "未知");
                HotSystemNotificationGroupBo bos = new HotSystemNotificationGroupBo();
                bos.setLevel("普通");
                bos.setContent("您的年度考核结果为" + result + "。");
                bos.setSourceType("驾驶员管理");
                bos.setSenderType("SYSTEM");
                bos.setReceiverType("驾驶员");
                bos.setReceiverIds(java.util.Collections.singletonList(add.getDriverId()));
                bos.setIsDeleted(0L);
                try {
                    notificationService.insertByBo(bos);
                } catch (Exception e) {
                    log.warn("发送年度考核结果通知失败 driverId={} companyId={}", add.getDriverId(), add.getCompanyId(), e);
                }
            }
        }
        return flag;
    }

    /**
     * 修改驾驶员年度考核
     *
     * @param bo 驾驶员年度考核
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotDriverAnnualAssessmentBo bo) {
        HotDriverAnnualAssessment before = null;
        if (bo.getId() != null) {
            before = baseMapper.selectById(bo.getId());
        }
        HotDriverAnnualAssessment update = MapstructUtils.convert(bo, HotDriverAnnualAssessment.class);
        validEntityBeforeSave(update);
        boolean ok = baseMapper.updateById(update) > 0;
        if (ok) {
            String driverId = org.dromara.common.core.utils.StringUtils.isNotBlank(update.getDriverId()) ? update.getDriverId()
                    : (before != null ? before.getDriverId() : null);
            if (org.dromara.common.core.utils.StringUtils.isNotBlank(driverId)) {
                String result = org.dromara.common.core.utils.StringUtils.blankToDefault(
                        update.getAssessmentResult() != null ? update.getAssessmentResult() : (before != null ? before.getAssessmentResult() : null),
                        "未知"
                );
                HotSystemNotificationGroupBo bos = new HotSystemNotificationGroupBo();
                bos.setLevel("普通");
                bos.setContent("您的年度考核结果已更新为" + result + "。");
                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("发送年度考核更新通知失败 driverId={} companyId={}", driverId, update.getCompanyId(), e);
                }
            }
        }
        return ok;
    }

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

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