package com.hwrj.cloud.admin.controller;

import com.hwrj.cloud.admin.dto.UmsMemberOriginProductParam;
import com.hwrj.cloud.admin.model.UmsMemberOriginProduct;
import com.hwrj.cloud.admin.service.UmsMemberOriginProductService;
import com.hwrj.cloud.common.api.CommonPage;
import com.hwrj.cloud.common.api.CommonResult;
import com.hwrj.cloud.common.exception.GlobalException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @className:
 * @description:
 * @author: lym
 * @date: 2020/9/7 9:57
 */
@RestController
@RequestMapping("/originProduct")
@Api(description = "生产地产出物管理")
public class UmsMemberOriginProductController {

    @Autowired
    private UmsMemberOriginProductService umsMemberOriginProductService;

    @ApiOperation(value = "查询列表",notes = "查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "originProduct",value = "",required = true,paramType = "UmsMemberOriginProductParam")
    })
    @PostMapping("/list")
    public CommonResult list(@RequestBody UmsMemberOriginProductParam originProduct){
        List<UmsMemberOriginProduct> list = umsMemberOriginProductService.list(originProduct);
        return CommonResult.success(CommonPage.restPage(list));
    }

    @ApiOperation(value = "添加产出物信息",notes = "添加产出物信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "originProduct",value = "产出物信息",required = true,paramType = "UmsMemberOriginProductParam")
    })
    @PostMapping("/save")
    public CommonResult save(@RequestBody @Valid UmsMemberOriginProductParam originProduct){
        int i = umsMemberOriginProductService.save(originProduct);
        if (i > 0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "修改产出物信息",notes = "修改产出物信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "originProduct",value = "产出物信息",required = true,paramType = "UmsMemberOriginProductParam")
    })
    @PostMapping("/update")
    public CommonResult update(@RequestBody @Valid UmsMemberOriginProductParam originProduct){
        if (StringUtils.isEmpty(originProduct.getId())){
            throw new GlobalException(500,"主键不为空!");
        }
        int i = umsMemberOriginProductService.update(originProduct);
        if (i > 0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "删除产出物信息",notes = "删除产出物信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "主键",required = true,paramType = "String")
    })
    @GetMapping("/delete")
    public CommonResult delete(@RequestParam("id") long id){
        int i = umsMemberOriginProductService.delete(id);
        if (i > 0){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }
}
