package com.ruoyi.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.demo.entity.App;
import com.ruoyi.demo.entity.Commercial;
import com.ruoyi.demo.entity.vo.AppVo;
import com.ruoyi.demo.entity.vo.CommercialVo;
import com.ruoyi.demo.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/app")
public class AppController {
    @Autowired
    AppService appService;

    /**
     * 添加小程序
     * @param appVo
     * @return
     */
    @PostMapping("/addApp")
    public R addApp(@RequestBody @Validated(value={AddGroup.class}) AppVo appVo){
        R r = appService.addApp(appVo);
        return r;
    }

    /**
     * 删除小程序
     * @param appKeys
     * @return
     */
    @DeleteMapping("/deleteApp")
    public R deleteApp(String[] appKeys){
        appService.deleteApp(appKeys);
        return R.ok();
    }

    @GetMapping("/getAppListByCommercialId")
    public R getAppListByCommercialId(AppVo appVo){
        PageInfo<App> appListByCommercialId = appService.getAppListByCommercialId(appVo);
        return R.ok(appListByCommercialId);
    }

    /**
     * 获取小程序详细信息
     * @param appVo
     * @return
     */
    @GetMapping("/getApp")
    public R getApp(@Validated(value = {QueryGroup.class}) AppVo appVo){
        App app = appService.getApp(appVo);
        return R.ok(app);
    }

    /**
     * 修改小程序信息
     * @param appVo
     * @return
     */
    @PutMapping("/updateApp")
    public R updateApp(@RequestBody @Validated(value={EditGroup.class})AppVo appVo){
        R r = appService.updateApp(appVo);
        return r;
    }

    @GetMapping("/testCallBack")
    public R testCallBack(@NotBlank String callBack){
        appService.testCallBack(callBack);
        return R.ok();
    }

    /**
     * 获取小程序列表
     * @param appVo
     * @return
     */
    @GetMapping("/getAppList")
    public R getAppList(AppVo appVo){
        Page<App> appList = appService.getAppList(appVo);
        return R.ok(appList);
    }

    /**
     * 获取商户列表（下拉框）
     * @return
     */
    @GetMapping("/getCommercialList")
    public R getCommercialList(){
        List<Commercial> commercialList = appService.getCommercialList();
        return R.ok(commercialList);
    }
}
