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

import cn.hutool.core.date.DateUtil;
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.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starry.admin.modules.clerk.mapper.PlayClerkPkMapper;
import com.starry.admin.modules.clerk.module.entity.ClerkPkEnum;
import com.starry.admin.modules.clerk.module.entity.PlayClerkPkEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.clerk.service.IPlayClerkPkService;
import com.starry.admin.modules.clerk.service.IPlayClerkUserInfoService;
import com.starry.common.utils.IdUtils;
import java.util.Arrays;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 店员pkService业务层处理
 *
 * @author admin
 * @since 2024-08-02
 */
@Service
public class PlayClerkPkServiceImpl extends ServiceImpl<PlayClerkPkMapper, PlayClerkPkEntity>
        implements
            IPlayClerkPkService {
    @Resource
    private PlayClerkPkMapper playClerkPkMapper;
    @Resource
    private IPlayClerkUserInfoService clerkUserInfoService;

    /**
     * 查询店员pk
     *
     * @param id
     *            店员pk主键
     * @return 店员pk
     */
    @Override
    public PlayClerkPkEntity selectPlayClerkPkById(String id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 查询店员pk列表
     *
     * @param playClerkPk
     *            店员pk
     * @return 店员pk
     */
    @Override
    public IPage<PlayClerkPkEntity> selectPlayClerkPkByPage(PlayClerkPkEntity playClerkPk) {
        Page<PlayClerkPkEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 新增店员pk
     *
     * @param playClerkPk
     *            店员pk
     * @return 结果
     */
    @Override
    public boolean create(PlayClerkPkEntity playClerkPk) {
        if (StrUtil.isBlankIfStr(playClerkPk.getId())) {
            playClerkPk.setId(IdUtils.getUuid());
        }
        if (DateUtil.compare(playClerkPk.getPkBeginTime(), new Date()) < 1) {
            throw new RuntimeException("开始时间必须大于当前时间");
        }
        if (DateUtil.compare(playClerkPk.getPkEndTime(), new Date()) < 1) {
            throw new RuntimeException("结束时间必须大于当前时间");
        }
        LambdaQueryWrapper<PlayClerkPkEntity> wrapper = Wrappers.lambdaQuery(PlayClerkPkEntity.class)
                .in(PlayClerkPkEntity::getClerkA, Arrays.asList(playClerkPk.getClerkA(), playClerkPk.getClerkB()))
                .in(PlayClerkPkEntity::getStatus,
                        Arrays.asList(ClerkPkEnum.TO_BE_STARTED.name(), ClerkPkEnum.IN_PROGRESS.name()));
        long count = this.count(wrapper);
        if (count > 0) {
            PlayClerkUserInfoEntity clerkUserInfo = clerkUserInfoService.selectById(playClerkPk.getClerkA());
            throw new RuntimeException("店员【" + clerkUserInfo.getNickname() + "】还有未完成的PK，无法继续添加");
        }
        wrapper.clear();
        wrapper = Wrappers.lambdaQuery(PlayClerkPkEntity.class)
                .in(PlayClerkPkEntity::getClerkB, Arrays.asList(playClerkPk.getClerkA(), playClerkPk.getClerkB()))
                .in(PlayClerkPkEntity::getStatus,
                        Arrays.asList(ClerkPkEnum.TO_BE_STARTED.name(), ClerkPkEnum.IN_PROGRESS.name()));
        count = this.count(wrapper);
        if (count > 0) {
            PlayClerkUserInfoEntity clerkUserInfo = clerkUserInfoService.selectById(playClerkPk.getClerkB());
            throw new RuntimeException("店员【" + clerkUserInfo.getNickname() + "】还有未完成的PK，无法继续添加");
        }

        playClerkPk.setStatus(ClerkPkEnum.TO_BE_STARTED.name());
        return save(playClerkPk);
    }

    /**
     * 修改店员pk
     *
     * @param playClerkPk
     *            店员pk
     * @return 结果
     */
    @Override
    public boolean update(PlayClerkPkEntity playClerkPk) {
        return updateById(playClerkPk);
    }

    /**
     * 批量删除店员pk
     *
     * @param ids
     *            需要删除的店员pk主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkPkByIds(String[] ids) {
        return playClerkPkMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除店员pk信息
     *
     * @param id
     *            店员pk主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkPkById(String id) {
        return playClerkPkMapper.deleteById(id);
    }
}
