
package com.tmerclub.cloud.search.controller.supplier;

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.dto.EsPageDTO;
import com.tmerclub.cloud.api.vo.EsPageVO;
import com.tmerclub.cloud.common.order.dto.PurchaseOrderSearchDTO;
import com.tmerclub.cloud.common.order.vo.EsPurchaseOrderVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.search.manager.PurchaseOrderSearchManager;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Objects;

/**
 * 商品搜索
 *
 * @author tmerclub
 * @date 2020/11/16
 */
@RestController("suplplierPurchaseOrderSearchController")
@RequestMapping("/s/purchaseOrderSearch")
@Tag(name = "supplier-采购订单管理接口")
public class PurchaseOrderSearchController {

    @Autowired
    private PurchaseOrderSearchManager purchaseOrderSearchManager;

    @GetMapping("/page")
    @Operation(summary = "分页查询采购订单", description = "分页查询采购订单")
    public ServerResponseEntity<EsPageVO<EsPurchaseOrderVO>> page(@Valid EsPageDTO pageDTO, PurchaseOrderSearchDTO purchaseOrderDTO) {
        purchaseOrderDTO.setSupplierId(AuthUserContext.get().getTenantId());
        // 供应商'待收货'订单包含'待入库'和'部分入库'
        if (Objects.equals(purchaseOrderDTO.getStatus(), 2)) {
            purchaseOrderDTO.setStatus(null);
            purchaseOrderDTO.setStatuses(Arrays.asList(2, 3));
        }
        EsPageVO<EsPurchaseOrderVO> page = purchaseOrderSearchManager.page(pageDTO, purchaseOrderDTO);
        for (EsPurchaseOrderVO esPurchaseOrderVO : page.getList()) {
            if (StrUtil.isNotBlank(esPurchaseOrderVO.getContactPhone())) {
                esPurchaseOrderVO.setContactPhone(PhoneUtil.hideBetween(esPurchaseOrderVO.getContactPhone()).toString());
            }
            if (StrUtil.isNotBlank(esPurchaseOrderVO.getMobile())) {
                esPurchaseOrderVO.setMobile(PhoneUtil.hideBetween(esPurchaseOrderVO.getMobile()).toString());
            }
        }
        return ServerResponseEntity.success(page);

    }
}
