package com.starry.admin.modules.system.controller;

import com.starry.admin.modules.system.module.entity.SysTenantPackageEntity;
import com.starry.admin.modules.system.service.ISysTenantPackageService;
import com.starry.admin.modules.system.vo.SimplePackage;
import com.starry.admin.modules.system.vo.SysTenantPackageVo;
import com.starry.admin.utils.ExcelUtils;
import com.starry.common.annotation.Log;
import com.starry.common.enums.BusinessType;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 租户套餐表Controller
 *
 * @author admin
 */
@Api(tags = "租户套餐管理", description = "系统租户套餐相关接口")
@RestController
@RequestMapping("/platform/package")
public class SysTenantPackageController {
    @Resource
    private ISysTenantPackageService SysTenantPackageService;

    /**
     * 查询租户套餐表列表
     */
    @ApiOperation(value = "查询租户套餐列表", notes = "分页查询租户套餐列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = SysTenantPackageEntity.class, responseContainer = "Page")})
    @PreAuthorize("@customSs.hasPermission('platform:package:list')")
    @GetMapping("/list")
    public R list(@ApiParam(value = "查询条件") SysTenantPackageVo sysTenantPackageEntity) {
        return R.ok(SysTenantPackageService.selectSysTenantPackageList(sysTenantPackageEntity));
    }

    /**
     * 导出租户套餐表列表
     */
    @ApiOperation(value = "导出租户套餐", notes = "导出租户套餐表数据到Excel")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @PreAuthorize("@customSs.hasPermission('platform:package:export')")
    @Log(title = "租户套餐表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,
            @ApiParam(value = "查询条件") SysTenantPackageVo sysTenantPackageEntity) {
        ExcelUtils.exportEasyExcel(response, SysTenantPackageEntity.class,
                SysTenantPackageService.selectSysTenantPackageList(sysTenantPackageEntity).getRecords(), "租户套餐表数据");
    }

    /**
     * 获取租户套餐表详细信息
     */
    @ApiOperation(value = "获取租户套餐详情", notes = "根据ID获取租户套餐详细信息")
    @ApiImplicitParam(name = "packageId", value = "套餐ID", required = true, dataType = "String", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = SysTenantPackageEntity.class)})
    @PreAuthorize("@customSs.hasPermission('platform:package:query')")
    @GetMapping(value = "/{packageId}")
    public R getInfo(@PathVariable("packageId") String packageId) {
        return R.ok(SysTenantPackageService.selectSysTenantPackageByPackageId(packageId));
    }

    /**
     * 新增租户套餐表
     */
    @ApiOperation(value = "新增租户套餐", notes = "创建新的租户套餐")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "添加失败")})
    @PreAuthorize("@customSs.hasPermission('platform:package:create')")
    @Log(title = "租户套餐表", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@ApiParam(value = "套餐信息", required = true) @RequestBody SysTenantPackageEntity vo) {
        SysTenantPackageEntity entity = ConvertUtil.entityToVo(vo, SysTenantPackageEntity.class);
        boolean success = SysTenantPackageService.create(entity);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改租户套餐表
     */
    @ApiOperation(value = "修改租户套餐", notes = "根据ID修改租户套餐信息")
    @ApiImplicitParam(name = "packageId", value = "套餐ID", required = true, dataType = "String", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功"), @ApiResponse(code = 500, message = "修改失败")})
    @PreAuthorize("@customSs.hasPermission('platform:package:edit')")
    @Log(title = "租户套餐表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{packageId}")
    public R update(@PathVariable String packageId,
            @ApiParam(value = "套餐信息", required = true) @RequestBody SysTenantPackageEntity sysTenantPackageEntity) {

        sysTenantPackageEntity.setPackageId(packageId);
        boolean success = SysTenantPackageService.update(sysTenantPackageEntity);
        if (success) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 删除租户套餐表
     */
    @ApiOperation(value = "删除租户套餐", notes = "根据ID批量删除租户套餐")
    @ApiImplicitParam(name = "packageIds", value = "套餐ID数组", required = true, dataType = "String[]", paramType = "path")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功", response = Boolean.class)})
    @PreAuthorize("@customSs.hasPermission('platform:package:remove')")
    @Log(title = "租户套餐表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{packageIds}")
    public R remove(@PathVariable String[] packageIds) {
        return R.ok(SysTenantPackageService.deleteSysTenantPackageByPackageIds(packageIds));
    }

    /**
     * 查询租户套餐精简列表
     */
    @ApiOperation(value = "查询套餐精简列表", notes = "获取租户套餐的精简信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = SimplePackage.class, responseContainer = "List")})
    @GetMapping("/get-simple-list")
    public R getSimpleList() {
        List<SimplePackage> list = SysTenantPackageService.getSimpleList();
        return R.ok(list);
    }
}
