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

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.annotation.Log;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.Jsons;
import com.cyksj.dto.Result;
import com.cyksj.enums.BusinessType;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.GoodsDonSkuMapper;
import com.cyksj.model.entity.GoodsDonSku;
import com.cyksj.model.entity.RenewUpgradePackage;
import com.cyksj.model.views.RenewUpgradePackageView;
import com.cyksj.service.mange.renew.RenewUpgradePackageService;
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.Map;
import java.util.stream.Collectors;

/**
 * 项目名: yhlxj2
 * 文件名: RenewUpgradePackageController
 * 创建者: JavaZou
 * 创建时间:2024/7/18 16:59
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/manage/renew/upgrade")
public class RenewUpgradePackageController {
	private final RenewUpgradePackageService renewUpgradePackageService;

	private final HttpServletRequest request;

	private final BeanSearcher beanSearcher;

	private final GoodsDonSkuMapper goodsDonSkuMapper;

	/**
	 * 新增套餐
	 */
	@PostMapping
	@SaCheckPermission("upgrade:add")
	@Log(module = "套餐管理/新增套餐", businessType = BusinessType.POST, isSaveRequestData = true)
	public Result<String> post(@RequestBody RenewUpgradePackage upgradePackage){
		log.info("新增套餐:{}", upgradePackage);
		renewUpgradePackageService.save(upgradePackage);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 修改套餐
	 */
	@PutMapping
	@SaCheckPermission("upgrade:edit")
	@Log(module = "套餐管理/修改套餐", businessType = BusinessType.PUT, isSaveRequestData = true)
	public Result<String> put(@RequestBody RenewUpgradePackage upgradePackage){
		log.info("修改套餐:{}", upgradePackage);
		renewUpgradePackageService.updateById(upgradePackage);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 删除套餐
	 */
	@DeleteMapping("/{id}")
	@SaCheckPermission("upgrade:delete")
	@Log(module = "套餐管理/删除套餐", businessType = BusinessType.DELETE, isSaveRequestData = true)
	public Result<String> delete(@PathVariable Long id){
		log.info("删除套餐:{}", id);
		RenewUpgradePackage byId = renewUpgradePackageService.getById(id);
		if (byId == null){
			throw BusinessRuntimeException.getInstance("套餐不存在");
		}
		byId.setDeleted(false);
		renewUpgradePackageService.updateById(byId);
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}

	/**
	 * 上下架套餐
	 */
	@PutMapping("/put/status/{id}")
	@SaCheckPermission("upgrade:putStatus")
	@Log(module = "套餐管理/上下架套餐", businessType = BusinessType.PUT, isSaveRequestData = true)
	public Result<Boolean> putStatus(@PathVariable Long id){
		log.info("上下架套餐:{}", id);
		RenewUpgradePackage byId = renewUpgradePackageService.getById(id);
		if (byId == null){
			throw BusinessRuntimeException.getInstance("套餐不存在");
		}
		byId.setStatus(!byId.getStatus());
		renewUpgradePackageService.updateById(byId);
		return GatewayResponse.SUCCESS.newBuilder().toResult(byId.getStatus());
	}

	/**
	 * 获取套餐
	 */
	@GetMapping("/{id}")
	public Result<RenewUpgradePackage> get(@PathVariable Long id){
		log.info("获取套餐:{}", id);
		RenewUpgradePackage byId = renewUpgradePackageService.getById(id);
		return GatewayResponse.SUCCESS.newBuilder().toResult(byId);
	}

	/**
	 * 获取套餐列表
	 */
	@GetMapping
	public Result<SearchResult<RenewUpgradePackageView>> list(Long skuId) {
		log.info("获取套餐列表");
		Map<String, Object> build = MapUtils.flatBuilder(request.getParameterMap()).build();
		SearchResult<RenewUpgradePackageView> search = beanSearcher.search(RenewUpgradePackageView.class, build);
		search.getDataList().forEach(upgradePackageView -> {
			try {
				String specVal = goodsDonSkuMapper.selectList(Wrappers.lambdaQuery(GoodsDonSku.class).in(GoodsDonSku::getId, Jsons.parseList(upgradePackageView.getUpgradeSkuIds(), Long.class))).stream().map(GoodsDonSku::getSpecVal).collect(Collectors.joining(","));
				upgradePackageView.setUpgradeSpecVal(specVal);
			} catch (Exception e) {
				throw BusinessRuntimeException.getInstance("获取套餐规格失败");
			}
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}
}
