/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.ldt.activity.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.common.constant.SystemConstant;
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.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.ldt.activityjoinrecord.entity.ActivityJoinRecord;
import org.springblade.modules.ldt.activityjoinrecord.service.IActivityJoinRecordService;
import org.springblade.modules.ldt.mall.entity.Mall;
import org.springblade.modules.ldt.mall.service.IMallService;
import org.springblade.modules.ldt.mallshoplabel.entity.MallShopLabel;
import org.springblade.modules.ldt.mallshoplabel.service.IMallShopLabelService;
import org.springblade.modules.ldt.shop.entity.Shop;
import org.springblade.modules.ldt.shop.service.IShopService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.ldt.activity.entity.Activity;
import org.springblade.modules.ldt.activity.vo.ActivityVO;
import org.springblade.modules.ldt.activity.service.IActivityService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *  控制器
 *
 * @author BladeX
 * @since 2021-07-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("cyzh-ldt/activity")
@Api(value = "", tags = "接口")
public class ActivityController extends BladeController {

	private final IActivityService activityService;
	private final IMallShopLabelService mallShopLabelService;
	private final IActivityJoinRecordService activityJoinRecordService;
	private final IMallService mallService;
	private final IShopService shopService;
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入activity")
	public R<ActivityVO> detail(Activity activity) {
		Activity detail = activityService.getOne(Condition.getQueryWrapper(activity));
		List<ActivityJoinRecord> activityJoinRecords = activityJoinRecordService.getBaseMapper().selectList(new QueryWrapper<>(new ActivityJoinRecord()).lambda()
			.eq(ActivityJoinRecord::getActivityId, activity.getId()));
		ActivityVO activityVO = Objects.requireNonNull(BeanUtil.copy(detail, ActivityVO.class));
		activityVO.setActivityJoinRecords(activityJoinRecords);
		return R.data(activityVO);
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入activity")
	public R<IPage<Activity>> list(@RequestParam Map<String,Object> activity, Query query) {
		IPage<Activity> pages = activityService.page(Condition.getPage(query), Condition.getQueryWrapper(activity,Activity.class));
		return R.data(pages);
	}

	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入activity")
	public R<IPage<ActivityVO>> page(ActivityVO activity, Query query) {
		IPage<ActivityVO> pages = activityService.selectActivityPage(Condition.getPage(query), activity);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入activity")
	public R save(@Valid @RequestBody Activity activity) {
		return R.status(activityService.save(activity));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入activity")
	public R update(@Valid @RequestBody Activity activity) {
		return R.status(activityService.updateById(activity));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入activity")
	public R submit(@Valid @RequestBody Activity activity) {
		Assert.notNull(activity.getSponsorType(),"活动发起者的类型不能为空");
		Assert.notNull(activity.getSponsorId(),"活动发起者不能为空");
		String labelIds = activity.getLabelIds();
		String[] strings = labelIds.split(",");
		StringBuffer sb = new StringBuffer();
		for (String labelId : strings){
			MallShopLabel mallShopLabel = mallShopLabelService.getById(Long.valueOf(labelId));
			sb.append(mallShopLabel.getName()+",");
		}
		activity.setLabelNames(sb.toString().substring(0,sb.toString().length()-1));

		if(activity.getSponsorType() == SystemConstant.MALLORSHOP.MALL.getValue()){
			Mall mall = mallService.getById(activity.getSponsorId());
			Assert.notNull(mall,"商场不存在");
			activity.setSponsorName(mall.getName());
			activity.setMallId(mall.getId());
		}else if(activity.getSponsorType() == SystemConstant.MALLORSHOP.SHOP.getValue()){
			Shop shop = shopService.getById(activity.getSponsorId());
			Assert.notNull(shop,"商店不存在");
			activity.setSponsorName(shop.getName());
			activity.setMallId(shop.getMallId());
		}
		return R.status(activityService.saveOrUpdate(activity));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(activityService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 批量更改
	 * 小程序使用
	 */
	@PostMapping("/updateBatch")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "批量更改", notes = "传入ids")
	public R updateBatch(@RequestBody List<Activity> activityList) {
		return R.status(activityService.updateBatchById(activityList));
	}
}
