package com.cyksj.service.mange.business.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.mapper.manage.distribute.UserDistributeBusinessRateMapper;
import com.cyksj.mapper.manage.distribute.UserDistributeMapper;
import com.cyksj.model.entity.UserDistribute;
import com.cyksj.model.entity.UserDistributeBusinessRate;
import com.cyksj.model.views.CmsUserVO;
import com.cyksj.model.views.GoodsDonSkuView;
import com.cyksj.service.mange.business.UserBusinessRateService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

/*
 *项目名: netflix
 *文件名: UserBusinessRateServiceImpl
 *创建者: JavaZou
 *创建时间:2023/3/23 14:02
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class UserBusinessRateServiceImpl implements UserBusinessRateService {
	private final UserDistributeBusinessRateMapper userDistributeBusinessRateMapper;

	private final BeanSearcher beanSearcher;

	private final UserDistributeMapper userDistributeMapper;

	@Override
	public void saveBusinessRate(Long businessId) {
		if (businessId != null) {
			List<GoodsDonSkuView> goodsDonSkuList = beanSearcher.searchAll(GoodsDonSkuView.class, MapUtils.builder().field(GoodsDonSkuView::getType, 1)
					.field(GoodsDonSkuView::getGoodsDonDeleted, true)
					.field(GoodsDonSkuView::getSkuId).op(Operator.NotNull)
					.build());
			goodsDonSkuList.forEach(goodsDonSku -> {
				Long goodsId = goodsDonSku.getGoodsId();
				Long skuId = goodsDonSku.getSkuId();
				UserDistributeBusinessRate businessRate = userDistributeBusinessRateMapper.selectOne(Wrappers.lambdaQuery(UserDistributeBusinessRate.class)
						.eq(UserDistributeBusinessRate::getGoodsId, goodsId)
						.eq(UserDistributeBusinessRate::getSkuId, skuId)
						.eq(UserDistributeBusinessRate::getBusinessId, businessId).last("limit 1"));
				if (businessRate == null) {
					try {
						businessRate = new UserDistributeBusinessRate();
						businessRate.setBusinessId(businessId);
						businessRate.setGoodsId(goodsId);
						businessRate.setSkuId(skuId);
						userDistributeBusinessRateMapper.insert(businessRate);
					} catch (DuplicateKeyException e) {

					}
				}
			});
		}
	}

	@Override
	public Boolean isBusiness(Long businessId) {
		//是否是商务人员
		CmsUserVO isBusiness = beanSearcher.searchFirst(CmsUserVO.class, MapUtils.builder().field(CmsUserVO::getId, businessId).field(CmsUserVO::getRoleNames, "商务").op(Operator.Contain).build());
		if (isBusiness != null) {
			return true;
		}
		return false;
	}

	@Override
	public void deleteBusinessRateByUid(Long businessId) {
		userDistributeBusinessRateMapper.delete(Wrappers.lambdaQuery(UserDistributeBusinessRate.class)
				.eq(UserDistributeBusinessRate::getBusinessId, businessId));

		userDistributeMapper.update(null, Wrappers.lambdaUpdate(UserDistribute.class)
				.set(UserDistribute::getBusinessId, 0)
				.eq(UserDistribute::getBusinessId, businessId));
	}
}
