package com.hwrj.cloud.admin.controller;

import com.hwrj.cloud.admin.model.UmsAdmin;
import com.hwrj.cloud.admin.model.UmsCompanyInfo;
import com.hwrj.cloud.admin.service.UmsAdminService;
import com.hwrj.cloud.admin.service.UmsCompanyService;
import com.hwrj.cloud.admin.service.impl.UmsCompanyInfoServiceImpl;
import com.hwrj.cloud.common.api.CommonPage;
import com.hwrj.cloud.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Authorization;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@Api(tags = "通过数据权限获取")
@RequestMapping(value = "/comp")
public class CompanyToCompanyController {
    @Autowired
    private UmsCompanyService umsCompanyService;

    @Autowired
    private UmsAdminService adminService;

    @ApiOperation("通用性获取供应商")
    @GetMapping("/search")
    public CommonResult searchCompany(@RequestParam(required = false,defaultValue = "")String key,@RequestParam(defaultValue = "1")int pageNo,@RequestParam(defaultValue = "15")int pageSize, Principal principal){
        if (principal == null){
            return CommonResult.unauthorized(null);
        }
        String username = principal.getName();
        UmsAdmin umsAdmin = adminService.getAdminByUsername(username);
        if(umsAdmin == null){
            return CommonResult.unauthorized(null);
        }
        Long compId = umsAdmin.getCompId();
        int type = 0;
        if (compId == null){
            type = 1;
        }
        List<UmsCompanyInfo> infos = umsCompanyService.searchCompanyInfo(key, pageNo, pageSize, compId);
        CommonPage<UmsCompanyInfo> infoCommonPage = CommonPage.restPage(infos);
        Map<String,Object> map = new HashMap<>();
        map.put("type",type);
        map.put("data",infoCommonPage);
        return CommonResult.success(map);
    }
}
