package shop.alien.lawyer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.alien.entity.result.R;
import shop.alien.entity.store.LifeUser;
import shop.alien.entity.store.StoreImg;
import shop.alien.entity.store.StoreOfficialAlbum;
import shop.alien.entity.store.vo.StoreImgTypeVo;
import shop.alien.lawyer.service.StoreImgService;
import shop.alien.mapper.LifeUserMapper;
import shop.alien.mapper.StoreImgMapper;
import shop.alien.mapper.StoreOfficialAlbumMapper;


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

/**
 * 二期-门店图片 服务实现类
 *
 * @author ssk
 * @since 2024-12-05
 */
@Transactional
@Service
@RequiredArgsConstructor
public class StoreImgServiceImpl extends ServiceImpl<StoreImgMapper, StoreImg> implements StoreImgService {

    private final StoreImgMapper storeImgMapper;
    private final StoreOfficialAlbumMapper storeOfficialAlbumMapper;
    private final LifeUserMapper lifeUserMapper;

    /**
     * 获取门店图片
     *
     * @param storeId 门店id
     * @param imgType 图片类型, 0:其他, 1:入口图, 2:相册, 3:菜品, 4:环境, 5:价目表, 6:推荐菜, 7:菜单, 8:用户评论, 9:商家申诉, 10:商家头像, 11:店铺轮播图
     * @return list
     */
    @Override
    public List<StoreImg> getStoreImg(Integer storeId, Integer imgType) {
        LambdaQueryWrapper<StoreImg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreImg::getStoreId, storeId).eq(StoreImg::getImgType, imgType).orderByAsc(StoreImg::getImgSort);
        return this.list(lambdaQueryWrapper);
    }

    /**
     * 获取所有图片类型和数量
     *
     * @param storeId 门店id
     * @return StoreImgTypeVo
     */
    @Override
    public List<StoreImgTypeVo> getStoreImgTypeCount(Integer storeId) {
        List<StoreImgTypeVo> storeImgTypeVoList = storeImgMapper.getStoreImgTypeCount(storeId);
        for (StoreImgTypeVo storeImgTypeVo : storeImgTypeVoList) {
            //图片类型, 0:其他, 1:入口图, 2:相册, 3:菜品, 4:环境, 5:价目表, 6:推荐菜, 7:菜单, 8:用户评论, 9:商家申诉, 10:商家头像, 11:店铺轮播图
            switch (storeImgTypeVo.getImgType()) {
                case 0:
                    storeImgTypeVo.setImgTypeStr("其他");
                    break;
                case 3:
                    storeImgTypeVo.setImgTypeStr("菜品");
                    break;
                case 4:
                    storeImgTypeVo.setImgTypeStr("环境");
                    break;
                case 5:
                    storeImgTypeVo.setImgTypeStr("价目表");
                    break;
            }
        }
        return storeImgTypeVoList.stream().sorted(Comparator.comparing(StoreImgTypeVo::getImgType)).collect(Collectors.toList());
    }

    /**
     * 获取轮播图
     *
     * @return List<StoreImg>
     */
    @Override
    public List<StoreImg> getCarouselImage() {
        return this.list(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getImgType, 11));
    }

    @Override
    public List<String> getUrl(String id, Integer imgType) {
        QueryWrapper qw = new QueryWrapper<StoreImg>();
        qw.eq(Objects.nonNull(id), "store_id", id).
                eq(Objects.nonNull(imgType), "img_type", imgType);
        List<StoreImg> resDb = storeImgMapper.selectList(qw);
        return resDb.stream().map(StoreImg::getImgUrl).collect(Collectors.toList());

    }

    /**
     * 获取用户打卡广场小人图片
     *
     * @param userId 用户ID，用于标识和获取特定用户的信息
     * @return
     */
    @Override
    public R<String> getUserClockImg(Integer userId) {
        // 查询用户信息
        LifeUser lifeUser = lifeUserMapper.selectById(userId);

        // 判断用户是否存在
        if (ObjectUtils.isNotEmpty(lifeUser)) {
            // 检查用户是否设置了自定义打卡图片
            if (ObjectUtils.isNotEmpty(lifeUser.getClockImgId())) {
                // 查询用户设置的自定义图片
                StoreImg storeImg = storeImgMapper.selectById(lifeUser.getClockImgId());
                // 返回自定义图片URL或提示素材缺失
                if (ObjectUtils.isNotEmpty(storeImg)) {
                    return R.data(storeImg.getImgUrl());
                } else {
                    return R.fail("素材缺失");
                }
            } else {
                /*// 查询默认打卡图片
                StoreImg storeImg = storeImgMapper.selectOne(new QueryWrapper<StoreImg>().eq("img_type", 17).orderByAsc("img_sort").last("limit 1"));
                // 设置默认图片给用户并返回图片URL或提示素材缺失
                if (ObjectUtils.isNotEmpty(storeImg)) {
                    lifeUser.setClockImgId(storeImg.getId());
                    lifeUserMapper.updateById(lifeUser);
                    return R.data(storeImg.getImgUrl());
                } else {
                    return R.fail("素材缺失");
                }*/
                return R.data("");
            }
        } else {
            // 提示用户不存在
            return R.fail("用户不存在");
        }
    }

    /**
     * 通过businessId获取图片
     *
     * @param storeId 门店id
     * @param imgType 图片类型, 0:其他, 1:入口图, 2:相册, 3:菜品, 4:环境, 5:价目表, 6:推荐菜, 7:菜单, 8:用户评论, 9:商家申诉, 10:商家头像, 11:店铺轮播图
     * @param businessId 业务ID
     * @return list
     */
    @Override
    public List<StoreImg> getByBusinessId(Integer storeId, Integer imgType, Integer businessId) {
        LambdaQueryWrapper<StoreImg> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreImg::getStoreId, storeId)
                .eq(StoreImg::getImgType, imgType)
                .eq(StoreImg::getBusinessId, businessId).orderByAsc(StoreImg::getImgSort);
        List<StoreImg> storeImgList = this.list(lambdaQueryWrapper);
        int imgCount = storeImgList.size();
        LambdaUpdateWrapper<StoreOfficialAlbum> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(StoreOfficialAlbum::getId, businessId).set(StoreOfficialAlbum::getImgCount, imgCount);
        storeOfficialAlbumMapper.update(null, updateWrapper);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public int saveOrUpdateImg(Integer storeId, Integer imgType) {
        int result = 0;
        if(storeId !=null && storeId > 0 && imgType != null){
            result = storeImgMapper.delete(new LambdaQueryWrapper<StoreImg>().eq(StoreImg::getStoreId, storeId).eq(StoreImg::getImgType, imgType));
        }
        return result;
    }
}
