package cn.ey.biz.module.system.controller.admin.app;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.common.util.i18n.I18nContextHolder;
import cn.ey.biz.framework.datapermission.core.annotation.DataPermission;
import cn.ey.biz.framework.excel.core.util.ExcelUtils;
import cn.ey.biz.framework.operatelog.core.annotations.OperateLog;
import cn.ey.biz.module.system.controller.admin.app.vo.*;
import cn.ey.biz.module.system.convert.app.AppConvert;
import cn.ey.biz.module.system.dal.dataobject.app.AppDO;
import cn.ey.biz.module.system.service.app.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static cn.ey.biz.module.system.enums.ErrorCodeConstants.ACCEPT_LANGUAGE_NOT_NULL;

@Api(tags = "管理后台 - 系统应用")
@RestController
@RequestMapping("/system/app")
@Validated
public class AppController {

    @Resource
    private AppService appService;

    @PostMapping("/create")
    @ApiOperation("创建系统应用")
    @PreAuthorize("@ss.hasPermission('system:app:create')")
    public R<Long> createApp(@Valid @RequestBody AppCreateReqVO createReqVO) {
        return success(appService.createApp(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新系统应用")
    @PreAuthorize("@ss.hasPermission('system:app:update')")
    public R<Boolean> updateApp(@Valid @RequestBody AppUpdateReqVO updateReqVO) {
        appService.updateApp(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除系统应用")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:app:delete')")
    public R<Boolean> deleteApp(@RequestParam("id") Long id) {
        appService.deleteApp(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得系统应用")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:app:query')")
    public R<AppRespVO> getApp(@RequestParam("id") Long id) {
        AppRespVO app = appService.getApp(id);
        return success(app);
    }

    @GetMapping("/list")
    @ApiOperation("获得系统应用列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:app:query')")
    @DataPermission
    public R<List<AppRespVO>> getAppList(@RequestParam("ids") Collection<Long> ids) {
        List<AppDO> list = appService.getAppList(ids);
        return success(AppConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得系统应用分页")
    @PreAuthorize("@ss.hasPermission('system:app:query')")
    @DataPermission
    public R<PageResult<AppRespVO>> getAppPage(@Valid AppPageReqVO pageVO) {
        String acceptLanguage = I18nContextHolder.getLangId();
        if (StringUtils.isEmpty(acceptLanguage)) {
            throw exception(ACCEPT_LANGUAGE_NOT_NULL);
        }
        PageResult<AppRespVO> pageResult = appService.getAppPage(pageVO, acceptLanguage);
        return success(pageResult);
    }

    @GetMapping("/v-apps")
    @ApiOperation("获得系统应用")
    public R<List<AppRespVO>> getAppLists() {
        return success(AppConvert.INSTANCE.convertList(appService.getAppList()));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出系统应用 Excel")
    @PreAuthorize("@ss.hasPermission('system:app:export')")
    @OperateLog(type = EXPORT)
    public void exportAppExcel(@Valid AppExportReqVO exportReqVO,
                               HttpServletResponse response) throws IOException {
        List<AppDO> list = appService.getAppList(exportReqVO);
        // 导出 Excel
        List<AppExcelVO> datas = AppConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "系统应用.xls", "数据", AppExcelVO.class, datas);
    }

}
