package com.gxwebsoft.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.cms.entity.Form;
import com.gxwebsoft.cms.entity.FormRecord;
import com.gxwebsoft.cms.param.FormRecordParam;
import com.gxwebsoft.cms.service.FormRecordService;
import com.gxwebsoft.cms.service.FormService;
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 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 科技小王子
 * @since 2024-03-14 08:59:33
 */
@Api(tags = "表单数据")
@RestController
@RequestMapping("/api/cms/form-record")
public class FormRecordController extends BaseController {
    @Resource
    private FormRecordService formRecordService;
    @Resource
    private FormService formService;

    @ApiOperation("分页查询表单数据记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<FormRecord>> page(FormRecordParam param) {
        // 使用关联查询
        return success(formRecordService.pageRel(param));
    }

    @ApiOperation("查询全部表单数据记录表")
    @GetMapping()
    public ApiResult<List<FormRecord>> list(FormRecordParam param) {
        // 使用关联查询
        return success(formRecordService.listRel(param));
    }

    @ApiOperation("根据id查询表单数据记录表")
    @GetMapping("/{id}")
    public ApiResult<FormRecord> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(formRecordService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:form:save')")
    @ApiOperation("添加表单数据记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody FormRecord formRecord) {
      final Form form = formService.getById(formRecord.getFormId());
      if (form.getSubmitNumber().equals(1)) {
        if (formRecordService.count(new LambdaQueryWrapper<FormRecord>().eq(FormRecord::getStatus,0).eq(FormRecord::getPhone,formRecord.getPhone())) > 0) {
          return fail("请勿重复提交");
        }
      }
      if (formRecordService.save(formRecord)) {
            return success("提交成功");
        }
        return fail("提交失败");
    }

    @PreAuthorize("hasAuthority('cms:form:update')")
    @ApiOperation("修改表单数据记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody FormRecord formRecord) {
        if (formRecordService.updateById(formRecord)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:form:remove')")
    @ApiOperation("删除表单数据记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (formRecordService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:form:save')")
    @ApiOperation("批量添加表单数据记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<FormRecord> list) {
        if (formRecordService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:form:update')")
    @ApiOperation("批量修改表单数据记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<FormRecord> batchParam) {
        if (batchParam.update(formRecordService, "form_record_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:form:remove')")
    @ApiOperation("批量删除表单数据记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (formRecordService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
