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

import com.starry.admin.modules.system.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 org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 租户套餐表Controller
 *
 * @author admin
 */
@RestController
@RequestMapping("/platform/package")
public class SysTenantPackageController {
    @Resource
    private ISysTenantPackageService SysTenantPackageService;

    /**
     * 查询租户套餐表列表
     */
    @PreAuthorize("@customSs.hasPermission('platform:package:list')")
    @GetMapping("/list")
    public R list(SysTenantPackageVo sysTenantPackageEntity) {
        return R.ok(SysTenantPackageService.selectSysTenantPackageList(sysTenantPackageEntity));
    }

    /**
     * 导出租户套餐表列表
     */
    @PreAuthorize("@customSs.hasPermission('platform:package:export')")
    @Log(title = "租户套餐表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTenantPackageVo sysTenantPackageEntity) {
        ExcelUtils.exportEasyExcel(response, SysTenantPackageEntity.class, SysTenantPackageService.selectSysTenantPackageList(sysTenantPackageEntity).getRecords(), "租户套餐表数据");
    }

    /**
     * 获取租户套餐表详细信息
     */
    @PreAuthorize("@customSs.hasPermission('platform:package:query')")
    @GetMapping(value = "/{packageId}")
    public R getInfo(@PathVariable("packageId") String packageId) {
        return R.ok(SysTenantPackageService.selectSysTenantPackageByPackageId(packageId));
    }

    /**
     * 新增租户套餐表
     */
    @PreAuthorize("@customSs.hasPermission('platform:package:create')")
    @Log(title = "租户套餐表", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public R create(@RequestBody SysTenantPackageEntity vo) {
        SysTenantPackageEntity entity = ConvertUtil.entityToVo(vo, SysTenantPackageEntity.class);
        boolean success = SysTenantPackageService.create(entity);
        if (success) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    /**
     * 修改租户套餐表
     */
    @PreAuthorize("@customSs.hasPermission('platform:package:edit')")
    @Log(title = "租户套餐表", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update/{packageId}")
    public R update(@PathVariable String packageId, @RequestBody SysTenantPackageEntity sysTenantPackageEntity) {

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

    /**
     * 删除租户套餐表
     */
    @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));
    }

    /**
     * 查询租户套餐精简列表
     */
    @GetMapping("/get-simple-list")
    public R getSimpleList() {
        List<SimplePackage> list = SysTenantPackageService.getSimpleList();
        return R.ok(list);
    }
}
