package net.lab1024.sa.admin.module.business.wms.base.address.controller;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.wms.base.address.domain.entity.AddressEntity;
import net.lab1024.sa.admin.module.business.wms.base.address.domain.form.AddressAddForm;
import net.lab1024.sa.admin.module.business.wms.base.address.domain.form.AddressQueryForm;
import net.lab1024.sa.admin.module.business.wms.base.address.domain.form.AddressSelect;
import net.lab1024.sa.admin.module.business.wms.base.address.domain.form.AddressUpdateForm;
import net.lab1024.sa.admin.module.business.wms.base.address.domain.vo.AddressExcelVO;
import net.lab1024.sa.admin.module.business.wms.base.address.domain.vo.AddressVO;
import net.lab1024.sa.admin.module.business.wms.base.address.service.AddressQueryService;
import net.lab1024.sa.admin.module.business.wms.base.address.service.AddressService;
import net.lab1024.sa.admin.module.business.wms.base.item.domain.form.ItemQueryForm;
import net.lab1024.sa.admin.module.business.wms.base.item.domain.vo.ItemsExcelVO;
import net.lab1024.sa.admin.module.business.wms.excel.ExportTaskService;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.domain.ValidateList;
import net.lab1024.sa.base.common.util.SmartExcelUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import net.lab1024.sa.base.module.support.operatelog.annotation.OperateLog;
import org.springframework.web.bind.annotation.*;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.domain.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

import static java.lang.Thread.sleep;

/**
 * 收货地址 Controller
 *
 * @author hj
 * @since 2024-12-26 15:35:23
 * copyright 友仓
 */

@RestController
@Tag(name = "收货地址")
@Slf4j
public class AddressController {

    @Resource
    private AddressService addressService;

    @Resource
    private AddressQueryService addressQueryService;

    @Resource
    private ExportTaskService exportTaskService;

    @Operation(summary = "分页查询 @author hj")
    @PostMapping("/address/queryPage")
    @SaCheckPermission("address:query")
    public ResponseDTO<PageResult<AddressVO>> queryPage(@RequestBody @Valid AddressQueryForm queryForm) {
        return ResponseDTO.ok(addressQueryService.queryPage(queryForm));
    }

    @Operation(summary = "添加 @author hj")
    @PostMapping("/address/add")
    @SaCheckPermission("address:add")
    @OperateLog
    public ResponseDTO<String> add(@RequestBody @Valid AddressAddForm addForm) {
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        addForm.setCreateUserId(requestUser.getUserId());
        addForm.setCreateUserName(requestUser.getUserName());
        return addressService.add(addForm);
    }

    @Operation(summary = "更新 @author hj")
    @PostMapping("/address/update")
    @SaCheckPermission("address:update")
    @OperateLog
    public ResponseDTO<String> update(@RequestBody @Valid AddressUpdateForm updateForm) {
        return addressService.update(updateForm);
    }

    @Operation(summary = "批量删除 @author hj")
    @PostMapping("/address/batchDelete")
    @SaCheckPermission("address:batchDelete")
    @OperateLog
    public ResponseDTO<String> batchDelete(@RequestBody ValidateList<Long> idList) {
        return addressService.batchDelete(idList);
    }

    @Operation(summary = "单个删除 @author hj")
    @GetMapping("/address/delete")
    @SaCheckPermission("address:delete")
    @OperateLog
    public ResponseDTO<String> delete(@RequestParam Long addressId) {
        return addressService.delete(addressId);
    }

    @Operation(summary = "地址下拉查询")
    @PostMapping("/address/queryAddress")
    public ResponseDTO<PageResult<AddressVO>> queryAddress(@RequestBody @Valid AddressSelect addressSelect) {
        return ResponseDTO.ok(addressQueryService.queryAddress(addressSelect));
    }

    @Operation(summary = "导入 霍锦")
    @PostMapping("/address/importAddress")
    @SaCheckPermission("address:importAddress")
    @OperateLog
    public ResponseDTO<String> importAddress(@RequestParam MultipartFile file) {
        return addressService.importAddress(file);
    }

    @Operation(summary = "导出 霍锦")
    @PostMapping("/address/exportAddress/{taskId}")
    @SaCheckPermission("address:exportAddress")
    public void exportAddress(@PathVariable String taskId, @RequestBody @Valid AddressQueryForm queryForm, HttpServletResponse response) {
        addressQueryService.exportAddress(taskId,queryForm, response);
        exportTaskService.cleanupTask(taskId);
    }
}
