package shop.alien.store.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import shop.alien.entity.store.LifeBrandedCard;
import shop.alien.entity.store.LifeCoupon;
import shop.alien.entity.store.vo.StoreInfoVo;
import shop.alien.mapper.LifeBrandedCardMapper;
import shop.alien.mapper.LifeCouponMapper;
import shop.alien.mapper.StoreInfoMapper;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 联名卡
 */
@Service
@RequiredArgsConstructor
public class LifeBrandedCardService {

    private final LifeCouponMapper lifeCouponMapper;

    private final LifeBrandedCardMapper lifeBrandedCardMapper;

    private final StoreInfoService storeInfoService;

    private final StoreInfoMapper storeInfoMapper;

    public int addOrUpdateBrandedCard(LifeBrandedCard card) {
        Date now = new Date();
        if (StringUtils.isEmpty(card.getId())) {
            card.setOnTime(now);
            card.setCreatedTime(now);
            card.setUpdatedTime(now);
            card.setStatus(0);
            card.setRemainQty(card.getTotalQty());
            return lifeBrandedCardMapper.insert(card);
        } else {
            LifeBrandedCard oldCard = lifeBrandedCardMapper.selectById(card.getId());
            int remainQtyOld = oldCard.getRemainQty();
            int totalQtyOld = oldCard.getTotalQty();
            card.setUpdatedTime(now);
            card.setRemainQty(card.getTotalQty() < remainQtyOld ? card.getTotalQty() : remainQtyOld + (card.getTotalQty() - totalQtyOld));
            return lifeBrandedCardMapper.updateById(card);
        }
    }

    public List<StoreInfoVo> getStoreList(String storeName) {
        QueryWrapper<StoreInfoVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!storeName.isEmpty(), "a.store_name", storeName).eq("a.delete_flag", 0);
        return storeInfoMapper.getStoreInfoVoList(queryWrapper);
    }

    public List<LifeCoupon> getQuanListByStoreId(List<String> storeIds) {
        LambdaUpdateWrapper<LifeCoupon> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(LifeCoupon::getStoreId, storeIds);
        return lifeCouponMapper.selectList(wrapper);
    }

    public Map<String, Object> getBrandedCardDetail(String cardId) {
        Map<String, Object> returnMap = new HashedMap<>();
        LifeBrandedCard card = lifeBrandedCardMapper.selectById(cardId);
        String quanIds = card.getQuanIds();
        String[] itemsArray = quanIds.split(";");
        List<String> quanIdList = Arrays.asList(itemsArray);
        LambdaUpdateWrapper<LifeCoupon> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(LifeCoupon::getId, quanIdList);
        List<LifeCoupon> quanList = lifeCouponMapper.selectList(wrapper);
        Set<String> storeIds = quanList.stream()
                .map(LifeCoupon::getStoreId)
                .collect(Collectors.toSet());
        List<String> storeNames = new ArrayList<>();
        for (String id : storeIds) {
            String storeName = storeInfoService.getById(id).getStoreName();
            storeNames.add(storeName);
        }
        returnMap.put("xuanzhongStores", storeNames);
        List<String> quanNames = new ArrayList<>();
        for (LifeCoupon quan : quanList) {
            String quanName = quan.getName();
            quanNames.add(quanName);
        }
        returnMap.put("xuanzhongQuans", quanNames);
        returnMap.put("card", card);
        return returnMap;
    }

    public int updateCardStatus(String id, String status) {
        int ret = 0;
        if (status.equals("2") || Integer.parseInt(status) == 2) {
            ret = lifeBrandedCardMapper.deleteById(id);
        } else {
            LambdaUpdateWrapper<LifeBrandedCard> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(LifeBrandedCard::getId, id).set(LifeBrandedCard::getStatus, status);
            ret = lifeBrandedCardMapper.update(null, wrapper);
        }
        return ret;
    }

    public Map<String, Object> getBrandedCardList(int page, int size) {
        Map<String, Object> returnMap = new HashMap<>();
        List<Map<String, Object>> brandedCardList = new ArrayList<>();
        IPage<LifeBrandedCard> brandedPage = new Page<>(page, size);
        IPage<LifeBrandedCard> brandedCards = lifeBrandedCardMapper.selectPage(brandedPage, new LambdaUpdateWrapper<>());
        for (LifeBrandedCard card : brandedCards.getRecords()) {
            Map<String, Object> cardMap = new HashedMap<>();
            cardMap.put("id", card.getId());
            cardMap.put("cardName", card.getName());
            cardMap.put("toufangQty", card.getTotalQty());
            cardMap.put("buyQty", card.getTotalQty() - card.getRemainQty());
            cardMap.put("remainQty", card.getRemainQty());
            cardMap.put("storeQty", card.getStoreQty());
            cardMap.put("onTime", card.getOnTime());
            cardMap.put("status", card.getStatus());
            brandedCardList.add(cardMap);
        }
        returnMap.put("data", brandedCardList);
        returnMap.put("total", lifeBrandedCardMapper.selectCount(new LambdaUpdateWrapper<>()));
        returnMap.put("current", brandedPage.getCurrent());
        returnMap.put("size", brandedPage.getSize());
        return returnMap;
    }
}