package com.platform.service.custom;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.platform.dao.SysUserBrandDao;
import com.platform.dao.custom.MySysUserBrandDao;
import com.platform.entity.SysUserBrand;
import com.platform.enums.IdStarterEnum;
import com.platform.service.SysUserBrandService;
import com.platform.service.impl.SysUserBrandServiceImpl;

import com.platform.utils.UUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

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

/**
 * 2 * @Author: Abner
 * 3 * @Date: 2020/11/20 14:34
 * 4
 */

@Slf4j
@Service
public class MySysUserBrandService extends SysUserBrandServiceImpl {

    @Autowired
    private MySysUserBrandDao mySysUserBrandDao;



    public int addRelation(SysUserBrand sysUserBrand){
        if(null == sysUserBrand){
            return -1;
        }
        if(StringUtils.isBlank(sysUserBrand.getRelationId())){
            sysUserBrand.setRelationId(UUidGenerator.generatorUuid(IdStarterEnum.DEFAULT_ID.getStarter()));
        }
        sysUserBrand.setCreateTime(LocalDateTime.now());
        sysUserBrand.setUpdateTime(LocalDateTime.now());
        sysUserBrand.setEnable(1);
        return getBaseMapper().insert(sysUserBrand);
    }


    public int updateSysUserBrand(SysUserBrand sysUserBrand){
        if(null == sysUserBrand || StringUtils.isBlank(sysUserBrand.getRelationId())){
            return -1;
        }
        UpdateWrapper<SysUserBrand> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("relation_id" , sysUserBrand.getRelationId());
        updateWrapper.last("limit 1");
        sysUserBrand.setUpdateTime(LocalDateTime.now());
        return getBaseMapper().update(sysUserBrand , updateWrapper);
    }

    public SysUserBrand getUserBrandById(Long userId , Integer brandId){
        QueryWrapper<SysUserBrand> queryWrapper = new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(userId)){
            queryWrapper.eq("user_id" , userId);
        }
        queryWrapper.eq("brand_id" , brandId);
        queryWrapper.eq("enable" , 1);
        return getBaseMapper().selectOne(queryWrapper);
    }

    public List<SysUserBrand> getBrandBindUserIdList(long brandId){

        return mySysUserBrandDao.getBrandBindUserIdList(brandId);
    }

    public List<String> getBindUserNameList(long brandId){

        return  mySysUserBrandDao.getBindUserName(brandId);
    }


    public List<Long> queryBrandIdList(Long userId) {
        return mySysUserBrandDao.queryBrandIdList(userId);
    }


    public List<SysUserBrand> queryList(Long userId) {
        if(null == userId){
            return new ArrayList<>();
        }
        List<SysUserBrand> brands = mySysUserBrandDao.queryList(userId);
        return brands;
    }

    public void reBindUser(Long fromUserId,Long userId ,String phone) {
        if ( ObjectUtils.isEmpty(fromUserId)) {
            throw new RuntimeException("参数有误！");
        }

        UpdateWrapper<SysUserBrand> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("user_id",userId);
        updateWrapper.set("phone_num",phone);
        updateWrapper.eq("user_id" , fromUserId);
        updateWrapper.eq("enable" , 1);
        update(updateWrapper);
    }

    public void deleteByBrandIds(Integer[] ids) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("brand_id",ids);
        remove(wrapper);
    }

    public void deleteByUserId(Long userId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        remove(wrapper);
    }

    public void unbindUserByBrandId(Integer brandId,List<Long> userIdList) {
        mySysUserBrandDao.unbindUserByBrandId(brandId,userIdList);
    }
}
