package com.huyi.service.common.company.controller;

import com.huyi.service.base.entity.SysCompany;
import com.huyi.service.base.service.ISysCompanyService;
import com.huyi.service.common.company.service.CompanyService;
import com.tianhu.common.core.domain.R;
import com.tianhu.common.core.utils.CommonUtil;
import com.tianhu.common.core.web.controller.BaseController;
import com.tianhu.common.core.web.domain.AjaxResult;
import com.tianhu.common.security.service.TokenService;
import com.tianhu.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 企业controller
 *
 * @author ch
 * @date 2021-8-16
 */

@RestController
@RequestMapping("/company")
public class companyController extends BaseController {
    @Autowired
    private TokenService tokenService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private ISysCompanyService iSysCompanyService;

    /**
     * 根据类型获取公司
     * @return
     */
    @GetMapping("/list/{scyType}/{scyId}")
    public AjaxResult list(@PathVariable String scyType,@PathVariable String scyId) {
        Map map = new HashMap();
        map.put("scyType",scyType);
        if (CommonUtil.isNotEmpty(scyId)) {
            map.put("scyId", scyId);
        }
        List<Map> list = companyService.selectCompanyList(map);
        return AjaxResult.success(list);
    }

    /**
     * 获取自己企业
     * @return
     */
    @GetMapping("/getOwnCompany")
    public R getOwnCompany()
    {
        //获取当前操作员
        LoginUser user = tokenService.getLoginUser();
        String companyId = user.getSysUser().getCompanyId();
        SysCompany sysCompany = iSysCompanyService.getById(companyId);
        return R.ok(sysCompany);
    }

    /**
     * 查询链属列表
     * @param companyName   企业名称
     * @return
     */
    @GetMapping("/companyRelList")
    public R companyRelList(String companyName)
    {
        //获取当前操作员
        LoginUser user = tokenService.getLoginUser();
        String companyId = user.getSysUser().getCompanyId();
        Map map = new HashMap();
        map.put("companyId",companyId);
        if(CommonUtil.isNotEmpty(companyName)) {
            map.put("companyName", companyName);
        }
        List<Map> list = companyService.selectCompanyRelList(map);
        return R.ok(list,companyId);
    }
}
