package com.bonus.zlpt.equip.controller;

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

import cn.hutool.core.util.BooleanUtil;
import com.bonus.zlpt.common.core.utils.poi.ExcelUtil;
import com.bonus.zlpt.common.core.web.controller.BaseController;
import com.bonus.zlpt.common.core.web.domain.AjaxResult;
import com.bonus.zlpt.common.core.web.page.TableDataInfo;
import com.bonus.zlpt.common.log.annotation.Log;
import com.bonus.zlpt.common.log.enums.BusinessType;
import com.bonus.zlpt.common.security.annotation.RequiresPermissions;
import com.bonus.zlpt.common.security.utils.SecurityUtils;
import com.bonus.zlpt.equip.api.domain.UserCollect;
import com.bonus.zlpt.system.api.model.LoginUser;
import io.swagger.annotations.Api;
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.bonus.zlpt.equip.service.IUserCollectService;

/**
 * 我的收藏
Controller
 * 
 * @author xsheng
 * @date 2023-12-02
 */
@Api(tags = "收藏装备接口")
@RestController
@RequestMapping("/collect")
public class UserCollectController extends BaseController
{
    @Autowired
    private IUserCollectService userCollectService;

    /**
     * 查询我的收藏
列表
     */
    @RequiresPermissions("equip:collect:list")
    @GetMapping("/list")
    public TableDataInfo list(UserCollect userCollect)
    {
        startPage();
        List<UserCollect> list = userCollectService.selectUserCollectList(userCollect);
        return getDataTable(list);
    }

    /**
     * 导出我的收藏
列表
     */
    @RequiresPermissions("equip:collect:export")
    @Log(title = "我的收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCollect userCollect)
    {
        List<UserCollect> list = userCollectService.selectUserCollectList(userCollect);
        ExcelUtil<UserCollect> util = new ExcelUtil<UserCollect>(UserCollect.class);
        util.exportExcel(response, list, "我的收藏数据");
    }

    /**
     * 获取我的收藏
详细信息
     */
    @RequiresPermissions("equip:collect:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userCollectService.selectUserCollectById(id));
    }

    /**
     * 新增我的收藏

     */
    @RequiresPermissions("equip:collect:add")
    @Log(title = "我的收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCollect userCollect)
    {
        return toAjax(userCollectService.insertUserCollect(userCollect));
    }

    /**
     * 修改我的收藏

     */
    @RequiresPermissions("equip:collect:edit")
    @Log(title = "我的收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCollect userCollect)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (Objects.nonNull(loginUser)) {
            userCollect.setUserId(loginUser.getUserid());
            if (BooleanUtil.isTrue(userCollect.getIsCollect())) {
                userCollectService.deleteUserCollectByMaId(userCollect.getMaId());
                return toAjax(userCollectService.insertUserCollect(userCollect));
            } else {
                return toAjax(userCollectService.deleteUserCollectByMaId(userCollect.getMaId()));
            }
        }
        return null;
    }

    /**
     * 删除我的收藏

     */
    @RequiresPermissions("equip:collect:remove")
    @Log(title = "我的收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userCollectService.deleteUserCollectByIds(ids));
    }
}
