package com.ruoyi.model.controller;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.model.domain.AppUsers;
import com.ruoyi.model.service.IAppUsersService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.model.domain.AppletUsersTeacher;
import com.ruoyi.model.service.IAppletUsersTeacherService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 伴宠师认证Controller
 * 
 * @author ruoyi
 * @date 2025-03-27
 */
@RestController
@RequestMapping("/model/AppletUsersTeacher")
public class AppletUsersTeacherController extends BaseController
{
    @Autowired
    private IAppletUsersTeacherService appletUsersTeacherService;
    @Autowired
    private IAppUsersService appUsersService;
    /**
     * 查询伴宠师认证列表
     */
    @PreAuthorize("@ss.hasPermi('model:AppletUsersTeacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppletUsersTeacher appletUsersTeacher)
    {
        startPage();
        List<AppletUsersTeacher> list = appletUsersTeacherService.selectAppletUsersTeacherList(appletUsersTeacher);
        //获取用户信息
        for (AppletUsersTeacher teacher : list) {
            AppUsers appUsers = appUsersService.selectAppUsersByUserId(teacher.getUserId());
            if(appUsers != null){
                teacher.setUserName(appUsers.getUserName());
            }

        }
        return getDataTable(list);
    }

    /**
     * 导出伴宠师认证列表
     */
    @PreAuthorize("@ss.hasPermi('model:AppletUsersTeacher:export')")
    @Log(title = "伴宠师认证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppletUsersTeacher appletUsersTeacher) throws IOException {
        List<AppletUsersTeacher> list = appletUsersTeacherService.selectAppletUsersTeacherList(appletUsersTeacher);
        ExcelUtil<AppletUsersTeacher> util = new ExcelUtil<AppletUsersTeacher>(AppletUsersTeacher.class);
        util.exportExcel(response, list, "伴宠师认证数据");
    }

    /**
     * 获取伴宠师认证详细信息
     */
    @PreAuthorize("@ss.hasPermi('model:AppletUsersTeacher:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(appletUsersTeacherService.selectAppletUsersTeacherById(id));
    }

    /**
     * 新增伴宠师认证
     */
    @PreAuthorize("@ss.hasPermi('model:AppletUsersTeacher:add')")
    @Log(title = "伴宠师认证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppletUsersTeacher appletUsersTeacher)
    {
        return toAjax(appletUsersTeacherService.insertAppletUsersTeacher(appletUsersTeacher));
    }

    /**
     * 修改伴宠师认证
     */
    @PreAuthorize("@ss.hasPermi('model:AppletUsersTeacher:edit')")
    @Log(title = "伴宠师认证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppletUsersTeacher appletUsersTeacher)
    {
//        //如果审核状态为通过status = 1
//        if(appletUsersTeacher.getStatus() == 1){
//            //审核通过，修改用户表
//            AppUsers appUsers = appUsersService.selectAppUsersByUserId(appletUsersTeacher.getUserId());
//            //用户不存在
//            if(appUsers == null){
//                return AjaxResult.error("用户不存在");
//            }
//            appUsers.setUserBcs(1);
//            appUsers.setUserBcsRole(1);
//            appUsersService.updateAppUsers(appUsers);
//        }
        return toAjax(appletUsersTeacherService.updateAppletUsersTeacher(appletUsersTeacher));
    }

    /**
     * 删除伴宠师认证
     */
    @PreAuthorize("@ss.hasPermi('model:AppletUsersTeacher:remove')")
    @Log(title = "伴宠师认证", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appletUsersTeacherService.deleteAppletUsersTeacherByIds(ids));
    }
}
