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.github.yulichang.wrapper.MPJLambdaWrapper;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.clerk.mapper.PlayClerkDataReviewInfoMapper;
import com.starry.admin.modules.clerk.module.entity.PlayClerkDataReviewInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserReviewInfoEntity;
import com.starry.admin.modules.clerk.module.vo.PlayClerkDataReviewQueryVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkDataReviewReturnVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkDataReviewStateEditVo;
import com.starry.admin.modules.clerk.service.IPlayClerkDataReviewInfoService;
import com.starry.admin.modules.clerk.service.IPlayClerkUserInfoService;
import com.starry.common.utils.IdUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * 店员资料审核Service业务层处理
 *
 * @author admin
 * @since 2024-05-19
 */
@Service
public class PlayClerkDataReviewInfoServiceImpl extends ServiceImpl<PlayClerkDataReviewInfoMapper, PlayClerkDataReviewInfoEntity> implements IPlayClerkDataReviewInfoService {
    @Resource
    private PlayClerkDataReviewInfoMapper playClerkDataReviewInfoMapper;

    @Resource
    private IPlayClerkUserInfoService playClerkUserInfoService;


    /**
     * 查询店员资料审核
     * @param id 店员资料审核主键
     * @return 店员资料审核
     */
    @Override
    public PlayClerkDataReviewInfoEntity selectPlayClerkDataReviewInfoById(String id) {
        LambdaQueryWrapper<PlayClerkDataReviewInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlayClerkDataReviewInfoEntity::getId, id);
        PlayClerkDataReviewInfoEntity entity = this.baseMapper.selectOne(queryWrapper);
        if (entity == null) {
            throw new CustomException("对象不存在");
        }
        return entity;
    }

    @Override
    public List<PlayClerkDataReviewInfoEntity> queryByClerkId(String clerkId) {
        LambdaQueryWrapper<PlayClerkDataReviewInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlayClerkDataReviewInfoEntity::getClerkId, clerkId);
        return this.baseMapper.selectList(queryWrapper);
    }


    @Override
    public PlayClerkDataReviewInfoEntity queryByClerkId(String clerkId, String dataType, String reviewState) {
        LambdaQueryWrapper<PlayClerkDataReviewInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PlayClerkDataReviewInfoEntity::getClerkId, clerkId);
        queryWrapper.eq(PlayClerkDataReviewInfoEntity::getReviewState, reviewState);
        queryWrapper.eq(PlayClerkDataReviewInfoEntity::getDataType, dataType);
        return this.baseMapper.selectOne(queryWrapper);
    }

    /**
     * 查询店员资料审核列表
     * @param vo 店员资料审核
     * @return 店员资料审核
     */
    @Override
    public IPage<PlayClerkDataReviewReturnVo> selectByPage(PlayClerkDataReviewQueryVo vo) {
        MPJLambdaWrapper<PlayClerkDataReviewInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(PlayClerkDataReviewInfoEntity.class);
        //店员表
        lambdaQueryWrapper.selectAs(PlayClerkUserInfoEntity::getAvatar, "clerkAvatar").selectAs(PlayClerkUserInfoEntity::getNickname, "clerkNickname");
        lambdaQueryWrapper.leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId, PlayClerkUserReviewInfoEntity::getClerkId);
        if (StrUtil.isNotBlank(vo.getClerkId())) {
            lambdaQueryWrapper.eq(PlayClerkDataReviewInfoEntity::getClerkId, vo.getClerkId());
        }
        if (StrUtil.isNotBlank(vo.getDataType())) {
            lambdaQueryWrapper.eq(PlayClerkDataReviewInfoEntity::getDataType, vo.getDataType());
        }
        if (StrUtil.isNotBlank(vo.getReviewState())) {
            lambdaQueryWrapper.like(PlayClerkDataReviewInfoEntity::getReviewState, vo.getReviewState());
        }
        if (vo.getAddTime() != null && vo.getAddTime().size() == 2) {
            lambdaQueryWrapper.between(PlayClerkDataReviewInfoEntity::getAddTime, vo.getAddTime().get(0), vo.getAddTime().get(1));
        }
        return this.baseMapper.selectJoinPage(new Page<>(vo.getPageNum(), vo.getPageSize()), PlayClerkDataReviewReturnVo.class, lambdaQueryWrapper);
    }

    /**
     * 新增店员资料审核
     * @param playClerkDataReviewInfo 店员资料审核
     * @return 结果
     */
    @Override
    public boolean create(PlayClerkDataReviewInfoEntity playClerkDataReviewInfo) {
        if (StrUtil.isBlankIfStr(playClerkDataReviewInfo.getId())) {
            playClerkDataReviewInfo.setId(IdUtils.getUuid());
        }
        if (StrUtil.isBlankIfStr(playClerkDataReviewInfo.getAddTime())) {
            playClerkDataReviewInfo.setAddTime(LocalDateTime.now());
        }
        return save(playClerkDataReviewInfo);
    }

    @Override
    public void updateDataReviewState(PlayClerkDataReviewStateEditVo vo) {
        PlayClerkDataReviewInfoEntity entity = this.selectPlayClerkDataReviewInfoById(vo.getId());
        BeanUtils.copyProperties(vo, entity);
        this.update(entity);
        if ("1".equals(vo.getReviewState())) {
            PlayClerkUserInfoEntity userInfo = new PlayClerkUserInfoEntity();
            userInfo.setId(entity.getClerkId());
            if ("1".equals(entity.getDataType())) {
                userInfo.setAvatar(entity.getDataContent().get(0));
            }
            if ("2".equals(entity.getDataType())) {
                userInfo.setAlbum(entity.getDataContent());
            }
            if ("3".equals(entity.getDataType())) {
                userInfo.setAudio(entity.getDataContent().get(0));
            }
            playClerkUserInfoService.update(userInfo);
        }
    }

    /**
     * 修改店员资料审核
     * @param playClerkDataReviewInfo 店员资料审核
     * @return 结果
     */
    @Override
    public boolean update(PlayClerkDataReviewInfoEntity playClerkDataReviewInfo) {
        return updateById(playClerkDataReviewInfo);
    }

    /**
     * 批量删除店员资料审核
     * @param ids 需要删除的店员资料审核主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkDataReviewInfoByIds(String[] ids) {
        return playClerkDataReviewInfoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除店员资料审核信息
     * @param id 店员资料审核主键
     * @return 结果
     */
    @Override
    public int deletePlayClerkDataReviewInfoById(String id) {
        return playClerkDataReviewInfoMapper.deleteById(id);
    }
}
