package cn.com.countrygarden.bi.main.controller.marketing.common;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.dup.DupOrgan;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysOrgan;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser;
import cn.com.countrygarden.bi.main.service.dup.DupCommService;
import cn.com.countrygarden.bi.main.service.twerp.hm.TbSysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

@Controller("commonCommController")
@RequestMapping("/api/biapp/common/comm")
public class CommController {

    @Autowired
    private TbSysUserService userService;

    @Autowired
    private DupCommService dupCommService;

    /**
     * 获取权限内区域、项目列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result comm() {
        TbSysUser sysUser = userService.getCurrentSysUser();
        if (userService == null) {
            return Response.response();
        }
        List<Map<String, Object>> response = new ArrayList<>();
        List<DupOrgan> dupOrgans = dupCommService.findDupCommByUserCode(
                sysUser.getUserCode(),
                null,
                false,
                false
        );
        for (DupOrgan dupOrgan : dupOrgans) {
            response.add(new HashMap<String, Object>() {{
                put("organCode", dupOrgan.getOrganCode());
                put("organName", dupOrgan.getOrganName());
            }});
        }
        return Response.response(response);
    }
    /**
     * 获取权限内区域、项目列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "all", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result commByAll() {
        TbSysUser sysUser = userService.getCurrentSysUser();
        if (userService == null) {
            return Response.response();
        }
        List<Map<String, Object>> response = new ArrayList<>();
        List<DupOrgan> dupOrgans = dupCommService.findDupCommByUserCode(
                sysUser.getUserCode(),
                null,
                false,
                false
        );
        for (DupOrgan dupOrgan : dupOrgans) {
            response.add(new HashMap<String, Object>() {{
                put("organCode", dupOrgan.getOrganCode());
                put("organName", dupOrgan.getOrganName());
            }});
        }
        return Response.response(response);
    }
    /**
     * 所有
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "allComm", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    public Result allComm() {
       
        List<Map<String, Object>> response = new ArrayList<>();
        List<DupOrgan> dupOrgans = dupCommService.listAllComm();
        for (DupOrgan dupOrgan : dupOrgans) {
            response.add(new HashMap<String, Object>() {{
                put("organCode", dupOrgan.getOrganCode());
                put("organName", dupOrgan.getOrganName());
            }});
        }
        return Response.response(response);
    }
}
