package com.openhis.web.departmentmanage.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.core.common.core.domain.R;
import com.openhis.common.enums.SupplyType;
import com.openhis.web.departmentmanage.appservice.IDepartmentCommonService;
import com.openhis.web.departmentmanage.appservice.IDepartmentReceiptApprovalService;
import com.openhis.web.departmentmanage.appservice.IDepartmentTransferInOrderService;
import com.openhis.web.departmentmanage.dto.DepartmentDetailDto;
import com.openhis.web.departmentmanage.dto.DepartmentSearchParam;

import lombok.extern.slf4j.Slf4j;

/**
 * 科室材料调入单
 *
 * @author  yuxj
 * @date 2025-07-22
 */
@RestController
@RequestMapping("/department/transferIn-order")
@Slf4j
public class DepartmentTransferInOrderController {

    @Autowired
    private IDepartmentTransferInOrderService departmentTransferInOrderService;

    @Autowired
    private IDepartmentCommonService departmentCommonService;

    @Autowired
    private IDepartmentReceiptApprovalService departmentReceiptApprovalService;

    /**
     * 调入单初始化
     *
     * @return 初始化信息
     */
    @GetMapping(value = "/init")
    public R<?> init() {
        return departmentTransferInOrderService.init();
    }

    /**
     * 获取物资
     *
     * @param purchaseOrderSearchParam 查询条件
     * @return 物资信息
     */
    @GetMapping(value = "/device-info")
    public R<?> getDeviceInfo(DepartmentSearchParam purchaseOrderSearchParam,
        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
        @RequestParam(name = "searchKey", required = false) String searchKey, HttpServletRequest request) {
        return departmentCommonService.getDeviceInfo(purchaseOrderSearchParam, pageNo, pageSize, searchKey, request);
    }
    /**
     * 调入单单据列表
     *
     * @param transferInOrderSearchParam 查询条件
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @param searchKey 模糊查询关键字
     * @param request 请求数据
     * @return 调入单据分页列表
     */
    @GetMapping(value = "/transferIn-order-page")
    public R<?> getPage(DepartmentSearchParam transferInOrderSearchParam,
        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
        @RequestParam(name = "searchKey", required = false) String searchKey, HttpServletRequest request) {
        return departmentCommonService.getPage(transferInOrderSearchParam, pageNo, pageSize, searchKey, request,
            null, SupplyType.DEPARTMENT_TRANSFERIN.getValue());
    }

    /**
     * 调入单单据详情
     *
     * @param busNo 单据号
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @return 调入单单据详情
     */
    @GetMapping(value = "/transferIn-order-detail-page")
    public R<?> getDetail(@RequestParam String busNo,
                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return departmentTransferInOrderService.getDetailPage(busNo, pageNo, pageSize);
    }
    /**
     * 获取单据号
     *
     * @return 初始化信息
     */
    @GetMapping(value = "/busNo-init")
    public R<?> getBusNo() {
        return departmentTransferInOrderService.getBusNo();
    }

    /**
     * 添加/编辑采购单
     *
     * @param transferInOrderDtoList 采购信息
     * @return 操作结果
     */
    @PutMapping("/addOrEdit-transferInOrder")
    public R<?> addOrEditTransferInOrder(@Validated @RequestBody List<DepartmentDetailDto> transferInOrderDtoList) {
        return departmentTransferInOrderService.addOrEditTransferInOrder(transferInOrderDtoList);
    }

    /**
     * 删除单据
     *
     * @param busNo 单据号
     * @return 操作结果
     */
    @DeleteMapping("/delete-transferInOrder")
    public R<?> deleteTransferInOrder(@RequestParam String busNo) {
        return departmentCommonService.deleteOrder(busNo);
    }

    /**
     * 同意审批
     *
     * @param busNo 单据号
     * @return 操作结果
     */
    @PutMapping("/agree-approval")
    public R<?> agreeApproval(@RequestParam String busNo) {
        return departmentReceiptApprovalService.transferInAgreeApproved(busNo);
    }
}
