package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.EssentialModuleInformation;
import shop.alien.entity.store.vo.EssentialModuleInformationVo;
import shop.alien.store.service.EssentialModuleInformationService;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ssk
 * @since 2025-03-20
 */
@Api(tags = {"八大类-过滤条件"})
@RestController
@CrossOrigin
@RequestMapping("/essential-module-information")
@RequiredArgsConstructor
public class EssentialModuleInformationController {

    private final EssentialModuleInformationService essentialModuleInformationService;

    @ApiOperation("按照层级树形结构拼接")
    @GetMapping(value = "getEssentialModuleInformationList")
    private R<List<EssentialModuleInformationVo>> getEssentialModuleInformationList(@RequestParam(value = "typeId", required = false) String typeId) {
        List<EssentialModuleInformationVo> essentialModuleInformationList = essentialModuleInformationService.getEssentialModuleInformationList(typeId);
        return R.data(essentialModuleInformationList);
    }

    @ApiOperation("获取一级数据")
    @GetMapping(value = "getPrimaryData")
    private R<List<EssentialModuleInformation>> getPrimaryData() {
        List<EssentialModuleInformation> essentialModuleInformationList = essentialModuleInformationService.list(
                new LambdaQueryWrapper<EssentialModuleInformation>().isNull(EssentialModuleInformation::getParentId));
        return R.data(essentialModuleInformationList);
    }

    @ApiOperation("根据上级id获取下级数据")
    @GetMapping(value = "getChildByParentId")
    private R<List<EssentialModuleInformation>> getPrimaryData(@RequestParam(value = "parentId", required = false) String parentId) {
        List<EssentialModuleInformation> essentialModuleInformationList = essentialModuleInformationService.list(
                new LambdaQueryWrapper<EssentialModuleInformation>().eq(EssentialModuleInformation::getParentId, parentId).eq(EssentialModuleInformation::getDeleteFlag, 0));
        return R.data(essentialModuleInformationList);
    }

    @ApiOperation("web端查询列表")
    @GetMapping(value = "getList")
    private List<EssentialModuleInformationVo> getList(@RequestParam(value = "parentId", required = false) String parentId) {
        List<EssentialModuleInformationVo> essentialModuleInformationList = essentialModuleInformationService.getList();
        return essentialModuleInformationList;
    }

    @ApiOperation("web端新增数据")
    @PostMapping(value = "addInformation")
    private R addInformation(@RequestBody EssentialModuleInformation essentialModuleInformation) {
        essentialModuleInformationService.addInformation(essentialModuleInformation);
        return R.success("新增成功");
    }

    @ApiOperation("web端编辑数据")
    @PutMapping(value = "editInformation")
    private R editInformation(@RequestBody EssentialModuleInformation essentialModuleInformation) {
        essentialModuleInformationService.editInformation(essentialModuleInformation);
        return R.success("编辑成功");
    }

    @ApiOperation("web端删除数据")
    @DeleteMapping(value = "deleteInformation")
    private R deleteInformation(@RequestParam(value = "id") String id) {
        essentialModuleInformationService.deleteInformation(id);
        return R.success("删除成功");
    }

}
