package com.sy.coinage.workshop.controller;

import com.sy.coinage.core.annotation.LoginNotRequired;
import com.sy.coinage.core.constants.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.sy.coinage.core.annotation.ActiveFastJsonProfile;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.aop.annotation.Permission;
import com.sy.coinage.core.aop.annotation.Operation;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.interceptor.Create;
import com.sy.coinage.core.interceptor.Update;
import com.sy.coinage.core.req.*;
import com.sy.coinage.core.vo.*;

import com.sy.coinage.workshop.model.*;
import com.sy.coinage.workshop.service.IProductService;
import com.sy.coinage.workshop.vo.*;
import com.sy.coinage.workshop.req.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * -产品表Controller
 */
@Api(value = "产品表接口", tags = {"产品表接口"})
@RestController
@RequestMapping(value = "/product")
public class ProductController extends BaseController {

    @Autowired
    private IProductService productService;

    /**
     * 产品表分页列表
     */
    @ApiOperation(value = "产品表分页列表")
    @PostMapping("/list")
    @ActiveFastJsonProfile(groups = "list", clazz = Product.class)
    public IPage<Product> list(@RequestBody ProductListReq req, @CurrentUser CacheUserVO user) {
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(req.getSecurities()), Product::getSecurities, req.getSecurities());
        Page<Product> page = new Page<>(req.getPage(), req.getRows());
        IPage<Product> data = this.productService.page(page, queryWrapper);
        return data;
    }

    // /**
    // * -产品表分页列表
    // */
    // @ApiOperation(value = "产品表分页列表")
    // @PostMapping("/list")
    // @ActiveFastJsonProfile(groups = "list",clazz = Product.class)
    // public IPage<ProductVO> list(@RequestBody ProductListReq req) {
    // IPage<ProductVO> data = this.productService.queryPageList(req);
    // return data;
    // }

    /**
     * 产品表详情
     */
    @ApiOperation(value = "产品表-通过ID查询单条数据")
    @PostMapping("/get")
    @LoginNotRequired
    @ActiveFastJsonProfile(groups = "get", clazz = Product.class)
    public ProductVO get(@RequestBody @ApiParam(value = "主键请求bean") @Valid IdReq req) {
        ProductVO data = null;
        Product model = this.productService.getById(req.getId());
        data = new ProductVO();
        BeanUtils.copyProperties(model, data);
        return data;
    }

    /**
     * 产品表列表项
     */
    @GetMapping("/opt")
    public List<OptVO> opt() {
        List<OptVO> data = this.productService.opt();
        return data;
    }

    /**
     * 新增产品表
     *
     * @param req {@link ProductReq}
     * @return
     */
    @ApiOperation(value = "新增产品表")
    @Operation(value = "新增产品表")
    @PostMapping("/add")
    @Permission("product:add")
    public Object add(@RequestBody @ApiParam(value = "新增请求bean") @Validated({Create.class}) ProductReq req,
                    @CurrentUser CacheUserVO user) {
        Product model = new Product();
        BeanUtils.copyProperties(req, model);
        // model.setTenantId(user.getTenantId());
        // model.setCreateBy(user.getName());
        // model.setUpdateBy(user.getName());
        this.productService.save(model);
        return "";
    }

    /**
     * 修改产品表
     */
    @ApiOperation(value = "修改产品表")
    @Operation(value = "修改产品表")
    @PostMapping("/update")
    @Permission("product:update")
    public Object update(@RequestBody @ApiParam(value = "修改请求bean") @Validated({Update.class}) ProductReq req,
                    @CurrentUser CacheUserVO user) {
        Product model = this.productService.getById(req.getId());
        // 验证是否相同租户
        // this.validTenant(user, model.getTenantId());
        BeanUtils.copyProperties(req, model);
        // model.setUpdateBy(user.getName());
        this.productService.updateById(model);
        return "";
    }

    /**
     * 修改产品表状态
     */
    @ApiOperation(value = "修改产品表状态")
    @Operation(value = "修改产品表状态")
    @PostMapping("/status")
    // @Permission("product:update")
    public Integer status(@RequestBody @Valid IdReq req, @CurrentUser CacheUserVO user) {
        Product model = this.productService.getById(req.getId());
        // 验证是否相同租户
        // this.validTenant(user, model.getTenantId());
        if (null != model.getStatus() && model.getStatus().intValue() == Constants.STATUS_FORBIDDEN) {
            model.setStatus(Constants.STATUS_USABLE);
        } else {
            model.setStatus(Constants.STATUS_FORBIDDEN);
        }
        this.productService.updateById(model);
        return model.getStatus();
    }

    /**
     * 删除产品表
     */
    @ApiOperation(value = "删除产品表")
    @Operation(value = "删除产品表")
    @PostMapping("/delete")
    // @Permission("product:delete")
    public Object delete(@RequestBody @ApiParam(value = "主键请求bean") @Valid IdReq req, @CurrentUser CacheUserVO user) {
        Product model = this.productService.getById(req.getId());
        // 验证是否相同租户
        // this.validTenant(user, model.getTenantId());
        // model.setDelFlag(Constants.STATUS_FORBIDDEN);
        // this.productService.updateById(model);
        this.productService.removeById(req.getId());
        return "";
    }

    // private static String[] codes = {"id", "securities", "spec", "crownPrefix", "boxAmount",
    // "amountPerBox", "status", "createTime"};
    // private static String[] names = {"id", "产品名称", "规格", "冠字号代码", "整拍箱数（箱）", "整箱枚数(枚)", "是否启用 1:启用
    // 2:停用", "创建时间"};

    // @Autowired
    // private CustomConfig customConfig;

    /**
     * 导出产品表
     */
    // @PostMapping("/exp")
    // public Object exp(@RequestBody IdsListReq req){
    // FileVO data = new FileVO();
    // LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
    // List<Product> list = this.productService.list(queryWrapper);
    // ExcelExportUtil<Product> excelExport = new ExcelExportUtil<>();
    // String fileName = "产品表.xlsx";
    // data.setName(fileName);
    // FileVO resp = excelExport.exportData(names, codes, list, fileName, customConfig.getAttaPath(),
    // customConfig.getAttaUrl());
    // data.setUrl(resp);
    // return data;
    // }

    /**
     * 导入产品表
     */
    // @PostMapping("/imp")
    // public Object imp(@RequestParam(required=true) MultipartFile file, @CurrentUser CacheUserVO
    // user){
    // Workbook workbook = ExcelUtil.readExcel(file);
    // List<ProductAddReq> list = ExcelImport.convertToList(ProductAddReq.class, workbook, codes, names,
    // 1, 1);
    // this.validData(list);
    // this.product.batchAdd(list, user);
    // return "";
    // }

    /**
     * 验证数据长度,非空
     */
    // private void validData(List<ProductAddReq> list) {
    // if(null != list && list.size() > 0) {
    // for (int i = 0; i < list.size(); i++) {
    // ValidatorUtils.validateEntity(i, list.get(i));
    // }
    // }
    // }

}
