package com.openhis.web.pharmacyWarehousemanage.appservice.impl;

import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.openhis.administration.domain.Location;
import com.openhis.administration.domain.Organization;
import com.openhis.common.enums.*;
import com.openhis.web.common.appservice.ICommonService;
import com.openhis.web.common.dto.LocationDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.common.core.domain.R;
import com.core.common.utils.AssignSeqUtil;
import com.core.common.utils.DateUtils;
import com.core.common.utils.MessageUtils;
import com.openhis.common.constant.CommonConstants;
import com.openhis.common.constant.PromptMsgConstant;
import com.openhis.common.utils.EnumUtils;
import com.openhis.web.inventorymanage.dto.ProductStocktakingInitDto;
import com.openhis.web.pharmacyWarehousemanage.appservice.IPharmacyWarehouseCommonService;
import com.openhis.web.pharmacyWarehousemanage.appservice.IPharmacyWarehouseDocumentManagementService;
import com.openhis.web.pharmacyWarehousemanage.dto.PharmacyWarehouseDetailDto;
import com.openhis.web.pharmacyWarehousemanage.dto.PharmacyWarehouseInitDto;
import com.openhis.web.pharmacyWarehousemanage.mapper.PharmacyWarehouseCommonMapper;
import com.openhis.web.pharmacyWarehousemanage.mapper.PharmacyWarehouseDocumentManagementMapper;
import com.openhis.workflow.domain.SupplyRequest;
import com.openhis.workflow.service.ISupplyRequestService;

/**
 * 药库单据
 *
 * @author yuxj
 * @date 2025-07-22
 */
@Service
public class PharmacyWarehouseDocumentManagementServiceImpl implements IPharmacyWarehouseDocumentManagementService {

    @Autowired
    private IPharmacyWarehouseCommonService pharmacyWarehouseCommonService;
    @Resource
    private ICommonService commonServiceImpl;

