package cn.iocoder.yudao.module.bpm.controller.admin.oa;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.workorderassignrule.BpmOAWorkOrderAssignRulePageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.workorderassignrule.BpmOAWorkOrderAssignRuleRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.workorderassignrule.BpmOAWorkOrderAssignRuleSaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAWorkOrderAssignRuleDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAWorkOrderAssignRuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

/**
 * 管理后台 - BPM OA 工单分配规则
 *
 * @author 系统
 */
@Tag(name = "管理后台 - BPM OA 工单分配规则")
@RestController
@RequestMapping("/bpm/work-order-assign-rule")
@Validated
public class BpmOAWorkOrderAssignRuleController {

    @Resource
    private BpmOAWorkOrderAssignRuleService workOrderAssignRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建工单分配规则")
    @PreAuthorize("@ss.hasPermission('bpm:work-order-assign-rule:create')")
    @OperateLog(type = CREATE)
    public CommonResult<Long> createWorkOrderAssignRule(@Valid @RequestBody BpmOAWorkOrderAssignRuleSaveReqVO createReqVO) {
        return success(workOrderAssignRuleService.createWorkOrderAssignRule(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工单分配规则")
    @PreAuthorize("@ss.hasPermission('bpm:work-order-assign-rule:update')")
    @OperateLog(type = UPDATE)
    public CommonResult<Boolean> updateWorkOrderAssignRule(@Valid @RequestBody BpmOAWorkOrderAssignRuleSaveReqVO updateReqVO) {
        workOrderAssignRuleService.updateWorkOrderAssignRule(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工单分配规则")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:work-order-assign-rule:delete')")
    @OperateLog(type = DELETE)
    public CommonResult<Boolean> deleteWorkOrderAssignRule(@RequestParam("id") Long id) {
        workOrderAssignRuleService.deleteWorkOrderAssignRule(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工单分配规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bpm:work-order-assign-rule:query')")
    public CommonResult<BpmOAWorkOrderAssignRuleDO> getWorkOrderAssignRule(@RequestParam("id") Long id) {
        BpmOAWorkOrderAssignRuleDO workOrderAssignRule = workOrderAssignRuleService.getWorkOrderAssignRule(id);
        return success(workOrderAssignRule);
    }

    @GetMapping("/page")
    @Operation(summary = "获得工单分配规则分页")
    @PreAuthorize("@ss.hasPermission('bpm:work-order-assign-rule:query')")
    public CommonResult<PageResult<BpmOAWorkOrderAssignRuleRespVO>> getWorkOrderAssignRulePage(@Valid BpmOAWorkOrderAssignRulePageReqVO pageReqVO) {
        PageResult<BpmOAWorkOrderAssignRuleRespVO> pageResult = workOrderAssignRuleService.getWorkOrderAssignRulePage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/list")
    @Operation(summary = "获得工单分配规则列表")
    @PreAuthorize("@ss.hasPermission('bpm:work-order-assign-rule:query')")
    public CommonResult<List<BpmOAWorkOrderAssignRuleRespVO>> getWorkOrderAssignRuleList(@Valid BpmOAWorkOrderAssignRulePageReqVO exportReqVO) {
        List<BpmOAWorkOrderAssignRuleRespVO> list = workOrderAssignRuleService.getWorkOrderAssignRuleList(exportReqVO);
        return success(list);
    }

    @GetMapping("/list-by-type")
    @Operation(summary = "根据工单类型获取有效的分配规则")
    @Parameter(name = "workOrderType", description = "工单类型", required = true)
    @PreAuthorize("@ss.hasPermission('bpm:work-order-assign-rule:query')")
    public CommonResult<List<BpmOAWorkOrderAssignRuleDO>> getEnabledRulesByType(@RequestParam("workOrderType") String workOrderType) {
        List<BpmOAWorkOrderAssignRuleDO> list = workOrderAssignRuleService.getEnabledRulesByType(workOrderType);
        return success(list);
    }

    @PutMapping("/update-status")
    @Operation(summary = "批量更新规则状态")
    @PreAuthorize("@ss.hasPermission('bpm:work-order-assign-rule:update')")
    @OperateLog(type = UPDATE)
    public CommonResult<Boolean> updateRuleStatus(@RequestParam("ids") Collection<Long> ids,
                                                  @RequestParam("status") Integer status) {
        workOrderAssignRuleService.updateRuleStatus(ids, status);
        return success(true);
    }

}
