package com.sy.coinage.workshop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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.IScanLogService;
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;

/**
 * -Controller
 */
@Api(value = "接口", tags = {"接口"})
@RestController
@RequestMapping(value = "/scanLog")
public class ScanLogController extends BaseController {

    @Autowired
    private IScanLogService scanLogService;

    /**
     * 分页列表
     */
    @ApiOperation(value = "分页列表")
    @PostMapping("/list")
    @ActiveFastJsonProfile(groups = "list", clazz = ScanLog.class)
    public IPage<ScanLog> list(@RequestBody ScanLogListReq req, @CurrentUser CacheUserVO user) {
        LambdaQueryWrapper<ScanLog> queryWrapper = new LambdaQueryWrapper<>();
        Page<ScanLog> page = new Page<>(req.getPage(), req.getRows());
        IPage<ScanLog> data = this.scanLogService.page(page, queryWrapper);
        return data;
    }

    // /**
    // * -分页列表
    // */
    // @ApiOperation(value = "分页列表")
    // @PostMapping("/list")
    // @ActiveFastJsonProfile(groups = "list",clazz = ScanLog.class)
    // public IPage<ScanLogVO> list(@RequestBody ScanLogListReq req) {
    // IPage<ScanLogVO> data = this.scanLogService.queryPageList(req);
    // return data;
    // }

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

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

    /**
     * 新增
     *
     * @param req {@link ScanLogReq}
     * @return
     */
    @ApiOperation(value = "新增")
    @Operation(value = "新增")
    @PostMapping("/add")
    @Permission("scanLog:add")
    public Object add(@RequestBody @ApiParam(value = "新增请求bean") @Validated({Create.class}) ScanLogReq req,
                      @CurrentUser CacheUserVO user) {
        ScanLog model = new ScanLog();
        BeanUtils.copyProperties(req, model);
        // model.setCreateBy(user.getName());
        // model.setUpdateBy(user.getName());
        this.scanLogService.save(model);
        return "";
    }

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

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

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

    // private static String[] codes = {"id", "rfid", "serialNumber", "isQuality", "isWhole", "qty",
    // "crownPrefix", "created", "wareId", "opType", "status", "securities", "securitiesId"};
    // private static String[] names = {"id", "rfid", "物料编号", "质量标识 1:无质量问题 2:有质量问题", "是否为整拍 1:整拍 2:
    // 拆拍", "拆拍的箱子数量", "冠字号前缀", "扫描时间", "仓库编号:101:造币一部临时库 102:造币二部临时库103:造币三部临时库 201:主库一 202 主库二
    // （同一主库下的分库）", "操作类型: 2 入库 3 出库", "1:扫描正常 2:扫描异常（没在基本信息表的rfid）", "品种", "品种ID"};

    // @Autowired
    // private CustomConfig customConfig;

    /**
     * 导出
     */
    // @PostMapping("/exp")
    // public Object exp(@RequestBody IdsListReq req){
    // FileVO data = new FileVO();
    // LambdaQueryWrapper<ScanLog> queryWrapper = new LambdaQueryWrapper<>();
    // List<ScanLog> list = this.scanLogService.list(queryWrapper);
    // ExcelExportUtil<ScanLog> 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<ScanLogAddReq> list = ExcelImport.convertToList(ScanLogAddReq.class, workbook, codes, names,
    // 1, 1);
    // this.validData(list);
    // this.scanLog.batchAdd(list, user);
    // return "";
    // }

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

}
