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

import com.xisc.pm.api.dto.PmSubHisRequestDTO;
import com.xisc.pm.api.dto.PmSubHisResponseDTO;
import com.xisc.pm.app.service.PmSubHisService;
import com.xisc.pm.domain.entity.PmSubHis;
import com.xisc.pm.domain.repository.PmSubHisRepository;
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;


/**
 * 自动替代履历表(PmSubHis)表控制层
 *
 * @author BKGY
 * @since 2024-12-26 09:57:36
 */

@RestController("pmSubHisController.v1")
@RequestMapping("/v1/{organizationId}/pm-sub-hiss")
public class PmSubHisController extends BaseController {

    @Autowired
    private PmSubHisRepository pmSubHisRepository;

    @Autowired
    private PmSubHisService pmSubHisService;

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

    @ApiOperation(value = "自动替代履历表列表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/listStatistics/ui")
    public ResponseEntity<Page<PmSubHisResponseDTO>> listStatistics (PmSubHisRequestDTO pmSubHisRequestDTO, @PathVariable Long organizationId,
                                                                     @ApiIgnore @SortDefault(value = PmSubHis.FIELD_SUB_TIME,
                                                       direction = Sort.Direction.DESC) PageRequest pageRequest) {
        Page<PmSubHisResponseDTO> list = pmSubHisService.selectStatisticsList(pageRequest, pmSubHisRequestDTO);
        return Results.success(list);
    }

    @ApiOperation(value = "自动替代履历表明细")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @GetMapping("/{id}")
    public ResponseEntity<PmSubHis> detail(@PathVariable Long id) {
        PmSubHis pmSubHis = pmSubHisRepository.selectByPrimary(id);
        return Results.success(pmSubHis);
    }

    @ApiOperation(value = "创建或更新自动替代履历表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping
    public ResponseEntity<List<PmSubHis>> save(@PathVariable Long organizationId, @RequestBody List<PmSubHis> pmSubHiss) {
        validObject(pmSubHiss);
        SecurityTokenHelper.validTokenIgnoreInsert(pmSubHiss);
        pmSubHiss.forEach(item -> item.setTenantId(organizationId));
        pmSubHisService.saveData(pmSubHiss);
        return Results.success(pmSubHiss);
    }

    @ApiOperation(value = "删除自动替代履历表")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<Void> remove(@RequestBody List<PmSubHis> pmSubHiss) {
        SecurityTokenHelper.validToken(pmSubHiss);
        pmSubHisRepository.batchDeleteByPrimaryKey(pmSubHiss);
        return Results.success();
    }

    @ApiOperation(value = "组板按钮")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @PostMapping("/subDataTest/ui")
    public ResponseEntity<PmSubHis> subDataTest(@PathVariable Long organizationId, @RequestBody PmSubHis pmSubHiss) {

        SecurityTokenHelper.validTokenIgnoreInsert(pmSubHiss);
        subData(pmSubHiss.getSlabNo());
        return Results.success(pmSubHiss);
    }

    public Boolean subData(String strSlab){
//        SubController.SubData(strSlab);
//        在这里调用restful接口 http://localhost:8081/Sub/SubData1  传参为String stabNo = stableNo
//        String url = "http://localhost:8081/Sub/SubData1?strSlab=" + strSlab;   // 替换为实际的URL
//        RestTemplate restTemplate = new RestTemplate();
//        ResponseEntity<String> response = restTemplate.postForEntity(url, null, String.class);
//        System.out.println("Response body: " + response.getBody());
        return true;
    }

//    public class ResponseData {
//        private Integer code;
//        private Object data;
//        private String msg;
//
//        public Integer getCode() {
//            return code;
//        }
//
//        public void setCode(Integer code) {
//            this.code = code;
//        }
//
//        public Object getData() {
//            return data;
//        }
//
//        public void setData(Object data) {
//            this.data = data;
//        }
//
//        public String getMsg() {
//            return msg;
//        }
//
//        public void setMsg(String msg) {
//            this.msg = msg;
//        }
//
//        // Getters and setters
//    }

}

