package com.dxhy.admin.sys.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.admin.sys.service.SellerUserService;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.R;

import cn.hutool.core.codec.Base64;
import lombok.extern.slf4j.Slf4j;

/**
 * 市场统计页面 用户绑定集团
 *
 * @author fth
 */
@RestController
@RequestMapping("/sys/userToCompany")
@Slf4j
public class SellerStatUserController extends AbstractController {

    @Resource
    private SellerUserService sellerUserService;

    /**
     * 所有用户列表
     */
    @RequestMapping("/list")
    public ResponseEntity<?> list(@RequestBody Map<String, Object> params) {
        JSONObject obj = sellerUserService.getUserTitle();
        PageUtils page = sellerUserService.queryPage(params);

        obj.put("datalist", page.getList());
        obj.put("total", page.getTotalCount());
        return ResponseEntity.ok(R.ok().put("data", Base64.encode(obj.toJSONString())));
    }

    /**
     * 点击绑定，查询未绑定的集团信息
     */
    @RequestMapping("/userNotBindCompany")
    public ResponseEntity<?> userAndCompany(@RequestBody Map<String, Object> params) {
        JSONObject obj = sellerUserService.getCompanyTitle();
        PageUtils page = sellerUserService.queryNoBindCompany(params);
        obj.put("datalist", page.getList());
        obj.put("total", page.getTotalCount());
        return ResponseEntity.ok(R.ok().put("data", Base64.encode(obj.toJSONString())));
    }

    /**
     * 勾选，绑定集团信息
     */
    @RequestMapping("/bindCompany")
    public ResponseEntity<?> bindCompany(@RequestBody Map<String, Object> params) {
        boolean isOk = sellerUserService.bindCompany(params);
        return ResponseEntity.ok(R.ok().put("data", isOk));
    }

    /**
     * 点击已绑定，查询已绑定的集团信息
     */
    @RequestMapping("/userBindedCompany")
    public ResponseEntity<?> userBindedCompany(@RequestBody Map<String, Object> params) {
        JSONObject obj = sellerUserService.getCompanyTitle();
        PageUtils page = sellerUserService.queryBindCompany(params);
        obj.put("datalist", page.getList());
        obj.put("total", page.getTotalCount());
        return ResponseEntity.ok(R.ok().put("data", Base64.encode(obj.toJSONString())));
    }

    /**
     * 勾选，取消绑定集团信息
     */
    @RequestMapping("/cancelBindCompany")
    public ResponseEntity<?> cancelBindCompany(@RequestBody Map<String, Object> params) {
        boolean isOk = sellerUserService.cancelBindCompany(params);
        return ResponseEntity.ok(R.ok().put("data", isOk));
    }
}
