package com.platform.service.custom;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.entity.ApiTmBrandAdminVo;
import com.platform.entity.BrandVo;
import com.platform.enums.IdStarterEnum;
import com.platform.service.ApiBrandService;
import com.platform.service.ApiUserService;
import com.platform.service.impl.ApiTmBrandAdminServiceImpl;
import com.platform.utils.UUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author abnerhou
 * @date 2020/9/18 15:39
 * @desciption
 */
@Service
@Slf4j
public class MyApiTmBrandAdminService extends ApiTmBrandAdminServiceImpl {

    @Autowired
    private ApiBrandService apiBrandService;

    public List<Map<String ,Object>> getUserBrand(long adminId){
        QueryWrapper<ApiTmBrandAdminVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id" , adminId);
        queryWrapper.eq("enable" , 1);
        List<ApiTmBrandAdminVo> relations = getBaseMapper().selectList(queryWrapper);
        final List<Map<String ,Object>> result = new ArrayList<>();
        if(!CollectionUtils.isEmpty(relations)){
            relations.forEach(brandAdmin ->{
                //TODO:这里要改成连表查询
                BrandVo brandVo = apiBrandService.queryObject(brandAdmin.getBrandId());
                if(null != brandVo){
                    Map<String ,Object> mp = new HashMap<>();
                    mp.put("brandId" , brandVo.getId());
                    mp.put("brandName" , brandVo.getName());
                    result.add(mp);
                }
            });
        }
        return result;
    }

    public ApiTmBrandAdminVo getByAdminIdAndBrandId(int adminId , int brandId){

        QueryWrapper<ApiTmBrandAdminVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id" , adminId);
        queryWrapper.eq("brand_id" , brandId);
        queryWrapper.eq("enable" , 1);
        queryWrapper.last("limit 1");
        return getBaseMapper().selectOne(queryWrapper);
    }

    public long countUserRelation(int adminId){
        QueryWrapper<ApiTmBrandAdminVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id" , adminId);
        queryWrapper.eq("enable" , 1);
        IPage<ApiTmBrandAdminVo> page = new Page<>(1 , 10);
        IPage<ApiTmBrandAdminVo> resultPage = new Page<>();
        resultPage = getBaseMapper().selectPage(page , queryWrapper);
        if(null == resultPage){
            return 0;
        }
        return resultPage.getTotal();
    }
}
