package com.nantian.cloud.dbcapp.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nantian.cloud.dbcapp.entity.TEnterpriseProduct;
import com.nantian.cloud.dbcapp.service.TEnterpriseProductService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tEnterpriseProduct")
public class TEnterpriseProductController {
    @Autowired
    private TEnterpriseProductService tEnterpriseProductService;

    @GetMapping("/getList")
    @SysLog("获取企业产品关联表")
    @ApiOperation(value = "获取企业产品关联表", notes = "获取企业产品关联表")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getList(String epc){
        if (epc == null) {
            return R.failed("企业信用编码为空，查询失败！");
        }
        return R.ok(tEnterpriseProductService.getList(epc));
    }

    @GetMapping("/getListByProductName")
    @SysLog("获取企业产品关联表")
    @ApiOperation(value = "获取企业产品关联表", notes = "获取企业产品关联表")
    @ApiImplicitParam(name = "epc", value = "企业信用编码", required = true, dataType = "string", paramType = "path")
    public R getListByProductName(String productName){
        return R.ok(tEnterpriseProductService.getListByProductName(productName));
    }

    @PostMapping("/addProduct")
    @SysLog("新增企业产品关联表")
    @ApiOperation(value = "新增企业产品关联表", notes = "新增企业产品关联表")
    @ApiImplicitParam(name = "tEnterpriseProduct", value = "新增企业产品关联表", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody TEnterpriseProduct tEnterpriseProduct) {
        TEnterpriseProduct t = new TEnterpriseProduct();
        if(tEnterpriseProductService.getOne(Wrappers.query(tEnterpriseProduct))!=null){
            return R.failed("记录已经存在不能重复添加！");
        }
        Boolean state=tEnterpriseProductService.save(tEnterpriseProduct);
        return state ? R.ok() : R.failed();
    }


    @ApiOperation(value = "修改企业产品关联表", notes = "修改企业产品关联表")
    @SysLog("修改企业产品关联表")
    @PutMapping("/updateProduct")
    public R updateCorrect(@RequestBody TEnterpriseProduct tEnterpriseProduct){
        return R.ok(tEnterpriseProductService.updateById(tEnterpriseProduct));
    }

    @ApiOperation(value = "删除企业产品关联表", notes = "删除企业产品关联表")
    @SysLog("删除企业产品关联表")
    @DeleteMapping("/deleteProduct")
    public R delete(Integer id) {
        TEnterpriseProduct tEnterpriseProduct = tEnterpriseProductService.getById(id);
        if (tEnterpriseProduct == null) {
            return R.failed("企业产品关联表不存在，删除失败！");
        }

        return R.ok(tEnterpriseProductService.removeById(id));
    }
}
