package com.jwipc.sys.controller.pc;

import com.alibaba.fastjson2.JSONArray;
import com.jwipc.page.PageView;
import com.jwipc.sys.dto.ValidationRuleSettingFieldSelectDTO;
import com.jwipc.sys.dto.ValidationRuleSettingInfoDTO;
import com.jwipc.sys.dto.ValidationRuleSettingInsetUpdateDTO;
import com.jwipc.sys.logic.ValidationRuleSettingLogic;
import com.jwipc.sys.model.query.TRegexRuleQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.jwipc.sys.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 *  表单校验规则配置-数据控制层
 * </p>
 *
 * @author Chen Tianshi
 * @since 2025/8/26 15:34
 */
@RestController
@RequestMapping(MODULE_ROOT + "/validationRuleSetting/rest")
@RequiredArgsConstructor
public class ValidationRuleSettingRestController {
    private final ValidationRuleSettingLogic validationRuleSettingLogic;

    /**
     * 查询数据
     * @param query 查询条件
     * @return 查询结果
     */
    @RequestMapping("/list")
    public PageView list(TRegexRuleQuery query) {
        return validationRuleSettingLogic.list(query);
    }

    /**
     * 新增或者更新
     * @param insetUpdateDTO 参数
     * @return 无
     */
    @PostMapping("/insertOrUpdate")
    public ResponseEntity<Boolean> insertOrUpdate(ValidationRuleSettingInsetUpdateDTO insetUpdateDTO) {
        validationRuleSettingLogic.insertOrUpdate(insetUpdateDTO);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     * @param regexRuleIds 规则ID
     * @return 无
     */
    @RequestMapping("/delete")
    public ResponseEntity<Boolean> delete(String regexRuleIds) {
        validationRuleSettingLogic.deleteRule(JSONArray.parseArray(regexRuleIds, Long.class));
        return ResponseEntity.ok().build();
    }

    /**
     * 启用规则
     * @param regexRuleIds 规则ID
     * @return 无
     */
    @RequestMapping("/updateRuleStatus")
    public ResponseEntity<Boolean> updateRuleStatus(String regexRuleIds, String status) {
        validationRuleSettingLogic.updateRuleStatus(regexRuleIds, status);
        return ResponseEntity.ok().build();
    }

    /**
     * 通过字段ID获取校验规则
     * @param fieldId 业务扩展字段表ID
     * @return 校验规则
     */
    @RequestMapping("/getFieldValidationById")
    public List<ValidationRuleSettingInfoDTO> getFieldValidationById(Long fieldId) {
        return validationRuleSettingLogic.getFieldValidationById(fieldId);
    }

    /**
     * 通过供应商ID获取校验规则
     * @param supplierId 通过供应商ID
     * @return 校验规则
     */
    @RequestMapping("/getAllValidationBySupplierId")
    public List<ValidationRuleSettingInfoDTO> getAllValidationBySupplierId(Long supplierId) {
        return validationRuleSettingLogic.getAllValidationBySupplierId(supplierId);
    }

    /**
     * 获取字段选择内容
     * @return 字段选择内容
     */
    @RequestMapping("/getFieldSelectList")
    public List<ValidationRuleSettingFieldSelectDTO> getFieldSelectList() {
        return validationRuleSettingLogic.getFieldSelectList();
    }
}
