package com.org.hktshop.controller.encapsulation;

import com.org.hktshop.base.BaseResult;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.encapsulation.FcMaterial;
import com.org.hktshop.service.encapsulation.FcMaterialService;
import com.org.hktshop.utils.HttpCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/material")
@Api(tags = "材料接口")
public class FcMaterialController {
    @Autowired
    private FcMaterialService materialService;

    @PostMapping("/save")
    @ApiOperation(value = "新增材料", notes = "新增材料")
    public BaseResult insert(@RequestBody FcMaterial fcMaterial) {
        try {
            materialService.saveMaterial(fcMaterial);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(),HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/deleteById")
    @ApiOperation(value = "删除材料", notes = "删除材料")
    public BaseResult deleteById(@RequestParam String id) {
        try {
            materialService.deleteMaterial(id);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(),HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改材料", notes = "修改材料")
    public BaseResult update(@RequestBody FcMaterial fcMaterial) {
        try {
            materialService.updateMaterial(fcMaterial);
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(),HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/getById")
    @ApiOperation(value = "材料 id查", notes = "材料 id查")
    public BaseResult<FcMaterial> getById(@RequestParam String id) {
        try {
            return BaseResult.successWithData(materialService.getMaterial(id));
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(),HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @PostMapping("/findPage")
    @ApiOperation(value = "材料 分页查", notes = "材料 分页查")
    public BaseResult<Page<FcMaterial>> findPage(@RequestBody FcMaterial fcMaterial) {
        try {
            return BaseResult.successWithData(materialService.getHktMaterialPage(fcMaterial));
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(),HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }


    @GetMapping("/getType")
    @ApiOperation(value = "获取类型", notes = "获取类型")
    @ApiImplicitParam(name = "id", value = "材料父级id", required = true, paramType = "query")
    public BaseResult<List<FcMaterial>> getType(@RequestParam String id) {
        try {
            return BaseResult.successWithData(materialService.getMaterialByParentId(id));
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(),HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }
}
