package com.yida.data.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yida.data.common.core.common.ResultBean;
import com.yida.data.common.core.entity.CurrentUser;
import com.yida.data.common.core.entity.system.EduApp;
import com.yida.data.common.core.utils.Asserts;
import com.yida.data.common.core.utils.FebsUtil;
import com.yida.data.system.service.EduAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "企业微信 应用管理")
@RestController
@RequestMapping("/app")
public class EduAppController {
    @Resource
    private EduAppService eduAppService;

    @ApiOperation("分页查询")
    @GetMapping("/listAppPage")
    public ResultBean listAppPage(@ApiParam("学校id") @RequestParam(required = false) Long schoolId,
                                  @ApiParam("当前页码") @RequestParam(defaultValue = "1") Integer pageNum,
                                  @ApiParam("页面大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        if (schoolId == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (currentUser.getDeptType() == 0) {
                schoolId = currentUser.getDeptId();
            }
        }
        return ResultBean.buildSuccess(eduAppService.listQywxAppPage(new Page<>(pageNum, pageSize), schoolId));
    }

    @ApiOperation("保存、编辑app")
    @PostMapping("/saveApp")
    public ResultBean saveApp(@RequestBody EduApp app) {
        if (app.getDeptId() == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            Asserts.isFalse(currentUser.getDeptType() == 5, "未选择学校");
            app.setDeptId(currentUser.getDeptId());
        }
        eduAppService.saveApp(app);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("删除app")
    @PostMapping("/delAppBatch")
    public ResultBean delAppBatch(@RequestBody List<Long> ids) {
        eduAppService.removeByIds(ids);
        return ResultBean.buildSuccess();
    }

    @ApiOperation("查询app列表")
    @PostMapping("/listApp")
    public ResultBean listApp(@ApiParam("应用类别，0-家长，1-教职工") @RequestParam(required = false) Integer type,
                              @ApiParam("学校id") @RequestParam(required = false) Long schoolId,
                              @ApiParam("能否在易达app中显示,0-否，1-是") @RequestParam(required = false) Integer appShow) {
        if (schoolId == null) {
            CurrentUser currentUser = FebsUtil.getCurrentUser();
            if (ObjectUtil.equal(currentUser.getDeptType(), 0)) {
                schoolId = currentUser.getDeptId();
            }
        }
        LambdaQueryWrapper<EduApp> condition = Wrappers.<EduApp>lambdaQuery();
        if (schoolId != null) {
            condition.eq(EduApp::getDeptId, schoolId);
        }
        if (type != null) {
            condition.eq(EduApp::getType, type);
        }
        if (appShow != null) {
            condition.eq(EduApp::getYidaAppShow, appShow);
        }
        return ResultBean.buildSuccess(eduAppService.list(condition));
    }
}
