package com.scm.contract.web.management;

import com.scm.contract.model.apply.LabelApplication;
import com.scm.contract.model.management.Provider;
import com.scm.contract.service.management.ProviderService;
import com.scm.contract.tools.HumpLineAdopter;
import com.set.direwolf.core.mybatis.Page;
import com.set.direwolf.core.web.OperationResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * 供应商信息
 *
 * @author zuo
 * @date 2024/6/7 11:09
 */

@Controller
@RequestMapping("${adminPath}/provider")
public class ProviderController {

    private final ProviderService providerService;

    @Autowired
    public ProviderController(ProviderService providerService) {
        this.providerService = providerService;
    }

    @PostMapping("/list")
    public @ResponseBody OperationResult List() {
        return OperationResult.buildSuccessResult("获取供应商信息成功", providerService.list());
    }

    /**
     * 分页查询数据获取
     *
     * @param entity   参数实体
     * @param request  请求
     * @param response 响应
     * @return Page
     */
    @PostMapping("/page")
    public @ResponseBody OperationResult page(@RequestBody Provider entity, HttpServletRequest request, HttpServletResponse response) {
        Page<Provider> page = Page.bootstrapPage(request, response);
        List<List<String>> orderList = entity.getOrder();
        String defaultOrder = HumpLineAdopter.humpToLine(orderList);
        if (StringUtils.isNotEmpty(defaultOrder)) {
            page.setOrderBy(defaultOrder);
        }
        return OperationResult.buildSuccessResult("获取供应商分页查询信息成功", providerService.findPage(page, entity));
    }
}
