package me.zhengjie.modules.company.rest;

import java.util.Set;

import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.company.domain.Company;
import me.zhengjie.modules.company.service.CompanyService;
import me.zhengjie.modules.company.service.criteria.CompanyQueryCriteria;
import me.zhengjie.modules.company.service.dto.CompanyDto;
import me.zhengjie.utils.PageResult;

@Api(tags = "基础资料维护：公司管理")
@RestController
@RequestMapping("/api/companies")
@RequiredArgsConstructor
public class CompanyController {
    private final CompanyService companyService;

    @ApiOperation("查询公司")
    @GetMapping
    @PreAuthorize("@el.check('company:list')")
    public ResponseEntity<PageResult<CompanyDto>> queryCompany(CompanyQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(companyService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @Log("新增公司")
    @ApiOperation("新增公司")
    @PostMapping
    @PreAuthorize("@el.check('company:add')")
    public ResponseEntity<Object> createCompany(@Validated @RequestBody Company resources) {
        companyService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("修改公司")
    @ApiOperation("修改公司")
    @PutMapping
    @PreAuthorize("@el.check('company:edit')")
    public ResponseEntity<Object> updateCompany(@Validated(Company.Update.class) @RequestBody Company resources)
            throws Exception {
        companyService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除公司")
    @ApiOperation("删除公司")
    @DeleteMapping
    @PreAuthorize("@el.check('company:del')")
    public ResponseEntity<Object> deleteCompany(@RequestBody Set<Long> ids) {
        companyService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
