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.modules.order.mapper.PlayOrderRandomInfoMapper;
import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
import com.starry.admin.modules.order.module.entity.PlayOrderRandomInfoEntity;
import com.starry.admin.modules.order.module.vo.ClerkEstimatedRevenueVo;
import com.starry.admin.modules.order.service.IPlayOrderInfoService;
import com.starry.admin.modules.order.service.IPlayOrderRandomInfoService;
import com.starry.admin.modules.weichat.entity.order.PlayClerkRandomOrderDetailReturnVo;
import com.starry.admin.modules.weichat.entity.order.PlayOrderInfoRandomQueryVo;
import com.starry.admin.modules.weichat.entity.order.PlayRandomOrderInfoReturnVo;
import com.starry.common.utils.IdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;

/**
 * 随机单列表Service业务层处理
 *
 * @author admin
 * @since 2024-05-29
 */
@Service
public class PlayOrderRandomInfoServiceImpl extends ServiceImpl<PlayOrderRandomInfoMapper, PlayOrderRandomInfoEntity> implements IPlayOrderRandomInfoService {
    @Resource
    private PlayOrderRandomInfoMapper playOrderRandomInfoMapper;

    @Resource
    private IPlayOrderInfoService playOrderInfoService;

    /**
     * 查询随机单列表
     *
     * @param id 随机单列表主键
     * @return 随机单列表
     */
    @Override
    public PlayOrderRandomInfoEntity selectPlayOrderRandomInfoById(String id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 查询随机单列表列表
     *
     * @param playOrderRandomInfo 随机单列表
     * @return 随机单列表
     */
    @Override
    public IPage<PlayOrderRandomInfoEntity> selectPlayOrderRandomInfoByPage(PlayOrderRandomInfoEntity playOrderRandomInfo) {
        Page<PlayOrderRandomInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<PlayOrderRandomInfoEntity>());
    }


    @Override
    public PlayClerkRandomOrderDetailReturnVo clerkSelectOrderDetails(String id, String clerkId) {
        MPJLambdaWrapper<PlayOrderRandomInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
        lambdaWrapper.selectAll(PlayOrderRandomInfoEntity.class);
        lambdaWrapper.selectAll(PlayOrderInfoEntity.class);
        lambdaWrapper.leftJoin(PlayOrderInfoEntity.class, PlayOrderInfoEntity::getId, PlayOrderRandomInfoEntity::getId);
        lambdaWrapper.eq(PlayOrderRandomInfoEntity::getId, id);
        PlayClerkRandomOrderDetailReturnVo vo = this.baseMapper.selectJoinOne(PlayClerkRandomOrderDetailReturnVo.class, lambdaWrapper);

        PlayOrderInfoEntity orderInfo = playOrderInfoService.selectOrderInfoById(id);
        ClerkEstimatedRevenueVo estimatedRevenueVo = playOrderInfoService.getClerkEstimatedRevenue(clerkId, orderInfo.getCouponIds(), orderInfo.getPlaceType(), orderInfo.getFirstOrder(), orderInfo.getFinalAmount());
        vo.setEstimatedRevenue(estimatedRevenueVo.getRevenueAmount());
        vo.setEstimatedRevenueRatio(estimatedRevenueVo.getRevenueRatio());
        return vo;
    }

    @Override
    public IPage<PlayRandomOrderInfoReturnVo> selectByPage(PlayOrderInfoRandomQueryVo vo) {
        MPJLambdaWrapper<PlayOrderRandomInfoEntity> lambdaWrapper = new MPJLambdaWrapper<>();
        // 查询随机单全部字段
        lambdaWrapper.selectAll(PlayOrderRandomInfoEntity.class);
        // 订单表
        lambdaWrapper.selectAll(PlayOrderInfoEntity.class);
        lambdaWrapper.leftJoin(PlayOrderInfoEntity.class, PlayOrderInfoEntity::getId, PlayOrderRandomInfoEntity::getId);
        lambdaWrapper.eq(PlayOrderInfoEntity::getOrderStatus, "0");
        return this.baseMapper.selectJoinPage(new Page<>(vo.getPageNum(), vo.getPageSize()), PlayRandomOrderInfoReturnVo.class, lambdaWrapper);
    }

    /**
     * 新增随机单列表
     *
     * @param playOrderRandomInfo 随机单列表
     * @return 结果
     */
    @Override
    public boolean create(PlayOrderRandomInfoEntity playOrderRandomInfo) {
        if (StrUtil.isBlankIfStr(playOrderRandomInfo.getId())) {
            playOrderRandomInfo.setId(IdUtils.getUuid());
        }
        return save(playOrderRandomInfo);
    }

    /**
     * 修改随机单列表
     *
     * @param playOrderRandomInfo 随机单列表
     * @return 结果
     */
    @Override
    public boolean update(PlayOrderRandomInfoEntity playOrderRandomInfo) {
        return updateById(playOrderRandomInfo);
    }

    /**
     * 批量删除随机单列表
     *
     * @param ids 需要删除的随机单列表主键
     * @return 结果
     */
    @Override
    public int deletePlayOrderRandomInfoByIds(String[] ids) {
        return playOrderRandomInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除随机单列表信息
     *
     * @param id 随机单列表主键
     * @return 结果
     */
    @Override
    public int deletePlayOrderRandomInfoById(String id) {
        return playOrderRandomInfoMapper.deleteById(id);
    }
}
