package com.hotwj.platform.securityManagement.serviceQualityComplaint.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.securityManagement.serviceQualityComplaint.domain.HotServiceQualityComplaint;
import com.hotwj.platform.securityManagement.serviceQualityComplaint.domain.bo.HotServiceQualityComplaintBo;
import com.hotwj.platform.securityManagement.serviceQualityComplaint.domain.vo.HotServiceQualityComplaintVo;
import com.hotwj.platform.securityManagement.serviceQualityComplaint.mapper.HotServiceQualityComplaintMapper;
import com.hotwj.platform.securityManagement.serviceQualityComplaint.service.IHotServiceQualityComplaintService;
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 2026-01-07
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotServiceQualityComplaintServiceImpl implements IHotServiceQualityComplaintService {

    private final HotServiceQualityComplaintMapper baseMapper;

    /**
     * 查询服务质量投诉
     *
     * @param id 主键
     * @return 服务质量投诉
     */
    @Override
    public HotServiceQualityComplaintVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询服务质量投诉列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 服务质量投诉分页列表
     */
    @Override
    public TableDataInfo<HotServiceQualityComplaintVo> queryPageList(HotServiceQualityComplaintBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotServiceQualityComplaint> lqw = buildQueryWrapper(bo);
        Page<HotServiceQualityComplaintVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的服务质量投诉列表
     *
     * @param bo 查询条件
     * @return 服务质量投诉列表
     */
    @Override
    public List<HotServiceQualityComplaintVo> queryList(HotServiceQualityComplaintBo bo) {
        LambdaQueryWrapper<HotServiceQualityComplaint> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotServiceQualityComplaint> buildQueryWrapper(HotServiceQualityComplaintBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotServiceQualityComplaint> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotServiceQualityComplaint::getId);
        lqw.eq(bo.getCompanyId() != null, HotServiceQualityComplaint::getCompanyId, bo.getCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getComplaintTarget()), HotServiceQualityComplaint::getComplaintTarget, bo.getComplaintTarget());
        lqw.eq(bo.getAcceptorId() != null, HotServiceQualityComplaint::getAcceptorId, bo.getAcceptorId());
        lqw.like(StringUtils.isNotBlank(bo.getAcceptorName()), HotServiceQualityComplaint::getAcceptorName, bo.getAcceptorName());
        lqw.eq(bo.getHandlerId() != null, HotServiceQualityComplaint::getHandlerId, bo.getHandlerId());
        lqw.like(StringUtils.isNotBlank(bo.getHandlerName()), HotServiceQualityComplaint::getHandlerName, bo.getHandlerName());
        lqw.eq(bo.getCaseTime() != null, HotServiceQualityComplaint::getCaseTime, bo.getCaseTime());
        lqw.like(StringUtils.isNotBlank(bo.getComplainantName()), HotServiceQualityComplaint::getComplainantName, bo.getComplainantName());
        lqw.eq(StringUtils.isNotBlank(bo.getComplainantPhone()), HotServiceQualityComplaint::getComplainantPhone, bo.getComplainantPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getComplaintChannel()), HotServiceQualityComplaint::getComplaintChannel, bo.getComplaintChannel());
        lqw.eq(StringUtils.isNotBlank(bo.getComplaintService()), HotServiceQualityComplaint::getComplaintService, bo.getComplaintService());
        lqw.eq(StringUtils.isNotBlank(bo.getCasePlace()), HotServiceQualityComplaint::getCasePlace, bo.getCasePlace());
        lqw.eq(StringUtils.isNotBlank(bo.getCaseSummary()), HotServiceQualityComplaint::getCaseSummary, bo.getCaseSummary());
        lqw.eq(StringUtils.isNotBlank(bo.getHandleResult()), HotServiceQualityComplaint::getHandleResult, bo.getHandleResult());
        lqw.eq(StringUtils.isNotBlank(bo.getComplaintRequest()), HotServiceQualityComplaint::getComplaintRequest, bo.getComplaintRequest());
        lqw.eq(StringUtils.isNotBlank(bo.getAttachmentUrl()), HotServiceQualityComplaint::getAttachmentUrl, bo.getAttachmentUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getAcceptorSignImage()), HotServiceQualityComplaint::getAcceptorSignImage, bo.getAcceptorSignImage());
        lqw.eq(StringUtils.isNotBlank(bo.getHandlerSignImage()), HotServiceQualityComplaint::getHandlerSignImage, bo.getHandlerSignImage());
        lqw.eq(bo.getIsDeleted() != null, HotServiceQualityComplaint::getIsDeleted, bo.getIsDeleted());
        if (params != null) {
            Object beginCaseTime = params.get("beginCaseTime");
            Object endCaseTime = params.get("endCaseTime");
            if (beginCaseTime instanceof java.util.Date) {
                lqw.ge(HotServiceQualityComplaint::getCaseTime, (java.util.Date) beginCaseTime);
            }
            if (endCaseTime instanceof java.util.Date) {
                lqw.le(HotServiceQualityComplaint::getCaseTime, (java.util.Date) endCaseTime);
            }
        }
        return lqw;
    }

    /**
     * 新增服务质量投诉
     *
     * @param bo 服务质量投诉
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotServiceQualityComplaintBo bo) {
        HotServiceQualityComplaint add = MapstructUtils.convert(bo, HotServiceQualityComplaint.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改服务质量投诉
     *
     * @param bo 服务质量投诉
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotServiceQualityComplaintBo bo) {
        HotServiceQualityComplaint update = MapstructUtils.convert(bo, HotServiceQualityComplaint.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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