    /**
     * 初始化
     *
     * @return 初始化信息
     */
    @Override
    public R<?> init() {

        PharmacyWarehouseInitDto purchaseOrderInitDto = pharmacyWarehouseCommonService.init();

        // 单据类型
        List<PharmacyWarehouseInitDto.IntegerOption> cabinetTypeOption = new ArrayList<>();
        cabinetTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.CABINET_PURCHASE.getValue(),
            SupplyType.CABINET_PURCHASE.getInfo()));
        cabinetTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.CABINET_STOCKIN.getValue(),
            SupplyType.CABINET_STOCKIN.getInfo()));
        cabinetTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.CABINET_RETURN.getValue(),
            SupplyType.CABINET_RETURN.getInfo()));
        cabinetTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.CABINET_STOCKOUT.getValue(),
            SupplyType.CABINET_STOCKOUT.getInfo()));
        cabinetTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.CABINET_PRODUCT_RETURN.getValue(),
            SupplyType.CABINET_PRODUCT_RETURN.getInfo()));
        cabinetTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.CABINET_PRPFITLOSS.getValue(),
            SupplyType.CABINET_PRPFITLOSS.getInfo()));
        cabinetTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.CABINET_STOCKTAKING.getValue(),
            SupplyType.CABINET_STOCKTAKING.getInfo()));
        // 单据类型
        List<PharmacyWarehouseInitDto.IntegerOption> pharmacyTypeOption = new ArrayList<>();
        pharmacyTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.PURCHASE_REQUISITION.getValue(),
            SupplyType.PURCHASE_REQUISITION.getInfo()));
        pharmacyTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.PURCHASE_STOCKIN.getValue(),
            SupplyType.PURCHASE_STOCKIN.getInfo()));
        pharmacyTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.PURCHASE_RETURN.getValue(),
            SupplyType.PURCHASE_RETURN.getInfo()));
        pharmacyTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.PURCHASE_PRPFITLOSS.getValue(),
            SupplyType.PURCHASE_PRPFITLOSS.getInfo()));
        pharmacyTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.PURCHASE_STOCKTAKING.getValue(),
            SupplyType.PURCHASE_STOCKTAKING.getInfo()));
        pharmacyTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.PURCHASE_TRANSFERIN.getValue(),
            SupplyType.PURCHASE_TRANSFERIN.getInfo()));
        pharmacyTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.PURCHASE_TRANSFEROUT.getValue(),
            SupplyType.PURCHASE_TRANSFEROUT.getInfo()));
        pharmacyTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.DISPENSING_ORDER.getValue(),
            SupplyType.DISPENSING_ORDER.getInfo()));
        // 单据类型
        List<PharmacyWarehouseInitDto.IntegerOption> warehouseTypeOption = new ArrayList<>();
        warehouseTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.MATERIAL_PURCHASE.getValue(),
            SupplyType.MATERIAL_PURCHASE.getInfo()));
        warehouseTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.MATERIAL_STOCKIN.getValue(),
            SupplyType.MATERIAL_STOCKIN.getInfo()));
        warehouseTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.MATERIAL_RETURN.getValue(),
            SupplyType.MATERIAL_RETURN.getInfo()));
        warehouseTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.MATERIAL_STOCKOUT.getValue(),
            SupplyType.MATERIAL_STOCKOUT.getInfo()));
        warehouseTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.MATERIAL_PRODUCT_RETURN.getValue(),
            SupplyType.MATERIAL_PRODUCT_RETURN.getInfo()));
        warehouseTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.MATERIAL_PRPFITLOSS.getValue(),
            SupplyType.MATERIAL_PRPFITLOSS.getInfo()));
        warehouseTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.MATERIAL_STOCKTAKING.getValue(),
            SupplyType.MATERIAL_STOCKTAKING.getInfo()));
        warehouseTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.MATERIAL_TRANSFERIN.getValue(),
            SupplyType.MATERIAL_TRANSFERIN.getInfo()));
        warehouseTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.MATERIAL_TRANSFEROUT.getValue(),
            SupplyType.MATERIAL_TRANSFEROUT.getInfo()));
        // 单据类型
        List<PharmacyWarehouseInitDto.IntegerOption> departmentTypeOption = new ArrayList<>();
        departmentTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.DEPARTMENT_REQUISITION.getValue(),
            SupplyType.DEPARTMENT_REQUISITION.getInfo()));
        departmentTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.DEPARTMENT_STOCKIN.getValue(),
            SupplyType.DEPARTMENT_STOCKIN.getInfo()));
        departmentTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.DEPARTMENT_RETURN.getValue(),
            SupplyType.DEPARTMENT_RETURN.getInfo()));
        departmentTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.DEPARTMENT_PRPFITLOSS.getValue(),
            SupplyType.DEPARTMENT_PRPFITLOSS.getInfo()));
        departmentTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.DEPARTMENT_STOCKTAKING.getValue(),
            SupplyType.DEPARTMENT_STOCKTAKING.getInfo()));
        departmentTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.DEPARTMENT_TRANSFERIN.getValue(),
            SupplyType.DEPARTMENT_TRANSFERIN.getInfo()));
        departmentTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.DEPARTMENT_TRANSFEROUT.getValue(),
            SupplyType.DEPARTMENT_TRANSFEROUT.getInfo()));
        departmentTypeOption.add(new PharmacyWarehouseInitDto.IntegerOption(SupplyType.DISPENSING_ORDER.getValue(),
            SupplyType.DISPENSING_ORDER.getInfo()));

        // 药库列表
        List<PharmacyWarehouseInitDto.item> cabinetList = new ArrayList<>();
        PharmacyWarehouseInitDto.item cabinetListItem;
        for (LocationDto item : commonServiceImpl.getCabinetList()) {
            cabinetListItem = new PharmacyWarehouseInitDto.item();
            BeanUtils.copyProperties(item, cabinetListItem);
            cabinetList.add(cabinetListItem);
        }

        // 药房列表
        List<PharmacyWarehouseInitDto.item> pharmacyList = new ArrayList<>();
        PharmacyWarehouseInitDto.item pharmacyListItem;
        for (LocationDto item : commonServiceImpl.getPharmacyList()) {
            pharmacyListItem = new PharmacyWarehouseInitDto.item();
            BeanUtils.copyProperties(item, pharmacyListItem);
            pharmacyList.add(pharmacyListItem);
        }

        // 仓库列表
        List<PharmacyWarehouseInitDto.item> warehouseList = new ArrayList<>();
        PharmacyWarehouseInitDto.item warehouseListItem;
        for (LocationDto item : commonServiceImpl.getWarehouseList()) {
            warehouseListItem = new PharmacyWarehouseInitDto.item();
            BeanUtils.copyProperties(item, warehouseListItem);
            warehouseList.add(warehouseListItem);
        }

        // 仓库列表
        List<PharmacyWarehouseInitDto.item> departmentList = new ArrayList<>();
        PharmacyWarehouseInitDto.item departmentListItem;
        for (Organization item : commonServiceImpl.departmentList()) {
            departmentListItem = new PharmacyWarehouseInitDto.item();
            BeanUtils.copyProperties(item, departmentListItem);
            departmentList.add(departmentListItem);
        }

        // 库房类型
        List<PharmacyWarehouseInitDto.WarehouseOption> typeOption = new ArrayList<>();
        typeOption.add(new PharmacyWarehouseInitDto.WarehouseOption(LocationForm.CABINET.getValue(),
            LocationForm.CABINET.getInfo(), cabinetList, cabinetTypeOption));
        typeOption.add(new PharmacyWarehouseInitDto.WarehouseOption(LocationForm.PHARMACY.getValue(),
            LocationForm.PHARMACY.getInfo(), pharmacyList, pharmacyTypeOption));
        typeOption.add(new PharmacyWarehouseInitDto.WarehouseOption(LocationForm.WAREHOUSE.getValue(),
            LocationForm.WAREHOUSE.getInfo(), warehouseList, warehouseTypeOption));
        typeOption.add(new PharmacyWarehouseInitDto.WarehouseOption(LocationForm.DEPARTMENT.getValue(),
            LocationForm.DEPARTMENT.getInfo(), departmentList, departmentTypeOption));

        purchaseOrderInitDto.setWarehouseTypeOption(typeOption);

        return R.ok(purchaseOrderInitDto);
    }

}
