package com.gxwebsoft.common.system.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.Company;
import com.gxwebsoft.common.system.entity.Tenant;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.param.CompanyParam;
import com.gxwebsoft.common.system.service.CompanyService;
import com.gxwebsoft.common.system.service.FileRecordService;
import com.gxwebsoft.common.system.service.TenantService;
import com.gxwebsoft.common.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 企业信息控制器
 *
 * @author 科技小王子
 * @since 2023-05-27 14:57:34
 */
@Api(tags = "企业信息管理")
@RestController
@RequestMapping("/api/system/company")
public class CompanyController extends BaseController {
    @Resource
    private CompanyService companyService;
    @Resource
    private TenantService tenantService;
    @Resource
    private UserService userService;
    @Resource
    private FileRecordService fileRecordService;

    @PreAuthorize("hasAuthority('sys:company:list')")
    @OperationLog
    @ApiOperation("分页查询企业信息")
    @GetMapping("/page")
    public ApiResult<PageResult<Company>> page(CompanyParam param) {
        // 使用关联查询
        return success(companyService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:company:list')")
    @OperationLog
    @ApiOperation("查询全部企业信息")
    @GetMapping()
    public ApiResult<List<Company>> list(CompanyParam param) {
        // 使用关联查询
        return success(companyService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:company:list')")
    @OperationLog
    @ApiOperation("根据id查询企业信息")
    @GetMapping("/{id}")
    public ApiResult<Company> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(companyService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('sys:company:profile')")
    @OperationLog
    @ApiOperation("添加企业信息")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Company company) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          company.setUserId(loginUser.getUserId());
        }
        if (companyService.save(company)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:company:profile')")
    @OperationLog
    @ApiOperation("修改企业信息")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Company company) {
      final int count = companyService.count();
      if (companyService.updateById(company)) {
            return success("修改成功");
        }
        return fail("修改失败",count);
    }

    @PreAuthorize("hasAuthority('sys:company:remove')")
    @OperationLog
    @ApiOperation("删除企业信息")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (companyService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:company:profile')")
    @OperationLog
    @ApiOperation("批量添加企业信息")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Company> list) {
        if (companyService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:company:update')")
    @OperationLog
    @ApiOperation("批量修改企业信息")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Company> batchParam) {
        if (batchParam.update(companyService, "company_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:company:remove')")
    @OperationLog
    @ApiOperation("批量删除企业信息")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (companyService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:company:profile')")
    @ApiOperation("根据id查询企业信息")
    @GetMapping("/profile")
    public ApiResult<Company> profile() {
      final CompanyParam param = new CompanyParam();
      param.setTenantId(getTenantId());
      param.setAuthoritative(true);
      final List<Company> list = companyService.listRel(param);
      if(!CollectionUtils.isEmpty(list)){
        return success(list.get(0));
      }
      return fail("企业不存在",null);
    }
}
