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.PmSubCheckChmService;
import com.xisc.pm.domain.entity.PmSubCheckChm;
import com.xisc.pm.domain.repository.PmSubCheckChmRepository;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 化学成分检测结果表(PmSubCheckChm)表控制层
 *
 * @author BKGY
 * @since 2025-03-26 16:22:54
 */

@RestController("pmSubCheckChmController.v1")
@RequestMapping("/v1/{organizationId}/pm-sub-check-chms")
public class PmSubCheckChmController extends BaseController {

    @Autowired
    private PmSubCheckChmRepository pmSubCheckChmRepository;

    @Autowired
    private PmSubCheckChmService pmSubCheckChmService;

    @ApiOperation(value = "化学成分检测结果表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping
    public ResponseEntity<Page<PmSubCheckChm>> list(PmSubCheckChm pmSubCheckChm, @PathVariable Long organizationId,
                                                    @ApiIgnore @SortDefault(value = PmSubCheckChm.FIELD_ID,
                                                            direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<PmSubCheckChm> list = pmSubCheckChmService.selectList(pageRequest, pmSubCheckChm);
        return Results.success(list);
    }

    @ApiOperation(value = "化学成分检测结果表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}")
    public ResponseEntity<PmSubCheckChm> detail(@PathVariable Long id) {
        PmSubCheckChm pmSubCheckChm = pmSubCheckChmRepository.selectByPrimary(id);
        return Results.success(pmSubCheckChm);
    }

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

    @ApiOperation(value = "删除化学成分检测结果表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<Void> remove(@RequestBody List<PmSubCheckChm> pmSubCheckChms) {
        SecurityTokenHelper.validToken(pmSubCheckChms);
        pmSubCheckChmRepository.batchDeleteByPrimaryKey(pmSubCheckChms);
        return Results.success();
    }

}

