package com.eleadmin.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.oa.entity.Customer;
import com.eleadmin.oa.entity.CustomerApply;
import com.eleadmin.oa.server.CustomerApplyService;
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;

/**
 * 申请免费体验
 *
 * @author gxwebsoft.com
 * @since 2020-03-14 11:29:03
 */
@Api(tags = "申请免费体验")
@RestController
@RequestMapping("/api/customer-apply")
public class CustomerApplyController extends BaseController {
    @Resource
    private CustomerApplyService customerApplyService;

    @PreAuthorize("hasAuthority('sys:customer:apply')")
    @ApiOperation("申请免费体验")
    @PostMapping()
    public ApiResult<?> apply(@RequestBody CustomerApply customerApply) {
      customerApply.setCustomerType("1");
      if (customerApplyService.count(new LambdaQueryWrapper<CustomerApply>()
        .eq(CustomerApply::getCustomerName, customerApply.getCustomerName())) > 0) {
        return fail("客户名称已存在");
      }
      if (customerApplyService.save(customerApply)) {
        return success("申请成功，请耐心等待工作人员与您联系！");
      }
      return fail("申请失败");
    }

}
