package com.kcim.web;

import com.kcim.common.util.PageUtils;
import com.kcim.common.util.Result;
import com.kcim.dao.model.dto.ProductDTO;
import com.kcim.dao.model.dto.ProductEditDTO;
import com.kcim.vo.CommonVO;
import com.kcim.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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


/**
 * 成本管理表
 *
 * @author huangrui
 * @date 2021-07-28 18:44:20
 */
@Api(tags = "收入及成本项目管理")
@RestController
@RequestMapping("product")
public class ProductController {

    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    /**
     * 分页查询列表
     */
    @ApiOperation("成本项目列表")
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1", value = "current") Integer current,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "filter", required = false) String filter){
        return Result.ok(productService.selectList(current, pageSize,filter));
    }

    @ApiOperation("新增成本项目")
    @PostMapping("/save")
    public Result save(@RequestBody @Valid ProductDTO productDTO){
		productService.saveProduct(productDTO);
        return Result.ok();
    }

    /**
     * 修改
     */
    @ApiOperation("编辑成本项目")
    @PostMapping("/edit")
    public Result update(@RequestBody @Valid ProductEditDTO productEditDTO){
        productService.editProduct(productEditDTO);
        return Result.ok();
    }

    /**
     * 删除
     */
    @ApiOperation("删除成本项目")
    @PostMapping("/delete")
    public Result delete(@RequestBody Integer[] ids){
        productService.deleteByIds(Arrays.asList(ids));
        return Result.ok();
    }

    @ApiOperation("修改或新增会计科目对照选择下拉列表")
    @GetMapping("/getProducts")
    public Result getProducts() {
        List<CommonVO> list = productService.getProducts();
        return Result.ok(list);
    }

}
