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

import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.RegisterGoodsDonMapper;
import com.cyksj.model.entity.RegisterGoodsDon;
import com.cyksj.redis.RedisService;
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;
import java.util.Set;

/*
 *项目名: netflix
 *文件名: RegisterGoodsDonController
 *创建者: JavaZou
 *创建时间:2023/7/3 15:03
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/manage/register/goods")
public class RegisterGoodsDonController {
	private final BeanSearcher beanSearcher;

	private final HttpServletRequest request;

	private final RegisterGoodsDonMapper registerGoodsDonMapper;

	private final RedisService redisService;

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

	/**
	 * 新增 商品spu
	 */
	@PostMapping(value = "/post")
	public Result<Long> spuInsert(@Validated @RequestBody RegisterGoodsDon registerGoodsDon) throws Exception {
		// 新增商品
		registerGoodsDonMapper.insert(registerGoodsDon);
		Set<String> keys = redisService.keys(RedisService.key.REGISTER_HOME_PAGE_CACHE.getName() + "*");
		redisService.del(keys.toArray(String[]::new));
		return GatewayResponse.SUCCESS.newBuilder().toResult(registerGoodsDon.getId());
	}

	/**
	 * 修改 商品spu
	 */
	@PutMapping(value = "/update")
	public Result<String> spuUpdate(@Validated @RequestBody RegisterGoodsDon registerGoodsDon) throws Exception{
		registerGoodsDonMapper.updateById(registerGoodsDon);
		Set<String> keys = redisService.keys(RedisService.key.REGISTER_HOME_PAGE_CACHE.getName() + "*");
		redisService.del(keys.toArray(String[]::new));
		return GatewayResponse.SUCCESS.newBuilder().toResult();
	}


	@PutMapping("/put/status/{id}")
	public Result<Boolean> updateStatus(@PathVariable Long id) {
		RegisterGoodsDon registerGoodsDon = registerGoodsDonMapper.selectById(id);
		if (registerGoodsDon == null) {
			throw BusinessRuntimeException.getInstance("平台不存在");
		}
		registerGoodsDon.setStatus(!registerGoodsDon.getStatus());
		registerGoodsDonMapper.updateById(registerGoodsDon);
		Set<String> keys = redisService.keys(RedisService.key.REGISTER_HOME_PAGE_CACHE.getName() + "*");
		redisService.del(keys.toArray(String[]::new));
		return GatewayResponse.SUCCESS.newBuilder().toResult(registerGoodsDon.getStatus());
	}

	@DeleteMapping("/delete/{id}")
	public Result<Long> deleteById(@PathVariable Long id) {
		RegisterGoodsDon registerGoodsDon = registerGoodsDonMapper.selectById(id);
		if (registerGoodsDon == null) {
			throw BusinessRuntimeException.getInstance("平台不存在");
		}
		registerGoodsDon.setDeleted(false);
		registerGoodsDonMapper.updateById(registerGoodsDon);
		Set<String> keys = redisService.keys(RedisService.key.REGISTER_HOME_PAGE_CACHE.getName() + "*");
		redisService.del(keys.toArray(String[]::new));
		return GatewayResponse.SUCCESS.newBuilder().toResult(id);
	}
}
