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

import com.starry.common.utils.IdUtils;
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.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.commodity.mapper.PlayClerkServiceMapper;
import com.starry.admin.modules.commodity.module.entity.PlayClerkServiceEntity;
import com.starry.admin.modules.commodity.module.entity.PlayServiceInfoEntity;
import com.starry.admin.modules.commodity.service.IPlayClerkServiceService;
import com.starry.admin.modules.commodity.service.IPlayServiceInfoService;
import org.springframework.stereotype.Service;

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

/**
 * 陪聊引用服务项目Service业务层处理
 *
 * @author admin
 * @since 2024-03-28
 */
@Service
public class PlayClerkServiceServiceImpl extends ServiceImpl<PlayClerkServiceMapper, PlayClerkServiceEntity> implements IPlayClerkServiceService {
    @Resource
    private PlayClerkServiceMapper playClerkServiceMapper;


    @Resource
    private IPlayServiceInfoService playServiceInfoService;


    @Override
    public void initPlayService(String playUserId) {
        for (PlayServiceInfoEntity playServiceInfoEntity : playServiceInfoService.selectPlayServiceInfoAll()) {
            PlayClerkServiceEntity entity = new PlayClerkServiceEntity();
            entity.setServiceId(playServiceInfoEntity.getId());
            entity.setTenantId(playServiceInfoEntity.getTenantId());
            entity.setPlayUserId(playUserId);
            entity.setEnablingState("0");
            this.create(entity);
        }
    }

    /**
     * 查询陪聊引用服务项目
     *
     * @param id 陪聊引用服务项目主键
     * @return 陪聊引用服务项目
     */
    @Override
    public PlayClerkServiceEntity selectPlayClerkServiceById(String id) {
        return this.baseMapper.selectById(id);
    }

    /**
     * 查询陪聊引用服务项目列表
     *
     * @param playClerkService 陪聊引用服务项目
     * @return 陪聊引用服务项目
     */
    @Override
    public IPage<PlayClerkServiceEntity> selectPlayClerkServiceByPage(PlayClerkServiceEntity playClerkService) {
        Page<PlayClerkServiceEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

    /**
     * 新增陪聊引用服务项目
     *
     * @param playClerkService 陪聊引用服务项目
     * @return 结果
     */
    @Override
    public boolean create(PlayClerkServiceEntity playClerkService) {
        if (StrUtil.isBlankIfStr(playClerkService.getId())) {
            playClerkService.setId(IdUtils.getUuid());
        }
        return save(playClerkService);
    }


    @Override
    public void updateServiceEnablingState(String playUserId, String serviceId, String enablingState) {
        LambdaQueryWrapper<PlayClerkServiceEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkServiceEntity::getServiceId, serviceId);
        lambdaQueryWrapper.eq(PlayClerkServiceEntity::getPlayUserId, playUserId);
        List<PlayClerkServiceEntity> list = this.baseMapper.selectList(lambdaQueryWrapper);
        if (list.size() != 1) {
            throw new CustomException("服务不存在，请查证");
        }
        // 更新服务状态
        PlayClerkServiceEntity item = list.get(0);
        item.setEnablingState(enablingState);
        this.update(item);
    }

    /**
     * 修改陪聊引用服务项目
     *
     * @param playClerkService 陪聊引用服务项目
     * @return 结果
     */
    @Override
    public boolean update(PlayClerkServiceEntity playClerkService) {
        return updateById(playClerkService);
    }

    /**
     * 批量删除陪聊引用服务项目
     *
     * @param ids 需要删除的陪聊引用服务项目主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkServiceByIds(String[] ids) {
        return playClerkServiceMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除陪聊引用服务项目信息
     *
     * @param id 陪聊引用服务项目主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkServiceById(String id) {
        return playClerkServiceMapper.deleteById(id);
    }
}
