package com.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.platform.entity.BrandEntity;
import com.platform.entity.Result;
import com.platform.entity.SysUserBrand;
import com.platform.service.BrandService;
import com.platform.service.custom.MySysUserBrandService;
import com.platform.service.impl.ZhiHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/zfb")
public class ZfbController {

    @Autowired
    private MySysUserBrandService mySysUserBrandService;

    @Autowired
    private BrandService brandService;


    @Autowired
    private ZhiHouseService zhiHouseService;

    @PostMapping("/deleteStaff")
    public Result deleteStaff(@RequestBody JSONObject param) {
        Long userId = param.getLong("userId");
        Long toUserId = param.getLong("toUserId");
        String toUserPone = param.getString("toUserPone");
        // 获取直播间绑定信息
        List<Long> bindUsers = mySysUserBrandService.queryBrandIdList(userId);
        if (!ObjectUtils.isEmpty(bindUsers)) {
           mySysUserBrandService.reBindUser(userId,toUserId,toUserPone);
        }
        return Result.success();
    }
    @PostMapping("/getCanShow")
    public Result getCanShow(@RequestBody JSONObject param) {
        Object userIds = param.get("userIds");
        List<Integer> jsonArray = (List<Integer>) userIds;
        Integer brandId = (Integer) param.get("brandId");
        HashMap<Long,Integer> resultMap = new HashMap<>();
        for (Integer userId : jsonArray) {
            SysUserBrand userBrand = mySysUserBrandService.getUserBrandById(Long.valueOf(userId), brandId);
            if(userBrand!=null){
                resultMap.put(userBrand.getUserId(),userBrand.getCanShow());
            }
        }
        return Result.success(resultMap);
    }
    @PostMapping("/queryBrandBySceneNums")
    public Result queryBrandBySceneNums(@RequestBody JSONObject param) {
        Object nums = param.get("nums");
        List<String> jsonArray = (List<String>) nums;

        HashMap<String,Integer> resultMap = new HashMap<>();
        for (String sceneNum : jsonArray) {
            BrandEntity brand = brandService.queryByScene(sceneNum);
            Integer lived = 0;
            if(brand != null){
                lived = 1;
            }
            resultMap.put(sceneNum,lived);
        }
        return Result.success(resultMap);
    }

    @PostMapping("/queryBrandByUserId")
    public Result queryBrandByUserId(@RequestBody JSONObject param) {
        Long  userId = param.getLong("userId");
        if(userId == null){
            return Result.failure("用户id为空");
        }
        List<Long> longs = mySysUserBrandService.queryBrandIdList(userId);
        return  Result.success(longs.size());
    }

    @PostMapping("/deleteBrandUser")
    public Result deleteBrandUser(@RequestBody JSONObject param) {
        Long  userId = param.getLong("userId");
        if(userId == null){
            return Result.failure("用户id为空");
        }
        mySysUserBrandService.deleteByUserId(userId);
        return  Result.success();
    }



}
