/**
 * 
 */
package org.springblade.modules.system.controller;

import java.util.Date;
import java.util.List;

import javax.validation.Valid;

import org.springblade.core.constant.AppConstant;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.SysPackage;
import org.springblade.modules.system.entity.SysPackageMenu;
import org.springblade.modules.system.service.normal.SysPackageMenuService;
import org.springblade.modules.system.service.normal.SysPackageService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

/**
 * @author wookvn
 *
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SYSTEM_NAME + "/package")
@Api(value = "套餐管理", tags = "套餐管理")
public class PackageController {
	
	private final SysPackageService packageService;
	
	private final SysPackageMenuService packageMenuService;

	/**
	 * 分页 可视化分类表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入SysPackage")
	public R<IPage<SysPackage>> list(SysPackage sysPackage, Query query) {
		QueryWrapper<SysPackage> queryWrapper = Condition.getQueryWrapper(sysPackage);
		IPage<SysPackage> pages = packageService.page(Condition.getPage(query),queryWrapper.lambda().orderByDesc(SysPackage::getSortNum).orderByDesc(SysPackage::getId));
		if(pages != null && CollectionUtil.isNotEmpty(pages.getRecords())) {
			pages.getRecords().forEach(item -> {
				List<Long> menuIds = packageMenuService.listObjs(Wrappers.<SysPackageMenu>lambdaQuery().select(SysPackageMenu::getMenuId).eq(SysPackageMenu::getPackageId, item.getId()), t -> Long.valueOf(t.toString()));
				item.setMenuIds(menuIds);
			});
		}
		return R.data(pages);
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入SysPackage")
	public R<String> submit(@Valid @RequestBody SysPackage sysPackage) {
		if(sysPackage.getId() == null) {
			sysPackage.setCreateTime(new Date());
		}
		return R.status(packageService.doSaveOrUpdate(sysPackage));
	}
	
	/**
	 * 删除 
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "根据ID逻辑删除", notes = "传入id")
	public R<String> remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(packageService.removeBatchByIds(Func.toIntList(ids)));
	}
}
