package com.jeeplus.pubmodules.purchase.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.pubmodules.purchase.domain.MaterialDetailed;
import com.jeeplus.pubmodules.purchase.service.MaterialService;
import com.jeeplus.pubmodules.purchase.service.dto.MaterialInfoDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: 王强
 * @create: 2022-12-02 16:25
 **/
@RestController
@Api(tags ="物资管理-采购申请")
@RequestMapping(value = "/material/management")
public class MaterialController {

    @Resource
    private MaterialService service;

    /**
     * 列表查询
     * @param dto
     * @return
     */
    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    public ResponseEntity<IPage<MaterialInfoDto>> list(Page<MaterialInfoDto> page, MaterialInfoDto dto) throws Exception{
        IPage<MaterialInfoDto> iPage = service.list(page, dto);
        return ResponseEntity.ok(iPage);
    }

    /**
     * 新增/修改
     * @param dto
     * @return
     */
    @ApiOperation(value = "新增、修改")
    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody MaterialInfoDto dto) throws Exception{
        String s = service.save(dto);
        return ResponseUtil.newInstance().add("businessTable", "material_management_pruchase_request_basics").add("businessId", s).ok ("操作成功");
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @ApiOperation(value = "查询")
    @GetMapping("/findById")
    public ResponseEntity<MaterialInfoDto> findById(@RequestParam String id) throws Exception{
        MaterialInfoDto dto = service.findById(id);
        return ResponseEntity.ok(dto);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除")
    @GetMapping("/remove")
    public ResponseEntity<String> remove(@RequestParam String id){
        String s = service.remove(id);
        return ResponseEntity.ok(s);
    }

    /**
     * 根据id修改状态status
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "updateStatusById")
    public void updateStatusById(@RequestBody MaterialInfoDto dto) {
        service.updateStatusById(dto);
    }

    /**
     * 根据采购类型查询商品
     * @param typeId
     * @return
     */
    @ApiOperation(value = "根据采购类型查询商品")
    @GetMapping("/findTradeByTypeId")
    public ResponseEntity findTradeByTypeId(@RequestParam String typeId) {
        List<MaterialDetailed> res = service.findTradeByTypeId(typeId);
        return ResponseEntity.ok(res);
    }
}
