package com.ruoyi.web.controller.est;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.ProductDetailVo;
import com.ruoyi.system.domain.vo.ProductVO;
import com.ruoyi.system.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("产品接口")
@RestController
@RequestMapping("api/product")
public class ProductController extends BaseController {
    @Autowired
    public IProductProducttypeService productProducttypeService;
    @Autowired
    public IProductService productService;
    //直流舱系统
    @Autowired
    public IProductDirectcurrentService iProductDirectcurrentService;
    //充电交流舱
    @Autowired
    public IProductChargingService iProductChargingService;
    //放电交流舱
    @Autowired
    public IProductDischargeService iProductDischargeService;
    //半挂车
    @Autowired
    public IProductProductsemitrailerService iProductProductsemitrailerService;
    //牵引头
    @Autowired
    public IProductTractionheadService iProductTractionheadService;
    @Autowired
    public IPropertyService iPropertyService;
    @Autowired
    public ISysUserService iSysUserService;


    @PostMapping("/category")
    @ApiOperation("获取产品分类列表数据子典")
    public AjaxResult productcategory(){
        List<ProductProducttype> productProducttypeList=productProducttypeService.selectProductProducttypeList();
        return AjaxResult.success(productProducttypeList);
    }

    @GetMapping("selectproduct")
    @ApiOperation("获取产品列表")
    public AjaxResult selectproduct(Product product,
                                    Integer pageNum,
                                    Integer pageSize){
        TableDataInfo productList=productService.selectProductListByPage(product,pageNum,pageSize);
        return AjaxResult.success(productList);
    }



    @GetMapping("/{ProductId}")
    @ApiOperation("获取产品详情")
    public AjaxResult productid(@PathVariable(value = "ProductId", required = false)String ProductId){
        ProductVO productVO=new ProductVO();
        //查询对应的产品基础信息
        Product product=productService.selectProductById(ProductId);
        productVO.setId(product.getId());
        productVO.setProductId(product.getProductId());
        productVO.setProductTypeId(product.getProductTypeId());
        productVO.setPicture(product.getPicture());
        productVO.setDocument(product.getDocument());
        productVO.setDescribe(product.getDescribe());
        productVO.setType(product.getType());
        SysUser sysUser=iSysUserService.selectUserById(product.getCreateBy());
        productVO.setCreateBy(sysUser.getUserName());
//        productVO=product;
        //查询产品类型id
        ProductProducttype productProducttype=productProducttypeService.selectProductProducttypeById(product.getProductTypeId());
        //查询根据产品类型查询对应的数据库中的表
        if (productProducttype.getName().equals("直流仓")){
            ProductDirectcurrent productDirectcurrent=iProductDirectcurrentService.selectProductDirectcurrentById(product.getProductId());
            productVO.setProductDirectcurrent(productDirectcurrent);
        }else if(productProducttype.getName().equals("充电交流仓")){
            ProductCharging productCharging=iProductChargingService.selectProductChargingById(product.getProductId());
            productVO.setProductCharging(productCharging);
        }else if(productProducttype.getName().equals("放电交流仓")){
            ProductDischarge productDischarge=iProductDischargeService.selectProductDischargeById(product.getProductId());
            productVO.setProductDischarge(productDischarge);
        }else if(productProducttype.getName().equals("牵引头")){
            ProductTractionhead productTractionhead=iProductTractionheadService.selectProductTractionheadById(product.getProductId());
            productVO.setProductTractionhead(productTractionhead);
        }else if(productProducttype.getName().equals("半挂车")){
            ProductProductsemitrailer productProductsemitrailer=iProductProductsemitrailerService.selectProductProductsemitrailerById(product.getProductId());
            productVO.setProductProductsemitrailer(productProductsemitrailer);
        }
        return AjaxResult.success(productVO);
    }

    @PostMapping("inserproduct")
    @ApiOperation("创建产品")
    public AjaxResult inserproduct(Product product, @RequestBody ProductDetailVo detail){
        int boo=productService.insertProduct(product);
        ProductProducttype productProducttype=productProducttypeService.selectProductProducttypeById(product.getProductTypeId());
        String productId = product.getProductId();
        if (productProducttype.getName().equals("直流仓") && detail.getProductDirectcurrent() != null){
            detail.getProductDirectcurrent().setProductId(productId);
            iProductDirectcurrentService.insertProductDirectcurrent(detail.getProductDirectcurrent());
        }else if(productProducttype.getName().equals("充电交流仓") && detail.getProductCharging() != null){
            detail.getProductCharging().setProductId(productId);
            iProductChargingService.insertProductCharging(detail.getProductCharging());
        }else if(productProducttype.getName().equals("放电交流仓") && detail.getProductDischarge() != null){
            detail.getProductDischarge().setProductId(productId);
            iProductDischargeService.insertProductDischarge(detail.getProductDischarge());
        }else if(productProducttype.getName().equals("牵引头") && detail.getProductTractionhead() != null){
            detail.getProductTractionhead().setProductId(productId);
            iProductTractionheadService.insertProductTractionhead(detail.getProductTractionhead());
        }else if(productProducttype.getName().equals("半挂车") && detail.getProductProductsemitrailer() != null){
            detail.getProductProductsemitrailer().setProductId(productId);
            iProductProductsemitrailerService.insertProductProductsemitrailer(detail.getProductProductsemitrailer());
        }
        return AjaxResult.success(boo);
    }

