package com.skr.mdm.api.company.controller;

import com.skr.mdm.api.company.service.CompanyService;
import com.skr.mdm.dto.LoginAdminDTO;
import com.skr.mdm.result.ResponseResult;
import com.skr.mdm.bean.qo.CompanyQO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author nfq
 * @date 2020/9/16 10:42
 */
@RestController
@RequestMapping("company")
@Api(tags = "公司管理")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    @PostMapping("add")
    @ApiOperation(value = "创建公司")
    public ResponseResult addCompany(@RequestBody CompanyQO companyQO) {
       return companyService.addCompany(companyQO);
    }

    @PostMapping("list")
    @ApiOperation(value = "公司列表")
    public ResponseResult listCompany(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                      @RequestParam(required = false) Integer searchType,
                                      @RequestParam(required = false) String searchValue) {
        return companyService.listCompany(pageNum, pageSize, searchType, searchValue);
    }

    @PostMapping("update")
    @ApiOperation(value = "编辑公司")
    public ResponseResult updateCompany(@RequestBody CompanyQO companyQO) {
        return companyService.updateCompany(companyQO);
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除公司")
    public ResponseResult deleteCompany(@RequestParam Integer companyId) {
        return companyService.deleteCompany(companyId);
    }

    @PostMapping("index")
    @ApiOperation(value = "配置/获取公司首页模块配置")
    public ResponseResult indexConfig(@RequestParam(required = false) String indexConfig, @ApiIgnore Authentication authentication) {
        LoginAdminDTO loginAdminDTO = (LoginAdminDTO) authentication.getPrincipal();
        if (StringUtils.isNotBlank(indexConfig)) {
            return companyService.setCompanyIndexConfig(indexConfig, loginAdminDTO);
        } else {
            return companyService.getCompanyIndexConfig(loginAdminDTO);
        }
    }
}