package com.sunda.spmsweb.wmscontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsorder.service.IFileOperationService;
import com.sunda.spmsuser.entity.SpmsUser;
import com.sunda.spmswms.service.IWhsOperateLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.List;
import java.util.Map;

/**
 * <p>
 * 库存操作日志表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-04-14
 */
@RestController
@RequestMapping("/whsOperateLog")
@Api(tags = "仓库/储位库存操作日志", description = "仓库/储位库存操作日志")
public class WhsOperateLogController {

    @Autowired
    IWhsOperateLogService iWhsOperateLogService;

    @Autowired
    IFileOperationService iFileOperationService;

    @RequestMapping("/getWhsOperateLog")
    @ApiOperation(value = "获取储位操作日志信息", notes = "获取储位操作日志信息，示例储位编码：b95946ac1dab42b79ade3438acb0b538", httpMethod = "GET")
    public ResponseResult getWhsOperateLog(String uuid) {

        try {
            return ResponseResult.success().add("whsOperateLogList", iWhsOperateLogService.getWhsOperateLog(uuid));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.error("获取储位操作日志信息出错");
        }
    }

    @PostMapping("/getWhsDeskAccount")
    @ApiOperation(value = "根据条件查询库存台账——全部数据", notes = "根据条件查询库存台账" +
            "{\n" +
            "    \"werks\": \"CN01\",\n" +
            "    \"whsLocationCode\": \"1061\",\n" +
            "    \"documentType\": \"1\", //1自管仓仓库库存日志；2自管仓储位库存日志；3海外仓仓库库存日志；4海外仓储位库存日志；\n" +
            "    \"storageNo\": \"A010101\",\n" +
            "    \"materialNo\": \"2019192\",\n" +
            "    \"boxNote\": \"BJ-BOXNOMBER\",\n" +
            "    \"logCategory\": \"X\", // X-备件 M-贸易\n" +
            "    \"operationType\": \"1\", //1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)；\n" +
            "    \"createTime\": [\n" +
            "        \"2021-04-01\",\n" +
            "        \"2021-05-01\"\n" +
            "    ],\n" +
            "    \"userId\": \"999904\",\n" +
            "}")
    public ResponseResult getWhsDeskAccount(@RequestBody JSONObject filterCriteria) {
        return iWhsOperateLogService.getWhsDeskAccount(filterCriteria);
    }




//    @RequestMapping("/exportWhsDeskAccount")
//    @ApiOperation(value = "",notes = "",
//            httpMethod = "GET")
//    public ResponseResult exportWhsDeskAccount(String uuid){
    @PostMapping("/exportWhsDeskAccount")
    @ApiOperation(value = "根据条件查询库存台账——全部数据", notes = "根据条件查询库存台账" +
            "{\n" +
            "    \"werks\": \"CN01\",\n" +
            "    \"whsLocationCode\": \"1061\",\n" +
            "    \"documentType\": \"1\", //1自管仓仓库库存日志；2自管仓储位库存日志；3海外仓仓库库存日志；4海外仓储位库存日志；\n" +
            "    \"storageNo\": \"A010101\",\n" +
            "    \"materialNo\": \"2019192\",\n" +
            "    \"boxNote\": \"BJ-BOXNOMBER\",\n" +
            "    \"logCategory\": \"X\", // X-备件 M-贸易\n" +
            "    \"operationType\": \"1\", //1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)；\n" +
            "    \"createTime\": [\n" +
            "        \"2021-04-01\",\n" +
            "        \"2021-05-01\"\n" +
            "    ],\n" +
            "    \"userId\": \"999904\",\n" +
            "}")
    public ResponseResult exportWhsDeskAccount(@RequestBody JSONObject filterCriteria){
        try {
//            JSONObject filterCriteria = new JSONObject();
//            String[] createTime = new String[2];
//            createTime[0] = "2023-08-21 16:00:00";
//            createTime[1] = "2023-08-28 16:00:00";
//            filterCriteria.put("werks","CN01" );
//            filterCriteria.put("whsLocationCode","1061" );
//            filterCriteria.put("createTime",createTime );
            ResponseResult responseResult = iWhsOperateLogService.getWhsDeskAccount(filterCriteria);
            List<Map<String,Object>> data = (List<Map<String,Object>>)responseResult.getData().get("whsOperateLog");
            String filename = "WhsInsOutDtl_" + System.currentTimeMillis()+".xlsx";
            // 1.附件缓存路径
            String excelPath = iFileOperationService.exportWhsDeskAccount(data, filename);
            if (excelPath.isEmpty()){
                return ResponseResult.error("附件创建失败");
            }
            System.out.println("附件缓存地址：" + excelPath);
            // 2.上传附件至minIO文件服务器
            String fileUrl = iFileOperationService.fileUploader(filename, excelPath);
            if (fileUrl.isEmpty()){
                return ResponseResult.error("附件上传失败:" + excelPath + "--" + fileUrl);
            }
            System.out.println("附件上传地址:" + fileUrl);
            // 3.返回附件地址，上传OA
            return ResponseResult.success()
                    .add("fileUrl",fileUrl);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @PostMapping("/getWhsDeskAccountPage")
    @ApiOperation(value = "根据条件查询库存台账——分页数据", notes = "根据条件查询库存台账" +
            "{\n" +
            "    \"werks\": \"CN01\",\n" +
            "    \"whsLocationCode\": \"1061\",\n" +
            "    \"documentType\": \"1\", //1自管仓仓库库存日志；2自管仓储位库存日志；3海外仓仓库库存日志；4海外仓储位库存日志；\n" +
            "    \"storageNo\": \"A010101\",\n" +
            "    \"materialNo\": \"2019192\",\n" +
            "    \"boxNote\": \"BJ-BOXNOMBER\",\n" +
            "    \"logCategory\": \"X\", // X-备件 M-贸易\n" +
            "    \"operationType\": \"1\", //1入库(加仓库库存)；2上架(加储位库存)；3下架(减储位库存)；4出库(减仓库库存)；\n" +
            "    \"createTime\": [\n" +
            "        \"2021-04-01\",\n" +
            "        \"2021-05-01\"\n" +
            "    ],\n" +
            "    \"userId\": \"999904\",\n" +
            "    \"pageNo\": 1,\n" +
            "    \"pageSize\": 20\n" +
            "}")
    @RequiresPermissions("whsOperateLog-getWhsDeskAccountPage")
    public ResponseResult getWhsDeskAccountPage(@RequestBody JSONObject filterCriteria) {
        return iWhsOperateLogService.getWhsDeskAccountPage(filterCriteria);
    }

}
