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

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 com.xisc.pm.app.service.PmSubCheckLowService;
import com.xisc.pm.domain.entity.PmSubCheckLow;
import com.xisc.pm.domain.repository.PmSubCheckLowRepository;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 低倍检测结果表(PmSubCheckLow)表控制层
 *
 * @author BKGY
 * @since 2025-03-26 16:27:39
 */

@RestController("pmSubCheckLowController.v1")
@RequestMapping("/v1/{organizationId}/pm-sub-check-lows")
public class PmSubCheckLowController extends BaseController {

    @Autowired
    private PmSubCheckLowRepository pmSubCheckLowRepository;

    @Autowired
    private PmSubCheckLowService pmSubCheckLowService;

    @ApiOperation(value = "低倍检测结果表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<PmSubCheckLow>> list(PmSubCheckLow pmSubCheckLow, @PathVariable Long organizationId,
                                                    @ApiIgnore @SortDefault(value = PmSubCheckLow.FIELD_ID,
                                                            direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<PmSubCheckLow> list = pmSubCheckLowService.selectList(pageRequest, pmSubCheckLow);
        return Results.success(list);
    }

    @ApiOperation(value = "低倍检测结果表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}")
    public ResponseEntity<PmSubCheckLow> detail(@PathVariable Long id) {
        PmSubCheckLow pmSubCheckLow = pmSubCheckLowRepository.selectByPrimary(id);
        return Results.success(pmSubCheckLow);
    }

    @ApiOperation(value = "创建或更新低倍检测结果表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<List<PmSubCheckLow>> save(@PathVariable Long organizationId, @RequestBody List<PmSubCheckLow> pmSubCheckLows) {
        validObject(pmSubCheckLows);
        SecurityTokenHelper.validTokenIgnoreInsert(pmSubCheckLows);
        pmSubCheckLows.forEach(item -> item.setTenantId(organizationId));
        pmSubCheckLowService.saveData(pmSubCheckLows);
        return Results.success(pmSubCheckLows);
    }

    @ApiOperation(value = "删除低倍检测结果表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<Void> remove(@RequestBody List<PmSubCheckLow> pmSubCheckLows) {
        SecurityTokenHelper.validToken(pmSubCheckLows);
        pmSubCheckLowRepository.batchDeleteByPrimaryKey(pmSubCheckLows);
        return Results.success();
    }

}

