package com.springboot.cloud.sysadmin.organization.rest;

/*
 **********************************************
 *      DATE           PERSON       REASON
 *    2020-12-29          FXY        Created
 **********************************************
 */

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.sysadmin.organization.exception.UserNotFoundException;
import com.springboot.cloud.sysadmin.organization.service.ApplicationService;
import com.springboot.cloud.sysadmin.organization.service.CompanyService;
import lombok.extern.slf4j.Slf4j;
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.util.Optional;

@RestController
@RequestMapping("/company")
@Slf4j
public class CompanyController {

    @Autowired
    CompanyService companyService;

    @Autowired
    ApplicationService applicationService;

    @GetMapping("/getPopedom")
    public Result getCompanyRoleUserPopedom(@RequestParam("companyId") String companyId, @RequestParam("username") String username) {
        Optional.ofNullable(companyId).orElseThrow(UserNotFoundException::new);
        Optional.ofNullable(username).orElseThrow(UserNotFoundException::new);
        return Result.success(companyService.getCompanyUserPopedom(companyId, username));
    }

    @GetMapping("/getCompanyAllPopedom")
    public Result getCompanyAllPopedom(@RequestParam("companyId") String companyId) {
        Optional.ofNullable(companyId).orElseThrow(UserNotFoundException::new);
        return Result.success(companyService.getCompanyAllPopedom(companyId));
    }

    @RequestMapping("/license")
    public Result license(@RequestParam("companyId") String companyId, @RequestParam("applicationCode") String applicationCode) {
        Optional.ofNullable(companyId).orElseThrow(UserNotFoundException::new);
        Optional.ofNullable(applicationCode).orElseThrow(UserNotFoundException::new);
        return Result.success(applicationService.license(companyId, applicationCode));
    }

    @GetMapping("/getAllPopedom")
    public Result getAllPopedom() {
        return Result.success(companyService.getAllPopedom());
    }

    @GetMapping("/getUserAllCompany")
    public Result getUserAllCompany(@RequestParam("userId") String userId){
        Optional.ofNullable(userId).orElseThrow(UserNotFoundException::new);
        return Result.success(companyService.getUserAllCompany(userId));
    }


//    @GetMapping("/getPopedom")
//    public Result getCompanyRoleUserPopedom() {
//        return Result.success(companyService.getCompanyUserPopedom(UserContextHolder.getInstance().getCurrentCompany(), UserContextHolder.getInstance().getUsername()));
//    }
//
//    @GetMapping("/getCompanyAllPopedom")
//    public Result getCompanyAllPopedom() {
//        return Result.success(companyService.getCompanyAllPopedom(UserContextHolder.getInstance().getCurrentCompany()));
//    }
//
//    @RequestMapping("/license")
//    public Result license(@RequestParam("applicationCode") String applicationCode) {
//        return Result.success(applicationService.license(UserContextHolder.getInstance().getCurrentCompany(), applicationCode));
//    }
//
//    @GetMapping("/getAllPopedom")
//    public Result getAllPopedom() {
//        return Result.success(companyService.getAllPopedom());
//    }
//
//    @GetMapping("/getUserAllCompany")
//    public Result getUserAllCompany(@RequestParam("userId") String userId) {
//        Optional.ofNullable(userId).orElseThrow(UserNotFoundException::new);
//        return Result.success(companyService.getUserAllCompany(userId));
//    }

}
