package com.ruoyi.interfaces.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.interfaces.core.page.Page;
import com.ruoyi.interfaces.core.page.PageParam;
import com.ruoyi.interfaces.core.page.PageUtils;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.interfaces.domain.MyAttention;
import com.ruoyi.interfaces.domain.PtService;
import com.ruoyi.interfaces.mapper.MyAttentionMapper;
import com.ruoyi.interfaces.mapper.PtServiceMapper;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping(value = "/my/my/attention")
public class MyAttentionController extends BaseController {

    @Autowired
    private MyAttentionMapper myAttentionMapper;

    @Autowired
    private PtServiceMapper ptServiceMapper;

    @ApiOperation("添加关注")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public AjaxResult insertAttention(@RequestBody MyAttention myAttention) {
        if (StringUtils.isBlank(myAttention.getSrvId())) {
            return AjaxResult.error("传参异常【srvId】");
        }
        myAttention.setUserId(String.valueOf(getUserId()));
        PtService ptService = ptServiceMapper.selectByPrimaryKey(myAttention.getSrvId());
        if (ptService == null) {
            return AjaxResult.error("传参异常【srvId】");
        }
        myAttention.setTitle(ptService.getName());
        myAttention.setUrl(ptService.getUrl());

        if (myAttentionMapper.findByIsTrue(myAttention) == 0) {
            myAttention.setId(myAttentionMapper.findByMaxId());
            myAttention.setTm(new Date());
            int insert = myAttentionMapper.insert(myAttention);
            if (insert > 0) {
                return AjaxResult.success("服务收藏成功");
            }
            return AjaxResult.error("服务收藏失败");
        }
        return AjaxResult.error(301, "服务已收藏");
    }

    @ApiOperation("删除关注")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public AjaxResult deleteAttention(String keys) {
        int deletes = 0;
        if (keys == null) {
            return AjaxResult.error();
        }
        String[] strs = keys.split(",");
        for (String str : strs) {
            deletes += myAttentionMapper.delete(Long.parseLong(str));
        }
        if (deletes == strs.length) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }

    @ApiOperation("查询用户的关注")
    @PostMapping("/list")
    public Page selectMyAttention(@RequestBody PageParam<PtService> ptServicePageParam) {
        PtService ptService = Optional.ofNullable(ptServicePageParam.getData()).orElse(new PtService());
        ptService.setUserId(String.valueOf(getUserId()));
        PageHelper.startPage(ptServicePageParam);
        PageHelper.orderBy("A.TM DESC");
        List<PtService> list = myAttentionMapper.list(ptService);
        return PageUtils.convert(list);
    }
}
