package com.xisc.pm.api.controller.v1;

import com.xisc.pm.api.dto.PmAutoSubSettingDTO;
import com.xisc.pm.app.service.PmAutoSubSettingService;
import com.xisc.pm.domain.entity.PmAutoSubSetting;
import com.xisc.pm.domain.repository.PmAutoSubSettingRepository;
import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.mybatis.pagehelper.domain.Sort;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 自动替代配置表(PmAutoSubSetting)表控制层
 *
 * @author BKGY
 * @since 2024-12-27 08:56:24
 */

@RestController("pmAutoSubSettingController.v1")
@RequestMapping("/v1/{organizationId}/pm-auto-sub-settings")
public class PmAutoSubSettingController extends BaseController {

    @Autowired
    private PmAutoSubSettingRepository pmAutoSubSettingRepository;

    @Autowired
    private PmAutoSubSettingService pmAutoSubSettingService;

    @ApiOperation(value = "自动替代配置表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/list/ui")
    public ResponseEntity<Page<PmAutoSubSetting>> list(PmAutoSubSetting pmAutoSubSetting, @PathVariable Long organizationId,
                                                       @ApiIgnore @SortDefault(value = PmAutoSubSetting.FIELD_ID,
                                                               direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<PmAutoSubSetting> list = pmAutoSubSettingService.selectList(pageRequest, pmAutoSubSetting);
        return Results.success(list);
    }

    @ApiOperation(value = "查询自动替代配置")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/query/ui")
    public ResponseEntity<PmAutoSubSettingDTO> queryInfo(@PathVariable Long organizationId) {
        PmAutoSubSettingDTO pmAutoSubSetting = pmAutoSubSettingService.selectPmAutoSubSetting();
        return Results.success(pmAutoSubSetting);
    }

    @ApiOperation(value = "自动替代配置表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}")
    public ResponseEntity<PmAutoSubSetting> detail(@PathVariable Long id) {
        PmAutoSubSetting pmAutoSubSetting = pmAutoSubSettingRepository.selectByPrimary(id);
        return Results.success(pmAutoSubSetting);
    }

    @ApiOperation(value = "创建或更新自动替代配置表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping("/save/ui")
    public ResponseEntity<PmAutoSubSettingDTO> save(@PathVariable Long organizationId, @RequestBody PmAutoSubSettingDTO pmAutoSubSettingDTO) {
        validObject(pmAutoSubSettingDTO);
        pmAutoSubSettingDTO.setTenantId(organizationId);
        pmAutoSubSettingService.saveData(pmAutoSubSettingDTO);
        return Results.success(pmAutoSubSettingDTO);
    }

    @ApiOperation(value = "删除自动替代配置表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<Void> remove(@RequestBody List<PmAutoSubSetting> pmAutoSubSettings) {
        SecurityTokenHelper.validToken(pmAutoSubSettings);
        pmAutoSubSettingRepository.batchDeleteByPrimaryKey(pmAutoSubSettings);
        return Results.success();
    }

}

