/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.ycwh.controller.wx;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.ycwh.entity.CollectInfoEntity;
import org.springblade.modules.ycwh.excel.CollectInfoExcel;
import org.springblade.modules.ycwh.service.ICollectInfoService;
import org.springblade.modules.ycwh.vo.CollectInfoVO;
import org.springblade.modules.ycwh.wrapper.CollectInfoWrapper;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 名菜名点名店收藏 控制器
 *
 * @author BladeX
 * @since 2023-08-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("yuecai-wx/collect_info")
@Api(value = "名菜名点名店收藏", tags = "名菜名点名店收藏接口")
public class WXCollectInfoController extends BladeController {

	private final ICollectInfoService collectInfoService;


	/**
	 * 名菜名点名店收藏 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "分页", notes = "传入collectInfo")
	public R<IPage<CollectInfoVO>> list(@ApiIgnore @RequestParam Map<String, Object> collectInfo, Query query) {
		BladeUser user = AuthUtil.getUser();
		if (Func.isEmpty(user)) {
			return R.fail("未登录");
		}
		QueryWrapper<CollectInfoEntity> qw = Condition.getQueryWrapper(collectInfo, CollectInfoEntity.class);
		qw.eq("member_id", user.getUserId());
		IPage<CollectInfoEntity> pages = collectInfoService.page(Condition.getPage(query), qw);
		return R.data(CollectInfoWrapper.build().pageVO(pages));
	}

	/**
	 * 名菜名点名店收藏 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "新增", notes = "传入collectInfo")
	public R save(@Valid @RequestBody CollectInfoEntity collectInfo) {
		BladeUser user = AuthUtil.getUser();
		if (Func.isEmpty(user)) {
			return R.fail("未登录");
		}
		if (Func.isEmpty(collectInfo.getCollectId()) || Func.isEmpty(collectInfo.getCollectType())) {
			return R.fail("收藏内容有误，请重新再试");
		}
		collectInfo.setMemberId(user.getUserId());
		return R.status(collectInfoService.save(collectInfo));
	}

	/**
	 * 名菜名点名店收藏 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "逻辑删除", notes = "id, 收藏类型")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam Long collectId, @ApiParam(value = "收藏类型", required = true) @RequestParam String collectType) {
		BladeUser user = AuthUtil.getUser();
		if (Func.isEmpty(user)) {
			return R.fail("未登录");
		}
		LambdaQueryWrapper<CollectInfoEntity> lqw = new LambdaQueryWrapper();
		lqw.eq(CollectInfoEntity::getMemberId, user.getUserId());
		lqw.eq(CollectInfoEntity::getCollectId, collectId);
		lqw.eq(CollectInfoEntity::getCollectType, collectType);
		return R.status(collectInfoService.remove(lqw));
	}

}
