package com.scm.contract.web.management;

import com.scm.contract.model.management.Materiel;
import com.scm.contract.model.management.Provider;
import com.scm.contract.service.management.MaterielService;
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.*;

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}/materiel")
public class MaterialController {

    private final MaterielService materielService;

    @Autowired
    public MaterialController(MaterielService materielService) {
        this.materielService = materielService;
    }

    @PostMapping("/list")
    public @ResponseBody OperationResult List() {
        return OperationResult.buildSuccessResult("获取物料信息成功", materielService.findList(new Materiel()));
    }

    /**
     * 分页查询数据获取
     *
     * @param entity   参数实体
     * @param request  请求
     * @param response 响应
     * @return Page
     */
    @PostMapping("/page")
    public @ResponseBody OperationResult page(@RequestBody Materiel entity, HttpServletRequest request, HttpServletResponse response) {
        Page<Materiel> 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("获取物料分页查询信息成功", materielService.findPage(page, entity));
    }

    /**
     * 获取物料详情信息
     *
     * @param id
     * @return Page
     */
    @GetMapping("/{id}")
    public @ResponseBody OperationResult getById(@PathVariable("id") String id) {
        return OperationResult.buildSuccessResult("获取物料详情信息成功", materielService.getById(id));
    }
}
