package com.cyksj.web.controller.sys;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.manage.sys.DynamicListMapper;
import com.cyksj.model.entity.DynamicList;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/*
 *项目名: netflix
 *文件名: DynamicListController
 *创建者: JavaZou
 *创建时间:2023/2/21 14:22
 */
@RequestMapping("/sys/dynamic")
@RestController
@RequiredArgsConstructor
public class DynamicListController {

	private final DynamicListMapper dynamicListMapper;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	/**
	 * 新增列表
	 */
	@PostMapping
	public Result<String> post(@RequestBody @Validated DynamicList dynamicList) {
		if (dynamicList.getType() == 1 && StrUtil.isBlank(dynamicList.getLink())) {
			throw BusinessRuntimeException.getInstance("链接不能为空");
		}
		dynamicListMapper.insert(dynamicList);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 编辑列表
	 */
	@PutMapping
	public Result<String> put(@RequestBody @Validated DynamicList dynamicList) {
		Long id = dynamicList.getId();
		if (id == null || id < 1) {
			throw BusinessRuntimeException.getInstance("该记录不存在");
		}
		DynamicList dl = dynamicListMapper.selectById(id);
		if (dl == null || !dl.getDeleted()) {
			throw BusinessRuntimeException.getInstance("该记录不存在");
		}
		if (dynamicList.getType() == 1 && StrUtil.isBlank(dynamicList.getLink())) {
			throw BusinessRuntimeException.getInstance("链接不能为空");
		}
		BeanUtil.copyProperties(dynamicList, dl);
		dynamicListMapper.updateById(dl);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 列表上下架
	 */
	@PutMapping("/put/status/{id}")
	public Result<String> updateStatus(@PathVariable Long id) {
		DynamicList dl = dynamicListMapper.selectById(id);
		if (dl == null || !dl.getDeleted()) {
			throw BusinessRuntimeException.getInstance("该记录不存在");
		}
		dl.setStatus(!dl.getStatus());
		dynamicListMapper.updateById(dl);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除列表
	 */
	@DeleteMapping("/{id}")
	public Result<String> deleteById(@PathVariable Long id) {
		DynamicList dl = dynamicListMapper.selectById(id);
		if (dl == null || !dl.getDeleted()) {
			throw BusinessRuntimeException.getInstance("该记录不存在");
		}
		dl.setDeleted(false);
		dynamicListMapper.updateById(dl);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 列表查询
	 */
	@GetMapping
	public Result<SearchResult<DynamicList>> get() {
		SearchResult<DynamicList> search = beanSearcher.search(DynamicList.class, MapUtils.flatBuilder(request.getParameterMap())
				.field(DynamicList::getDeleted, true)
				.orderBy(DynamicList::getSortNum).desc()
				.orderBy(DynamicList::getId).desc()
				.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 列表详情
	 */
	@GetMapping("/get/detail/{id}")
	public Result<DynamicList> getDetailById(@PathVariable Long id) {
		DynamicList dynamicList = dynamicListMapper.selectById(id);
		if (dynamicList == null || !dynamicList.getDeleted()) {
			throw BusinessRuntimeException.getInstance("该记录不存在");
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult(dynamicList);
	}
}
