package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.ProductInfo;
import com.nantian.cloud.jljc.entity.TradeProduct;
import com.nantian.cloud.jljc.service.ProductInfoService;
import com.nantian.cloud.jljc.service.TradeProductService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;


/**
 * 产品信息
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/productInfo")
@Api(value = "productInfo", tags = "产品信息")
public class ProductInfoController {
    private final ProductInfoService productInfoService;
    private final TradeProductService tradeProductService;

    /**
     * 分页查询
     *
     * @param page        分页对象
     * @param productInfo
     * @return
     */
    @GetMapping("/page")
    @SysLog("分页获取产品信息")
    @ApiOperation(value = "分页获取产品信息", notes = "分页获取产品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "productInfo", value = "产品信息对象（查询条件：产品代码productNum、产品名称productName、行业代码tradeProductCode、行业名称tradeName）", required = false, dataType = "json", paramType = "path")
    })
    public R getProductInfoPage(Page page, ProductInfo productInfo) {
        //return R.ok(productInfoService.page(page, Wrappers.query(productInfo)));
        productInfo.setDelFlag("1");  //筛选未删除项
        return R.ok(productInfoService.getPageScope(page, productInfo));
    }


    /**
     * 获取所有产品信息列表
     */
    @GetMapping("/list")
    @SysLog("获取所有产品信息列表")
    @ApiOperation(value = "获取所有产品信息列表", notes = "获取所有产品信息列表")
    @ApiImplicitParam(name = "productInfo", value = "产品信息对象", required = false,  paramType = "path")
    public R getProductInfoList() {
        return R.ok(productInfoService.getProductInfoList());
    }


    /**
     * 获取树形结构
     */
    @GetMapping("/tree")
    @SysLog("获取行业-产品树形结构")
    @ApiOperation(value = "获取行业-产品树形结构", notes = "获取行业-产品树形结构")
    public R getTradeProductTree() {
        return R.ok(productInfoService.getTreeByTrade());
    }

    /**
     * 获取树形结构
     */
    @GetMapping("/productTree")
    @SysLog("获取产品树形结构")
    @ApiOperation(value = "获取产品树形结构", notes = "获取产品树形结构")
    public R getTree() {
        return R.ok(productInfoService.getTree());
    }

    /**
     * 根据产品名称获取产品信息
     */
    @GetMapping("/infoByName")
    @SysLog("根据id获取产品信息")
    @ApiOperation(value = "根据id获取产品信息", notes = "根据id获取产品信息")
    @ApiImplicitParam(name = "name", value = "产品名称", required = true, dataType = "String", paramType = "path")
    public R getInfoByName(String name) {
        if (null == name | "" == name) {
            return R.failed("产品名称不能为空");
        } else {
            return R.ok(productInfoService.getInfoByName(name));
        }
    }

    /**
     * 新增自定义产品，获取其自定义编码
     */
    @GetMapping("/getCustomCode")
    @SysLog("新增自定义产品，获取其自定义编码")
    @ApiOperation(value = "新增自定义产品，获取其自定义产品代码", notes = "新增自定义产品，获取其自定义产品代码")
    @ApiImplicitParam(name = "prevProductNum", value = "上级产品代码", required = true, dataType = "String", paramType = "path")
    public R getCustomCode(String prevProductNum) {
        return R.ok(productInfoService.getCustomCode(prevProductNum));
    }

    /**
     * 根据产品名称获取产品信息
     */
    @GetMapping("/{productInfoId}")
    @SysLog("根据产品名称获取产品信息")
    @ApiOperation(value = "根据id查询产品信息", notes = "根据id查询产品信息")
    @ApiImplicitParam(name = "productInfoId", value = "id", required = true, dataType = "int", paramType = "path")
    public R getById(@PathVariable Integer productInfoId) {
        return R.ok(productInfoService.getById(productInfoId));
    }

    /**
     * 保存
     *
     * @param productInfo
     * @return R
     */
    @PostMapping
    @SysLog("新增产品信息")
    @ApiOperation(value = "新增产品信息", notes = "新增产品信息")
    @ApiImplicitParam(name = "ProductInfo", value = "产品信息对象", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody ProductInfo productInfo) {
        //判断产品信息是否存在
        ProductInfo p = new ProductInfo();
        p.setProductNum(productInfo.getProductNum());
        if (productInfoService.list(Wrappers.query(p)).size() > 0) {
            return R.failed("产品代码已存在，新增失败！");
        }
        ProductInfo p2 = new ProductInfo();
        p2.setProductName(productInfo.getProductName());
        if (productInfoService.list(Wrappers.query(p2)).size() > 0) {
            return R.failed("产品名称已存在，新增失败！");
        }

        ScUser u = SecurityUtils.getUser();
        productInfo.setUserid(u.getId());
        productInfo.setDeptId(u.getDeptId());
        productInfo.setUpdateTime(new Date());
        productInfo.setDelFlag("1");
        if (productInfo.getTradeProductCode() != null) {
            productInfo = getTradeName(productInfo);
        }
        return R.ok(productInfoService.save(productInfo));
    }

    /**
     * 修改
     *
     * @param productInfo
     * @return R
     */
    @PutMapping
    @SysLog("修改产品信息")
    @ApiOperation(value = "修改产品信息", notes = "修改产品信息")
    @ApiImplicitParam(name = "ProductInfo", value = "产品信息对象", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody ProductInfo productInfo) {
        //判断产品信息是否存在，修改时，允许存在一条代码名称相同的数据，但是id需一致，否则报错
        ProductInfo p = new ProductInfo();
        p.setProductNum(productInfo.getProductNum());
        List<ProductInfo> list = productInfoService.list(Wrappers.query(p));
        if (list.size() > 0 && productInfo.getId().longValue() != list.get(0).getId().longValue()) {
            return R.failed("产品代码已存在，修改失败！");
        }
        ProductInfo p2 = new ProductInfo();
        p2.setProductName(productInfo.getProductName());
        List<ProductInfo> list2 = productInfoService.list(Wrappers.query(p2));
        if (list2.size() > 0 && productInfo.getId().longValue() != list2.get(0).getId().longValue()) {
            return R.failed("产品名称已存在，修改失败！");
        }

        ScUser u = SecurityUtils.getUser();
        productInfo.setUserid(u.getId());
        productInfo.setUpdateTime(new Date());
        if (productInfo.getTradeProductCode() != null) {
            productInfo = getTradeName(productInfo);
        }
        return R.ok(productInfoService.updateById(productInfo));
    }

    private ProductInfo getTradeName(@RequestBody ProductInfo productInfo) {
        TradeProduct tradeProduct = new TradeProduct();
        tradeProduct.setCode(productInfo.getTradeProductCode());
        List<TradeProduct> list = tradeProductService.list(Wrappers.query(tradeProduct));
        if (list.size() > 0 && list.get(0) != null) {
            productInfo.setTradeName(list.get(0).getName());
        }
        return productInfo;
    }

    /**
     * 删除
     *
     * @param productInfoId
     * @return R
     */
    @DeleteMapping("/{productInfoId}")
    @SysLog("删除产品信息")
    @ApiOperation(value = "删除产品信息", notes = "删除产品信息")
    @ApiImplicitParam(name = "productInfoId", value = "id", required = true, dataType = "int", paramType = "path")
    public R removeById(@PathVariable Integer productInfoId) {
        ProductInfo productInfo = productInfoService.getById(productInfoId);
        if (productInfo == null) {
            return R.failed("产品信息不存在，删除失败！");
        }
        ScUser u = SecurityUtils.getUser();
        productInfo.setUserid(u.getId());
        productInfo.setUpdateTime(new Date());
        productInfo.setDelFlag("4");
        return R.ok(productInfoService.updateById(productInfo));
    }

}
