package com.ruoyi.info.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.info.order.domain.OrderReviews;
import com.ruoyi.info.order.domain.bo.OrderBo;
import com.ruoyi.info.order.domain.bo.OrderReviewsBo;
import com.ruoyi.info.order.domain.vo.OrderReviewsVo;
import com.ruoyi.info.order.domain.vo.OrderVo;
import com.ruoyi.info.order.domain.vo.OrderWorkerChangeRecordVo;
import com.ruoyi.info.order.exception.OrderReviewsExceptionEnum;
import com.ruoyi.info.order.mapper.OrderReviewsMapper;
import com.ruoyi.info.order.service.IOrderReviewsService;
import com.ruoyi.info.order.service.IOrderService;
import com.ruoyi.info.order.service.IOrderWorkerChangeRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 订单评价Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-29
 */
@RequiredArgsConstructor
@Service
public class OrderReviewsServiceImpl implements IOrderReviewsService {

    private final OrderReviewsMapper baseMapper;
    @Lazy
    @Resource
    private IOrderService orderService;
    private final IOrderWorkerChangeRecordService orderWorkerChangeRecordService;

    /**
     * 查询订单评价分页
     *
     * @param bo 订单评价
     * @return 订单评价
     */
    @Override
    public TableDataInfo<OrderReviewsVo> queryPageList(OrderReviewsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OrderReviews> lqw = buildQueryWrapper(bo);
        Page<OrderReviewsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询订单评价列表
     *
     * @param bo 订单评价
     * @return 订单评价
     */
    @Override
    public List<OrderReviewsVo> queryList(OrderReviewsBo bo) {
        LambdaQueryWrapper<OrderReviews> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OrderReviews> buildQueryWrapper(OrderReviewsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OrderReviews> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOrderId() != null, OrderReviews::getOrderId, bo.getOrderId());
        lqw.eq(bo.getUserId() != null, OrderReviews::getUserId, bo.getUserId());
        lqw.eq(bo.getServiceAttitude() != null, OrderReviews::getServiceAttitude, bo.getServiceAttitude());
        lqw.eq(bo.getDressAppearance() != null, OrderReviews::getDressAppearance, bo.getDressAppearance());
        lqw.eq(bo.getCommunicationAbility() != null, OrderReviews::getCommunicationAbility, bo.getCommunicationAbility());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), OrderReviews::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 查询订单评价
     *
     * @param id 订单评价主键
     * @return 订单评价
     */
    @Override
    public OrderReviewsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 详情订单评价
     *
     * @param id 订单评价主键
     * @return 订单评价
     */
    @Override
    public OrderReviews loadById(Long id, Boolean tw){
        OrderReviews info = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(OrderReviewsExceptionEnum.OrderReviews_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增订单评价
     *
     * @param bo 订单评价
     * @return 结果
     */
    @Override
    public Boolean insertByBo(OrderReviewsBo bo) {
        OrderReviews add = BeanUtil.toBean(bo, OrderReviews.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单评价
     *
     * @param bo 订单评价
     * @return 结果
     */
    @Override
    public Boolean updateByBo(OrderReviewsBo bo) {
        OrderReviews orderReviews = baseMapper.selectById(bo.getId());
        OrderReviews update = BeanCopyUtils.copy(bo, orderReviews);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(OrderReviews entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除订单评价
     *
     * @param ids 需要删除的订单评价主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public OrderReviewsVo queryByOrderId(Long orderId) {
        return baseMapper.selectVoOne(new LambdaQueryWrapper<OrderReviews>().eq(OrderReviews::getOrderId, orderId).last("limit 1"));
    }

    @Override
    public TableDataInfo<OrderVo> queryOrderList(OrderBo bo, PageQuery pageQuery) {
        TableDataInfo<OrderVo> orderVoTableDataInfo = orderService.queryAllPageList(bo, pageQuery);
        orderVoTableDataInfo.setRows(convert(orderVoTableDataInfo.getRows()));
        return orderVoTableDataInfo;
    }

    private List<OrderVo> convert(List<OrderVo> vos) {
        return vos.stream().map(vo -> {
            OrderReviewsVo orderReviewsVo = queryByOrderId(vo.getId());
            if (orderReviewsVo != null) {
                vo.setOrderReviews(orderReviewsVo);
            }
            OrderWorkerChangeRecordVo orderWorkerChangeRecordVo = orderWorkerChangeRecordService.queryByOrderAndWorker(vo.getId(), vo.getWorkerId());
            if (orderWorkerChangeRecordVo != null) {
                vo.setOrderWorkerChangeRecord(orderWorkerChangeRecordVo);
            }
            return vo;
        }).collect(Collectors.toList());
    }

}
