package com.sqx.modules.address.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.sqx.common.exception.SqxException;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.address.dto.InsideAddressDTO;
import com.sqx.modules.address.dto.InsideAddressUpdateStatusDto;
import com.sqx.modules.address.entity.InsideAddress;
import com.sqx.modules.address.query.InsideAddressQuery;
import com.sqx.modules.address.service.InsideAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;


@RestController
@Api(value = "管理端-内部地址", tags = {"管理端-内部地址"})
@RequestMapping(value = "admin/inside-address")
@RequiredArgsConstructor
public class AdminInsideAddressController {

    private final InsideAddressService insideAddressService;

    @GetMapping("pages")
    @ApiOperation("分页查询内部地址列表")
    public Result pages(@Valid InsideAddressQuery query) {
        PageUtils pageUtils = insideAddressService.pages(query);
        return Result.success().put("data", pageUtils);
    }

    @PostMapping("add")
    @ApiOperation("新增内部地址")
    public Result add(@Valid @RequestBody InsideAddressDTO insideAddressDTO) {
        insideAddressService.addInsideAddress(insideAddressDTO);
        return Result.success();
    }

    @PostMapping("update")
    @ApiOperation("更新内部地址")
    public Result update(@Valid @RequestBody InsideAddressDTO insideAddressDTO) {
        if (ObjectUtil.isNull(insideAddressDTO.getId())) {
            throw new SqxException("id不能为空");
        }
        insideAddressService.updateInsideAddress(insideAddressDTO);
        return Result.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除内部地址")
    public Result delete(@RequestBody List<Long> ids) {
        insideAddressService.deleteByIds(ids);
        return Result.success();
    }

    @PostMapping("updateStatus")
    @ApiOperation("更改状态")
    public Result updateStatus(@Valid @RequestBody InsideAddressUpdateStatusDto dto) {
        if (ObjectUtil.isNull(dto.getId())) {
            throw new SqxException("id不能为空");
        }
        InsideAddress insideAddress = new InsideAddress();
        BeanUtil.copyProperties(dto,insideAddress);
        insideAddressService.updateById(insideAddress);
        return Result.success();
    }

    @GetMapping("export")
    @ApiOperation("导出内部地址列表")
    public void export(@Valid InsideAddressQuery query) {
        insideAddressService.exportData(query);
    }

    @PostMapping("import")
    @ApiOperation("导入内部地址列表")
    public Result importData(@RequestParam("file") MultipartFile file) throws IOException, ParseException {
        if (file.isEmpty()) {
            throw new SqxException("请选择需要上传的文件");
        }

        insideAddressService.importData(file);
        return Result.success();
    }

    @GetMapping( "/downloadInsideAddressExcel")
    @ApiOperation(value = "内部地址导出模板", notes = "内部地址导出模板", httpMethod = "GET")
    public Result downloadInsideAddressExcel() {
        return Result.success().put("data", "https://mxys.chuanghai-tech.com/wmfile/file/收货地址管理导出模板.xlsx");
    }
}
