package com.cyksj.web.controller.manage.mj;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.MidjourneyPaintingPlaza;
import com.cyksj.service.midjourney.MidjourneyPaintingPlazaService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 项目名: yhlxj
 * 文件名: MidjourneyPaintingController
 * 创建者: JavaZou
 * 创建时间:2024/6/3 17:24
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/manage/mj/painting")
public class MidjourneyPaintingController {
	private final MidjourneyPaintingPlazaService midjourneyPaintingPlazaService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	/**
	 * 新增记录
	 */
	@PostMapping("/post")
	public Result<String> savePainting(@RequestBody MidjourneyPaintingPlaza midjourneyPaintingPlaza) {
		midjourneyPaintingPlazaService.saveOrUpdatePainting(midjourneyPaintingPlaza);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除记录
	 */
	@DeleteMapping("/delete/{id}")
	public Result<String> deleteById(@PathVariable Long id) {
		MidjourneyPaintingPlaza byId = midjourneyPaintingPlazaService.getById(id);
		if (byId != null && byId.getDeleted()) {
			byId.setDeleted(false);
			midjourneyPaintingPlazaService.updateById(byId);
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	/**
	 * 修改记录
	 */
	@PutMapping("/update")
	public Result<String> updatePainting(@RequestBody MidjourneyPaintingPlaza midjourneyPaintingPlaza) {
		midjourneyPaintingPlazaService.saveOrUpdatePainting(midjourneyPaintingPlaza);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 列表
	 */
	@GetMapping("/get")
	public Result<SearchResult<MidjourneyPaintingPlaza>> saveOrUpdatePainting() {
		SearchResult<MidjourneyPaintingPlaza> search = beanSearcher.search(MidjourneyPaintingPlaza.class, MapUtils.flatBuilder(request.getParameterMap())
				.field(MidjourneyPaintingPlaza::getDeleted, 1)
				.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 上下架
	 */
	@PutMapping("/update/status/{id}")
	public Result<String> updateStatus(@PathVariable Long id) {
		MidjourneyPaintingPlaza byId = midjourneyPaintingPlazaService.getById(id);
		if (byId != null) {
			byId.setStatus(!byId.getStatus());
			midjourneyPaintingPlazaService.updateById(byId);
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 批量上下架
	 */
	@PutMapping("/update/status")
	public Result<String> updateStatus(@RequestParam(value = "ids") List<Long> ids, Boolean status) {
		if (CollUtil.isEmpty(ids) || status == null) {
			throw BusinessRuntimeException.getInstance("参数错误");
		}
		midjourneyPaintingPlazaService.update(null, Wrappers.lambdaUpdate(MidjourneyPaintingPlaza.class)
				.set(MidjourneyPaintingPlaza::getStatus, status)
				.in(MidjourneyPaintingPlaza::getId, ids)
				.ne(MidjourneyPaintingPlaza::getStatus, status));
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}
}
