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

import jakarta.servlet.http.HttpServletResponse;
import net.lab1024.sa.admin.module.business.base.address.domain.entity.AddressEntity;
import net.lab1024.sa.admin.module.business.base.address.domain.form.AddressAddForm;
import net.lab1024.sa.admin.module.business.base.address.domain.form.AddressQueryForm;
import net.lab1024.sa.admin.module.business.base.address.domain.form.AddressSelect;
import net.lab1024.sa.admin.module.business.base.address.domain.form.AddressUpdateForm;
import net.lab1024.sa.admin.module.business.base.address.domain.vo.AddressExcelVO;
import net.lab1024.sa.admin.module.business.base.address.domain.vo.AddressVO;
import net.lab1024.sa.admin.module.business.base.address.service.AddressQueryService;
import net.lab1024.sa.admin.module.business.base.address.service.AddressService;
import net.lab1024.sa.admin.module.business.base.item.domain.entity.ItemEntity;
import net.lab1024.sa.admin.module.business.base.item.domain.form.ItemSelect;
import net.lab1024.sa.admin.module.business.base.item.domain.vo.ItemsExcelVO;
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 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;

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

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

    @Resource
    private AddressService addressService;

    @Resource
    private AddressQueryService addressQueryService;

    @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")
    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")
    public ResponseDTO<String> update(@RequestBody @Valid AddressUpdateForm updateForm) {
        return addressService.update(updateForm);
    }

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

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

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

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

    @Operation(summary = "导出 霍锦")
    @GetMapping("/address/exportAddress")
    @SaCheckPermission("address:exportAddress")
    public void exportAddress(HttpServletResponse response) throws IOException {
        List<AddressExcelVO> addressList = addressQueryService.getAddressExcelVOList();
        SmartExcelUtil.exportExcel(response, "收货地址信息.xlsx", "收货地址", AddressExcelVO.class, addressList);
    }
}
