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.PlayOrderEvaluateInfoMapper;
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.PlayOrderEvaluateQueryVo;
import com.starry.admin.modules.order.module.vo.PlayOrderEvaluateReturnVo;
import com.starry.admin.modules.order.service.IPlayOrderEvaluateInfoService;
import com.starry.admin.modules.personnel.service.IPlayPersonnelGroupInfoService;
import com.starry.admin.utils.SecurityUtils;
import com.starry.common.utils.IdUtils;
import com.starry.common.utils.StringUtils;
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 PlayOrderEvaluateInfoServiceImpl
        extends
            ServiceImpl<PlayOrderEvaluateInfoMapper, PlayOrderEvaluateInfoEntity>
        implements
            IPlayOrderEvaluateInfoService {
    @Resource
    private PlayOrderEvaluateInfoMapper playOrderEvaluateInfoMapper;

    @Resource
    private PlayOrderInfoServiceImpl playOrderInfoService;

    @Resource
    private IPlayPersonnelGroupInfoService playClerkGroupInfoService;

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

    /**
     * 查询订单评价信息
     *
     * @param id
     *            订单评价信息主键
     * @return 订单评价信息
     */
    @Override
    public PlayOrderEvaluateInfoEntity selectPlayOrderEvaluateInfoById(String id) {
        PlayOrderEvaluateInfoEntity entity = this.baseMapper.selectById(id);
        if (entity == null) {
            throw new CustomException("评价信息不存在");
        }
        return this.baseMapper.selectById(id);
    }

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

    @Override
    public IPage<PlayOrderEvaluateReturnVo> selectByPage(PlayOrderEvaluateQueryVo vo) {
        MPJLambdaWrapper<PlayOrderEvaluateInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<PlayOrderEvaluateInfoEntity>();
        // 查询主表全部字段
        lambdaQueryWrapper.selectAll(PlayOrderEvaluateInfoEntity.class);

        // 查询顾客表字段
        lambdaQueryWrapper.selectAs(PlayCustomUserInfoEntity::getId, "customId")
                .selectAs(PlayCustomUserInfoEntity::getAvatar, "customAvatar")
                .selectAs(PlayCustomUserInfoEntity::getNickname, "customNickname");
        lambdaQueryWrapper.leftJoin(PlayCustomUserInfoEntity.class, PlayCustomUserInfoEntity::getId,
                PlayOrderEvaluateInfoEntity::getCustomId);
        // 查询店员表字典
        lambdaQueryWrapper.selectAs(PlayClerkUserInfoEntity::getId, "clerkId")
                .selectAs(PlayClerkUserInfoEntity::getAvatar, "clerkAvatar")
                .selectAs(PlayClerkUserInfoEntity::getNickname, "clerkNickname");
        lambdaQueryWrapper.leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId,
                PlayOrderEvaluateInfoEntity::getClerkId);
        // 查询订单表
        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);

        if (StrUtil.isNotBlank(vo.getClerkId())) {
            lambdaQueryWrapper.eq(PlayOrderEvaluateInfoEntity::getClerkId, vo.getClerkId());
        }
        if (StrUtil.isNotBlank(vo.getCustomId())) {
            lambdaQueryWrapper.eq(PlayOrderEvaluateInfoEntity::getCustomId, vo.getCustomId());
        }
        if (StrUtil.isNotBlank(vo.getId())) {
            lambdaQueryWrapper.eq(PlayOrderEvaluateInfoEntity::getId, vo.getId());
        }
        if (StrUtil.isNotBlank(vo.getEvaluateCon())) {
            lambdaQueryWrapper.like(PlayOrderEvaluateInfoEntity::getEvaluateCon, vo.getEvaluateCon());
        }
        if (StrUtil.isNotBlank(vo.getAnonymous())) {
            lambdaQueryWrapper.eq(PlayOrderEvaluateInfoEntity::getAnonymous, vo.getAnonymous());
        }
        if (StrUtil.isNotBlank(vo.getHidden())) {
            lambdaQueryWrapper.eq(PlayOrderEvaluateInfoEntity::getHidden, vo.getHidden());
        }
        if (vo.getEvaluateTime() != null && vo.getEvaluateTime().size() == 2) {
            lambdaQueryWrapper.between(PlayOrderEvaluateInfoEntity::getEvaluateTime, vo.getEvaluateTime().get(0),
                    vo.getEvaluateTime().get(1));
        }
        // 加入组员的筛选
        if (StringUtils.isEmpty(vo.getClerkId())) {
            List<String> clerkIdList = playClerkGroupInfoService.getValidClerkIdList(SecurityUtils.getLoginUser(),
                    null);
            lambdaQueryWrapper.in(PlayOrderEvaluateInfoEntity::getClerkId, clerkIdList);
        }

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

    /**
     * 查询订单评价信息列表
     *
     * @param playOrderEvaluateInfo
     *            订单评价信息
     * @return 订单评价信息
     */
    @Override
    public IPage<PlayOrderEvaluateInfoEntity> selectPlayOrderEvaluateInfoByPage(
            PlayOrderEvaluateInfoEntity playOrderEvaluateInfo) {
        Page<PlayOrderEvaluateInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 新增订单评价信息
     *
     * @param playOrderEvaluateInfo
     *            订单评价信息
     * @return 结果
     */
    @Override
    public boolean create(PlayOrderEvaluateInfoEntity playOrderEvaluateInfo) {
        if (allowComplaint(playOrderEvaluateInfo.getCustomId(), playOrderEvaluateInfo.getOrderId())) {
            throw new CustomException("一个订单只能评价一次");
        }
        if (StrUtil.isBlankIfStr(playOrderEvaluateInfo.getId())) {
            playOrderEvaluateInfo.setId(IdUtils.getUuid());
        }
        PlayOrderInfoEntity orderInfo = playOrderInfoService.selectOrderInfoById(playOrderEvaluateInfo.getOrderId());
        playOrderEvaluateInfo.setClerkId(orderInfo.getAcceptBy());
        playOrderEvaluateInfo.setCommodityId(orderInfo.getCommodityId());
        return save(playOrderEvaluateInfo);
    }

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

    }

    /**
     * 修改订单评价信息
     *
     * @param playOrderEvaluateInfo
     *            订单评价信息
     * @return 结果
     */
    @Override
    public boolean update(PlayOrderEvaluateInfoEntity playOrderEvaluateInfo) {
        return updateById(playOrderEvaluateInfo);
    }

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

    /**
     * 删除订单评价信息信息
     *
     * @param id
     *            订单评价信息主键
     * @return 结果
     */
    @Override
    public int deletePlayOrderEvaluateInfoById(String id) {
        return playOrderEvaluateInfoMapper.deleteById(id);
    }
}