    @PostMapping("/updateproduct")
    @ApiOperation("更新产品")
    public AjaxResult updateproductid(Product product,
                                      @RequestBody ProductDetailVo detail){
        //需要先获取对应的产品的类型信息
        Product product1=productService.selectProductId(product.getId());
        int boo = productService.updateProduct(product);
        ProductProducttype productProducttype=productProducttypeService.selectProductProducttypeById(product1.getProductTypeId());
        String productId=product1.getProductId();
        if (productProducttype.getName().equals("直流仓") && detail.getProductDirectcurrent() != null){
            ProductDirectcurrent productDirectcurrent=iProductDirectcurrentService.selectProductDirectcurrentById(productId);
            detail.getProductDirectcurrent().setProductId(product.getProductId());
            detail.getProductDirectcurrent().setId(productDirectcurrent.getId());
            iProductDirectcurrentService.updateProductDirectcurrent(detail.getProductDirectcurrent());

        }else if(productProducttype.getName().equals("充电交流仓") && detail.getProductCharging() != null){
            ProductCharging productCharging=iProductChargingService.selectProductChargingById(productId);
            detail.getProductCharging().setProductId(product.getProductId());
            detail.getProductCharging().setId(productCharging.getId());
            iProductChargingService.updateProductCharging(detail.getProductCharging());
        }else if(productProducttype.getName().equals("放电交流仓") && detail.getProductDischarge() != null){
            ProductDischarge productDischarge=iProductDischargeService.selectProductDischargeById(productId);
            detail.getProductDischarge().setProductId(product.getProductId());
            detail.getProductDischarge().setId(productDischarge.getId());
            iProductDischargeService.updateProductDischarge(detail.getProductDischarge());

        }else if(productProducttype.getName().equals("牵引头") && detail.getProductTractionhead() != null){
            ProductTractionhead productTractionhead=iProductTractionheadService.selectProductTractionheadById(productId);

            detail.getProductTractionhead().setProductId(product.getProductId());
            detail.getProductTractionhead().setId(productTractionhead.getId());

            iProductTractionheadService.updateProductTractionhead(detail.getProductTractionhead());
        }else if(productProducttype.getName().equals("半挂车") && detail.getProductProductsemitrailer() != null){
            ProductProductsemitrailer productProductsemitrailer=iProductProductsemitrailerService.selectProductProductsemitrailerById(productId);
            detail.getProductProductsemitrailer().setProductId(product.getProductId());
            detail.getProductProductsemitrailer().setId(productProductsemitrailer.getId());
            iProductProductsemitrailerService.updateProductProductsemitrailer(detail.getProductProductsemitrailer());
        }
        return AjaxResult.success(boo);
    }
    @DeleteMapping("deleteproduct/{ProductId}")
    @ApiOperation("删除产品")
    public AjaxResult deleteproduct(@PathVariable(value = "ProductId", required = false)String ProductId){
        //查询产品
        Product product=productService.selectProductById(ProductId);
        //产品查询
        ProductProducttype productProducttype=productProducttypeService.selectProductProducttypeById(product.getProductTypeId());
        //添加对应产品信息(判断添加的是哪个产品)
        if (productProducttype.getName().equals("直流仓")){
            iProductDirectcurrentService.deleteProductDirectcurrentById(ProductId);
        }else if(productProducttype.getName().equals("充电交流仓")){
            iProductChargingService.deleteProductChargingById(ProductId);
        }else if(productProducttype.getName().equals("放电交流仓")){
            iProductDischargeService.deleteProductDischargeById(ProductId);
        }else if(productProducttype.getName().equals("牵引头")){
            iProductTractionheadService.deleteProductTractionheadById(ProductId);
        }else if(productProducttype.getName().equals("半挂车")){
            iProductProductsemitrailerService.deleteProductProductsemitrailerById(ProductId);
        }
        //删除产品
        int productboole=productService.deleteProductById(ProductId);
        return AjaxResult.success(productboole);
    }

    @PostMapping("/updateproduct/{ProductId}/status")
    @ApiOperation("更改产品状态")
    public AjaxResult updateproductidstatus(@PathVariable(value = "ProductId", required = true)String ProductId,
                                            @RequestParam(name = "status",required = false)String status){
        // enabled: 启用
        if (status.equals("enabled")){
            Product product=new Product();
            product.setProductId(ProductId);
            product.setType(0);
            return AjaxResult.success(productService.updateProduct(product));
        }else if(status.equals("disabled")){
            // disabled: 禁用
            Product product=new Product();
            product.setProductId(ProductId);
            product.setType(0);
            return AjaxResult.success(productService.updateProduct(product));
        }else{
            return AjaxResult.success("更新失败");
        }
    }

}
