package com.kxs.product.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kxs.common.core.constant.CacheConstants;
import com.kxs.common.core.constant.enums.MachineTypeEnum;
import com.kxs.common.security.util.SecurityUtils;
import com.kxs.product.api.model.KxsBrand;
import com.kxs.product.api.model.KxsMachine;
import com.kxs.product.api.vo.kxsapp.brand.BrandListVO;
import com.kxs.product.biz.constant.ProductConstants;
import com.kxs.product.biz.mapper.KxsBrandMapper;
import com.kxs.product.biz.mapper.KxsMachineMapper;
import com.kxs.product.biz.service.KxsBrandService;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 客小爽品牌表(KxsBrand)表服务实现类
 *
 * @author 系统
 * @since 2023-12-20 10:09:35
 */
@Service("kxsBrandService")
@RequiredArgsConstructor
public class KxsBrandServiceImpl extends ServiceImpl<KxsBrandMapper, KxsBrand> implements KxsBrandService {

    private final KxsMachineMapper kxsMachineMapper;

    @Override
//    @CacheEvict(value = ProductConstants.BRAND_DETAILS, key = "'list'")
    public Boolean sortByList(List<Integer> sorts) {

        for (int i = 0; i < sorts.size(); i++) {

            LambdaUpdateWrapper<KxsBrand> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(KxsBrand::getId, sorts.get(i)).set(KxsBrand::getSort, i);

            baseMapper.update(null, updateWrapper);
        }
        return Boolean.TRUE;
    }

    @Override
//    @Cacheable(value = ProductConstants.BRAND_DETAILS, key = "'list'", unless = "#result.isEmpty()")
    public List<BrandListVO> getSortByList() {

        Long userId = SecurityUtils.getUser().getId();

        List<BrandListVO> brandList = baseMapper.getSortByList();
        for (BrandListVO brandListVO : brandList) {
            Long totalCount = kxsMachineMapper.selectCount(Wrappers.<KxsMachine>lambdaQuery()
                    .eq(KxsMachine::getUserId, userId)
                    .eq(KxsMachine::getBrandId, brandListVO.getId()));
            brandListVO.setTotalMachineCount(totalCount == null ? 0L : totalCount);

            Long unBindCount = kxsMachineMapper.selectCount(Wrappers.<KxsMachine>lambdaQuery()
                    .eq(KxsMachine::getUserId, userId)
                    .eq(KxsMachine::getBindStatus, MachineTypeEnum.NO_BIND.getType())
                    .eq(KxsMachine::getBrandId, brandListVO.getId()));
            brandListVO.setUnBindCount(unBindCount == null ? 0L : unBindCount);

            Long bindCount = kxsMachineMapper.selectCount(Wrappers.<KxsMachine>lambdaQuery()
                    .eq(KxsMachine::getUserId, userId)
                    .eq(KxsMachine::getBindStatus, MachineTypeEnum.BIND.getType())
                    .eq(KxsMachine::getBrandId, brandListVO.getId()));
            brandListVO.setBindCount(bindCount == null ? 0L : bindCount);

        }


        return brandList;
    }








}

