package com.sqx.modules.activity.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.SpringContextUtils;
import com.sqx.modules.activity.dao.ActivityGoodsDao;
import com.sqx.modules.activity.dto.ActivityGoodsDTO;
import com.sqx.modules.activity.entity.ActivityGoods;
import com.sqx.modules.activity.entity.ActivityShop;
import com.sqx.modules.activity.service.ActivityGoodsService;
import com.sqx.modules.activity.service.ActivityShopService;
import com.sqx.modules.datacentre.entity.SysUserShop;
import com.sqx.modules.sys.entity.SysUserEntity;
import com.sqx.modules.sys.service.SysUserShopService;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 活动商品
 * @author : codingliang
 * @date : 2024-6-17
 */
@Service
@RequiredArgsConstructor
public class ActivityGoodsServiceImpl extends ServiceImpl<ActivityGoodsDao, ActivityGoods> implements ActivityGoodsService {

    private final SysUserShopService sysUserShopService;

    private ActivityShopService getActivityShopService() {
        return SpringContextUtils.applicationContext.getBean(ActivityShopService.class);
    }

    @Transactional
    @Override
    public void updateActivityGoods(ActivityGoodsDTO activityGoodsDTO) {
        // 检查权限
        checkAuth(activityGoodsDTO);

        // 删除活动商家原有商品
        deleteByActivityShopId(activityGoodsDTO.getActivityShopId());

        ActivityGoods activityGoods = new ActivityGoods();
        activityGoods.setActivityShopId(activityGoodsDTO.getActivityShopId());
        activityGoods.setGoodsIds(activityGoodsDTO.getGoodsIds().stream().map(String::valueOf).collect(Collectors.joining(",")));

        this.save(activityGoods);
    }

    @Override
    public void deleteByActivityShopId(Long activityShopId) {
        LambdaQueryWrapper<ActivityGoods> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ActivityGoods::getActivityShopId, activityShopId);
        this.remove(queryWrapper);
    }

    @Override
    public List<Long> getByActivityShopId(Long activityShopId) {
        LambdaQueryWrapper<ActivityGoods> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ActivityGoods::getActivityShopId, activityShopId);

        ActivityGoods activityGoods = this.getOne(queryWrapper);
        if (ObjectUtil.isNull(activityGoods)) {
            return Collections.emptyList();
        }
        return Arrays.stream(activityGoods.getGoodsIds().split(",")).map(Long::new).collect(Collectors.toList());
    }

    private void checkAuth(ActivityGoodsDTO activityGoodsDTO) {
        ActivityShop activityShop = getActivityShopService().getById(activityGoodsDTO.getActivityShopId());

        if (ObjectUtil.isNull(activityShop)) {
            throw new SqxException("无效的activityShopId");
        }

        SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        // 1平台管理员、2商户管理员
        Integer userType = user.getUserType();
        if (userType.intValue() == 2) {
            Long curShopId = activityShop.getShopId();
            SysUserShop userShop = sysUserShopService.getByUserId(user.getUserId());
            if (ObjectUtil.notEqual(curShopId, userShop.getShopId())) {
                throw new SqxException("权限不足：商户管理员只能添加自己店铺的商品到活动中");
            }
        }
    }
}
