package com.platform.controller;

import com.alibaba.fastjson.JSONObject;
import com.platform.entity.Result;
import com.platform.entity.SysUserBrand;
import com.platform.service.custom.MySysUserBrandService;
import com.platform.service.impl.ZhiHouseService;
import com.platform.utils.PageUtilsPlus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 系统用户
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2016年10月31日 上午10:40:10
 */
@RestController
@RequestMapping("/sys/user")
@Slf4j
public class SysUserController extends AbstractController {

    @Autowired
    private MySysUserBrandService mySysUserBrandService;

    @Autowired
    private ZhiHouseService zhiHouseService;

    /**
     * 店铺绑定的用户列表
     */
    @PostMapping("/bindList")
    public Result userBindList(@RequestBody JSONObject param) {
        // 查询用户信息
        JSONObject result = zhiHouseService.getSysUserListPageByDeptIdsAndUserName(getDeptId(),
                param.getLong("page"),param.getLong("limit"), param.getString("username"));
        List<Object> users = result.getJSONArray("records");
        if(!ObjectUtils.isEmpty(users)){
            users.stream().map(object->JSONObject.parseObject(JSONObject.toJSONString(object))).forEach(user->{
                // 查找直播间数据
                SysUserBrand userBrand = mySysUserBrandService.getUserBrandById(user.getLong("id"), param.getInteger("brandId"));
                if(!ObjectUtils.isEmpty(userBrand.getCanShow())){
                    user.put("canShow",userBrand.getCanShow());
                }
            });
        }
        // 处理场景数据
        PageUtilsPlus pageResult = PageUtilsPlus.page(users, result.getLong("totalCount"), param.getLong("limit"), param.getLong("page"));
        return Result.success(pageResult);
    }
}
