/*
 * Author: wangjian
 * 生成controller
 */
package com.tofly.install.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.entity.ResultRespone;
import com.tofly.common.log.annotation.ToFlyAppLog;
import com.tofly.install.entity.MaterialUse;
import com.tofly.install.service.MaterialUseService;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Arrays;
import java.util.List;
/**
 * 报装业务表
 *
 * @author admin
 * @date Fri Nov 25 00:00:00 CST 2022
 */
@RestController
@AllArgsConstructor
@RequestMapping("/materialUse")
@Api(tags="用料使用接口")
public class MaterialUseController {

    private final  MaterialUseService materialUseService;

    /**
    * 分页查询
    * @param page 分页对象
    * @param materialType 库存物资类型表
    * @return
    */
    @GetMapping("/page" )
    @ApiOperation(value = "分页查询")
    public ResultRespone getMaterialTypePage(Page page, @ModelAttribute MaterialUse MaterialUse) {
      return ResultRespone.success(materialUseService.page(page, Wrappers.query(MaterialUse)));
    }


    /**
     * 通过id查询报装业务表
     * @param id id
     * @return ResultRespone
     */
    @GetMapping("/{id}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    public ResultRespone getById(@PathVariable("id" ) Long id) {
      return ResultRespone.success(materialUseService.getById(id));
    }

    /**
     * 新增报装业务
     * @param applyInstall 报装业务
     * @return ResultRespone
     */
    @ToFlyAppLog(title="新增用料使用" )
    @ApiOperation(value = "新增用料使用")
    @PostMapping
    public ResultRespone save(@RequestBody String objects) {    	
    	List<MaterialUse> dataList = JSONObject.parseArray(objects,MaterialUse.class);    	
    	for(MaterialUse materialUse:dataList) {
    		materialUseService.save(materialUse);
    	}    	    	
    	return ResultRespone.success("新增成功");
    }

    /**
     * 修改报装业务
     * @param applyInstall 报装业务表
     * @return ResultRespone
     */
    @ToFlyAppLog(title="修改用料使用" )
    @ApiOperation(value = "修改用料使用")
    @PutMapping
    public ResultRespone updateById(@RequestBody String objects) {    	
    	List<MaterialUse> dataList = JSONObject.parseArray(objects,MaterialUse.class);    
    	for(MaterialUse materialUse:dataList) {
    		materialUseService.updateById(materialUse);
    	}  
    	return ResultRespone.success("修改完成");
    }

    /**
     * 通过id删除报装业务
     * @param id id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id删除用料使用" )
    @ApiOperation(value = "通过ID删除用料使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "Long")
    })
    @DeleteMapping("/{id}" )
    public ResultRespone removeById(@PathVariable Long id) {
      return ResultRespone.success(materialUseService.removeById(id));
    }

    /**
     * 通过id批量删除库存物资类型表
     * @param ids id
     * @return ResultRespone
     */
    @ToFlyAppLog(title="通过id批量删除用料使用" )
    @ApiOperation(value = "通过ID批量删除用料使用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "批量删除，多个id以逗号隔开", required = true, dataType = "String")
    })
    @DeleteMapping("/deleteByIds" )
    public ResultRespone removeById(String ids) {
        return ResultRespone.success(materialUseService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
