package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.alien.entity.store.LawyerImg;
import shop.alien.entity.store.vo.LawyerImgTypeVo;
import shop.alien.mapper.LawyerImgMapper;
import shop.alien.store.service.LawyerImgService;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 律师图片 服务实现类
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Transactional
@Service
@RequiredArgsConstructor
public class LawyerImgServiceImpl extends ServiceImpl<LawyerImgMapper, LawyerImg> implements LawyerImgService {

    private final LawyerImgMapper lawyerImgMapper;

    /**
     * 获取律师图片
     *
     * @param lawyerId 律师ID
     * @param imgType  图片类型, 0:其他, 1:头像, 2:执业证照片, 3:身份证正面, 4:身份证反面, 5:案例图片, 6:相册图片, 7:资质证书, 8:荣誉证书, 9:工作照片
     * @return list
     */
    @Override
    public List<LawyerImg> getLawyerImg(Integer lawyerId, Integer imgType) {
        log.info("LawyerImgServiceImpl.getLawyerImg?lawyerId={}&imgType={}", lawyerId, imgType);
        LambdaQueryWrapper<LawyerImg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LawyerImg::getLawyerId, lawyerId)
                .eq(LawyerImg::getImgType, imgType)
                .orderByAsc(LawyerImg::getImgSort);
        return this.list(lambdaQueryWrapper);
    }

    /**
     * 获取所有图片类型和数量
     *
     * @param lawyerId 律师ID
     * @return LawyerImgTypeVo
     */
    @Override
    public List<LawyerImgTypeVo> getLawyerImgTypeCount(Integer lawyerId) {
        log.info("LawyerImgServiceImpl.getLawyerImgTypeCount?lawyerId={}", lawyerId);
        List<LawyerImgTypeVo> lawyerImgTypeVoList = lawyerImgMapper.getLawyerImgTypeCount(lawyerId);
        for (LawyerImgTypeVo lawyerImgTypeVo : lawyerImgTypeVoList) {
            //图片类型, 0:其他, 1:头像, 2:执业证照片, 3:身份证正面, 4:身份证反面, 5:案例图片, 6:相册图片, 7:资质证书, 8:荣誉证书, 9:工作照片
            switch (lawyerImgTypeVo.getImgType()) {
                case 0:
                    lawyerImgTypeVo.setImgTypeStr("其他");
                    break;
                case 1:
                    lawyerImgTypeVo.setImgTypeStr("头像");
                    break;
                case 2:
                    lawyerImgTypeVo.setImgTypeStr("执业证照片");
                    break;
                case 3:
                    lawyerImgTypeVo.setImgTypeStr("身份证正面");
                    break;
                case 4:
                    lawyerImgTypeVo.setImgTypeStr("身份证反面");
                    break;
                case 5:
                    lawyerImgTypeVo.setImgTypeStr("案例图片");
                    break;
                case 6:
                    lawyerImgTypeVo.setImgTypeStr("相册图片");
                    break;
                case 7:
                    lawyerImgTypeVo.setImgTypeStr("资质证书");
                    break;
                case 8:
                    lawyerImgTypeVo.setImgTypeStr("荣誉证书");
                    break;
                case 9:
                    lawyerImgTypeVo.setImgTypeStr("工作照片");
                    break;
            }
        }
        return lawyerImgTypeVoList.stream().sorted(Comparator.comparing(LawyerImgTypeVo::getImgType)).collect(Collectors.toList());
    }

    @Override
    public List<String> getUrl(String id, Integer imgType) {
        log.info("LawyerImgServiceImpl.getUrl?id={}&imgType={}", id, imgType);
        QueryWrapper<LawyerImg> qw = new QueryWrapper<>();
        qw.eq(Objects.nonNull(id), "lawyer_id", id)
                .eq(Objects.nonNull(imgType), "img_type", imgType);
        List<LawyerImg> resDb = lawyerImgMapper.selectList(qw);
        return resDb.stream().map(LawyerImg::getImgUrl).collect(Collectors.toList());
    }

    /**
     * 通过businessId获取图片
     *
     * @param lawyerId  律师ID
     * @param imgType   图片类型
     * @param businessId 业务ID
     * @return list
     */
    @Override
    public List<LawyerImg> getByBusinessId(Integer lawyerId, Integer imgType, Integer businessId) {
        log.info("LawyerImgServiceImpl.getByBusinessId?lawyerId={}&imgType={}&businessId={}", lawyerId, imgType, businessId);
        LambdaQueryWrapper<LawyerImg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(LawyerImg::getLawyerId, lawyerId)
                .eq(LawyerImg::getImgType, imgType)
                .eq(LawyerImg::getBusinessId, businessId)
                .orderByAsc(LawyerImg::getImgSort);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public int saveOrUpdateImg(Integer lawyerId, Integer imgType) {
        log.info("LawyerImgServiceImpl.saveOrUpdateImg?lawyerId={}&imgType={}", lawyerId, imgType);
        int result = 0;
        if (lawyerId != null && lawyerId > 0 && imgType != null) {
            result = lawyerImgMapper.delete(new LambdaQueryWrapper<LawyerImg>()
                    .eq(LawyerImg::getLawyerId, lawyerId)
                    .eq(LawyerImg::getImgType, imgType));
        }
        return result;
    }
}

