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

import com.xisc.pm.api.dto.PmIrsubableContRequestDTO;
import com.xisc.pm.api.dto.PmIrsubableSlabRequestDTO;
import com.xisc.pm.api.dto.export.PmIrsubableContDTO;
import com.xisc.pm.api.dto.export.PmIrsubableSlabDTO;
import com.xisc.pm.domain.vo.util.JsonData;
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.export.annotation.ExcelExport;
import org.hzero.export.vo.ExportParam;
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.PmIrsubableSlabService;
import com.xisc.pm.domain.entity.PmIrsubableSlab;
import com.xisc.pm.domain.repository.PmIrsubableSlabRepository;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 不可替代板坯表(PmIrsubableSlab)表控制层
 *
 * @author BKGY
 * @since 2024-12-20 15:57:39
 */

@RestController("pmIrsubableSlabController.v1")
@RequestMapping("/v1/{organizationId}/pm-irsubable-slabs")
public class PmIrsubableSlabController extends BaseController {

    @Autowired
    private PmIrsubableSlabRepository pmIrsubableSlabRepository;

    @Autowired
    private PmIrsubableSlabService pmIrsubableSlabService;



    @ApiOperation(value = "不可替代板坯表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/list/ui")
    public ResponseEntity<Page<PmIrsubableSlab>> list(PmIrsubableSlabRequestDTO pmIrsubableSlab, @PathVariable Long organizationId,
                                                      @ApiIgnore @SortDefault(value = PmIrsubableSlab.FIELD_ID,
                                                              direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<PmIrsubableSlab> list = pmIrsubableSlabService.selectList(pageRequest, pmIrsubableSlab);
        return Results.success(list);
    }

    @ApiOperation(value = "不可替代板坯表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}")
    public ResponseEntity<PmIrsubableSlab> detail(@PathVariable Long id) {
        PmIrsubableSlab pmIrsubableSlab = pmIrsubableSlabRepository.selectByPrimary(id);
        return Results.success(pmIrsubableSlab);
    }

    @ApiOperation(value = "创建或更新不可替代板坯表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping("/save/ui")
    public ResponseEntity<List<PmIrsubableSlab>> save(@PathVariable Long organizationId, @RequestBody List<PmIrsubableSlab> pmIrsubableSlabs) {
        validObject(pmIrsubableSlabs);
        SecurityTokenHelper.validTokenIgnoreInsert(pmIrsubableSlabs);
        pmIrsubableSlabs.forEach(item -> item.setTenantId(organizationId));
        pmIrsubableSlabService.saveData(organizationId,pmIrsubableSlabs);
        return Results.success(pmIrsubableSlabs);
    }

    @ApiOperation(value = "删除不可替代板坯表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping("/delete/ui")
    public ResponseEntity<Void> remove(@RequestBody List<PmIrsubableSlab> pmIrsubableSlabs) {
        SecurityTokenHelper.validToken(pmIrsubableSlabs);
        pmIrsubableSlabRepository.batchDeleteByPrimaryKey(pmIrsubableSlabs);
        return Results.success();
    }

    @ApiOperation(value = "不可替代板坯表导出")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @ExcelExport(PmIrsubableSlabDTO.class)
    @GetMapping("/export/ui")
    public ResponseEntity<List<PmIrsubableSlabDTO>> exportList(PmIrsubableSlabRequestDTO pmIrsubableSlab,
                                                               @PathVariable Long organizationId,
                                                               ExportParam exportParam, HttpServletResponse response) {
        List<PmIrsubableSlabDTO> list = pmIrsubableSlabService.exportList(organizationId,pmIrsubableSlab);
        return Results.success(list);
    }

    @ApiOperation("数据处理")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping("/SubData1")
    public JsonData SubData1(String strSlab) {
        System.out.println("调用成功");
        JsonData xx= new JsonData();
        xx.setCode(0);
        xx.setData(null);
        xx.setMsg("123");
//        pmIrsubableSlabService.xx();
        return   xx;
    }

}

