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

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.mapper.mini.MiniPopularizeConfigCategoryMapper;
import com.cyksj.mapper.mini.MiniPopularizeConfigMapper;
import com.cyksj.model.entity.MiniPopularizeConfig;
import com.cyksj.model.entity.MiniPopularizeConfigCategory;
import com.cyksj.model.request.MiniPopularizeConfigReq;
import com.cyksj.model.views.MiniPopularizeConfigView;
import com.cyksj.service.mini.MiniPopularizeService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

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

/**
 * 项目名: yhlxj2
 * 文件名: MiniPopularizeController
 * 创建者: JavaZou
 * 创建时间:2025/4/7 16:12
 */
@RestController
@RequestMapping("/manage/mini/popularize")
@RequiredArgsConstructor
public class MiniPopularizeController {
	private final MiniPopularizeService miniPopularizeService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final MiniPopularizeConfigMapper miniPopularizeConfigMapper;

	private final MiniPopularizeConfigCategoryMapper miniPopularizeConfigCategoryMapper;

	/**
	 * 配置列表
	 */
	@GetMapping("/get/config")
	public Result<SearchResult<MiniPopularizeConfigView>> getConfig() {
		SearchResult<MiniPopularizeConfigView> search = beanSearcher.search(MiniPopularizeConfigView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}


	/**
	 * 新增配置
	 */
	@PostMapping("/save")
	public Result<String> save(@RequestBody MiniPopularizeConfig miniPopularizeConfig) {
		miniPopularizeConfigMapper.insert(miniPopularizeConfig);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 编辑配置
	 */
	@PostMapping("/update")
	public Result<String> update(@RequestBody MiniPopularizeConfig miniPopularizeConfig) {
		miniPopularizeConfigMapper.updateById(miniPopularizeConfig);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除配置
	 */
	@DeleteMapping("/delete/{id}")
	public Result<String> deleteById(@PathVariable Long id) {
		miniPopularizeConfigMapper.deleteById(id);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 上下架配置
	 */
	@PutMapping("/update/status/{id}")
	public Result<String> updateStatusById(@PathVariable Long id) {
		Optional.ofNullable(miniPopularizeConfigMapper.selectById(id)).ifPresent(data -> {
			data.setStatus(!data.getStatus());
			miniPopularizeConfigMapper.updateById(data);
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 批量上下架
	 */
	@PutMapping("/batch/update/status")
	public Result<String> updateStatusByIds(@RequestBody MiniPopularizeConfigReq req) {
		miniPopularizeConfigMapper.update(null, Wrappers.lambdaUpdate(MiniPopularizeConfig.class)
				.set(MiniPopularizeConfig::getStatus, req.getStatus())
				.in(MiniPopularizeConfig::getId, req.getIds())
				.ne(MiniPopularizeConfig::getStatus, req.getStatus()));
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	//==============================================================分类==========================================================================================

	/**
	 * 新增分类
	 */
	@PostMapping("/category/save")
	public Result<String> saveCategory(@RequestBody MiniPopularizeConfigCategory configCategory) {
		try {
			miniPopularizeConfigCategoryMapper.insert(configCategory);
		} catch (DataAccessException e) {
			throw BusinessRuntimeException.getInstance("重复分类");
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 编辑分类
	 */
	@PutMapping("/category/save")
	public Result<String> updateCategory(@RequestBody MiniPopularizeConfigCategory configCategory) {
		miniPopularizeConfigCategoryMapper.updateById(configCategory);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除分类
	 */
	@DeleteMapping("/del/category/{id}")
	public Result<String> delCategoryById(@PathVariable Long id) {
		Integer selectCount = miniPopularizeConfigMapper.selectCount(Wrappers.lambdaQuery(MiniPopularizeConfig.class).eq(MiniPopularizeConfig::getCid, id));
		if (selectCount > 0) {
			throw BusinessRuntimeException.getInstance("该分类下存在数据");
		}
		miniPopularizeConfigCategoryMapper.deleteById(id);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 分类排序
	 */
	@PutMapping("/category/sorted")
	public Result<String> sortedCategory(@RequestBody MiniPopularizeConfigCategory configCategory) {
		miniPopularizeConfigCategoryMapper.updateById(configCategory);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 分类列表
	 */
	@GetMapping("/category/get")
	public Result<List<MiniPopularizeConfigCategory>> getCategory() {
		List<MiniPopularizeConfigCategory> miniPopularizeConfigCategories = beanSearcher.searchAll(MiniPopularizeConfigCategory.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(miniPopularizeConfigCategories);
	}
}
