package shop.alien.store.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreDictionary;
import shop.alien.store.service.PlatformBusinessSectionService;

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

@Api(tags = {"2.5平台-经营版块管理"})
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/platformBusinessSection")
@RequiredArgsConstructor
public class PlatformBusinessSectionController {

    private final PlatformBusinessSectionService platformBusinessSectionService;

    @ApiOperation("查询经营种类（三级树形结构），支持根据dictDetail过滤")
    @ApiOperationSupport(order = 1)
    @GetMapping("/queryBusinessSectionTree")
    public R<List<StoreDictionary>> queryBusinessSectionTree(@RequestParam(required = false) String dictDetail) {
        log.info("platformBusinessSection.queryBusinessSectionTree dictDetail={}", dictDetail);
        List<StoreDictionary> result = platformBusinessSectionService.queryBusinessSectionTree(dictDetail);
        return R.data(result);
    }

    @ApiOperation("新增经营种类（支持一级、二级、三级）")
    @ApiOperationSupport(order = 2)
    @PostMapping("/addBusinessSection")
    public R<Boolean> addBusinessSection(@RequestBody StoreDictionary storeDictionary) {
        log.info("platformBusinessSection.addBusinessSection:{}", storeDictionary);
        try {
            boolean result = platformBusinessSectionService.addBusinessSection(storeDictionary);
            if (result) {
                return R.success("新增成功");
            } else {
                return R.fail("新增失败");
            }
        } catch (IllegalArgumentException e) {
            log.error("platformBusinessSection.addBusinessSection error: {}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("platformBusinessSection.addBusinessSection error", e);
            return R.fail("新增失败：" + e.getMessage());
        }
    }

    @ApiOperation("修改经营种类（支持一级、二级、三级）")
    @ApiOperationSupport(order = 3)
    @PutMapping("/updateBusinessSection")
    public R<Boolean> updateBusinessSection(@RequestBody StoreDictionary storeDictionary) {
        log.info("platformBusinessSection.updateBusinessSection:{}", storeDictionary);
        try {
            boolean result = platformBusinessSectionService.updateBusinessSection(storeDictionary);
            if (result) {
                return R.success("修改成功");
            } else {
                return R.fail("修改失败");
            }
        } catch (IllegalArgumentException e) {
            log.error("platformBusinessSection.updateBusinessSection error: {}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("platformBusinessSection.updateBusinessSection error", e);
            return R.fail("修改失败：" + e.getMessage());
        }
    }

    @ApiOperation("批量导入经营版块")
    @ApiOperationSupport(order = 4)
    @PostMapping("/import")
    public R<String> importBusinessSection(@RequestParam("file") MultipartFile file) {
        log.info("platformBusinessSection.importBusinessSection fileName={}", 
                file != null ? file.getOriginalFilename() : "null");
        try {
            return platformBusinessSectionService.importBusinessSection(file);
        } catch (Exception e) {
            log.error("platformBusinessSection.importBusinessSection error", e);
            return R.fail("导入失败：" + e.getMessage());
        }
    }

    @ApiOperation("下载经营版块导入模板")
    @ApiOperationSupport(order = 5)
    @GetMapping("/downloadTemplate")
    public void downloadTemplate(HttpServletResponse response) throws IOException {
        log.info("platformBusinessSection.downloadTemplate");
        platformBusinessSectionService.downloadTemplate(response);
    }

}
