package com.gxwebsoft.cms.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.cms.entity.Form;
import com.gxwebsoft.cms.param.FormParam;
import com.gxwebsoft.cms.service.FormService;
import com.gxwebsoft.common.core.security.JwtUtil;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.utils.RequestUtil;
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.shop.service.MerchantService;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 表单设计表控制器
 *
 * @author 科技小王子
 * @since 2024-03-14 08:59:33
 */
@Api(tags = "表单设计")
@RestController
@RequestMapping("/api/cms/form")
public class FormController extends BaseController {
    @Resource
    private FormService formService;
    @Resource
    private MerchantService merchantService;
    @Resource
    private RedisUtil redisUtil;

    @ApiOperation("分页查询表单设计表")
    @GetMapping("/page")
    public ApiResult<PageResult<Form>> page(FormParam param) {
        // 使用关联查询
        return success(formService.pageRel(param));
    }

    @ApiOperation("查询全部表单设计表")
    @GetMapping()
    public ApiResult<List<Form>> list(FormParam param) {
        // 使用关联查询
        return success(formService.listRel(param));
    }

    @ApiOperation("根据id查询表单设计表")
    @GetMapping("/{id}")
    public ApiResult<Form> get(@PathVariable("id") Integer id,HttpServletRequest request) {
        String key = "FormId:" + id;
        final String formInfo = redisUtil.get(key);
        // 从缓存读取信息
        if(StrUtil.isNotBlank(formInfo)){
          return success(JSONObject.parseObject(formInfo, Form.class));
        }
        // 使用关联查询shop_merchant
        final Form form = formService.getByIdRel(id);
        // 获取商户信息
        form.setMerchant(merchantService.getById(form.getMerchantId()));
        return success(form);
    }

    @PreAuthorize("hasAuthority('cms:form:save')")
    @ApiOperation("添加表单设计表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Form form) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          form.setUserId(loginUser.getUserId());
        }
        if (formService.save(form)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:form:update')")
    @ApiOperation("修改表单设计表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Form form) {
        if (formService.updateById(form)) {
            if (form.getClearCache() != null) {
              String key = "FormId:" + form.getFormId();
              redisUtil.set(key,form);
            }
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

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

    @PreAuthorize("hasAuthority('cms:form:update')")
    @ApiOperation("批量修改表单设计表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Form> batchParam) {
        if (batchParam.update(formService, "form_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
