package com.xmomen.module.beforehandpackagerecord.controller;

import com.xmomen.framework.exception.BusinessException;
import com.xmomen.framework.mybatis.page.Page;
import com.xmomen.module.base.constant.AppConstants;
import com.xmomen.module.logger.Log;
import com.xmomen.module.beforehandpackagerecord.model.BeforehandPackageRecordQuery;
import com.xmomen.module.beforehandpackagerecord.model.BeforehandPackageRecordModel;
import com.xmomen.module.beforehandpackagerecord.service.BeforehandPackageRecordService;

import org.apache.commons.io.IOUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.result.ExcelImportResult;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.poi.exception.excel.ExcelImportException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.io.Serializable;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;


@RestController
@RequestMapping(value = "/beforehandPackageRecord")
public class BeforehandPackageRecordController {

    @Autowired
    BeforehandPackageRecordService beforehandPackageRecordService;

    // 查询预包装记录表列表
    @RequestMapping(method = RequestMethod.GET)
    @Log(actionName = "查询预包装记录表列表")
    public Page<BeforehandPackageRecordModel> getBeforehandPackageRecordList(@RequestParam(value = "limit") Integer limit,
                                                                             @RequestParam(value = "offset") Integer offset,
                                                                             @RequestParam(value = "id", required = false) String id,
                                                                             @RequestParam(value = "ids", required = false) String[] ids,
                                                                             @RequestParam(value = "excludeIds", required = false) String[] excludeIds) {
        // 创建查询对象
        BeforehandPackageRecordQuery beforehandPackageRecordQuery = new BeforehandPackageRecordQuery();
        // 设置查询条件
        beforehandPackageRecordQuery.setId(id);
        beforehandPackageRecordQuery.setExcludeIds(excludeIds);
        // 获取当前用户ID
        Integer userId = (Integer) SecurityUtils.getSubject().getSession().getAttribute(AppConstants.SESSION_USER_ID_KEY);
        // 设置查询条件
        beforehandPackageRecordQuery.setInsertUserId(userId);
        beforehandPackageRecordQuery.setIds(ids);
        // 调用服务层方法获取分页对象
        return beforehandPackageRecordService.getBeforehandPackageRecordModelPage(limit, offset, beforehandPackageRecordQuery);
    }

    // 查询单个预包装记录表
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @Log(actionName = "查询预包装记录表")
    public BeforehandPackageRecordModel getBeforehandPackageRecordById(@PathVariable(value = "id") String id) {
        // 调用服务层方法获取单个对象
        return beforehandPackageRecordService.getOneBeforehandPackageRecordModel(id);
    }

    // 新增预包装记录表
    @RequestMapping(method = RequestMethod.POST)
    @Log(actionName = "新增预包装记录表")
    public BeforehandPackageRecordModel createBeforehandPackageRecord(@RequestBody @Valid BeforehandPackageRecordModel beforehandPackageRecordModel) {
        // 调用服务层方法新增对象
        return beforehandPackageRecordService.createBeforehandPackageRecord(beforehandPackageRecordModel);
    }

    // 更新预包装记录表
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    @Log(actionName = "更新预包装记录表")
    public void updateBeforehandPackageRecord(@PathVariable(value = "id") String id,
                                              @RequestBody @Valid BeforehandPackageRecordModel beforehandPackageRecordModel) {
        // 调用服务层方法更新对象
        beforehandPackageRecordService.updateBeforehandPackageRecord(beforehandPackageRecordModel);
    }

    // 删除单个预包装记录表
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @Log(actionName = "删除单个预包装记录表")
    public void deleteBeforehandPackageRecord(@PathVariable(value = "id") String id) {
        // 调用服务层方法删除对象
        beforehandPackageRecordService.deleteBeforehandPackageRecord(id);
    }

    // 批量删除预包装记录表
    @RequestMapping(method = RequestMethod.DELETE)
    @Log(actionName = "批量删除预包装记录表")
    public void deleteBeforehandPackageRecords(@RequestParam(value = "ids") String[] ids) {
        // 调用服务层方法批量删除对象
        beforehandPackageRecordService.deleteBeforehandPackageRecord(ids);
    }


}
