package com.zfire.mall.manager.controller.stock;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.admin.respDto.AdminUserDto;
import com.zfire.mall.manager.bean.stock.*;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.stock.StockForceLogic;
import com.zfire.mall.manager.plus.entity.AdminUserForceStock;
import com.zfire.mall.manager.utils.CommonUtils;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author CHANGE
 */
@Slf4j
@RestController
@Api(value = "账号强制仓库选择", tags ={"账号强制仓库选择"} )
@RequestMapping(value = "/stock/forceUser", produces = "application/json; charset=utf-8")
public class StockForceUserController {

    @Autowired
    StockForceLogic stockForceLogic;



    @ApiOperation(value = "仓库品类选择列表")
    @GetMapping("/list")
    public ResponseHelper<List<StockCategory>> list(

    ) throws Exception {
        List<StockCategory> stockBeans = stockForceLogic.list();
        return ResponseHelper.success(stockBeans);
    }

    @ApiOperation(value = "添加账号强制仓")
    @PostMapping("/add")
    public ResponseHelper add(
            @RequestBody StockForceUserAdd stockForceUserAdd
    ) throws Exception {

        stockForceLogic.addForceUser(stockForceUserAdd);
        return ResponseHelper.success();

    }

    @ApiOperation(value = "用户的强制仓选择")
    @GetMapping("/forceStockList")
    public ResponseHelper<List<AdminUserForceStock>> forceStockList(
            @ApiParam(required = true, value = "账号ID") @RequestParam(required = true) String adminUserId
            ) throws Exception {
        List<AdminUserForceStock> stockBeans = stockForceLogic.forceStockList(adminUserId);
        return ResponseHelper.success(stockBeans);
    }


    @GetMapping("/export")
    @ApiOperation(value = "导出账号强制仓")
    public void exportMch(

            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<AdminUserForceStockExcel> page = stockForceLogic.listExcel(1, -1);
        ExcelData excelData = stockForceLogic.listExcelEx(page.getRecords());
        ExcelUtils.exportExcel(request,response,"force.xlsx",excelData);
    }

    @PostMapping("/batch/update")
    @ApiOperation("批量导入强制仓")
    public ResponseHelper batchUpdate(
            @ApiParam(required = true, value = "附件") @RequestPart("file") MultipartFile file,
            HttpServletRequest request,HttpServletResponse response
    ) throws Exception {

        stockForceLogic.batchUpdate(file);
        return ResponseHelper.success();
    }

    @GetMapping("/downloadGongcheng")
    @ApiOperation("批量导入强制仓模板下载")
    public void downloadGongcheng(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("导入账号强制仓库.xlsx",response);
    }


}

