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

import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.GoodsDonSkuSubsidy;
import com.cyksj.model.views.GoodsDonSkuSubsidyView;
import com.cyksj.service.mange.GoodsDonSkuSubsidyService;
import com.cyksj.service.mange.RefreshCacheService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/** admin/百亿补贴
 * 项目名: yhlxj2
 * 文件名: GoodsDonSkuSubsidyController
 * 创建者: JavaZou
 * 创建时间:2024/4/11 12:05
 */
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/manage/goods/sku/subsidy")
@RestController
public class GoodsDonSkuSubsidyController {
	private final GoodsDonSkuSubsidyService goodsDonSkuSubsidyService;

	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final RefreshCacheService refreshCacheService;

	/**
	 * 新增补贴规格
	 */
	@PostMapping("/insert")
	public Result<String> insertGoodsDonSkuSubsidy(@RequestBody @Validated GoodsDonSkuSubsidy goodsDonSkuSubsidy) {
		goodsDonSkuSubsidyService.saveOrUpdateSubsidy(goodsDonSkuSubsidy);
		refreshCacheService.refreshGoodsSkuSubsidyPageCache();
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 更新补贴规格
	 */
	@PutMapping("/update")
	public Result<String> updateGoodsDonSkuSubsidy(@RequestBody @Validated GoodsDonSkuSubsidy goodsDonSkuSubsidy) {
		goodsDonSkuSubsidyService.saveOrUpdateSubsidy(goodsDonSkuSubsidy);
		refreshCacheService.refreshGoodsSkuSubsidyPageCache();
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除商品补贴价格记录
	 */
	@DeleteMapping("/delete/{id}")
	public Result<String> deleteGoodsDonSkuSubsidy(@PathVariable Long id) {
		GoodsDonSkuSubsidy byId = goodsDonSkuSubsidyService.getById(id);
		if (byId != null && byId.getDeleted()) {
			byId.setDeleted(false);
			goodsDonSkuSubsidyService.updateById(byId);
			refreshCacheService.refreshGoodsSkuSubsidyPageCache();
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 上下架补贴规格
	 */
	@PutMapping("/status/{id}")
	public Result<String> updateStatusById(@PathVariable Long id) {
		GoodsDonSkuSubsidy byId = goodsDonSkuSubsidyService.getById(id);
		if (byId != null) {
			byId.setStatus(!byId.getStatus());
			goodsDonSkuSubsidyService.updateById(byId);
			refreshCacheService.refreshGoodsSkuSubsidyPageCache();
		}
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 查询商品补贴价格记录 列表
	 */
	@GetMapping("/get")
	public Result<SearchResult<GoodsDonSkuSubsidyView>> getGoodsDonSkuSubsidy() {
		SearchResult<GoodsDonSkuSubsidyView> search = beanSearcher.search(GoodsDonSkuSubsidyView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}
}
