package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.ViewHistoryService;
import com.gxwebsoft.shop.entity.ViewHistory;
import com.gxwebsoft.shop.param.ViewHistoryParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 浏览历史控制器
 *
 * @author 科技小王子
 * @since 2024-11-22 22:13:47
 */
@Api(tags = "浏览历史管理")
@RestController
@RequestMapping("/api/shop/view-history")
public class ViewHistoryController extends BaseController {
    @Resource
    private ViewHistoryService viewHistoryService;

    @ApiOperation("分页查询浏览历史")
    @GetMapping("/page")
    public ApiResult<PageResult<ViewHistory>> page(ViewHistoryParam param) {
        // 使用关联查询
        param.setUserId(getLoginUserId());
        return success(viewHistoryService.pageRel(param));
    }

    @ApiOperation("查询全部浏览历史")
    @GetMapping()
    public ApiResult<List<ViewHistory>> list(ViewHistoryParam param) {
        // 使用关联查询
        param.setUserId(getLoginUserId());
        return success(viewHistoryService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:viewHistory:list')")
    @ApiOperation("根据id查询浏览历史")
    @GetMapping("/{id}")
    public ApiResult<ViewHistory> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(viewHistoryService.getByIdRel(id));
    }

    @ApiOperation("添加浏览历史")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ViewHistory viewHistory) {
        ViewHistory check = null;
        if (viewHistory.getType().equals(1)) {
            if (getLoginUser() != null) {
                check = viewHistoryService.check(viewHistory.getType(), viewHistory.getPk(), getLoginUserId());
                viewHistory.setUserId(getLoginUserId());
            } else {
                check = viewHistoryService.check(viewHistory.getType(), viewHistory.getPk(), viewHistory.getOpenId());
            }
        } else {
            // 记录当前登录用户id
            User loginUser = getLoginUser();
            if (loginUser != null) {
                viewHistory.setUserId(loginUser.getUserId());
            }
            check = viewHistoryService.check(viewHistory.getType(), viewHistory.getPk(), getLoginUserId());
        }
        if (check == null) {
            if (viewHistoryService.save(viewHistory)) {
                return success("添加成功");
            }
        }
        return fail("已经浏览过了");
    }

    @ApiOperation("修改浏览历史")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ViewHistory viewHistory) {
        if (viewHistoryService.updateById(viewHistory)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除浏览历史")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (viewHistoryService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加浏览历史")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ViewHistory> list) {
        if (viewHistoryService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改浏览历史")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ViewHistory> batchParam) {
        if (batchParam.update(viewHistoryService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除浏览历史")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (viewHistoryService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
