package com.gxwebsoft.oa.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.oa.entity.Customer;
import com.gxwebsoft.oa.param.CustomerParam;
import com.gxwebsoft.oa.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 客户管理记录表控制器
 *
 * @author WebSoft
 * @since 2022-11-16 11:16:14
 */
@Api(tags = "客户管理记录表管理")
@RestController
@RequestMapping("/api/oa/customer")
public class CustomerController extends BaseController {
    @Resource
    private CustomerService customerService;

    @PreAuthorize("hasAuthority('oa:customer:list')")
    @OperationLog
    @ApiOperation("分页查询客户管理记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<Customer>> page(CustomerParam param) {
        // 使用关联查询
        return success(customerService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('oa:customer:list')")
    @OperationLog
    @ApiOperation("查询全部客户管理记录表")
    @GetMapping()
    public ApiResult<List<Customer>> list(CustomerParam param) {
        // 使用关联查询
        return success(customerService.listRel(param));
    }

    @PreAuthorize("hasAuthority('oa:customer:list')")
    @OperationLog
    @ApiOperation("根据id查询客户管理记录表")
    @GetMapping("/{id}")
    public ApiResult<Customer> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(customerService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('oa:customer:save')")
    @OperationLog
    @ApiOperation("添加客户管理记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Customer customer) {
        // 记录当前登录用户id、租户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          customer.setUserId(loginUser.getUserId());
        }
        if (customerService.save(customer)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:customer:update')")
    @OperationLog
    @ApiOperation("修改客户管理记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Customer customer) {
        if (customerService.updateById(customer)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:customer:remove')")
    @OperationLog
    @ApiOperation("删除客户管理记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (customerService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('oa:customer:save')")
    @OperationLog
    @ApiOperation("批量添加客户管理记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Customer> list) {
        if (customerService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('oa:customer:update')")
    @OperationLog
    @ApiOperation("批量修改客户管理记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Customer> batchParam) {
        if (batchParam.update(customerService, "customer_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('oa:customer:remove')")
    @OperationLog
    @ApiOperation("批量删除客户管理记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (customerService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
