package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.biz.dal.dto.search.MemberPriceSettingsSearch;
import com.yonge.cooleshow.biz.dal.entity.MemberPriceSettings;
import com.yonge.cooleshow.biz.dal.enums.EVipType;
import com.yonge.cooleshow.biz.dal.service.MemberPriceSettingsService;
import com.yonge.cooleshow.biz.dal.service.SysUserService;
import com.yonge.cooleshow.biz.dal.vo.MemberPriceSettingsVo;
import com.yonge.cooleshow.biz.dal.wrapper.ActivityPlayWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("${app-config.url.admin:}/memberPriceSettings")
@Api(tags = "会员价格设置")
@RestController
public class MemberPriceSettingsController extends BaseController {
	@Resource
	private MemberPriceSettingsService memberPriceSettingsService;
	@Resource
	private SysUserService sysUserService;

	@GetMapping("/detail/{id}")
	@ApiOperation(value = "详情", notes = "传入id")
	public HttpResponseResult<MemberPriceSettingsVo> detail(@PathVariable("id") Long id) {
		return succeed(memberPriceSettingsService.detail(id));
	}

	@GetMapping("/list")
	@ApiOperation(value = "查询分页", notes = "传入memberPriceSettingsSearch")
	@PreAuthorize("@pcs.hasPermissions('memberPriceSettings/list')")
	public HttpResponseResult<PageInfo<MemberPriceSettingsVo>> page(MemberPriceSettingsSearch query) {
		IPage<MemberPriceSettingsVo> pages = memberPriceSettingsService.selectPage(PageUtil.getPage(query), query);
		return succeed(PageUtil.pageInfo(pages));
	}

	@GetMapping("/groupList")
	@ApiOperation(value = "按分类分组")
	@PreAuthorize("@pcs.hasPermissions('memberPriceSettings/groupList')")
	public HttpResponseResult<List<ActivityPlayWrapper.GroupListDto>> groupList() {
		MemberPriceSettingsSearch query = new MemberPriceSettingsSearch();
		query.setStatus(true);
		query.setSearchType("VIP,SVIP,DISCOUNT");
		query.setPage(1);
		query.setRows(1000);
		IPage<MemberPriceSettingsVo> pages = memberPriceSettingsService.selectPage(PageUtil.getPage(query), query);
		List<MemberPriceSettingsVo> records = pages.getRecords();
		if (CollectionUtils.isNotEmpty(records)) {
			List<ActivityPlayWrapper.GroupListDto> groupListDto = new ArrayList<>();
			Map<EVipType, List<MemberPriceSettingsVo>> collect = records.stream().collect(Collectors.groupingBy(MemberPriceSettingsVo::getVipType));
			collect.forEach((k, v) -> {
				ActivityPlayWrapper.GroupListDto dto = new ActivityPlayWrapper.GroupListDto();
				dto.setType(k.getCode());
				dto.setMemberList(v);
				groupListDto.add(dto);
			});
			return succeed(groupListDto);
		}
		return succeed();
	}

	@PostMapping(value = "/update", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiOperation(value = "修改", notes = "传入memberPriceSettings")
	@PreAuthorize("@pcs.hasPermissions('memberPriceSettings/update')")
	public HttpResponseResult<Boolean> update(@Valid @RequestBody MemberPriceSettings memberPriceSettings) {
		memberPriceSettings.setUpdateBy(sysUserService.getUserId());
		memberPriceSettings.setUpdateTime(new Date());
		boolean update = memberPriceSettingsService.update(memberPriceSettings, new UpdateWrapper<MemberPriceSettings>().lambda()
				.set(MemberPriceSettings::getDesc, memberPriceSettings.getDesc())
				.eq(MemberPriceSettings::getId, memberPriceSettings.getId()));
		return status(update);
	}
}
