package com.tofly.feesapi.dzfp.controller;

import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.common.model.CollectionData;
import com.tofly.feesapi.dzfp.entity.EiSpbm;
import com.tofly.feesapi.dzfp.mapper.EiSpbmMapper;
import com.tofly.feesapi.dzfp.service.EiSpbmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eispbm")
@Api(tags="电子发票-商品编码接口")
public class EiSpbmController {

    @Autowired
    private  EiSpbmService eiSpbmService;
    @Autowired
    private  EiSpbmMapper eiSpbmMapper;

    @GetMapping("/list")
    @ApiOperation(value = "商品编码list")
    public ResultRespone getei_spbmList( String searchText) {
        List<Map<String, Object>> list = eiSpbmMapper.selectListMap(searchText);
        return ResultRespone.success(new CollectionData(list, list.size()));
    }

    @GetMapping("/{code}" )
    @ApiOperation(value = "通过ID查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "ID", required = true, dataType = "String")
    })
    public ResultRespone getById(@PathVariable("code" ) String code) {
      return ResultRespone.success(eiSpbmService.getById(code));
    }

    @ApiOperation(value = "新增电子发票-商品编码")
    @PostMapping
    public ResultRespone save(@RequestBody EiSpbm eiSpbm) {
        eiSpbm.setCode(String.format("%02d", eiSpbmMapper.selectMaxId()+1));
        return ResultRespone.success(eiSpbmService.save(eiSpbm));
    }

    @ApiOperation(value = "修改电子发票-商品编码")
    @PutMapping
    public ResultRespone updateById(@RequestBody EiSpbm eiSpbm) {
      return ResultRespone.success(eiSpbmService.updateById(eiSpbm));
    }

    @ApiOperation(value = "通过ID删除电子发票-商品编码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "ID", required = true, dataType = "String")
    })
    @DeleteMapping("/{code}" )
    public ResultRespone removeById(@PathVariable String code) {
      return ResultRespone.success(eiSpbmService.removeById(code));
    }
}
