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.order.mapper.PlayOrderContinueInfoMapper;
import com.starry.admin.modules.order.module.entity.PlayOrderContinueInfoEntity;
import com.starry.admin.modules.order.module.entity.PlayOrderInfoEntity;
import com.starry.admin.modules.order.module.vo.PlayOrderContinueQueryVo;
import com.starry.admin.modules.order.module.vo.PlayOrderContinueReturnVo;
import com.starry.admin.modules.order.module.vo.PlayOrderReviewStateEditVo;
import com.starry.admin.modules.order.service.IPlayOrderContinueInfoService;
import com.starry.admin.modules.personnel.service.IPlayPersonnelGroupInfoService;
import com.starry.admin.utils.SecurityUtils;
import com.starry.common.context.CustomSecurityContextHolder;
import com.starry.common.utils.IdUtils;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 订单续单信息Service业务层处理
 *
 * @author admin
 * @since 2024-05-10
 */
@Service
public class PlayOrderContinueInfoServiceImpl
        extends
            ServiceImpl<PlayOrderContinueInfoMapper, PlayOrderContinueInfoEntity>
        implements
            IPlayOrderContinueInfoService {
    @Resource
    private PlayOrderContinueInfoMapper playOrderContinueInfoMapper;

    @Resource
    private IPlayPersonnelGroupInfoService playClerkGroupInfoService;

    @Override
    public PlayOrderContinueInfoEntity selectPlayOrderId(String orderId) {
        MPJLambdaWrapper<PlayOrderContinueInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.eq(PlayOrderContinueInfoEntity::getOrderId, orderId);
        return playOrderContinueInfoMapper.selectOne(lambdaQueryWrapper);
    }

    /**
     * 查询订单续单信息
     *
     * @param id
     *            订单续单信息主键
     * @return 订单续单信息
     */
    @Override
    public PlayOrderContinueInfoEntity selectPlayOrderContinueInfoById(String id) {
        PlayOrderContinueInfoEntity entity = this.baseMapper.selectById(id);
        if (entity != null) {
            return entity;
        }
        throw new CustomException("续单申请不存在");
    }

    @Override
    public void updateReviewState(PlayOrderReviewStateEditVo vo) {
        PlayOrderContinueInfoEntity entity = this.selectPlayOrderContinueInfoById(vo.getId());
        entity.setReviewedState(vo.getReviewState());
        entity.setReviewedRemark(vo.getRemark());
        entity.setReviewedBy(CustomSecurityContextHolder.getUserId());
        entity.setReviewedTime(LocalDateTime.now());
        this.baseMapper.updateById(entity);
        // 添加订单信息
        if ("1".equals(vo.getReviewState())) {

        }
    }

    @Override
    public IPage<PlayOrderContinueReturnVo> selectPlayByPage(PlayOrderContinueQueryVo vo) {
        MPJLambdaWrapper<PlayOrderContinueInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        // 主表
        lambdaQueryWrapper.selectAll(PlayOrderContinueInfoEntity.class);
        lambdaQueryWrapper.selectAs(PlayClerkUserInfoEntity::getId, "clerkId")
                .selectAs(PlayClerkUserInfoEntity::getAvatar, "clerkAvatar")
                .selectAs(PlayClerkUserInfoEntity::getNickname, "clerkNickname")
                .selectAs(PlayClerkUserInfoEntity::getSex, "clerkSex");
        lambdaQueryWrapper.leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId,
                PlayOrderContinueInfoEntity::getClerkId);
        lambdaQueryWrapper.leftJoin(PlayOrderInfoEntity.class, PlayOrderInfoEntity::getId,
                PlayOrderContinueInfoEntity::getOrderId);
        if (StrUtil.isNotBlank(vo.getCustomId())) {
            lambdaQueryWrapper.like(PlayOrderContinueInfoEntity::getCustomId, vo.getCustomId());
        }
        if (StrUtil.isNotBlank(vo.getClerkId())) {
            lambdaQueryWrapper.like(PlayClerkUserInfoEntity::getId, vo.getClerkId());
        }
        if (StrUtil.isNotBlank(vo.getOrderNo())) {
            lambdaQueryWrapper.like(PlayClerkUserInfoEntity::getNickname, vo.getNickname());
        }
        if (StrUtil.isNotBlank(vo.getReviewState())) {
            lambdaQueryWrapper.like(PlayOrderContinueInfoEntity::getReviewedState, vo.getReviewState());
        }
        if (vo.getContinueTime() != null && vo.getContinueTime().size() == 2) {
            lambdaQueryWrapper.between(PlayOrderContinueInfoEntity::getContinueTime, vo.getContinueTime().get(0),
                    vo.getContinueTime().get(1));
        }
        if (vo.getContinueTime() != null && vo.getContinueTime().size() == 2) {
            lambdaQueryWrapper.between(PlayOrderContinueInfoEntity::getContinueTime, vo.getContinueTime().get(0),
                    vo.getContinueTime().get(1));
        }
        // 加入组员的筛选
        List<String> clerkIdList = playClerkGroupInfoService.getValidClerkIdList(SecurityUtils.getLoginUser(), null);
        lambdaQueryWrapper.in(PlayOrderContinueInfoEntity::getClerkId, clerkIdList);

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

    /**
     * 查询订单续单信息列表
     *
     * @param playOrderContinueInfo
     *            订单续单信息
     * @return 订单续单信息
     */
    @Override
    public IPage<PlayOrderContinueInfoEntity> selectPlayOrderContinueInfoByPage(
            PlayOrderContinueInfoEntity playOrderContinueInfo) {
        Page<PlayOrderContinueInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 新增订单续单信息
     *
     * @param playOrderContinueInfo
     *            订单续单信息
     * @return 结果
     */
    @Override
    public boolean create(PlayOrderContinueInfoEntity playOrderContinueInfo) {
        if (StrUtil.isBlankIfStr(playOrderContinueInfo.getId())) {
            playOrderContinueInfo.setId(IdUtils.getUuid());
        }
        return save(playOrderContinueInfo);
    }

    /**
     * 修改订单续单信息
     *
     * @param playOrderContinueInfo
     *            订单续单信息
     * @return 结果
     */
    @Override
    public boolean update(PlayOrderContinueInfoEntity playOrderContinueInfo) {
        return updateById(playOrderContinueInfo);
    }

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

    /**
     * 删除订单续单信息信息
     *
     * @param id
     *            订单续单信息主键
     * @return 结果
     */
    @Override
    public int deletePlayOrderContinueInfoById(String id) {
        return playOrderContinueInfoMapper.deleteById(id);
    }
}
