package com.starry.admin.modules.order.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.custom.module.entity.PlayCustomUserInfoEntity;
import com.starry.admin.modules.order.mapper.PlayOrderComplaintInfoMapper;
import com.starry.admin.modules.order.module.entity.PlayOrderComplaintInfoEntity;
import com.starry.admin.modules.order.module.entity.PlayOrderEvaluateInfoEntity;
import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
import com.starry.admin.modules.order.module.vo.PlayOrderComplaintQueryVo;
import com.starry.admin.modules.order.module.vo.PlayOrderComplaintReturnVo;
import com.starry.admin.modules.order.service.IPlayOrderComplaintInfoService;
import com.starry.admin.modules.personnel.service.IPlayPersonnelGroupInfoService;
import com.starry.admin.modules.weichat.service.NotificationSender;
import com.starry.admin.utils.SecurityUtils;
import com.starry.common.utils.IdUtils;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 订单投诉信息Service业务层处理
 *
 * @author admin
 * @since 2024-05-07
 */
@Service
public class PlayOrderComplaintInfoServiceImpl
        extends
            ServiceImpl<PlayOrderComplaintInfoMapper, PlayOrderComplaintInfoEntity>
        implements
            IPlayOrderComplaintInfoService {
    @Resource
    private PlayOrderComplaintInfoMapper playOrderComplaintInfoMapper;

    @Resource
    private PlayOrderInfoServiceImpl playOrderInfoService;

    @Resource
    private IPlayPersonnelGroupInfoService playClerkGroupInfoService;
    @Resource
    private NotificationSender notificationSender;

    /**
     * 查询订单投诉信息
     *
     * @param id
     *            订单投诉信息主键
     * @return 订单投诉信息
     */
    @Override
    public PlayOrderComplaintInfoEntity selectPlayOrderComplaintInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public PlayOrderComplaintInfoEntity queryCustomToOrderOrderComplaintInfo(String customId, String orderId) {
        LambdaQueryWrapper<PlayOrderComplaintInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayOrderComplaintInfoEntity::getCustomId, customId);
        lambdaQueryWrapper.eq(PlayOrderComplaintInfoEntity::getOrderId, orderId);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public List<PlayOrderComplaintInfoEntity> selectByList(PlayOrderComplaintInfoEntity entity) {
        LambdaQueryWrapper<PlayOrderComplaintInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(entity.getClerkId())) {
            lambdaQueryWrapper.eq(PlayOrderComplaintInfoEntity::getClerkId, entity.getClerkId());
        }
        if (StrUtil.isNotBlank(entity.getCustomId())) {
            lambdaQueryWrapper.eq(PlayOrderComplaintInfoEntity::getCustomId, entity.getCustomId());
        }
        if (StrUtil.isNotBlank(entity.getOrderId())) {
            lambdaQueryWrapper.eq(PlayOrderComplaintInfoEntity::getOrderId, entity.getOrderId());
        }
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public IPage<PlayOrderComplaintReturnVo> selectByPage(PlayOrderComplaintQueryVo vo) {
        MPJLambdaWrapper<PlayOrderComplaintInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<PlayOrderComplaintInfoEntity>();
        // 查询主表全部字段
        lambdaQueryWrapper.selectAll(PlayOrderComplaintInfoEntity.class);
        // 查询订单表
        lambdaQueryWrapper.selectAs(PlayOrderInfoEntity::getOrderNo, "orderNo")
                .selectAs(PlayOrderInfoEntity::getPlaceType, "placeType")
                .selectAs(PlayOrderInfoEntity::getFinalAmount, "finalAmount")
                .selectAs(PlayOrderInfoEntity::getCommodityPrice, "commodityPrice")
                .selectAs(PlayOrderInfoEntity::getServiceDuration, "serviceDuration")
                .selectAs(PlayOrderInfoEntity::getCommodityName, "commodityName")
                .selectAs(PlayOrderInfoEntity::getCommodityType, "commodityType")
                .selectAs(PlayOrderInfoEntity::getCommodityId, "commodityId")
                .selectAs(PlayOrderInfoEntity::getCommodityNumber, "commodityNumber");
        lambdaQueryWrapper.leftJoin(PlayOrderInfoEntity.class, PlayOrderInfoEntity::getId,
                PlayOrderEvaluateInfoEntity::getOrderId);
        // 查询顾客表字段
        lambdaQueryWrapper.selectAs(PlayCustomUserInfoEntity::getId, "customId")
                .selectAs(PlayCustomUserInfoEntity::getAvatar, "customAvatar")
                .selectAs(PlayCustomUserInfoEntity::getNickname, "customNickname");
        lambdaQueryWrapper.leftJoin(PlayCustomUserInfoEntity.class, PlayCustomUserInfoEntity::getId,
                PlayOrderComplaintInfoEntity::getCustomId);
        // 查询陪聊表
        lambdaQueryWrapper.selectAs(PlayClerkUserInfoEntity::getId, "clerkId")
                .selectAs(PlayClerkUserInfoEntity::getAvatar, "clerkAvatar")
                .selectAs(PlayClerkUserInfoEntity::getNickname, "clerkNickname");
        lambdaQueryWrapper.leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId,
                PlayOrderComplaintInfoEntity::getClerkId);
        if (StrUtil.isNotBlank(vo.getClerkId())) {
            lambdaQueryWrapper.eq(PlayOrderComplaintInfoEntity::getClerkId, vo.getClerkId());
        }
        if (StrUtil.isNotBlank(vo.getCustomId())) {
            lambdaQueryWrapper.eq(PlayOrderComplaintInfoEntity::getCustomId, vo.getCustomId());
        }
        if (StrUtil.isNotBlank(vo.getOrderNo())) {
            lambdaQueryWrapper.eq(PlayOrderInfoEntity::getOrderNo, vo.getOrderNo());
        }
        if (StrUtil.isNotBlank(vo.getWeiChatCode())) {
            lambdaQueryWrapper.eq(PlayOrderComplaintInfoEntity::getWxChatCode, vo.getWeiChatCode());
        }
        if (vo.getComplaintTime() != null && vo.getComplaintTime().size() == 2) {
            lambdaQueryWrapper.between(PlayOrderComplaintInfoEntity::getComplaintTime, vo.getComplaintTime().get(0),
                    vo.getComplaintTime().get(1));
        }
        // 加入组员的筛选
        List<String> clerkIdList = playClerkGroupInfoService.getValidClerkIdList(SecurityUtils.getLoginUser(), null);
        lambdaQueryWrapper.in(PlayOrderComplaintInfoEntity::getClerkId, clerkIdList);

        return this.baseMapper.selectJoinPage(new Page<>(vo.getPageNum(), vo.getPageSize()),
                PlayOrderComplaintReturnVo.class, lambdaQueryWrapper);
    }

    /**
     * 查询订单投诉信息列表
     *
     * @param playOrderComplaintInfo
     *            订单投诉信息
     * @return 订单投诉信息
     */
    @Override
    public IPage<PlayOrderComplaintInfoEntity> selectPlayOrderComplaintInfoByPage(
            PlayOrderComplaintInfoEntity playOrderComplaintInfo) {
        Page<PlayOrderComplaintInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 新增订单投诉信息
     *
     * @param playOrderComplaintInfo
     *            订单投诉信息
     * @return 结果
     */
    @Override
    public boolean create(PlayOrderComplaintInfoEntity playOrderComplaintInfo) {
        if (allowComplaint(playOrderComplaintInfo.getCustomId(), playOrderComplaintInfo.getOrderId())) {
            throw new CustomException("一个订单只能投诉一次");
        }
        if (StrUtil.isBlankIfStr(playOrderComplaintInfo.getId())) {
            playOrderComplaintInfo.setId(IdUtils.getUuid());
        }
        PlayOrderInfoEntity orderInfo = playOrderInfoService.selectOrderInfoById(playOrderComplaintInfo.getOrderId());
        playOrderComplaintInfo.setClerkId(orderInfo.getAcceptBy());
        playOrderComplaintInfo.setCommodityId(orderInfo.getCommodityId());
        // 发送投诉消息给管理员以及客服
        notificationSender.sendComplaintMessage(playOrderComplaintInfo, orderInfo);
        return save(playOrderComplaintInfo);
    }

    /**
     * 当前订单是否已投诉
     *
     * @param customerId
     *            顾客ID
     * @param orderId
     *            订单ID
     * @return true:订单已评价，false:订单未评价
     */
    public boolean allowComplaint(String customerId, String orderId) {
        LambdaQueryWrapper<PlayOrderComplaintInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayOrderComplaintInfoEntity::getCustomId, customerId);
        lambdaQueryWrapper.eq(PlayOrderComplaintInfoEntity::getOrderId, orderId);
        return this.baseMapper.selectOne(lambdaQueryWrapper) != null;

    }

    /**
     * 修改订单投诉信息
     *
     * @param playOrderComplaintInfo
     *            订单投诉信息
     * @return 结果
     */
    @Override
    public boolean update(PlayOrderComplaintInfoEntity playOrderComplaintInfo) {
        return updateById(playOrderComplaintInfo);
    }

    /**
     * 批量删除订单投诉信息
     *
     * @param ids
     *            需要删除的订单投诉信息主键
     * @return 结果
     */
    @Override
    public int deletePlayOrderComplaintInfoByIds(String[] ids) {
        return playOrderComplaintInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除订单投诉信息信息
     *
     * @param id
     *            订单投诉信息主键
     * @return 结果
     */
    @Override
    public int deletePlayOrderComplaintInfoById(String id) {
        return playOrderComplaintInfoMapper.deleteById(id);
    }
}
