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

import cn.hutool.core.collection.CollUtil;
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.PlayClerkUserReviewInfoMapper;
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.PlayClerkUserReviewQueryVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkUserReviewReturnVo;
import com.starry.admin.modules.clerk.module.vo.PlayClerkUserReviewStateEditVo;
import com.starry.admin.modules.clerk.service.IPlayClerkCommodityService;
import com.starry.admin.modules.clerk.service.IPlayClerkUserInfoService;
import com.starry.admin.modules.clerk.service.IPlayClerkUserReviewInfoService;
import com.starry.admin.modules.order.module.constant.OrderConstant;
import com.starry.admin.modules.weichat.service.NotificationSender;
import com.starry.common.utils.IdUtils;
import java.util.Arrays;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 店员资料审核Service业务层处理
 *
 * @author admin
 * @since 2024-05-19
 */
@Service
public class PlayClerkUserReviewInfoServiceImpl
        extends
            ServiceImpl<PlayClerkUserReviewInfoMapper, PlayClerkUserReviewInfoEntity>
        implements
            IPlayClerkUserReviewInfoService {

    @Resource
    private PlayClerkUserReviewInfoMapper playClerkUserReviewInfoMapper;
    @Resource
    private IPlayClerkUserInfoService playClerkUserInfoService;
    @Resource
    private IPlayClerkCommodityService clerkCommodityService;
    @Resource
    private NotificationSender notificationSender;

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

    /**
     * 查询店员资料审核
     *
     * @param id
     *            店员资料审核主键
     * @return 店员资料审核
     */
    @Override
    public PlayClerkUserReviewInfoEntity selectPlayClerkUserReviewInfoById(String id) {
        PlayClerkUserReviewInfoEntity entity = this.baseMapper.selectById(id);
        if (entity == null) {
            throw new CustomException("对象不存在");
        }
        return entity;
    }

    @Override
    public IPage<PlayClerkUserReviewReturnVo> selectByPage(PlayClerkUserReviewQueryVo vo) {
        MPJLambdaWrapper<PlayClerkUserReviewInfoEntity> lambdaQueryWrapper = new MPJLambdaWrapper<>();
        lambdaQueryWrapper.selectAll(PlayClerkUserReviewInfoEntity.class);
        // 店员表
        lambdaQueryWrapper.selectAs(PlayClerkUserInfoEntity::getAvatar, "clerkAvatar")
                .selectAs(PlayClerkUserInfoEntity::getSignature, "signature");
        lambdaQueryWrapper.leftJoin(PlayClerkUserInfoEntity.class, PlayClerkUserInfoEntity::getId,
                PlayClerkUserReviewInfoEntity::getClerkId);
        if (StrUtil.isNotBlank(vo.getClerkId())) {
            lambdaQueryWrapper.eq(PlayClerkUserReviewInfoEntity::getClerkId, vo.getClerkId());
        }
        if (StrUtil.isNotBlank(vo.getSex())) {
            lambdaQueryWrapper.eq(PlayClerkUserReviewInfoEntity::getSex, vo.getSex());
        }
        if (StrUtil.isNotBlank(vo.getNickname())) {
            lambdaQueryWrapper.like(PlayClerkUserReviewInfoEntity::getNickname, vo.getNickname());
        }
        if (StrUtil.isNotBlank(vo.getPhone())) {
            lambdaQueryWrapper.like(PlayClerkUserReviewInfoEntity::getPhone, vo.getPhone());
        }
        if (StrUtil.isNotBlank(vo.getReviewState())) {
            lambdaQueryWrapper.eq(PlayClerkUserReviewInfoEntity::getReviewState, vo.getReviewState());
        }
        if (StrUtil.isNotBlank(vo.getWeiChatCode())) {
            lambdaQueryWrapper.like(PlayClerkUserReviewInfoEntity::getWeiChatCode, vo.getWeiChatCode());
        }
        if (CollUtil.isNotEmpty(vo.getAddTime())) {
            if (vo.getAddTime().size() >= 2) {
                lambdaQueryWrapper.between(PlayClerkUserReviewInfoEntity::getAddTime, vo.getAddTime().get(0),
                        vo.getAddTime().get(1));
            } else if (vo.getAddTime().size() == 1) {
                lambdaQueryWrapper.ge(PlayClerkUserReviewInfoEntity::getAddTime, vo.getAddTime().get(0));
            }
        }
        // 加入组员的筛选
        // List<String> clerkIdList =
        // playClerkGroupInfoService.getValidClerkIdList(SecurityUtils.getLoginUser(),
        // null);
        // lambdaQueryWrapper.in(PlayClerkUserInfoEntity::getId, clerkIdList);
        lambdaQueryWrapper.orderByDesc(PlayClerkUserReviewInfoEntity::getAddTime);
        lambdaQueryWrapper.orderByDesc(PlayClerkUserReviewInfoEntity::getClerkId);
        return this.baseMapper.selectJoinPage(new Page<>(vo.getPageNum(), vo.getPageSize()),
                PlayClerkUserReviewReturnVo.class, lambdaQueryWrapper);
    }

    /**
     * 查询店员资料审核列表
     *
     * @param playClerkUserReviewInfo
     *            店员资料审核
     * @return 店员资料审核
     */
    @Override
    public IPage<PlayClerkUserReviewInfoEntity> selectPlayClerkUserReviewInfoByPage(
            PlayClerkUserReviewInfoEntity playClerkUserReviewInfo) {
        Page<PlayClerkUserReviewInfoEntity> page = new Page<>(1, 10);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<>());
    }

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

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

    /**
     * 修改店员资料审核
     *
     * @param vo
     *            店员资料审核对象
     */
    @Override
    public void updateDataReviewState(PlayClerkUserReviewStateEditVo vo) {
        PlayClerkUserReviewInfoEntity entity = this.selectPlayClerkUserReviewInfoById(vo.getId());
        PlayClerkUserInfoEntity userInfo = playClerkUserInfoService.selectById(entity.getClerkId());
        if ("1".equals(vo.getReviewState())) {
            BeanUtils.copyProperties(entity, userInfo);
            userInfo.setClerkState("1");
            userInfo.setId(entity.getClerkId());
            userInfo.setAlbum(entity.getAlbum());
            if (StrUtil.isNotBlank(vo.getGroupId())) {
                userInfo.setGroupId(vo.getGroupId());
            }
            if(OrderConstant.Gender.UNKNOWN.getCode().equals(entity.getSex())){
                userInfo.setSex(OrderConstant.Gender.FEMALE.getCode());
            }
            playClerkUserInfoService.update(userInfo);
            clerkCommodityService.initClerkCommodity(userInfo.getId());
        }
        BeanUtils.copyProperties(vo, entity);
        this.update(entity);

        // 发送消息
        notificationSender.sendCheckMessage(entity, userInfo, vo.getReviewState());
    }

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

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