package com.yunzhupaas.base.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import com.yunzhupaas.base.controller.SuperController;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import com.yunzhupaas.base.ActionResult;
import com.yunzhupaas.base.service.ComFieldsService;
import com.yunzhupaas.base.vo.ListVO;
import com.yunzhupaas.base.entity.ComFieldsEntity;
import com.yunzhupaas.constant.GenerateConstant;
import com.yunzhupaas.constant.MsgCode;
import com.yunzhupaas.exception.DataException;
import com.yunzhupaas.base.model.comfields.ComFieldsCrForm;
import com.yunzhupaas.base.model.comfields.ComFieldsInfoVO;
import com.yunzhupaas.base.model.comfields.ComFieldsListVO;
import com.yunzhupaas.base.model.comfields.ComFieldsUpForm;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.JsonUtilEx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.ArrayList;
import java.util.List;

/**
 * 常用字段
 *
 * @author 云筑产品开发平台组
 * @version V3.1.0
 * @copyright 深圳市乐程软件有限公司
 * @date 2024/3/12 15:31
 */
@Tag(name = "常用字段", description = "CommonFields")
@RestController
@RequestMapping("/api/system/CommonFields")
public class ComFieldsController extends SuperController<ComFieldsService, ComFieldsEntity> {

    @Autowired
    private ComFieldsService comFieldsService;

    /**
     * 获取常用字段列表
     *
     * @return ignore
     */
    @Operation(summary = "获取常用字段列表")
    @SaCheckPermission("systemData.dataModel")
    @GetMapping
    public ActionResult<ListVO<ComFieldsListVO>> list() {
        List<ComFieldsEntity> data = comFieldsService.getList();
        List<ComFieldsListVO> list = JsonUtil.getJsonToList(data, ComFieldsListVO.class);
        ListVO<ComFieldsListVO> vo = new ListVO<>();
        vo.setList(list);
        return ActionResult.success(vo);
    }

    /**
     * 获取常用字段
     *
     * @param id 主键
     * @return ignore
     */
    @Operation(summary = "获取常用字段")
    @Parameter(name = "id", description = "主键", required = true)
    @SaCheckPermission("systemData.dataModel")
    @GetMapping("/{id}")
    public ActionResult<ComFieldsInfoVO> info(@PathVariable("id") String id) throws DataException {
        ComFieldsEntity entity = comFieldsService.getInfo(id);
        ComFieldsInfoVO vo = JsonUtilEx.getJsonToBeanEx(entity, ComFieldsInfoVO.class);
        return ActionResult.success(vo);
    }

    /**
     * 新增常用字段
     *
     * @param comFieldsCrForm 新增常用字段模型
     * @return ignore
     */
    @Operation(summary = "添加常用字段")
    @Parameter(name = "comFieldsCrForm", description = "新建模型", required = true)
    @SaCheckPermission("systemData.dataModel")
    @PostMapping
    public ActionResult create(@RequestBody @Valid ComFieldsCrForm comFieldsCrForm) {
        ComFieldsEntity entity = JsonUtil.getJsonToBean(comFieldsCrForm, ComFieldsEntity.class);
        List<String> javaSql = new ArrayList<>();
        javaSql.addAll(GenerateConstant.JAVA_KEYWORD);
        javaSql.addAll(GenerateConstant.SQL_KEYWORD);
        if(javaSql.contains(entity.getField())){
            return ActionResult.fail(MsgCode.SYS128.get("列名" + entity.getField()));
        }
        if (comFieldsService.isExistByFullName(entity.getField(), entity.getId())) {
            return ActionResult.fail(MsgCode.EXIST001.get());
        }
        comFieldsService.create(entity);
        return ActionResult.success(MsgCode.SU001.get());
    }

    /**
     * 修改常用字段
     *
     * @param id              主键
     * @param comFieldsUpForm 修改常用字段模型
     * @return ignore
     */
    @Operation(summary = "修改常用字段")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
            @Parameter(name = "comFieldsUpForm", description = "修改模型", required = true)
    })
    @SaCheckPermission("systemData.dataModel")
    @PutMapping("/{id}")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid ComFieldsUpForm comFieldsUpForm) {
        ComFieldsEntity entity = JsonUtil.getJsonToBean(comFieldsUpForm, ComFieldsEntity.class);
        List<String> javaSql = new ArrayList<>();
        javaSql.addAll(GenerateConstant.JAVA_KEYWORD);
        javaSql.addAll(GenerateConstant.SQL_KEYWORD);
        if(javaSql.contains(entity.getField())){
            return ActionResult.fail(MsgCode.SYS128.get("列名" + entity.getField()));
        }
        if (comFieldsService.isExistByFullName(entity.getField(), id)) {
            return ActionResult.fail(MsgCode.EXIST001.get());
        }
        boolean flag = comFieldsService.update(id, entity);
        if (!flag) {
            return ActionResult.fail(MsgCode.FA002.get());
        }
        return ActionResult.success(MsgCode.SU004.get());
    }

    /**
     * 删除常用字段
     *
     * @param id 主键
     * @return ignore
     */
    @Operation(summary = "删除常用字段")
    @Parameter(name = "id", description = "主键", required = true)
    @SaCheckPermission("systemData.dataModel")
    @DeleteMapping("/{id}")
    public ActionResult delete(@PathVariable("id") String id) {
        ComFieldsEntity entity = comFieldsService.getInfo(id);
        if (entity != null) {
            comFieldsService.delete(entity);
            return ActionResult.success(MsgCode.SU003.get());
        }
        return ActionResult.fail(MsgCode.FA003.get());
    }
}


