package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.LifeBrandedCard;
import shop.alien.entity.store.LifeCoupon;
import shop.alien.entity.store.StoreImg;
import shop.alien.entity.store.StoreInfo;
import shop.alien.entity.store.vo.LifeBrandedCardVo;
import shop.alien.entity.store.vo.LifeCouponVo;
import shop.alien.mapper.LifeBrandedCardMapper;
import shop.alien.mapper.LifeCouponMapper;
import shop.alien.mapper.StoreImgMapper;
import shop.alien.mapper.StoreInfoMapper;
import shop.alien.store.service.PlatformBrandedCardService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class PlatformBrandedCardServiceImpl implements PlatformBrandedCardService {

    private final LifeBrandedCardMapper lifeBrandedCardMapper;

    private final LifeCouponMapper lifeCouponMapper;


    private final StoreImgMapper storeImgMapper;

    private final StoreInfoMapper storeInfoMapper;

    @Override
    public IPage<LifeBrandedCard> getBrandedCardList(Integer page, Integer size, String name, String status) {
        LambdaQueryWrapper<LifeBrandedCard> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), LifeBrandedCard::getName, name);
        wrapper.eq(StringUtils.isNotEmpty(status), LifeBrandedCard::getStatus, status);
        wrapper.orderByDesc(LifeBrandedCard::getCreatedTime);
        return lifeBrandedCardMapper.selectPage(new Page<>(page, size), wrapper);
    }

    @Override
    public int removeShelf(Integer id) {
        LambdaUpdateWrapper<LifeBrandedCard> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(LifeBrandedCard::getId, id);
        wrapper.set(LifeBrandedCard::getStatus, 1);
        return lifeBrandedCardMapper.update(null, wrapper);
    }

    @Override
    public int putawayShelf(Integer id) {
        LambdaUpdateWrapper<LifeBrandedCard> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(LifeBrandedCard::getId, id);
        wrapper.set(LifeBrandedCard::getStatus, 0);
        return lifeBrandedCardMapper.update(null, wrapper);
    }

    @Override
    public int deleteById(Integer id) {
        return lifeBrandedCardMapper.deleteById(id);
    }

    @Override
    public List<StoreInfo> getStoreList(String storeName) {
        LambdaUpdateWrapper<StoreInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(storeName), StoreInfo::getStoreName, storeName);
        return storeInfoMapper.selectList(wrapper);
    }

    @Override
    public List<LifeCouponVo> getQuanListByStoreId(String storeIds) {
        String[] split = storeIds.split(",");
        LambdaUpdateWrapper<LifeCoupon> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(LifeCoupon::getStoreId, split);
        List<LifeCoupon> lifeCoupons = lifeCouponMapper.selectList(wrapper);
        List<LifeCouponVo> lifeCouponVos = new ArrayList<>();
        for (LifeCoupon lifeCoupon : lifeCoupons) {
            LifeCouponVo lifeCouponVo = new LifeCouponVo();
            BeanUtils.copyProperties(lifeCoupon, lifeCouponVo);
            StoreInfo storeInfo = storeInfoMapper.selectById(lifeCoupon.getStoreId());
            lifeCouponVo.setStoreName(storeInfo.getStoreName());
            lifeCouponVos.add(lifeCouponVo);
        }
        return lifeCouponVos;
    }

    @Override
    public boolean saveBrandedCard(LifeBrandedCardVo brandedCard) {
        LifeBrandedCard lifeBrandedCard = new LifeBrandedCard();
        BeanUtils.copyProperties(brandedCard, lifeBrandedCard);
        List<String> detailsImages = brandedCard.getDetailsImages();
        String detailsImagesStr = String.join(",", detailsImages);
        lifeBrandedCard.setDetailsImagesStr(detailsImagesStr);
        //存首页图片
        StoreImg storeImgBanner = new StoreImg();
        storeImgBanner.setStoreId(1);
        storeImgBanner.setImgType(12);
        storeImgBanner.setImgSort(0);
        storeImgBanner.setImgUrl(brandedCard.getImages());
        storeImgMapper.insert(storeImgBanner);
        for (int i = 0; i < detailsImages.size(); i++) {
            StoreImg storeImg = new StoreImg();
            storeImg.setStoreId(1);
            storeImg.setImgType(12);
            storeImg.setImgSort(i + 1);
            storeImg.setImgUrl(detailsImages.get(i));
            storeImgMapper.insert(storeImg);
        }

        // 套餐
        LambdaUpdateWrapper<LifeCoupon> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(LifeCoupon::getId, Arrays.asList(lifeBrandedCard.getQuanIds().split(",")));
        List<LifeCoupon> quanList = lifeCouponMapper.selectList(wrapper);
        Set<String> storeIds = quanList.stream().map(LifeCoupon::getStoreId).collect(Collectors.toSet());
        lifeBrandedCard.setStoreQty(storeIds.size());
        int insert = lifeBrandedCardMapper.insert(lifeBrandedCard);
        return true;
    }

    @Override
    public LifeBrandedCardVo getBrandedCardDetail(Integer id) {
        // 联名卡
        LifeBrandedCard card = lifeBrandedCardMapper.selectById(id);
        LifeBrandedCardVo cardVo = new LifeBrandedCardVo();
        BeanUtils.copyProperties(card, cardVo);
        // 套餐
        LambdaUpdateWrapper<LifeCoupon> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(LifeCoupon::getId, Arrays.asList(card.getQuanIds().split(",")));
        List<LifeCoupon> quanList = lifeCouponMapper.selectList(wrapper);

        // 商家
        Set<String> storeIds = quanList.stream().map(LifeCoupon::getStoreId).collect(Collectors.toSet());
        LambdaUpdateWrapper<StoreInfo> storeWrapper = new LambdaUpdateWrapper<>();
        storeWrapper.in(StoreInfo::getId, storeIds);
        List<StoreInfo> storeInfoList = storeInfoMapper.selectList(storeWrapper);

        // 图片
//        LambdaUpdateWrapper<StoreImg> imgWrapper = new LambdaUpdateWrapper<>();
//        wrapper.in(LifeCoupon::getId, Arrays.asList(card.getImages().split(";")));
//        imgWrapper.eq(StoreImg::getImgType, 12);
//        imgWrapper.orderByAsc(StoreImg::getImgSort);
//        List<StoreImg> storeImgList = storeImgMapper.selectList(imgWrapper);

        cardVo.setName(card.getName());
        cardVo.setTotalQty(card.getTotalQty());
        cardVo.setCouponNameList(quanList.stream().map(LifeCoupon::getName).collect(Collectors.toList()));
        cardVo.setStoreNameList(storeInfoList.stream().map(StoreInfo::getStoreName).collect(Collectors.toList()));
        String images = card.getImages();
        List<String> imagesList = Arrays.asList(images.split(","));
        cardVo.setImageUrlList(imagesList);
        String detailImages = card.getDetailsImagesStr();
        List<String> detailImagesList = Arrays.asList(images.split(","));
        cardVo.setDetailsImages(detailImagesList);
        return cardVo;
    }
}
