package com.starry.admin.modules.clerk.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.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.clerk.mapper.PlayClerkCommodityMapper;
import com.starry.admin.modules.clerk.module.entity.PlayClerkCommodityEntity;
import com.starry.admin.modules.clerk.service.IPlayClerkCommodityService;
import com.starry.admin.modules.shop.module.entity.PlayCommodityInfoEntity;
import com.starry.admin.modules.shop.service.IPlayCommodityInfoService;
import com.starry.common.utils.IdUtils;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 陪聊服务项目Service业务层处理
 *
 * @author admin
 * @since 2024-03-31
 */
@Service
public class PlayClerkCommodityServiceImpl extends ServiceImpl<PlayClerkCommodityMapper, PlayClerkCommodityEntity>
        implements
            IPlayClerkCommodityService {
    @Resource
    private PlayClerkCommodityMapper playClerkCommodityMapper;

    @Resource
    private IPlayCommodityInfoService playCommodityInfoService;

    @Override
    public void initClerkCommodity(String userId) {
        // 查询当前陪聊的所有服务项目
        LambdaQueryWrapper<PlayClerkCommodityEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkCommodityEntity::getClerkId, userId);
        this.baseMapper.delete(lambdaQueryWrapper);
        // 根据当前租户的服务项目，生成陪聊项目数据
        for (PlayCommodityInfoEntity commodityInfo : playCommodityInfoService.selectByType()) {
            PlayClerkCommodityEntity entity = new PlayClerkCommodityEntity();
            entity.setClerkId(userId);
            entity.setCommodityId(commodityInfo.getId());
            entity.setCommodityName(commodityInfo.getItemName());
            entity.setEnablingState("1");
            entity.setSort(commodityInfo.getSort());
            this.create(entity);
        }

    }

    @Override
    public List<String> getClerkCommodityList(String userId, String enablingState) {
        List<PlayClerkCommodityEntity> list = this.selectCommodityTypeByUser(userId, enablingState);
        return list.stream().map(PlayClerkCommodityEntity::getCommodityName).collect(Collectors.toList());
    }

    @Override
    public List<PlayClerkCommodityEntity> selectCommodityTypeByUser(String userId, String enablingState) {
        LambdaQueryWrapper<PlayClerkCommodityEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(PlayClerkCommodityEntity::getSort);
        lambdaQueryWrapper.eq(PlayClerkCommodityEntity::getClerkId, userId);
        if (StrUtil.isNotBlank(enablingState)) {
            lambdaQueryWrapper.eq(PlayClerkCommodityEntity::getEnablingState, enablingState);
        }
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<PlayClerkCommodityEntity> selectByUser(String userId) {
        LambdaQueryWrapper<PlayClerkCommodityEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkCommodityEntity::getClerkId, userId);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public List<PlayClerkCommodityEntity> selectAll() {
        return this.baseMapper.selectList(new LambdaQueryWrapper<>());
    }

    @Override
    public void startStopClerkItem(String commodityName, String enablingState, String clerkId) {
        PlayClerkCommodityEntity entity = new PlayClerkCommodityEntity();
        entity.setEnablingState(enablingState);
        LambdaQueryWrapper<PlayClerkCommodityEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PlayClerkCommodityEntity::getClerkId, clerkId);
        lambdaQueryWrapper.eq(PlayClerkCommodityEntity::getCommodityName, commodityName);
        this.baseMapper.update(entity, lambdaQueryWrapper);
    }

    /**
     * 查询陪聊服务项目
     *
     * @param id
     *            陪聊服务项目主键
     * @return 陪聊服务项目
     */
    @Override
    public PlayClerkCommodityEntity selectPlayClerkCommodityById(String id) {
        PlayClerkCommodityEntity entity = this.baseMapper.selectById(id);
        if (entity == null) {
            throw new CustomException("服务项目不存在");
        }
        return entity;
    }

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

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

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

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

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