package com.zbf.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zbf.common.annotation.Log;
import com.zbf.common.core.controller.BaseController;
import com.zbf.common.core.domain.AjaxResult;
import com.zbf.common.core.page.TableDataInfo;
import com.zbf.common.enums.BusinessType;
import com.zbf.common.utils.poi.ExcelUtil;
import com.zbf.system.domain.TUserFavorites;
import com.zbf.system.mapper.TUserFavoritesMapper;
import com.zbf.system.service.ITUserFavoritesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 常用功能收藏夹Controller
 *
 * @author tony
 * @date 2024-07-03
 */
@RestController
@RequestMapping("/system/favorites")
public class TUserFavoritesController extends BaseController {
    private final ITUserFavoritesService tUserFavoritesService;
    private TUserFavoritesMapper favoritesMapper;

    public TUserFavoritesController(ITUserFavoritesService tUserFavoritesService, TUserFavoritesMapper favoritesMapper) {
        this.tUserFavoritesService = tUserFavoritesService;
        this.favoritesMapper = favoritesMapper;
    }

    /**
     * 查询常用功能收藏夹列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TUserFavorites tUserFavorites) {
        LambdaQueryWrapper query = new LambdaQueryWrapper(tUserFavorites);
        List<TUserFavorites> list = favoritesMapper.selectList(query);
        return getDataTable(list);
    }

    /**
     * 导出常用功能收藏夹列表
     */
    @Log(title = "常用功能收藏夹", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TUserFavorites tUserFavorites) {
        List<TUserFavorites> list = tUserFavoritesService.selectTUserFavoritesList(tUserFavorites);
        ExcelUtil<TUserFavorites> util = new ExcelUtil<TUserFavorites>(TUserFavorites.class);
        util.exportExcel(response, list, "常用功能收藏夹数据");
    }

    /**
     * 获取常用功能收藏夹详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(tUserFavoritesService.selectTUserFavoritesById(id));
    }

    /**
     * 新增常用功能收藏夹
     */
    @Log(title = "常用功能收藏夹", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TUserFavorites tUserFavorites) {
        favoritesMapper.insert(tUserFavorites);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("favorites", tUserFavorites);
        return ajax;
    }

    /**
     * 修改常用功能收藏夹
     */
    @Log(title = "常用功能收藏夹", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TUserFavorites tUserFavorites) {
        return toAjax(favoritesMapper.updateById(tUserFavorites));
    }

    /**
     * 删除常用功能收藏夹
     */
    @Log(title = "常用功能收藏夹", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(tUserFavoritesService.deleteTUserFavoritesByIds(ids));
    }

    /**
     * 禁用
     */
    @Log(title = "常用功能收藏夹", businessType = BusinessType.UPDATE)
    @DeleteMapping("/disable/{id}")
    public AjaxResult disable(@PathVariable String id) {
        TUserFavorites favorites = new TUserFavorites();
        favorites.setId(id);
        favorites.setStatus("1"); // state=1 表示禁用
        return toAjax(favoritesMapper.updateById(favorites));
    }

    /**
     * 启用
     */
    @Log(title = "常用功能收藏夹", businessType = BusinessType.UPDATE)
    @DeleteMapping("/enable/{id}")
    public AjaxResult enable(@PathVariable String id) {
        TUserFavorites favorites = new TUserFavorites();
        favorites.setId(id);
        favorites.setStatus("0");// state=1 表示启用
        return toAjax(favoritesMapper.updateById(favorites));
    }

    /**
     * 修改状态
     */
    @Log(title = "常用功能收藏夹", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody TUserFavorites tUserFavorites) {
        return toAjax(tUserFavoritesService.updateStatus(tUserFavorites));
    }

}

